PDA

View Full Version : Update host profile fields using SNMP


generix
26-09-2011, 13:40
We needed the ability to sync SNMP data fields into host profile (and extended profile).
The problem was solved using a template and the following external script.
Actually it is quite simple but I can imagine some others will face similar tasks.

Attached there's a readme file with further information.


#!/bin/bash

LOG=/tmp/snmp2hostprofile.log
dbuser=zabbix
dbpass=password
dbname=zabbix
dbhost=localhost

OIFS=$IFS
IFS=","

declare -a args=($*)

IFS=$OIFS

if [ ${#args } -eq 5 ]; then
snmpcmd="snmpget ${args[1]} ${args[0]} ${args[2]}"
snmpoutput=` eval ${snmpcmd} `
tmp1="${snmpoutput#* = }"
value=${tmp1#*: }
profiletype="hosts_"${args[3]}
profilefield=${args[4]}
hostip=${args[0]}
mysql --user=${dbuser} --password=${dbpass} --database=${dbname} -h ${dbhost} -e "INSERT INTO hosts_profiles (hostid, ${profilefield}) VALUES ((SELECT hostid FROM hosts WHERE ip = '${hostip}'), '${value}') ON DUPLICATE KEY UPDATE ${profilefield}='${value}';"
fi

echo ${value}

JitseH
07-10-2011, 11:26
Thanks for sharing this script.
It works excellent for the standard profile.
Before it is usable for the extended profile there must be a little correction at the query:

INSERT INTO ${profiletype} ...

Instead of

INSERT INTO hosts_profiles ...

generix
07-10-2011, 12:19
You are right.
My bad.

But actually it needs to be:
INSERT INTO ${profiletype}

You'll find the current version attached.

youtube
29-02-2012, 08:14
that's nice script, i learning so much from that script
We needed the ability to sync SNMP data fields into host profile (and extended profile).
The problem was solved using a template and the following external script.
Actually it is quite simple but I can imagine some others will face similar tasks.

Attached there's a readme file with further information.


#!/bin/bash

LOG=/tmp/snmp2hostprofile.log
dbuser=zabbix
dbpass=password
dbname=zabbix
dbhost=localhost

OIFS=$IFS
IFS=","

declare -a args=($*)

IFS=$OIFS

if [ ${#args } -eq 5 ]; then
snmpcmd="snmpget ${args[1]} ${args[0]} ${args[2]}"
snmpoutput=` eval ${snmpcmd} `
tmp1="${snmpoutput#* = }"
value=${tmp1#*: }
profiletype="hosts_"${args[3]}
profilefield=${args[4]}
hostip=${args[0]}
mysql --user=${dbuser} --password=${dbpass} --database=${dbname} -h ${dbhost} -e "INSERT INTO hosts_profiles (hostid, ${profilefield}) VALUES ((SELECT hostid FROM hosts WHERE ip = '${hostip}'), '${value}') ON DUPLICATE KEY UPDATE ${profilefield}='${value}';"
fi

echo ${value}