NetXMS Support Forum

English Support => General Support => Topic started by: Egert143 on October 30, 2024, 11:41:25 AM

Title: MQTT with json format
Post by: Egert143 on October 30, 2024, 11:41:25 AM
Hello, i did agent config as following:

MasterServers = x.x.x.x
ConfigIncludeDir = C:\NetXMS\etc\nxagentd.conf.d
LogFile = {syslog}
FileStore = C:\NetXMS\var
SubAgent = mqtt
SubAgent = winperf

[MQTT/Brokers/Test]
Hostname = y.y.y.y
Port = 1883
[MQTT/Brokers/Test/Metrics]
Volt = "metrics/grid/voltage/L1"



the output looks like this:

{
  "timestamp": "2024-10-30T09:42:12.145495",
  "type": "float",
  "value": 236.21934509277344
}

how can i extract the value ?

Currently in netxms i just get unsuported.
Title: Re: MQTT with json format
Post by: Filipp Sudanov on November 01, 2024, 04:57:09 PM
This is interesting, seems that json payload is a popular thing these days.

Unsupported could be in case if agent subscribed to topic, but there is no new value on the broker. I've used similar config as what you have and once I've pushed a new value with mosquitto_pub, I can get value of "Volt" from the agent - whole contents of the json appears in last values on the server.

Max length of DCI value is 255 chars, so if you payload is within that, you are lucky. Ideally agent could parse the payload, I've added ticker for that: https://track.radensolutions.com/issue/NX-2626

Now, in transformation script you can parse the json and extract your value:
j = JsonParse($1);
if (j == null) return null;
value = j.get("value");
return value;


P.S. It's not necessary to specify metrics in agent config - you can use MQTT origin, in this case agent automatically subscribes to topics. I've updated the documentation on this.
Title: Re: MQTT with json format
Post by: Egert143 on November 01, 2024, 05:37:52 PM
So i need only following in agent config ?

[MQTT/Brokers/Test]
Hostname = y.y.y.y
Port = 1883

I made new DCI:
Origin: mqtt
Metric: Test:metrics/grid/voltage/L1
Data type: Float

Btw the Broker is in same server as agent itself, if it makes difference. With mqtt explorer i see that value changes every few seconds. Yet dci remains in not supported status.
Title: Re: MQTT with json format
Post by: Filipp Sudanov on November 04, 2024, 11:55:43 AM
Or just
[MQTT/Brokers/Test]
Hostname = y.y.y.y
if using default port number.

First thing to check if mqtt subagent actually loads during agent start. This is visible in agent log, e.g.:
2024.11.04 11:43:11.911 *I* [subagents          ] Subagent "MQTT" (/opt/netxms-master/lib/netxms/mqtt.nsm) loaded successfully (version 5.1.0-rc326)
On Linux you might need to install netxms-agent-mqtt package as mqtt subagent is in it.

Then a bit tricky part is if using MQTT origin, server tries to get the value from agent which runs along with your server (or agent which is zone proxy, if node where DCI is configured is in some zone). This is the same logic as for ssh monitoring. The idea is that you might have a node (or sensor) object which corresponds to your mqtt device and you have DCIs on it, but data collection happens through agent on another node. You can explicitly specify MQTT proxy in node or sensor properties.

You can enable DebugLevel=6 in agent config, the you should see lines like this:

2024.11.04 09:53:27.692 *D* [comm.cs.2          ] Requesting metric "MQTT.TopicData("test:metrics/grid/voltage/L2")"
2024.11.04 09:53:27.693 *D* [mqtt               ] Subscribed to topic metrics/grid/voltage/L2 on MQTT broker 127.0.0.1:1883

...

2024.11.04 09:54:26.671 *D* [mqtt               ] MQTT message received: metrics/grid/voltage/L2="456"


Title: Re: MQTT with json format
Post by: Egert143 on November 04, 2024, 12:27:32 PM
Can it be version problem, i currently run V5.0.5?

In agent logs i get following:

config
2024.11.04 12:23:22.778 *I* [config            ]  +- CORE
2024.11.04 12:23:22.778 *I* [config            ]  |  +- DebugLevel
2024.11.04 12:23:22.778 *I* [config            ]  |  |    value: 6
2024.11.04 12:23:22.778 *I* [config            ]  |  +- MasterServers
2024.11.04 12:23:22.778 *I* [config            ]  |  |    value: 192.168.3.73
2024.11.04 12:23:22.778 *I* [config            ]  |  +- ConfigIncludeDir
2024.11.04 12:23:22.778 *I* [config            ]  |  |    value: C:\NetXMS\etc\nxagentd.conf.d
2024.11.04 12:23:22.778 *I* [config            ]  |  +- LogFile
2024.11.04 12:23:22.778 *I* [config            ]  |  |    value: C:\NetXMS\log\log.txt
2024.11.04 12:23:22.778 *I* [config            ]  |  +- FileStore
2024.11.04 12:23:22.778 *I* [config            ]  |  |    value: C:\NetXMS\var
2024.11.04 12:23:22.778 *I* [config            ]  |  +- SubAgent
2024.11.04 12:23:22.778 *I* [config            ]  |        value: winperf
2024.11.04 12:23:22.778 *I* [config            ]  |        value: mqtt
2024.11.04 12:23:22.778 *I* [config            ]  +- MQTT
2024.11.04 12:23:22.778 *I* [config            ]      +- Brokers
2024.11.04 12:23:22.778 *I* [config            ]          +- Local
2024.11.04 12:23:22.778 *I* [config            ]              +- Hostname
2024.11.04 12:23:22.778 *I* [config            ]                  value: 127.0.0.1
...
2024.11.04 12:23:23.214 *D* [mqtt               ] 0 MQTT parameters added from configuration
2024.11.04 12:23:23.214 *D* [mqtt               ] Using libmosquitto 2.0.15
2024.11.04 12:23:23.214 *D* [mqtt               ] Automatic registration of MQTT topics is enabled
2024.11.04 12:23:23.214 *I* [subagents          ] Subagent "MQTT" (mqtt.nsm) loaded successfully (version 5.0.1)
2024.11.04 12:23:23.214 *D* [mqtt               ] Connected to MQTT broker 127.0.0.1:1883 as anonymous
Title: Re: MQTT with json format
Post by: Filipp Sudanov on November 04, 2024, 12:31:29 PM
Version is fine, last changes in mqtt were somewhere in 4.x, so any 5.x version has the latest and greatest code.

If you node where you have this agent is not your netxms server and not zone's proxy, try setting the node itself as MQTT proxy in it's properties.
Title: Re: MQTT with json format
Post by: Egert143 on November 04, 2024, 12:46:48 PM
Seems like setting MQTT proxy solved the problem. Thanks!

Egert