Category: monitor

Use node-red to monitor pages

This flow checks a HTTP or HTTPS URL on its contents. If a certain string is encountered (in this example ‘A visual tool for wiring’,
as it checks the Node-Red website) the page is considered to be available. The page is considered to be down if the string is not encountered or
the URL could not be retrieved.

There are two outputs for the check function, one for failure and one for success after the page is back up.
This allows for the throttling of failure messages via a Rate Limiter, while the message that the page is back up is send immediately.

There is currently one downside on this flow: if a page check goes down, a message is send. When the page comes up and goes down again
a little later, no message is send. This is because of the Rate Limiter, which does not get reset when the check is back up. Only
after a day the notification will be send when the Rate Limiter forwards the message.

Flow

Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option.

[{
    "id": "3fc2ea78.549ace",
    "type": "http request",
    "name": "Nodered site check",
    "method": "GET",
    "url": "http://nodered.org",
    "x": 261.2499694824219,
    "y": 135,
    "z": "1dca54.d12be5ac",
    "wires": [["29e68ad0.cc2c0e"]]
},
{
    "id": "29e68ad0.cc2c0e",
    "type": "function",
    "name": "String check",
    "func": "var title = 'Node-red check', result;\n\nif(msg.payload.indexOf('A visual tool for wiring') !== -1) {\n\tif(context.lastWasError) {\n\t\tcontext.lastWasError = false;\n\t\tresult = {\n\t\t\tpayload: 'Check is back up!',\n\t\t\ttopic: title\n\t\t};\n\t\treturn [null, result];\n\t}\n\treturn;\n}\n\nresult = {\n\tpayload: msg.payload,\n\ttopic: title\n};\ncontext.lastWasError = true;\nreturn [result, null];",
    "outputs": "2",
    "x": 456.7499694824219,
    "y": 136.25,
    "z": "1dca54.d12be5ac",
    "wires": [["cba65a05.00c3"],
    ["3839cb5d.1c34ac"]]
},
{
    "id": "cba65a05.00c3",
    "type": "delay",
    "name": "Rate limiter",
    "pauseType": "rate",
    "timeout": "5",
    "timeoutUnits": "seconds",
    "rate": "1",
    "rateUnits": "day",
    "randomFirst": "1",
    "randomLast": "5",
    "randomUnits": "seconds",
    "drop": true,
    "x": 624.2499694824219,
    "y": 91,
    "z": "1dca54.d12be5ac",
    "wires": [["bb03cc6a.16d6b8"]]
},
{
    "id": "ff61d2c5.281c58",
    "type": "inject",
    "name": "",
    "topic": "Trigger",
    "payload": "",
    "payloadType": "none",
    "repeat": "1800",
    "crontab": "",
    "once": true,
    "x": 81.25,
    "y": 135,
    "z": "1dca54.d12be5ac",
    "wires": [["3fc2ea78.549ace"]]
},
{
    "id": "bb03cc6a.16d6b8",
    "type": "debug",
    "name": "Check failure",
    "active": true,
    "console": "false",
    "complete": "false",
    "x": 817.5,
    "y": 91.25,
    "z": "1dca54.d12be5ac",
    "wires": []
},
{
    "id": "3839cb5d.1c34ac",
    "type": "debug",
    "name": "Check up",
    "active": true,
    "console": "false",
    "complete": "false",
    "x": 593.75,
    "y": 170,
    "z": "1dca54.d12be5ac",
    "wires": []
}]

Leave a Comment