How to collect mdadm data with Telegraf?

Hi Telegraf team,

I would like to know if it exists a native MDStats collector for Telegraf (or if it’s planned) as it exists for node_exporter: https://github.com/prometheus/node_exporter/blob/master/collector/mdadm_linux.go?

Thanks

Hi @aurrelhebert ,

I don’t know if there are plans for it , I am sure somebody will reply.
It is currently not available , I checked it with the following command

 telegraf --input-list |sort

have a nice weekend

You can use telegraf input_exec with simple python script via package mdstat:

pip3 install mdstat
# or pip install mdstat for python v2

Save script /usr/local/bin/mdstatus and give it x permision:

#!/usr/bin/python3
import mdstat

mdjson = mdstat.parse()

lines = []
for md in mdjson['devices']:
  ln = []
  ln.append('mdstat,')
  ln.append('personality=' + mdjson['devices'][md]['personality'])
  ln.append(' ')
  ln.append('active=' + str(int(mdjson['devices'][md]['active'])) + ',')
  ln.append('read_only=' + str(int(mdjson['devices'][md]['read_only'])))
  lines.append(''.join(ln))
  for disk in mdjson['devices'][md]['disks']:
    ln = []
    ln.append('mdstat-disk,')
    ln.append('md=' + md + ',')
    ln.append('disk=' + disk + ',')
    ln.append('number=' + str(int(mdjson['devices'][md]['disks'][disk]['number'])))
    ln.append(' ')
    ln.append('faulty=' + str(int(mdjson['devices'][md]['disks'][disk]['faulty'])) + ',')
    ln.append('spare=' + str(int(mdjson['devices'][md]['disks'][disk]['spare'])) + ',')
    ln.append('replacement=' + str(int(mdjson['devices'][md]['disks'][disk]['replacement'])) + ',')
    ln.append('write_mostly=' + str(int(mdjson['devices'][md]['disks'][disk]['write_mostly'])) + ',')
    ln.append('replacement=' + str(int(mdjson['devices'][md]['disks'][disk]['replacement'])))
    lines.append(''.join(ln))

if len(lines):
  print('\n'.join(lines))

Then add telegraf input:

[[inputs.exec]]
  commands = ["/usr/local/bin/mdstatus"]
  timeout = "2s"
  data_format = "influx"

Thanks for your answer @sweb! I will give it a try