Report OS and Node Red Service Uptime to MQTT

Report the uptime of the server OS and the uptime of the Node Red service in seconds via MQTT.

This is useful to detect instability, or enforce periodic hardware reboot. If you're running in a failover cluster you can check if you're failing over between nodes, if a node is unstable.

I just wanted to have some feedback that a "restart node red" function I had was actually working.

[{"id":"ab6bd3e2.6bb818","type":"debug","z":"f10ef821.804e28","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1030,"y":620,"wires":[]},{"id":"e0edaea0.abd4f","type":"inject","z":"f10ef821.804e28","name":"Trigger","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":true,"onceDelay":"10","x":200,"y":620,"wires":[["be4bd4a5.dcd24"]]},{"id":"be4bd4a5.dcd24","type":"Uptime","z":"f10ef821.804e28","name":"","x":410,"y":620,"wires":[["6f601b56.f6c6c4"]]},{"id":"753d93f6.72f6cc","type":"comment","z":"f10ef821.804e28","name":"Report OS and Node Red service uptime","info":"","x":300,"y":560,"wires":[]},{"id":"6f601b56.f6c6c4","type":"function","z":"f10ef821.804e28","name":"Report OS Uptime","func":"//Define filesystem to report\nvar fileSystem=\"/\";\nvar fileSystemReportedMQTT=\"root\"; // This is the name reported in MQTT - change for /!!!\nvar capacity=null;\n\n\n//Walk through all the filesystems in msg.payload to find the array element for fileSystem\nfor ( var i = 0; i < msg.payload.length; i++ ) {\n        if (msg.payload[i].mount==fileSystem){\n        capacity=msg.payload[i].capacity*100; //capacity = %age\n    }\n}\n\nvar returnMsg={topic: \"/system/os/uptime\", payload: msg.payload.uptime};\n\nreturn returnMsg;\n","outputs":1,"noerr":0,"x":670,"y":620,"wires":[["ab6bd3e2.6bb818","636dfdb4.d391c4"]]},{"id":"636dfdb4.d391c4","type":"mqtt out","z":"f10ef821.804e28","name":"Report OS Uptime","topic":"","qos":"0","retain":"false","broker":"8552f64b.80b9c","x":1070,"y":660,"wires":[]},{"id":"3ba07cb8.c6a074","type":"mqtt in","z":"f10ef821.804e28","name":"Report OS Uptime","topic":"/system/os/uptime","qos":"2","datatype":"auto","broker":"8552f64b.80b9c","x":230,"y":720,"wires":[["504808b8.524d6"]]},{"id":"504808b8.524d6","type":"debug","z":"f10ef821.804e28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1050,"y":720,"wires":[]},{"id":"8961a16b.202de8","type":"inject","z":"f10ef821.804e28","name":"Timestamp at Node Red start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":270,"y":780,"wires":[["9321c40c.f3b94"]]},{"id":"9321c40c.f3b94","type":"function","z":"f10ef821.804e28","name":"Set Global Node Red Start Timestamp","func":"//Timestamp from an inject node set to fire once at startup\nglobal.set ('nodeRedStartTimestamp',msg.payload);\n\nvar reportStartTimestamp=global.get('nodeRedStartTimestamp') || 0;\n\nvar returnMsg={topic: \"Node Red Start Timestamp\", payload: reportStartTimestamp};\n\nreturn returnMsg;\n","outputs":1,"noerr":0,"x":730,"y":780,"wires":[["9a94a5f5.fa566"]]},{"id":"bf691515.870818","type":"function","z":"f10ef821.804e28","name":"Report Node Red Uptime","func":"var reportStartTimestamp=global.get('nodeRedStartTimestamp') || 0;\n\nvar upTime=parseInt((msg.payload-reportStartTimestamp)/1000);\n\nvar returnMsg={topic: \"/system/os/service/nodeRed/uptime\", payload: upTime};\nreturn returnMsg;\n\n\n\n\n","outputs":1,"noerr":0,"x":690,"y":820,"wires":[["cfcb6271.bb45e8","8a4418a9.4ec3"]]},{"id":"9a94a5f5.fa566","type":"debug","z":"f10ef821.804e28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1030,"y":780,"wires":[]},{"id":"7a32c182.669fe8","type":"inject","z":"f10ef821.804e28","name":"Timestamp now","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":true,"onceDelay":"5","x":230,"y":820,"wires":[["bf691515.870818"]]},{"id":"cfcb6271.bb45e8","type":"debug","z":"f10ef821.804e28","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1050,"y":820,"wires":[]},{"id":"8a4418a9.4ec3","type":"mqtt out","z":"f10ef821.804e28","name":"Report Node Red Uptime","topic":"","qos":"0","retain":"false","broker":"8552f64b.80b9c","x":1090,"y":860,"wires":[]},{"id":"6bb02587.c25e04","type":"mqtt in","z":"f10ef821.804e28","name":"Report Node Red Uptime","topic":"/system/os/service/nodeRed/uptime","qos":"2","datatype":"auto","broker":"8552f64b.80b9c","x":250,"y":920,"wires":[["e5474926.b92108"]]},{"id":"e5474926.b92108","type":"debug","z":"f10ef821.804e28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1050,"y":920,"wires":[]},{"id":"8552f64b.80b9c","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","tls":null,"clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":"","willTopic":"","willQos":"0","willRetain":null,"willPayload":""}]

Flow Info

Created 5 years, 10 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • comment (x1)
  • debug (x5)
  • function (x3)
  • inject (x3)
  • mqtt in (x2)
  • mqtt out (x2)
  • mqtt-broker (x1)
Other
  • Uptime (x1)

Tags

  • service
  • os
  • uptime
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option