C:\Users\Student1>ipconfig ... IPv4-адрес. . . . . . . . . . . . : 192.168.1.5 Маска подсети . . . . . . . . . . : 255.255.255.0 Основной шлюз. . . . . . . . . : 192.168.1.1
# cat /etc/hostname
server.corp1.un
# cat /etc/hosts
127.0.0.1 localhost 192.168.1.100 server.corp1.un server
# cat /etc/resolv.conf
search corp1.un nameserver 192.168.1.1
# cat /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
configure terminal hostname switchN ip domain-name corp1.un int vlan 1 ip address 192.168.1.10N 255.255.255.0 no shut no ip domain lookup end copy running-config startup-config
conf t snmp-server community public RO end wr
# service zabbix-server restart
!!! Можно, для примера, добавить одну линию связи
Name: Switches map
Edit Map
Add
Type: Host
Label: switch1
Host: switch1.corp1.un
Icons: Switch_(96)
...
Label: switch2
Host: switch2.corp1.un
...
Label: switch3
Host: switch3.corp1.un
...
switch1#show cdp neighbors
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater
Device ID Local Intrfce Holdtme Capability Platform Port ID
switch2.corp1.un Fas 0/N 164 R S I 3640 Fas 0/V
switch3.corp1.un Fas 0/M 165 R S I 3640 Fas 0/W
conf t ip host server 192.168.1.100 ip rcmd rsh-enable ip rcmd remote-host root server root enable end wr
server.corp1.un:~# apt install rsh-client server.corp1.un:~# rsh switch1 show cdp neighbors server.corp1.un:~# apt install dos2unix server.corp1.un:~# rsh switch1 -n show cdp nei | dos2unix | grep switch | tr -s " " | cut -d " " -f1,2,3,9,10 или server.corp1.un:~# rsh switch1 -n show cdp nei | dos2unix | tr '\n' ' ' | sed 's/switch/\nswitch/g' | grep switch | tr -s " " | cut -d " " -f1,2,3,9,10
server.corp1.un:~# apt install curl
server.corp1.un:~# curl -s -k -X POST -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1
} ' http://127.0.0.1/zabbix/api_jsonrpc.php
{"jsonrpc":"2.0","result":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","id":1}
server.corp1.un:~# apt install jq
server.corp1.un:~# curl -s -k -X POST -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": "extend"
},
"auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"id": 2
} ' http://127.0.0.1/zabbix/api_jsonrpc.php | jq
...
...
"params": {},
...
...
"params": {
"output": ["hostid", "host"]
},
...
server.corp1.un:~# export AUTH=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx server.corp1.un:~# cat /root/zab_get_hosts.sh
#!/bin/sh
curl -s -k -X POST -H 'Content-Type: application/json-rpc' -d "
{
\"jsonrpc\": \"2.0\",
\"method\": \"host.get\",
\"params\": {},
\"auth\": \"${AUTH}\",
\"id\": 2
} " http://127.0.0.1/zabbix/api_jsonrpc.php
server.corp1.un:~# /root/zab_get_hosts.sh | jq '.result | .[] | .host'
server.corp1.un:~# /root/zab_get_hosts.sh | jq '.result | .[] | {hostid: .hostid, host: .host}'
server.corp1.un:~# /root/zab_get_hosts.sh | jq '.result | .[] | {hostid: .hostid, host: .host} | tostring'
server.corp1.un:~# /root/zab_get_hosts.sh | jq '.result | .[] | {hostid: .hostid, host: .host} | tostring' | grep switch | tr -d '{}\\' | tr '"' ' ' | cut -d ' ' -f5,9
Добавляем конвейер в конец скрипта и записываем результат в файл:
server.corp1.un:~# /root/zab_get_hosts.sh | tee list_hostid_host.txt
10111 switch1.corp1.un 10112 switch2.corp1.un 10113 switch3.corp1.un
server.corp1.un:~# cat /root/zab_get_maps.sh
#!/bin/sh
curl -s -k -X POST -H 'Content-Type: application/json-rpc' -d "
{
\"jsonrpc\": \"2.0\",
\"method\": \"map.get\",
\"params\": {
\"selectLinks\": \"extend\",
\"selectSelements\": \"extend\"
},
\"auth\": \"${AUTH}\",
\"id\": 2
} " http://127.0.0.1/zabbix/api_jsonrpc.php | \
#jq
###zabbix3
jq '.result | .[] | .selements | .[] | {elementid: .elementid, selementid: .selementid} | tostring' | tr -d '{}\\' | tr '"' ' ' | cut -d ' ' -f5,9
###zabbix4
#jq '.result[1].selements[] | {elements, selementid} | tostring' | tr -d '{}\\' | tr '"' ' ' | cut -d ' ' -f7,11
server.corp1.un:~# /root/zab_get_maps.sh | tee list_hostid_selementid.txt
10084 1 10111 2 10112 3 10113 4
server.corp1.un:~# cat /root/rsh_get_links.sh
#!/bin/sh
LIST_HOSTID_HOST=/root/list_hostid_host.txt
LIST_HOSTID_SELEMENTID=/root/list_hostid_selementid.txt
while read HOSTID HOST
do
rsh $HOST -n show cdp nei | dos2unix | grep switch | tr -s " " | cut -d " " -f1,2,3,9,10 |
while read CDPNEI LINKINTFACES
do
HOSTID2=`grep $CDPNEI $LIST_HOSTID_HOST | cut -d' ' -f1`
SELEMENT2=`grep $HOSTID2 $LIST_HOSTID_SELEMENTID | cut -d' ' -f2`
SELEMENT1=`grep $HOSTID $LIST_HOSTID_SELEMENTID | cut -d' ' -f2`
echo $SELEMENT1 $SELEMENT2 $LINKINTFACES
done
done < $LIST_HOSTID_HOST
server.corp1.un:~# /root/rsh_get_links.sh | tee list_selements_label.txt
2 3 Fas 0/2 Fas 0/5 2 4 Fas 0/8 Fas 0/5 3 2 Fas 0/5 Fas 0/2 4 2 Fas 0/5 Fas 0/8
Простой пример изменения конфигурации через Zabbix API
server.corp1.un:~# cat /root/zab_set_map_name.sh
#!/bin/sh
MAPID=$1
MAPNAME=$2
curl -s -k -X POST -H 'Content-Type: application/json-rpc' -d "
{
\"jsonrpc\": \"2.0\",
\"method\": \"map.update\",
\"params\": {
\"sysmapid\": \"${MAPID}\",
\"name\": \"${MAPNAME}\"
},
\"auth\": \"${AUTH}\",
\"id\": 2
} " http://127.0.0.1/zabbix/api_jsonrpc.php
server.corp1.un:~# /root/zab_set_map_name.sh 2 "Super MAP"
server.corp1.un:~# cat /root/zab_set_link_name.sh
#!/bin/sh
MAPID=2
SELEMENTS_LABEL=/root/list_selements_label.txt
LINKS=""
while read SELEMENTID1 SELEMENTID2 LABEL
do
LINKS="$LINKS
{
\"label\": \"${LABEL}\",
\"selementid1\": \"${SELEMENTID1}\",
\"selementid2\": \"${SELEMENTID2}\"
},"
done < $SELEMENTS_LABEL
#LINKS=`echo $LINKS | rev | cut -c 2- | rev`
#LINKS=`echo -n ${LINKS::-1}`
JSON="
{
\"jsonrpc\": \"2.0\",
\"method\": \"map.update\",
\"params\": {
\"sysmapid\": \"${MAPID}\",
\"links\": [
${LINKS}
]
},
\"auth\": \"${AUTH}\",
\"id\": 2
} "
curl -s -k -X POST -H 'Content-Type: application/json-rpc' -d "$JSON" http://127.0.0.1/zabbix/api_jsonrpc.php
server.corp1.un:~# /root/zab_set_link_name.sh
server.corp1.un:~# /root/zab_get_hosts.sh | tee list_hostid_host.txt
server.corp1.un:~# /root/zab_get_maps.sh | tee list_hostid_selementid.txt
server.corp1.un:~# /root/rsh_get_links.sh | tee list_selements_label.txt
server.corp1.un:~# /root/zab_set_link_name.sh