Getting XML data from site to Zabbix

This is a horrible problem that i solved in a very dirty way.

And here is how!

First i get the required information from the website and store it in a file.
This is automatically done witch crontab on linux with the command sudo crontab -e
I will deconstruct bash line i wrote
*/1 * * * *  – Crontab information that the bash command should run every minute

curl -s ”http://10.10.20.2/index.html” emerson | tac | tac – Get the data from the site and pipe it through tac. We do this so the grep command have time to regex the XML table. when this is done the data looks like this:
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<?xml-stylesheet type=”text/xsl” href=”/simon.xsl” ?>
<icom:SimpleMonitoring schemaVersion=”1.01″ appVersion=”PA 104037″ fileVersion=”(null)” xmlns:icom=”http://www.emersonnetworkpower.com/icom” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://www.emersonnetworkpower.com/icom icom-1-02.xsd”>
<Item id=”354″ name=”SinglState”>
<Label>Unit Status</Label>
<Value valueType=”4″ offset=”0.000″ gain=”1.000″ precision=”0″>Unit On</Value>
<Unit></Unit>
</Item>
<Item id=”361″ name=”LocTemp”>
<Label>Return Air Temperature</Label>
<Value valueType=”6″ offset=”0.000″ gain=”1.000″ precision=”1″>26.8</Value>
<Unit>&#176;C</Unit>
</Item>
<Item id=”379″ name=”Std. Sensor Humidity”>
<Label>Return Air Humidity</Label>
<Value valueType=”6″ offset=”0.000″ gain=”1.000″ precision=”1″>40.1</Value>
<Unit>%rH</Unit>
</Item>
<Item id=”380″ name=”Supply Air Temperature”>
<Label>Supply Air Temperature</Label>
<Value valueType=”6″ offset=”0.000″ gain=”1.000″ precision=”1″>—</Value>
<Unit></Unit>

grep -oP ‘(?<=\”>).*?(?![a-zA-Z]|\d|[.]|\s)’  – Regex the output of the curl output. after we have done this the data looks like this:
Unit On
26.8
40.1
23.0
24
18.0
On
On
Off
Off
Off
Off
Off
Off

We then store this data in a file somewhere where zabbix_client can easily get it:
> /home/zabbix/zabbixdata/emerson.txt

The complete constructed bash line looks like this:
*/1 * * * * curl -s ”http://10.10.20.2/index.html” emerson | tac | tac | grep -oP ‘(?<=\”>).*?(?![a-zA-Z]|\d|[.]|\s)’ > /home/zabbix/zabbixdata/emerson.txt

Now we need to configure the zabbix agents user defined script. Its located in the /etc/zabbix/zabbix_agentd.d directory.
There i created a file with nano,
command: nano etc/zabbix/zabbix_agentd.d/userparameter_emerson.conf
Here i wrote the following user parameters. i Use head and tail to get the specified line and the correct value.
UserParameter=emerson.unit.status,head -1 /home/zabbix/zabbixdata/emerson.txt
UserParameter=emerson.unit.returntemp,head -2 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.returhum,head -3 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.supplytemp,head -4 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.returntempsetpoint,head -5 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.returnhumsetpoint,head -6 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.supplytempsetpoint,head -7 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.fanstatus,head -8 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.coolingstatus,head -9 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.freecoolingstatus,head -10 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.electricalheatingstatus,head -11 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.hotwaterstatus,head -12 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.dehumstatus,head -13 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.humstatus,head -14 /home/zabbix/zabbixdata/emerson.txt | tail -1
UserParameter=emerson.unit.maintstatus,head -15 /home/zabbix/zabbixdata/emerson.txt | tail -1

The syntax is: UserParameter=key,value
The key needs to correspond to the zabbix item i’m going to create and the value is the bash script to get the correct value from the file we are creating every minute.

Now we need to restart the agent so it detects the userparameters: sudo service zabbix-agent restart

In Zabbix we then on the zabbix server add an items with corresponding keys

DONE!

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *