通过Twitter在Node-RED上进行报告来监视网站的可用性

假设您有一个论坛站点,它崩溃了几分钟,但崩溃的时间是24/7。有必要以某种方式警告站点访问者该问题已经开始并且问题已经结束,以便他们不必担心,也不要认为事情对他们有影响!我将使用我的Twitter帐户作为备用通信渠道。Node-Red是一种可视化编程系统,在其中可以轻松地通过排列和连接各个节点来开始编程。它通常用于物联网。



hh-nodered-001



开始之前



您应该已经在矩节点上安装了Node-Red。在开发人员站点/nodered.org/docs/getting-started上描述了各种安装方法/



将流代码导入到Node-RED中,您将拥有所有节点,如图所示



[{"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":[]}]




这个流程是做什么的



  • 检查是否可以上网
  • 检查是否可以访问该站点
  • 如果无法访问/已恢复有关它的推文
  • 计算站点闲置多长时间
  • 将日志写入文件


需要配置什么



hh-nodered-002



  • 节点1:您需要写站点地址进行监视
  • 节点2:您需要为推文设置文本的第一部分
  • 节点3:此处将临时数据添加到推文中,您也可以添加自己的文本
  • -节点4:更改后,您需要在此处设置您的Twitter帐户和日志文件的文件夹,不要忘记单击Deploy。


怎么运行的



我们从左上方开始



  • [timestamp]节点-将时间写入时间变量
  • [Started!]节点-在开始时重置事件计数器,并设置触发器
  • 节点[1分钟]-每分钟开始监视过程。


首先,我们将检查Internet节点[google.com]> [检查我的Internet是否可用],如果我们收到200的响应,则表明Internet对我们有用,如果没有,则表明Internet对我们不起作用或google.com处于关闭状态



接下来,我们检查我们的网站[name.com]>([在网络上找到字词] [没有200条回应])。在这里,我们不仅要等待200个响应,还可以通过存根页面(例如,不支付托管费用)发出该响应-因此,如果在其内容中找到一个单词,则该站点将被视为正常工作-我有此Google ID。如果没有答案200(该站点根本无法运行或出现404错误等),则我们认为该站点无法运行。



接下来,我们开始对这些事件进行计数,如果连续出现3条有关无效站点的消息,则将其传递到触发器-跳过一条消息并被阻止-这样就不会有太多推文。为了使触发器发送一条推文以确保一切正常,您需要连续5条关于该站点工作的消息。发生消息时,消息计数器会彼此重置,从而连续提供3-5条消息的计数。

当另一方打开接收时,将触发该触发器。



All Articles