I’m trying to catch up with a whole load of stuff I did before I started this blog; as much as anything, so I can remember what I did for when I need to do it again!
Following on from installing Monit, I wanted to use Node-RED to monitor Monit so that ultimately I can have a dashboard for all sorts of things to be monitored and possibly alerts (rather than Monit doing that directly).
As previously mentioned, Monit communicates via an http interface so extracting the data was remarkably simple. All you need is a websocket flow and a URL like http://x.x.x.x:2812/_status?format=xml . It seemed that the obvious way to use this data was to convert the XML to JSON.
However, when I tried the built-in XML parser it did not work. Some digging brought up the fact that this built-in node has a limited depth. Not very useful. Further research led me to the xml2json node on npm. I’d never really understood the relationship between npm and Node-RED. What I think I understand is that Node-RED uses some command line nodes from npm which in itself is a Javascript package manager.
So in order to use this command line node (rather than a Node-RED flow) I needed to install it then ‘require’ it. The whole Javascript ecosystem is rather alien to me at the moment so getting it installed took a fair bit of experimenting although it is actually very simple to do.
It comprises of 3 steps:
- Install the node from npm. As you want to use it from Node-RED, you need to *not* install it globally – counter-intuitive right? So simply go into your node red folder (usually ~/.node-red) and then npm install xml2json (reference).
- Next, you need to edit your settings.js file to ‘require’ the node (reference). This file is also usually found in the ~/.node-red directory.
Look for the functionGlobalContext and add in a line for the xml2json node.
12functionGlobalContext: {xml2json:require('xml2json')}
Save and restart Node-RED sudo service nodered restart . - Add the code into a function such as:
1234567891011121314151617181920212223242526272829/*Version: 1.0Date: 17 Mar 2017Author: BP OrpinTo use this node install xml2json.sudo npm install xml2jsonDo not install it globallyEdit settings.js to 'require' the module*/// get an instance of the modulevar parser = context.global.get('xml2json');// set the optionsvar options = {object: true, //returns a JSON objectreversible: false,coerce: false,sanitize: true,trim: true,arrayNotation: false,alternateTextNode: false};// xml to jsonvar json = parser.toJson(msg.payload, options);msg.payload = json;return msg;
This will now enable you to take an XML flow and break it down into its constituent parts
The complete Node-RED flow:
1 |
[{"id":"8e8a5ac5.2e1c7","type":"debug","z":"15dbf9.ea436c07","name":"","active":true,"console":"false","complete":"true","x":732,"y":389,"wires":[]},{"id":"86773489.13778","type":"http request","z":"15dbf9.ea436c07","name":"Get Monit Status","method":"GET","ret":"txt","url":"http://192.168.X.X:2812/_status?format=xml","tls":"","x":373,"y":390,"wires":[["cd3d2245.f84dd8"]]},{"id":"975fee03.4faa2","type":"inject","z":"15dbf9.ea436c07","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":187,"y":391,"wires":[["86773489.13778"]]},{"id":"cd3d2245.f84dd8","type":"function","z":"15dbf9.ea436c07","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;\n\nreturn msg;","outputs":1,"noerr":0,"x":573,"y":390,"wires":[["8e8a5ac5.2e1c7"]]},{"id":"338a4d11.2fd6a2","type":"comment","z":"15dbf9.ea436c07","name":"To use this flow you first need to install xml2json","info":"","x":492.5,"y":319,"wires":[]}] |
by