Website availability monitoring with Twitter reporting on Node-RED

Let's say you have a forum site and it crashes for a couple of minutes, but 24/7. It is necessary to somehow warn site visitors that the problem has begun and the problem is over, so that they do not worry and do not think that something is broken for them! I will use my Twitter account as a backup communication channel. Node-Red is a visual programming system in which it is easy to start programming by arranging and connecting various nodes. It is often used for IOT.



hh-nodered-001



Before the beginning



You should already have Node-Red installed with the moment node. Various installation methods are described on the developer site /nodered.org/docs/getting-started /



Import the flow code into your Node-RED and you will have all the nodes as in the picture



[{"id":"ff8e6c15.1db9e","type":"inject","z":"c20c2bf8.f5bba8","name":"1 min","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":240,"wires":[["aa173003.800e9"]]},{"id":"fa2dd78f.64ef58","type":"http request","z":"c20c2bf8.f5bba8","name":"name.com","method":"GET","ret":"txt","url":"https://name.com","tls":"","x":210,"y":500,"wires":[["3c3eb473.fddb4c","6c05ab54.c151a4"]]},{"id":"3c3eb473.fddb4c","type":"switch","z":"c20c2bf8.f5bba8","name":"not 200 respons","property":"statusCode","propertyType":"msg","rules":[{"t":"neq","v":"200","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":440,"y":520,"wires":[["5a06f9e1.e303d8","cc3ecdf3.6ce24"]]},{"id":"6c05ab54.c151a4","type":"switch","z":"c20c2bf8.f5bba8","name":"found word on web","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ca-pub-1234545647","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":450,"y":480,"wires":[["834b9f2b.cfb89","cc3ecdf3.6ce24"]]},{"id":"107dec81.7d3953","type":"function","z":"c20c2bf8.f5bba8","name":"web up","func":"\nreturn {\n    payload: ` ${msg.responseUrl} its OK`\n}","outputs":1,"noerr":0,"x":1140,"y":420,"wires":[["e3f88e59.88023"]]},{"id":"d5bf4f53.b8ae2","type":"function","z":"c20c2bf8.f5bba8","name":"web down","func":"\nreturn {\n    payload: ` name.com it is DOWN at`\n}","outputs":1,"noerr":0,"x":1140,"y":620,"wires":[["300022a2.3d497e"]]},{"id":"ddeb8430.749ed8","type":"inject","z":"c20c2bf8.f5bba8","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":100,"wires":[["e82f93c1.0e89e"]]},{"id":"e82f93c1.0e89e","type":"moment","z":"c20c2bf8.f5bba8","name":"","topic":"","input":"","inputType":"msg","inTz":"Europe/Moscow","adjAmount":"0","adjType":"hours","adjDir":"add","format":"date","locale":"en_US","output":"time","outputType":"flow","outTz":"Europe/Moscow","x":460,"y":100,"wires":[[]]},{"id":"4c032cf4.424084","type":"file","z":"c20c2bf8.f5bba8","name":"Write www.log","filename":"/root/iot_data/www.log","appendNewline":true,"createDir":false,"overwriteFile":"false","x":1820,"y":480,"wires":[[]]},{"id":"834e384f.425cb8","type":"function","z":"c20c2bf8.f5bba8","name":"Add time to data","func":"var timenow=flow.get('time');\nvar downtimenow=flow.get('downmin')-5;\nmsg.payload = msg.payload + ' ' + timenow + '. Downtime: ' + downtimenow + 'min #myhost' ;\nreturn msg;","outputs":1,"noerr":0,"x":1640,"y":500,"wires":[["4c032cf4.424084","4c25ef47.71b94"]]},{"id":"e3f88e59.88023","type":"trigger","z":"c20c2bf8.f5bba8","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"0","extend":true,"units":"min","reset":"reset","bytopic":"all","name":"","x":1360,"y":420,"wires":[["e24a68a2.c83f58"]]},{"id":"300022a2.3d497e","type":"trigger","z":"c20c2bf8.f5bba8","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"0","extend":true,"units":"min","reset":"reset","bytopic":"all","name":"","x":1360,"y":620,"wires":[["419d4a24.ec1604","11c46064.1ffc7"]]},{"id":"aac1daf2.c77e18","type":"function","z":"c20c2bf8.f5bba8","name":"send reset","func":"\nreturn {\n    payload: `reset`\n}","outputs":1,"noerr":0,"x":1150,"y":540,"wires":[["e3f88e59.88023"]]},{"id":"59f336.30325ccc","type":"function","z":"c20c2bf8.f5bba8","name":"send reset","func":"\nreturn {\n    payload: `reset`\n}","outputs":1,"noerr":0,"x":1150,"y":480,"wires":[["300022a2.3d497e"]]},{"id":"5a06f9e1.e303d8","type":"counter","z":"c20c2bf8.f5bba8","name":"count to 10","init":"0","step":"1","lower":"0","upper":"10","mode":"increment","outputs":1,"x":690,"y":660,"wires":[["e93a8fe8.8bb23"]]},{"id":"323380d1.a71af","type":"switch","z":"c20c2bf8.f5bba8","name":"if its 3","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"3","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":990,"y":660,"wires":[["d5bf4f53.b8ae2","aac1daf2.c77e18"]]},{"id":"24d1969a.a4e5da","type":"function","z":"c20c2bf8.f5bba8","name":"reset counter","func":"msg.reset = true; return msg","outputs":1,"noerr":0,"x":890,"y":440,"wires":[["5a06f9e1.e303d8"]]},{"id":"834b9f2b.cfb89","type":"counter","z":"c20c2bf8.f5bba8","name":"count to 10","init":"0","step":"1","lower":"0","upper":"10","mode":"increment","outputs":1,"x":670,"y":380,"wires":[["641f8b3.143cd74"]]},{"id":"440de7c7.eb6d48","type":"function","z":"c20c2bf8.f5bba8","name":"reset counter","func":"msg.reset = true; return msg","outputs":1,"noerr":0,"x":850,"y":580,"wires":[["834b9f2b.cfb89"]]},{"id":"1b410fb8.f2e8b","type":"switch","z":"c20c2bf8.f5bba8","name":"if its 5","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"5","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":990,"y":380,"wires":[["107dec81.7d3953","59f336.30325ccc"]]},{"id":"641f8b3.143cd74","type":"switch","z":"c20c2bf8.f5bba8","name":"it its > 0","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":840,"y":380,"wires":[["24d1969a.a4e5da","1b410fb8.f2e8b"]]},{"id":"e93a8fe8.8bb23","type":"switch","z":"c20c2bf8.f5bba8","name":"it its > 0","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":840,"y":660,"wires":[["323380d1.a71af","440de7c7.eb6d48"]]},{"id":"e24a68a2.c83f58","type":"switch","z":"c20c2bf8.f5bba8","name":"not a on start web up message","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"Started!","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1650,"y":420,"wires":[["834e384f.425cb8"]]},{"id":"2ef6b867.dd9328","type":"inject","z":"c20c2bf8.f5bba8","name":"","topic":"","payload":"Started!","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":"","x":240,"y":160,"wires":[["e3f88e59.88023","24d1969a.a4e5da","440de7c7.eb6d48"]]},{"id":"f3ac9999.c58e48","type":"switch","z":"c20c2bf8.f5bba8","name":"Check if my Internet is up","property":"statusCode","propertyType":"msg","rules":[{"t":"eq","v":"200","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":230,"y":420,"wires":[["fa2dd78f.64ef58"]]},{"id":"aa173003.800e9","type":"http request","z":"c20c2bf8.f5bba8","name":"google.com","method":"GET","ret":"txt","url":"https://www.google.com/","tls":"","x":210,"y":320,"wires":[["f3ac9999.c58e48"]]},{"id":"cc3ecdf3.6ce24","type":"counter","z":"c20c2bf8.f5bba8","name":"count to  3000","init":"3","step":"1","lower":"0","upper":"3000","mode":"increment","outputs":1,"x":1360,"y":160,"wires":[["aeda28f4.55d248"]]},{"id":"419d4a24.ec1604","type":"function","z":"c20c2bf8.f5bba8","name":"reset counter","func":"msg.reset = true; return msg","outputs":1,"noerr":0,"x":1370,"y":240,"wires":[["cc3ecdf3.6ce24"]]},{"id":"e540e4d1.e10a98","type":"file","z":"c20c2bf8.f5bba8","name":"Write www.log","filename":"/root/iot_data/www.log","appendNewline":true,"createDir":false,"overwriteFile":"false","x":1820,"y":600,"wires":[[]]},{"id":"11c46064.1ffc7","type":"function","z":"c20c2bf8.f5bba8","name":"Add time to data","func":"var timenow=flow.get('time');\nmsg.payload = msg.payload + ' ' + timenow + ' #myhost';\nreturn msg;","outputs":1,"noerr":0,"x":1600,"y":620,"wires":[["e540e4d1.e10a98","c51c74c3.bdf448"]]},{"id":"aeda28f4.55d248","type":"function","z":"c20c2bf8.f5bba8","name":"Set downmin","func":"flow.set(\"downmin\", msg.count);","outputs":1,"noerr":0,"x":1550,"y":160,"wires":[[]]},{"id":"c51c74c3.bdf448","type":"twitter out","z":"c20c2bf8.f5bba8","twitter":"","name":"Tweet","x":1790,"y":640,"wires":[]},{"id":"4c25ef47.71b94","type":"twitter out","z":"c20c2bf8.f5bba8","twitter":"","name":"Tweet","x":1790,"y":520,"wires":[]}]




What does this flow do



  • Checks if there is Internet access
  • Checks if there is access to the site
  • If no access / restored tweet about it
  • Counts how long the site has been idle
  • Writes a log to a file


What needs to be configured



hh-nodered-002



  • node 1: You need to write the site address for monitoring
  • Nodes 2: You need to set the first part of the text for the tweet
  • Nodes 3: Here temporary data is added to the tweet, you can also add your own text
  • - nodes 4: Here you need to set your Twitter account and the folder for the log file after the changes, do not forget to click Deploy


How it works



We start from the top left



  • [timestamp] node - writes time to the time variable
  • [Started!] node - resets the event counters at the start, and sets triggers
  • node [1 min] - starts the monitoring process every minute.


First of all, we will check whether the Internet nodes [google.com]> [Check if my Internet is up] if we receive a response of 200, then the Internet is working for us, if not, then either the Internet does not work for us or google.com is lying down



Next, we check our website [name.com]> ([found word on web] [not 200 respons]). Here we cannot just wait for a 200 response, it can also be issued by stub pages (for example, non-payment for hosting) - therefore, the site is considered working if a word is found in its content - I have this Google ID. If there is no answer 200 (the site does not work at all or gives 404 errors, etc.) then we consider that the site is not working.



Next, we start counting these events, if 3 messages about an inoperative site come in a row, then it is passed on to the trigger - which skips one message and is blocked - so that there are not many tweets about the same. For the trigger to send a tweet to work that everything is OK, you need 5 messages in a row about the site's work. The message counters reset each other when a message occurs, this provides a count of 3-5 messages in a row.

The trigger is fired when the opposite one opens to receive.



All Articles