My goal is to collect health information of different servers and have them put into MQTT-topics.
As I don't like to copy the flows for each server (I'm collecting the exact same data, but from multiple sources), I come from a list of servers and like to push them into seperate MQTT-topics, which I then can monitor and take necessary action if e.g. one server runs into problems.
So, basically I do the following:
global.set("hostname", "openhabmain");
global.set("hostip", "192.168.xx.xx");
global.set("metricsurl", "http://192.168.xx.xx:8080/rest/metrics/prometheus")
msg.url = global.get("metricsurl");
msg.payload = global.get("hostip");
node.send(msg);
global.set("hostname", "openhabutilities");
global.set("hostip", "192.168.xx.xx");
global.set("metricsurl", "http://192.168.xx.xx:8080/rest/metrics/prometheus")
msg.url = global.get("metricsurl");
msg.payload = global.get("hostip");
node.send(msg);
later on, if a got my response I send them to different mqtt-topics:
msg1.payload = Object.assign(msg.payload[key]);
msg1.topic = "openHAB/master/snmp/" + global.get("hostname") + "/" + key;
node.send(msg1)
But as soon as I inject the flow, the "global variable" gets overwritten and I basically get only one set of information. So what is the best practise here, to have flow specific variables?
if it helps, here a screenshot and the flow:
Flow
[
{
"id": "eb920791.e124e8",
"type": "inject",
"z": "84ed84b2.83431",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "300",
"crontab": "",
"once": false,
"onceDelay": "",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 230,
"y": 600,
"wires": [
[
"de4a1af6.4ae9b"
]
]
},
{
"id": "71173d2a.39309c",
"type": "http request",
"z": "84ed84b2.83431",
"name": "getOpenhabMetrics",
"method": "GET",
"ret": "txt",
"paytoqs": "body",
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"authType": "",
"x": 370,
"y": 720,
"wires": [
[
"344c1065.04ad48"
]
]
},
{
"id": "344c1065.04ad48",
"type": "function",
"z": "84ed84b2.83431",
"name": "Metrics2JSON",
"func": "var lines = msg.payload.split(\"\\n\");\n\nvar json = \"{\";\nvar json_object = {};\n\nfor(var i = 0;i < lines.length;i++){\n // ist es eine Kommentar-Zeile?\n if (!lines[i].startsWith(\"#\")) {\n // ist es eine kurze \"Leerzeile\"?\n lines[i] = lines[i];\n if (lines[i].length > 5) {\n var data_tmp1 = lines[i].split(\"{\");\n var data_name = data_tmp1[0]; // Datenbezeichnung \n var data_tmp2 = data_tmp1[1].split(\"}\");\n var data_value = data_tmp2[1].replace(\" \", \"\"); // Datenwert\n var data_meta = data_tmp2[0]; // Daten-Meta\n \n data_meta = data_meta.replace(/=/g, '\": ');\n data_meta = data_meta.replace(/,/g, ',\"');\n if(data_name in json_object == false){\n json_object[data_name] = {};\n }\n \n var data_injson = true;\n var data_distinquisher = \"\";\n // json zusammensetzen\n switch(true){\n case data_name == \"jvm_threads_daemon_threads\":\n data_distinquisher = \"\";\n break;\n case data_name == \"jvm_buffer_count_buffers\":\n data_distinquisher = \"id\";\n break;\n case data_name == \"jvm_threads_states_threads\":\n data_distinquisher = \",state\";\n break;\n case data_name == \"jvm_memory_max_bytes\":\n data_distinquisher = \"id\";\n break;\n case data_name == \"executor_queued_tasks\":\n data_distinquisher = \"pool\";\n break;\n case data_name == \"executor_pool_core_threads\":\n data_distinquisher = \"pool\";\n break;\n case data_name == \"event_count_total\":\n data_injson = false;\n break;\n case data_name == \"executor_pool_core_threads\":\n data_distinquisher = \"pool\";\n break;\n case data_name == \"jvm_buffer_total_capacity_bytes\":\n data_distinquisher = \"id\";\n break;\n case data_name == \"jvm_memory_used_bytes\":\n data_distinquisher = \"id\";\n break;\n case data_name == \"openhab_rule_runs_total\":\n data_injson = false;\n break;\n case data_name == \"executor_pool_size_threads\":\n data_injson = false;\n break;\n case data_name == \"jvm_buffer_memory_used_bytes\":\n data_distinquisher = \"id\";\n break;\n case data_name == \"executor_active_threads\":\n data_injson = false;\n break;\n case data_name == \"jvm_memory_committed_bytes\":\n data_injson = false;\n break;\n case data_name == \"executor_completed_tasks_total\":\n data_injson = false;\n break;\n case data_name == \"openhab_bundle_state\":\n data_injson = false;\n break;\n case data_name == \"executor_pool_max_threads\":\n data_injson = false;\n break;\n case data_name == \"jvm_gc_pause_seconds_count\":\n data_injson = false;\n break;\n case data_name == \"jvm_gc_pause_seconds_sum\":\n data_injson = false;\n break;\n case data_name == \"jvm_gc_pause_seconds_max\":\n data_injson = false;\n break;\n case data_name == \"openhab_thing_state\":\n data_distinquisher = \"thing\";\n break;\n case data_name == \"executor_queue_remaining_tasks\":\n data_injson = false;\n break;\n default:\n data_distinquisher = \"\";\n }\n \n if (data_injson == true){\n if (data_distinquisher != \"\") {\n var data_att_tmp1 = lines[i].split(data_distinquisher);\n var data_att_tmp2 = data_att_tmp1[1].split(',');\n data_name_sub = data_att_tmp2[0].replace('=\"', '');\n data_name_sub = data_name_sub.replace('\"', '');\n // json_object[data_name].push(data_name_sub);\n json_object[data_name][data_name_sub] = data_value;\n }\n if (data_distinquisher == \"\") {\n data_name_sub = \"value\";\n // json_object[data_name].push(data_name_sub);\n json_object[data_name][data_name_sub] = data_value;\n }\n\n json += '\"' + data_name + '\": {';\n json += '\"' + data_meta;\n json += 'value\": \"' + data_value + '\"},';\n }\n }\n }\n}\njson += '\"timestamp\": 123}';\n\n/*\nmsg.payload = data_name; // executor_queue_remaining_tasks\nnode.send(msg);\nmsg.payload = data_value; // 2.147483647E9\nnode.send(msg);\nmsg.payload = data_meta; // metric=\"openhab.core.metric.threadpools\",name=\"lsp\",openhab_core_metric=\"true\",pool=\"lsp\",\nnode.send(msg);\n*/\n\nmsg.payload = JSON.stringify(json_object);\nreturn msg;\n\n\n// executor_queue_remaining_tasks{metric=\"openhab.core.metric.threadpools\",name=\"lsp\",openhab_core_metric=\"true\",pool=\"lsp\",} 2.147483647E9\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 320,
"y": 840,
"wires": [
[
"543607f4.df7328",
"ce6de5b9.c96a7"
]
]
},
{
"id": "543607f4.df7328",
"type": "json",
"z": "84ed84b2.83431",
"name": "JSON2JSObject",
"property": "payload",
"action": "obj",
"pretty": true,
"x": 320,
"y": 940,
"wires": [
[
"a65939ae.ad9d88"
]
]
},
{
"id": "9aa67c3f.8ef998",
"type": "mqtt out",
"z": "84ed84b2.83431",
"name": "",
"topic": "",
"qos": "1",
"retain": "true",
"broker": "4672ca10.309984",
"x": 730,
"y": 1060,
"wires": []
},
{
"id": "a65939ae.ad9d88",
"type": "function",
"z": "84ed84b2.83431",
"name": "Object2MQTT",
"func": "for (let key in msg.payload){\n let msg1 = {payload:{}};\n // msg1.payload[key] = Object.assign(msg.payload[key],{\"key\":key});\n msg1.payload = Object.assign(msg.payload[key]);\n msg1.topic = \"openHAB/master/snmp/\" + global.get(\"hostname\") + \"/\" + key;\n node.send(msg1)\n}\nreturn;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 520,
"y": 940,
"wires": [
[
"9b49727.034581",
"462efda4.e53c84"
]
]
},
{
"id": "9b49727.034581",
"type": "debug",
"z": "84ed84b2.83431",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 750,
"y": 940,
"wires": []
},
{
"id": "ce6de5b9.c96a7",
"type": "debug",
"z": "84ed84b2.83431",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 550,
"y": 880,
"wires": []
},
{
"id": "462efda4.e53c84",
"type": "rbe",
"z": "84ed84b2.83431",
"name": "only changes",
"func": "rbe",
"gap": "",
"start": "",
"inout": "out",
"property": "payload",
"x": 590,
"y": 1060,
"wires": [
[
"9aa67c3f.8ef998"
]
]
},
{
"id": "de4a1af6.4ae9b",
"type": "function",
"z": "84ed84b2.83431",
"name": "openHAB-Metrics",
"func": "/*\nglobal.set(\"hostname\", \"nodered\");\nglobal.set(\"hostip\", \"192.168.xx.xx\");\n\nmsg.payload = global.get(\"hostip\");\nnode.send(msg);\n*/\n\nglobal.set(\"hostname\", \"openhabmain\");\nglobal.set(\"hostip\", \"192.168.xx.xx\");\nglobal.set(\"metricsurl\", \"http://192.168.xx.xx:8080/rest/metrics/prometheus\")\n\nmsg.url = global.get(\"metricsurl\");\nmsg.payload = global.get(\"hostip\");\nnode.send(msg);\n\nglobal.set(\"hostname\", \"openhabutilities\");\nglobal.set(\"hostip\", \"192.168.xx.xx\");\nglobal.set(\"metricsurl\", \"http://192.168.xx.xx:8080/rest/metrics/prometheus\")\n\nmsg.url = global.get(\"metricsurl\");\nmsg.payload = global.get(\"hostip\");\nnode.send(msg);",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 290,
"y": 660,
"wires": [
[
"71173d2a.39309c"
]
]
},
{
"id": "4672ca10.309984",
"type": "mqtt-broker",
"name": "Synology",
"broker": "192.168.xx.xx",
"port": "1883",
"clientid": "nodered",
"usetls": false,
"compatmode": false,
"keepalive": "60",
"cleansession": true,
"birthTopic": "openHAB/master/nodered",
"birthQos": "2",
"birthRetain": "true",
"birthPayload": "ON",
"closeTopic": "openHAB/master/nodered",
"closeQos": "2",
"closeRetain": "true",
"closePayload": "OFF",
"willTopic": "openHAB/master/nodered",
"willQos": "2",
"willRetain": "true",
"willPayload": "OFF"
}
]
Thanks!