Monit Component

Is there a Monit component? Ie something that can connect to a Monit server and display its data as sensors?

It seems like a decent usecase but after a search I’m surprised to find it’s not there, so just checking if I’ve missed anything.

I’m not aware of any Monit component but you may still define command line sensors to execute “$MONIT/bin/monit -c $MONIT/conf/monitrc summary -B | grep yourcheck |awk ‘{print $2}’” via ssh at regular intervals.

1 Like

It’s actually a remote monit instance I’d like to check, and perhaps have all checks already presented as sensors as appropriate. Neat workaround though!

I wonder if anyone has done this at all? I have a workaround that uses Node-Red, sends an http request for an XML output http://192.168.x.x:2812/_status?format=xml converts it to JSON and then sends to MQTT. In theory there is a JSON output from the API, but it is not a valid JSON string and it is easier to convert the XML.

As it is possible to use the http call, it should be reasonably easy to generate a component I’d have thought but outside my knowledge though.

How did you do that, since i am a newby on MQTT and Node-Red?

I used this

[{"id":"6f894dcc.5b6a74","type":"http request","z":"25794873.4b59d8","name":"Get Monit Status - 47","method":"GET","ret":"txt","url":"http://192.168.7.47:2812/_status?format=xml","tls":"","x":333.55556869506836,"y":309.99999618530273,"wires":[["410792c0.467a0c"]]},{"id":"410792c0.467a0c","type":"function","z":"25794873.4b59d8","name":"xmlToJsonStr","func":"/*\nTo use this node install xml2json.\nsudo npm install xml2json\nDo not install it globally\nEdit settings.js to 'require' the module\n*/\n// get an instance of the module\nvar parser = context.global.get('xml2json');\n// set the options\nvar options = {\n    object: true, //returns a JSON object\n    reversible: false,\n    coerce: false,\n    sanitize: true,\n    trim: true,\n    arrayNotation: false,\n    alternateTextNode: false\n};\n\n//var xml = msg.payload;\n\n// xml to json \nvar json = parser.toJson(msg.payload, options);\n\nmsg.payload = json;\nmsg.topic = \"monit/\"+json.monit.server.localhostname;\n\n\nreturn msg;","outputs":1,"noerr":0,"x":607.1110610961914,"y":315.55554580688477,"wires":[["153a3fe7.46012"]]}]

and passed the result to MQTT.

BUT you need the xml2json npm package installed and I could not get it to work on the Node-Red add-on (it worked on a standalone Node-Red).

I have started using the RESTful API (RESTful sensor, XML and Monit) although I can’t work out how to get the information out cleanly using templates.

Remember to enable the httpd in the /etc/monitrc (or even better create a drop-in by adding the file /etc/monitd/00-default with your settings.

I have also found the httpd service does not always start correctly on boot. It is because the documentation recomends the network.target as the wants. I create this service file /lib/systemd/system/monit.service and load the service from that.

[Unit]
Description=Pro-active monitoring utility for unix systems

After=network-online.target
Documentation=man:monit(1) https://mmonit.com/wiki/Monit/HowTo

[Service]
Type=simple
ExecStart= /usr/bin/monit -I -c /etc/monit/monitrc

[Install]
WantedBy=basic.target

Note you must use network-online.target rather than network.target to ensure the httpd service starts after a reboot.

1 Like

There is some sort of Monit basic API:

http://your_ip_address:2812/_status?format=xml

You may get more parameters looking at the code [1].

[1] https://bitbucket.org/tildeslash/monit/src/612e5d326ce293c7bdce40097b3c34e4f4d00640/src/http/cervlet.c?at=master&fileviewer=file-view-default#cervlet.c-2557

Thanks. I will try that.

@Gaijin66 my current setup (sensor component);

- platform: rest
  name: Unifi Memory
  resource: http://192.168.X.X:2812/_status?format=xml
  unit_of_measurement: '%'
  value_template: '{{ (value_json.monit.service.system.memory.percent | int) }}'

- platform: rest
  name: Monit Hassio
  resource: http://192.168.Y.Y:2812/_status?format=xml
  json_attributes_path: "$.monit"
  value_template: 'OK'
  json_attributes:
    - "platform"
    - "server"
    - "service"

- platform: template
  sensors:
    hassio_system_cpu:
      value_template: "{{ state_attr('sensor.monit_hassio', 'service').system.cpu.user }}"
    hassio_system_load:
      value_template: "{{ state_attr('sensor.monit_hassio', 'service').system.load.avg01 }}"
    hassio_system_mem:
      value_template: "{{ state_attr('sensor.monit_hassio', 'service').system.memory.percent }}"

On the Monit front, you need to enable the httpd function. Plus I discovered that in the systemd service file the After=network.target should be After=network-online.target else the service does not start correctly (they have updated the Wiki).

1 Like

@baz123 That is interesting information. I can use that. Thanks for that.

1 Like