Skip to content

Instantly share code, notes, and snippets.

@smcgann99
Created October 25, 2021 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smcgann99/1c8e6bfab35fb2526b9588d677a7955b to your computer and use it in GitHub Desktop.
Save smcgann99/1c8e6bfab35fb2526b9588d677a7955b to your computer and use it in GitHub Desktop.
Get Client info from Chrony - Console text ouput to msg.payload

Simple flow to get console output from a Chrony time server, running on same host as node-red, and output connected clients as a payload object.

As a bit of a learning exercise I decided to write a flow to check state of my Chrony time server.

After much trial and error I have taken the output of an exec node, split on new lines, removed spaces and managed to create an object as above. This is all done in the one function node.

[{"id":"5a7c73bfa8a68758","type":"exec","z":"6afa8a68.6c130c","command":" sudo chronyc clients","addpay":"","append":"","useSpawn":"false","timer":"5","winHide":false,"oldrc":false,"name":"NTP","x":1470,"y":660,"wires":[["4f2859f763e44a50"],[],[]]},{"id":"cffba9853d94c3aa","type":"inject","z":"6afa8a68.6c130c","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1310,"y":660,"wires":[["5a7c73bfa8a68758"]]},{"id":"4f2859f763e44a50","type":"function","z":"6afa8a68.6c130c","name":"","func":"msg.payload = msg.payload.split(\"\\n\") // split on newline\nvar list = []\n\nfor (var i = 0; i < msg.payload.length - 1; i++) {\n\n list[i] = msg.payload[i].replace(/\\s+/g, ' ').trim(); // remove extra spaces\n list[i] = list[i].split(\" \") // split on space\n}\n\nvar items = {};\nvar rec = 2; // exclude table header, top 2 lines\n\nfor ( i = 0; i < list.length - 2; i++) {\n\n items[list[rec][0].toUpperCase()] = { \"NTP\": list[rec][1], \"Drop\": list[rec][2], \"Int\": list[rec][3], \"IntL\": list[rec][4], \"Last\": list[rec][5]} // get required fields\n rec++\n}\n\nmsg.payload = Object.keys(items).sort().reduce((r, k) => Object.assign(r, { [k]: items[k] }), {}) // sort by key -just because we can ;-)\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1620,"y":660,"wires":[["f7fe699f8a849a45"]],"info":"msg.payload = msg.payload.split(\"\\n\") //split on newline\r\nvar list = []\r\n\r\nfor (var i = 0; i < msg.payload.length - 1; i++) {\r\n\r\n list[i] = msg.payload[i].replace(/\\s+/g, ' ').trim(); //remove extra spaces\r\n list[i] = list[i].split(\" \") //split on space\r\n\r\n}\r\n\r\nvar items = {};\r\nvar rec = 2; // to exclude table header, top 2 lines\r\n\r\nfor ( i = 0; i < list.length - 2; i++) {\r\n\r\n items[list[rec][0].toUpperCase()] = { \"NTP\": list[rec][1], \"Int\": list[rec][3], \"Last\": list[rec][5] }\r\n rec++\r\n}\r\n\r\nmsg.payload = items\r\n\r\n//let obj = msg.payload;\r\n//msg.payload = Object.keys(obj).sort().reduce((r, k) => Object.assign(r, { [k]: obj[k] }), {})\r\n\r\nreturn msg;"},{"id":"f7fe699f8a849a45","type":"debug","z":"6afa8a68.6c130c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1750,"y":660,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment