ITCooky Recipes

Lets cooky it yammy things!

Lets monitor website with Node-Red and reports on Twitter!

дата April 18, 2020

I noticed that the marketing department of any hoster is more concerned with the quality of the service than the IT department! And they even respond on Twitter and they even promise to improve something … That on the one hand, on the other hand, when my site is dead, I don’t understand how it happened and when – that’s why I search on Twitter for the answer: if a meteorite fell on M9 or crooked hands again! So I decided to help myself and the marketing specialists to automatically report to Twitter when the site dies. I’ll do it on Node-Red, this is a Scratch adult analog! Image for a better understanding:

As the base I will use this wonderful article. dev.to/webhookrelay/ creating-your-own-health-check-monitor-with-node-red-51fn , but I did almost everything differentlye!

Node-Red is already installed, you must also install it use that article IoT по-взрослому: на ESP8266 по MQTT через Mosquitto в Node-RED! . Everything is operating in Orange Pi Plus

Here is the solution. We ask the website if it works or not, if it says no 5 times in a row, we tweet its status!

Code

[{"id":"3de70177.6f5dce","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"c38790b7.a3975","type":"inject","z":"3de70177.6f5dce","name":"1 min","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":600,"wires":[["3f78434e.8b637c"]]},{"id":"3f78434e.8b637c","type":"http request","z":"3de70177.6f5dce","name":"itcooky.com","method":"GET","ret":"txt","url":"https://itcooky.com/test.html","tls":"","x":330,"y":600,"wires":[["ceebbb5c.62eaf8","f87f78c5.18fa88"]]},{"id":"ceebbb5c.62eaf8","type":"switch","z":"3de70177.6f5dce","name":"not 200 respons","property":"statusCode","propertyType":"msg","rules":[{"t":"neq","v":"200","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":500,"y":620,"wires":[["ea702142.5db75"]]},{"id":"f87f78c5.18fa88","type":"switch","z":"3de70177.6f5dce","name":"found word on web","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"good","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":580,"wires":[["178958c1.6fbce7"]]},{"id":"93035dbe.000c6","type":"function","z":"3de70177.6f5dce","name":"web up","func":"\nreturn {\n    payload: ` ${msg.responseUrl} its OK`\n}","outputs":1,"noerr":0,"x":1080,"y":500,"wires":[["d01ee6d3.843018"]]},{"id":"330afeba.4f7082","type":"function","z":"3de70177.6f5dce","name":"web down","func":"\nreturn {\n    payload: ` ${msg.responseUrl} it is DOWN at`\n}","outputs":1,"noerr":0,"x":1080,"y":700,"wires":[["c245ac03.ce738"]]},{"id":"454d2f34.514b8","type":"inject","z":"3de70177.6f5dce","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":380,"wires":[["fed3dc57.4834b"]]},{"id":"fed3dc57.4834b","type":"moment","z":"3de70177.6f5dce","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":380,"y":380,"wires":[[]]},{"id":"3b35c8fb.f5e9d8","type":"file","z":"3de70177.6f5dce","name":"Write www.log","filename":"/root/iot_data/www.log","appendNewline":true,"createDir":false,"overwriteFile":"false","x":1540,"y":580,"wires":[[]]},{"id":"f3cb04e1.0ca058","type":"function","z":"3de70177.6f5dce","name":"Add time to data","func":"var timenow=flow.get('time');\nmsg.payload = msg.payload + ' ' + timenow + ' #itcookycomstatus';\nreturn msg;","outputs":1,"noerr":0,"x":1340,"y":600,"wires":[["3b35c8fb.f5e9d8","e208bc85.2ac83"]]},{"id":"d01ee6d3.843018","type":"trigger","z":"3de70177.6f5dce","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"0","extend":true,"units":"min","reset":"reset","bytopic":"all","name":"","x":1300,"y":500,"wires":[["f3cb04e1.0ca058"]]},{"id":"c245ac03.ce738","type":"trigger","z":"3de70177.6f5dce","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"0","extend":true,"units":"min","reset":"reset","bytopic":"all","name":"","x":1300,"y":700,"wires":[["f3cb04e1.0ca058"]]},{"id":"707304cf.d7390c","type":"function","z":"3de70177.6f5dce","name":"send reset","func":"\nreturn {\n    payload: `reset`\n}","outputs":1,"noerr":0,"x":1090,"y":620,"wires":[["d01ee6d3.843018"]]},{"id":"a8927f54.37138","type":"function","z":"3de70177.6f5dce","name":"send reset","func":"\nreturn {\n    payload: `reset`\n}","outputs":1,"noerr":0,"x":1090,"y":560,"wires":[["c245ac03.ce738"]]},{"id":"ea702142.5db75","type":"counter","z":"3de70177.6f5dce","name":"count to 10","init":"0","step":"1","lower":"0","upper":"10","mode":"increment","outputs":1,"x":630,"y":740,"wires":[["4a9cb8c8.819ab8"]]},{"id":"ae37990e.ebade8","type":"switch","z":"3de70177.6f5dce","name":"if its 5","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"5","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":930,"y":740,"wires":[["330afeba.4f7082","707304cf.d7390c"]]},{"id":"d9fd593b.9b8998","type":"function","z":"3de70177.6f5dce","name":"reset counter","func":"msg.reset = true; return msg","outputs":1,"noerr":0,"x":830,"y":520,"wires":[["ea702142.5db75"]]},{"id":"178958c1.6fbce7","type":"counter","z":"3de70177.6f5dce","name":"count to 10","init":"0","step":"1","lower":"0","upper":"10","mode":"increment","outputs":1,"x":610,"y":460,"wires":[["173d5018.b4d1d"]]},{"id":"88d5dc8d.ba9eb","type":"function","z":"3de70177.6f5dce","name":"reset counter","func":"msg.reset = true; return msg","outputs":1,"noerr":0,"x":790,"y":660,"wires":[["178958c1.6fbce7"]]},{"id":"3252995a.ea5296","type":"switch","z":"3de70177.6f5dce","name":"if its 5","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"5","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":930,"y":460,"wires":[["93035dbe.000c6","a8927f54.37138"]]},{"id":"173d5018.b4d1d","type":"switch","z":"3de70177.6f5dce","name":"it its > 0","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":780,"y":460,"wires":[["d9fd593b.9b8998","3252995a.ea5296"]]},{"id":"4a9cb8c8.819ab8","type":"switch","z":"3de70177.6f5dce","name":"it its > 0","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":780,"y":740,"wires":[["ae37990e.ebade8","88d5dc8d.ba9eb"]]},{"id":"e208bc85.2ac83","type":"twitter out","z":"3de70177.6f5dce","name":"Tweet","x":1510,"y":620,"wires":[]}]


Brick 0

They form a variable over time. The brick on the left kicks the brick on the right every second.

The second brick overlays the data format and inserts it into the flow.time global variable for this sequence; will appear on brick 7
Be careful moment , this is not a standard brick, I install it separately


Brick 1

To his left is a brick that kicks him every minute! To change the interval, change every

This is a brick http he (when left brick kicks it) asks for https://itcooky.com/test.html and passes the answer


Brick 2

These are the brick switches that pass the answer forward according to the appropriate condition
The bottom brick not 200 respos is activated if the response to the page request came without the 200 code (everything is fine)

The top brick is looking for the word good on the page, if so it means it is my page and not some other


Brick 3

counetr This is not a standard brick, it must be installed separately. Here it counts from 0 to 10, and receives a signal from brick 5 to reset the score to zero! Every time the value increases, it sends this value forward and simultaneously resets the neighbors counter to zero, so it starts counting from zero when its time comes.


Brick 3.1

This brick bypasses the loop, if you reset the counters without it then the counters start resetting each other in a loop!

Brick 4
This normal switch is activated when the counter is raised to 5. After that, the signal is believed to have worked, the site is not working (or working) at the same time a message is sent, and the triggerr works (or doesn’t work) is reset so that it was ready to work when the status changed, after receiving 5 dropped messages of different status.


Brick 5

This is a function brick that forms a message that serves to reset the counter


Brick 6

It is just a function that collects the text of the message to transmit


Brick 7

This is a wonderful brick, it also just forms the text and sends the word reset when one of bricks 4 is activated. This is important because then brick 8 is the trigger which works here like this: it passes the first message and blocks all the following until it receives a reset cancellation signal. This is necessary so that: it only passes one message to Twitter, the trigger is blocked and it no longer passes those messages, then the state changes again and, together with the new text, the signal to unlock the trigger went to the previous trigger. This allows you not to send a bunch of identical messages.


Brick 8

Its trigger

By the way, a triggered trigger is marked, now in the photo next to the upper trigger there is a small blue square


Brick 9

It is also just function form the text take the received message msg.payload and add the time – the variable flow.time and some other text in ‘ ‘


Brick 10

This brick writes messages to a file www.log


Brick 11

This is a tweet brick. In setup, you have four API keys there: it took the same as for tweeting through WordPress

After clicking Deploy , everything starts working. I simulate the necessary conditions by deleting the test.html file and restoring it with the text good – it works!

UPDATE: Some improve monitoring, get rid of unnecessary messages!
Unwanted messages appear by:
– at first start, messages pass – site is working
– if the server with the monitoring system has internet problems, after they disappear, it also passes a message: the site is working

I add a couple of bricks to the circuit

Code:

[{"id":"5ae667a8.990c28","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"d86f0b01.fc1078","type":"inject","z":"5ae667a8.990c28","name":"1 min","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":820,"wires":[["b992cd67.2391f"]]},{"id":"9a8d7311.e507a","type":"http request","z":"5ae667a8.990c28","name":"itcooky.com","method":"GET","ret":"txt","url":"https://itcooky.com","tls":"","x":430,"y":900,"wires":[["925113a1.d444b","2f4d784f.f2ebe8"]]},{"id":"925113a1.d444b","type":"switch","z":"5ae667a8.990c28","name":"not 200 respons","property":"statusCode","propertyType":"msg","rules":[{"t":"neq","v":"200","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":920,"wires":[["9cd19e03.225d6"]]},{"id":"2f4d784f.f2ebe8","type":"switch","z":"5ae667a8.990c28","name":"found word on web","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ca-pub-8951271257139515","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":880,"wires":[["436faffb.4b885"]]},{"id":"d46f96d3.12d7d8","type":"function","z":"5ae667a8.990c28","name":"web up","func":"\nreturn {\n    payload: ` ${msg.responseUrl} its OK`\n}","outputs":1,"noerr":0,"x":1180,"y":800,"wires":[["efb7ffad.fda06"]]},{"id":"be5c108b.3eab5","type":"function","z":"5ae667a8.990c28","name":"web down","func":"\nreturn {\n    payload: ` ${msg.responseUrl} it is DOWN at`\n}","outputs":1,"noerr":0,"x":1180,"y":1000,"wires":[["9267dbd8.407258"]]},{"id":"1a284ae1.1dcac5","type":"inject","z":"5ae667a8.990c28","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":680,"wires":[["f7482ea6.8105b"]]},{"id":"f7482ea6.8105b","type":"moment","z":"5ae667a8.990c28","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":480,"y":680,"wires":[[]]},{"id":"af783a84.80d858","type":"file","z":"5ae667a8.990c28","name":"Write www.log","filename":"/root/iot_data/www.log","appendNewline":true,"createDir":false,"overwriteFile":"false","x":1660,"y":820,"wires":[[]]},{"id":"9639759c.7c6198","type":"function","z":"5ae667a8.990c28","name":"Add time to data","func":"var timenow=flow.get('time');\nmsg.payload = msg.payload + ' ' + timenow + ' #itcookycomstatus #RUVDS https://itcooky.com/?p=5624';\nreturn msg;","outputs":1,"noerr":0,"x":1700,"y":900,"wires":[["af783a84.80d858","3957fc42.0c4ab4"]]},{"id":"efb7ffad.fda06","type":"trigger","z":"5ae667a8.990c28","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"0","extend":true,"units":"min","reset":"reset","bytopic":"all","name":"","x":1400,"y":800,"wires":[["3579b0a7.3e3bf"]]},{"id":"9267dbd8.407258","type":"trigger","z":"5ae667a8.990c28","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"0","extend":true,"units":"min","reset":"reset","bytopic":"all","name":"","x":1400,"y":1000,"wires":[["3579b0a7.3e3bf"]]},{"id":"dba48d4d.0d8b6","type":"function","z":"5ae667a8.990c28","name":"send reset","func":"\nreturn {\n    payload: `reset`\n}","outputs":1,"noerr":0,"x":1190,"y":920,"wires":[["efb7ffad.fda06"]]},{"id":"462aca20.8d45d4","type":"function","z":"5ae667a8.990c28","name":"send reset","func":"\nreturn {\n    payload: `reset`\n}","outputs":1,"noerr":0,"x":1190,"y":860,"wires":[["9267dbd8.407258"]]},{"id":"9cd19e03.225d6","type":"counter","z":"5ae667a8.990c28","name":"count to 10","init":"0","step":"1","lower":"0","upper":"10","mode":"increment","outputs":1,"x":730,"y":1040,"wires":[["3ca7e769.d56e28"]]},{"id":"8bf834a2.7a2388","type":"switch","z":"5ae667a8.990c28","name":"if its 5","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"5","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1030,"y":1040,"wires":[["be5c108b.3eab5","dba48d4d.0d8b6"]]},{"id":"35d0b846.bc61e8","type":"function","z":"5ae667a8.990c28","name":"reset counter","func":"msg.reset = true; return msg","outputs":1,"noerr":0,"x":930,"y":820,"wires":[["9cd19e03.225d6"]]},{"id":"436faffb.4b885","type":"counter","z":"5ae667a8.990c28","name":"count to 10","init":"0","step":"1","lower":"0","upper":"10","mode":"increment","outputs":1,"x":710,"y":760,"wires":[["748e6fa9.a3619"]]},{"id":"5ca50054.d7b44","type":"function","z":"5ae667a8.990c28","name":"reset counter","func":"msg.reset = true; return msg","outputs":1,"noerr":0,"x":890,"y":960,"wires":[["436faffb.4b885"]]},{"id":"fca0c7c6.d82e68","type":"switch","z":"5ae667a8.990c28","name":"if its 5","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"5","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1030,"y":760,"wires":[["d46f96d3.12d7d8","462aca20.8d45d4"]]},{"id":"748e6fa9.a3619","type":"switch","z":"5ae667a8.990c28","name":"it its > 0","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":880,"y":760,"wires":[["35d0b846.bc61e8","fca0c7c6.d82e68"]]},{"id":"3ca7e769.d56e28","type":"switch","z":"5ae667a8.990c28","name":"it its > 0","property":"count","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":880,"y":1040,"wires":[["8bf834a2.7a2388","5ca50054.d7b44"]]},{"id":"2cb243e5.13928c","type":"inject","z":"5ae667a8.990c28","name":"","topic":"","payload":"Started!","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":"","x":280,"y":540,"wires":[["efb7ffad.fda06"]]},{"id":"3579b0a7.3e3bf","type":"switch","z":"5ae667a8.990c28","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":1470,"y":900,"wires":[["9639759c.7c6198"]]},{"id":"b992cd67.2391f","type":"http request","z":"5ae667a8.990c28","name":"google.com","method":"GET","ret":"txt","url":"https://www.google.com/","tls":"","x":310,"y":1020,"wires":[["2e2214d1.0eda6c"]]},{"id":"2e2214d1.0eda6c","type":"switch","z":"5ae667a8.990c28","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":330,"y":1080,"wires":[["9a8d7311.e507a"]]},{"id":"3957fc42.0c4ab4","type":"twitter out","z":"5ae667a8.990c28","name":"Tweet","x":1700,"y":1000,"wires":[]}]

In this scheme, I request a site, not a specific file, and search for the Google ID as text; the meaning of this is, for example, that the money ran out and the hoster put on my website a “pay damn” message, the monitoring may think that the site is still working ok!

Brick 1
Just send a Start! message at the moment of script start

Brick 1.1
The message reaches the trigger, it blocks, but passes message. Brick 1.1 gets this message, if its only the text Start! so it doesn’t tweet, so we get rid of the tweets when the script starts

Brick 2
Request google.com if its alive should give the status of 200

Brick 2.1
Receive the answer, if it is not 200 we flow stops, it means that we have a problem with the Internet or Google stopped working, but most likely we have a problem with our Internet!

UPDATE: I add a website down state minute count!
It is also useful to know how long the website was unavailable, when the problem started you can see ir by the date of the tweet, and here it should be noted that the message about the problems comes after three confirmations (3 minutes), and when the web comes back, they are 5.

Brick 1
This is a counter, it is connected to bricks with the state of the site. And a reset, when a signal comes from it, it resets to 3

Brick 2
This is a function that writes the value of brick counter 1 to a variable downmin

Brick 3
This brick sends reset to brick only when message that the site is down is send. Since the site deems unavailable with 3 minutes of inactivity, brick 1 is thrown at value 3

Brick 4
And the brick with the text of the tweet

var timenow=flow.get('time');
var downtimenow=flow.get('downmin')-5;
msg.payload = msg.payload + ' ' + timenow + '. Downtime: ' + downtimenow + '  #itcookycomstatus';
return msg;

Here we calculate how many minutes the site actually worked, minus 5: the number of good statuses through which the status up site tweet is sent


Leave a Reply

Your email address will not be published. Required fields are marked *