📜 ⬆️ ⬇️

Automatic renaming of hosts in Zabbix, according to data from snmp sysName

The cycle of articles about Zabbix is ​​continued by an unregistered user. The first article can be read here .

Why do you need it?


You can add a new host to Zabbix in two main ways: autodetect and manually add a host. When we have a very large number of nodes, adding them to the monitoring system with pens takes a lot of time, tracking their numbers and keeping them up to date is also not an easy task. In this case, it is convenient to use auto detection. How to use it in this article will not be described (if anyone is interested it will be possible to prepare a small overview of the main functions). The main disadvantage of auto-detection is that the added hosts get a name equal to the IP device. With a large number of devices to figure out where that is problematic.

Decision.


As a rule, any device controlled by snmp has a standard OID .1.3.6.1.2.1.1.5.0 or system.sysName.0. This is the so-called hostname glands. As a device name, it is impossible to use any value obtained by OID by standard means in Zabbix.
The easiest way is to use external scripts.
')

For PostgresSQL:


#!/bin/bash

PGSQL="psql -d < > -h < > -U < > -W -c"

$PGSQL" SELECT ip FROM hosts where ip ILIKE '192.168.1.%' " | while read line
do
arr=($line)
x=`snmpwalk -Ou -Oq -v2c -c <community string> ${arr[0]} system.sysName.0 2> /dev/ null | awk '{print$2}' | tr "[: upper :]" "[: lower :] " `
[[ -n $x ]] && $PGSQL" UPDATE hosts SET host = '$x' WHERE ip = '${arr[0]}' "

done


* This source code was highlighted with Source Code Highlighter .


For MYSQL:


#!/bin/bash

MYSQL="mysql --connect_timeout 10 < > -h < > -u < > --password=<> --skip- column - names -B -e"

$MYSQL " SELECT ip FROM hosts where ip like '192.168.1.%' " | while read line
do
arr=($line)
x=`snmpwalk -Ou -Oq -v2c -c <community string> ${arr[0]} system.sysName.0 2> /dev/ null | awk '{print$2}' | tr "[: upper :]" "[: lower :] " `
[[ -n $x ]] && $MYSQL " UPDATE hosts SET host = '$x' WHERE ip = '${arr[0]}' "

done


* This source code was highlighted with Source Code Highlighter .


Instead of '192.168.1.%' We substitute the desired range of ip addresses of the glands.
Save the script in a convenient place to use, do chmod + X and run as needed. You can register this script in cron.

There is an option to go further and slightly changing the script to execute it under the action of auto-detection by Zabiks himself, but only for the piece of iron that was found. It is not difficult to make it, but there was no need.

The article is written by an unregistered user sersad

Source: https://habr.com/ru/post/82465/


All Articles