Допустим у вас есть сайт-форум и он падает на пару минут, но 24/7. Надо как-то предупреждать посетителей сайта что проблема началась и проблема кончилась, чтобы не переживали и не думал что это у них что-то сломалось! В качестве резервного канала общения буду использовать Twitter аккаунт. Node-Red визуальная система программирования, в которой легко начать программировать расставляя и соединяя различные ноды. Её часто используют для IOT.

hh-nodered-001

Перед началом


У вас уже должен быть установлен Node-Red c нодой moment. Различные способы установки описаны на сайте разработчиков /nodered.org/docs/getting-started/

Импортите код flow в свой 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":[]}]


Что делает этот flow


  • Проверяет есть ли доступ в Интернет
  • Проверяет есть ли доступ к сайту
  • Если доступа нет/восстановлен твитит об этом
  • Считает сколько времени сайт простаивал
  • Пишет в файл лог

Что нужно настроить


hh-nodered-002

  • нода 1: Надо написать адрес сайта для мониторинга
  • ноды 2: Надо задать первую часть текста для твита
  • ноды 3: Здесь к твиту добавляются временные данные, так же можно дописать свой текст
  • — ноды 4: Здесь надо задать свой аккаунт Twitter и папку для файла логов после изменений не забывайте наживать Deploy

Как это работает


Начинаем слева с верху

  • нода [timestamp] — пишет время в переменную time
  • нода [Started!] — обнуляет счётчики событий на старте, и взводит тригеры
  • нода [1 min] — запускает каждую минуту процесс мониторинга.

Первым делом проверем работает ли у нас интернет ноды [google.com] > [Check if my Internet is up] если получаем ответ 200 значит интернет у нас работает, если нет то или у нас интернет не работает или google.com прилег

Далее проверяем свой сайт [name.com] > ( [found word on web] [not 200 respons] ). Тут нам нельзя просто ждать ответа 200, его могут выдавать и страницы заглушки(например неоплата хостинга) — поэтому сайт считается работающим если найдено слово в его контенте — у меня это идентификатор Google. Если нет ответа 200(сайт вообще не работает или выдает ошибки 404 и пр) то считаем что сайт не работает.

Далее начинаем считать эти события, если подряд приходят 3 сообщения о неработающем сайте, то оно передаётся дальше на триггер — который пропускает одно сообщение и блокируется — чтобы не было много твитов об одном и том же. Чтобы сработал триггер отправки твита что все ОК нужно подряд 5 сообщения о работе сайта. Счетчики сообщений обнуляют друга друга при возникновении сообщения, это обеспечивает подсчёт 3-5 сообщения подряд.
Триггер срабатывает когда открывает противоположный на прием.