Surveillance de la disponibilité du site Web avec rapports Twitter sur Node-RED

Disons que vous avez un site de forum et qu'il plante pendant quelques minutes, mais 24/7. Il est nécessaire d'avertir en quelque sorte les visiteurs du site que le problème a commencé et que le problème est terminé, afin qu'ils ne s'inquiètent pas et ne pensent pas que quelque chose est cassé pour eux! J'utiliserai mon compte Twitter comme canal de communication de secours. Node-Red est un système de programmation visuelle dans lequel il est facile de commencer à programmer en organisant et en connectant divers nœuds. Il est souvent utilisé pour l'IOT.



hh-hocher la tête-001



Avant le début



Vous devriez déjà avoir Node-Red installé avec le nœud moment. Différentes méthodes d'installation sont décrites sur le site développeur /nodered.org/docs/getting-started /



Importez le code de flux dans votre Node-RED et vous aurez tous les nœuds comme sur l'image



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




Que fait ce flux



  • Vérifie s'il y a un accès Internet
  • Vérifie s'il y a accès au site
  • Si aucun accès / tweet restauré à ce sujet
  • Compte depuis combien de temps le site est inactif
  • Écrit un journal dans un fichier


Ce qui doit être configuré



hh-hocher la tête-002



  • nœud 1: vous devez écrire l'adresse du site pour la surveillance
  • Nœuds 2: vous devez définir la première partie du texte pour le tweet
  • nœuds 3: Ici, des données temporaires sont ajoutées au tweet, vous pouvez également ajouter votre propre texte
  • - nœuds 4: Ici, vous devez définir votre compte Twitter et le dossier du fichier journal après les modifications, n'oubliez pas de cliquer sur Déployer


Comment ça fonctionne



Nous partons du haut à gauche



  • [timestamp] node - écrit l'heure dans la variable de temps
  • node [Started!] - réinitialise les compteurs d'événements au début, et cocks déclenche
  • node [1 min] - démarre le processus de surveillance toutes les minutes.


Tout d'abord, nous allons vérifier si les nœuds Internet [google.com]> [Vérifier si mon Internet est actif] si nous recevons une réponse de 200, alors Internet fonctionne pour nous, sinon, soit Internet ne fonctionne pas pour nous, soit google.com est allongé



Ensuite, nous vérifions notre site [name.com]> ([mot trouvé sur le web] [not 200 respons]). Ici, nous ne pouvons pas simplement attendre une réponse 200, elle peut également être émise par des pages stub (par exemple, non-paiement pour l'hébergement) - par conséquent, le site est considéré comme fonctionnel si un mot est trouvé dans son contenu - j'ai cet identifiant Google. S'il n'y a pas de réponse 200 (le site ne fonctionne pas du tout ou donne 404 erreurs, etc.), alors nous considérons que le site ne fonctionne pas.



Ensuite, nous commençons à compter ces événements, si 3 messages sur un site inopérant arrivent d'affilée, alors il est transmis au déclencheur - qui saute un message et est bloqué - afin qu'il n'y ait pas beaucoup de tweets sur le même. Pour que le déclencheur envoie un tweet pour que tout fonctionne correctement, vous avez besoin de 5 messages d'affilée sur le travail du site. Les compteurs de messages se réinitialisent lorsqu'un message se produit, cela fournit un décompte de 3 à 5 messages d'affilée.

La gâchette est déclenchée lorsque l'opposé s'ouvre pour recevoir.



All Articles