Check if ping fails 3 times

I send ping to hosts and check if fails, and I have a lot of alarms.
How to check if ping false 3 times. When only 2 times or 1 times reset counter to 0.

[{"id":"f7fb5a53.530398","type":"inject","z":"1842eb59.625d05","name":"array of objects with host ip and name","topic":"","payload":"[{\"name\":\"T1\",\"host\":\"10.0.0.71\",\"opis_ip_alarm\":\"ip alarm 1\"},{\"name\":\"T2\",\"host\":\"10.0.0.72\",\"opis_ip_alarm\":null}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":440,"wires":[["fa19be08.bdf9b8"]]},{"id":"dee73236.1b36f","type":"debug","z":"1842eb59.625d05","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":440,"wires":[]},{"id":"fa19be08.bdf9b8","type":"ping","z":"1842eb59.625d05","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":470,"y":440,"wires":[["dee73236.1b36f"]]}]

for tests I create something like this

[{"id":"482675f2.756a44","type":"inject","z":"1842eb59.625d05","name":"","topic":"10.0.0.1","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":220,"wires":[["bb3f6ca3.d3fae8"]]},{"id":"bb3f6ca3.d3fae8","type":"function","z":"1842eb59.625d05","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":280,"wires":[["d252ecf8.8d0358","df2ce6ec.8cbf28"]]},{"id":"6ba48bec.f5a0ec","type":"inject","z":"1842eb59.625d05","name":"","topic":"10.0.0.2","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":340,"wires":[["bb3f6ca3.d3fae8"]]},{"id":"1fae7899.5f01af","type":"inject","z":"1842eb59.625d05","name":"","topic":"10.0.0.1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":260,"wires":[["bb3f6ca3.d3fae8"]]},{"id":"be9a5e97.c1c1a8","type":"inject","z":"1842eb59.625d05","name":"","topic":"10.0.0.2","payload":"false","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":300,"wires":[["bb3f6ca3.d3fae8"]]},{"id":"d252ecf8.8d0358","type":"debug","z":"1842eb59.625d05","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":470,"y":200,"wires":[]},{"id":"df2ce6ec.8cbf28","type":"function","z":"1842eb59.625d05","name":"","func":"var arr = flow.get('arr1') || [];\nvar counter = 0;\nvar isExist = 0;\n\nif (!arr.length){\n    arr.push({\n            \"ip\": msg.topic, \n            \"state\":  msg.payload,\n            \"counter\": counter\n        });\n        flow.set(\"arr1\", arr);\n}\n\n\nfor (var i = 0; i < arr.length; i++) {\n    \nif(arr[i].counter==3 & arr[i].ip == msg.topic & arr[i].state == msg.payload )\n{\n    arr[i].counter =0;\n    \n}    \n    \nif(arr[i].counter<3 & arr[i].ip == msg.topic & arr[i].state == msg.payload)\n{\n    arr[i].counter=arr[i].counter +1\n    \n}\n\n// zeruje jezeli state > 0\nif(arr[i].counter>0 & arr[i].ip == msg.topic & msg.payload >0)\n{\n    arr[i].counter=0;\n    arr[i].state = msg.payload;\n    \n}\n\n}\n\n\n\nvar exists = arr.some(c => c.ip === msg.topic);\nif (!exists){\n        arr.push({\n            \"ip\": msg.topic, \n            \"state\":  msg.payload,\n            \"counter\": counter\n        });\n    \n}\nmsg.exists = exists;\n\nflow.set(\"arr1\", arr);\n\n\nmsg.arr = flow.get(\"arr1\");\n\n\nmsg.payload = arr;\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":280,"wires":[["fd6791de.6738e8"]]},{"id":"fd6791de.6738e8","type":"debug","z":"1842eb59.625d05","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":280,"wires":[]}]

Do you need it to fail a certain number of times or would it be good enough if it failed for a particular length of time? The latter is very easy to accomplish using a Trigger node set to Send Nothing then wait for the appropriate time then Send the alarm message. Then arrange for a good ping to Reset the trigger node (you can specify a payload value to do that in the Trigger node, so that if a good ping appears in that time then it will cancel the alarm message.

1 Like

As an option have look at the dsm Wiki:

1 Like