前言:
通过这篇文章你可以学会用shell写相关采集器推送脚本,推送自动发现规则所需json格式数据,以及推送自动发现的监控项数据。在zabbix原生监控项无法实现的时候非常有用,当然使用外部脚本也是一样哦,只不过是换了zabbix监控项的监控模式而已。采集器属于主动模式监控哦,因为是zabbix_sender主动推送给zabbix server的,所以对于效率很高、server负载也低。在创建触发器时大部分都是数值判断,在对于返回字符串时要去怎么判断是否异常呢,比如监控项返回字符串是OK_(5)、90%,第一种先说监控项值OK_(5)这种形式如何创建触发器,触发器函数那里自带str函数可以实现关键字匹配,匹配到返回1反之返回0。第二种监控项值为90%如何创建触发器呢,就需要使用监控项的预处理功能,将%去除就可以了,监控项预处理功能中右裁剪功能实现。
我希望通过这个文章大家要知道举一反三,深刻理解zabbix自动发现机制,除了采集器类型还有其他主动、被动、外部脚本、snmp agent的自动发现规则都是类似的,只不过snmp的自动发现特殊一点点,后面有机会发表相关文章。
实例:
监控神州云科DCN-4250(Dell EMC Unity400存储),由于不支持snmp agent模式监控所以只能通过unisphere cli工具来获取相关信息,有需要的可以留言或私聊,
shell 脚本
下面是完整脚本,我后面以个别举例说明,不用看全部的哦,
#!/bin/bash
#下面为正则切割内容
#uemcli -d *.*.*.* -u admin -p xxxxxx /env/bat show |awk '/^[0-9]+:.+spb_bbu_0\>/,/.*Health[[:space:]]state/ {print $0}'
#echo "客户端地址为:$1"zbx_ip="x.x.x.x"
zbx_port=10051
client_ip="*.*.*.*"
client_user="*****"
client_pwd="******"#返回自动发现规则需要的json格式数据
jsonput(){i=1echo -e "{\n\t\"data\":["for j in $*doecho -e "\t\t{\n\t\t\"{#ETHNAME}\":\"$j\""echo -en "\t\t}"if ((i<$#))thenecho "," fii=$i+1doneecho -e "\n\t]"echo "}"
}#推送到zabbix
zbx_sender(){zabbix_sender -z $zbx_ip -p $zbx_port -s $client_ip -k $1 -o $2
}#传输参数:命令、自动发现键值前缀、监控项键值前缀、单个指标信息相关行数(偏移量方便分割)、
putstatuszbx(){cmdstar="uemcli -d $client_ip -u $client_user -p $client_pwd"alllist=`$cmdstar $1`#输出以太网网卡ID列表IDlist=`echo "$alllist"|grep "^[0-9]:.*ID"|awk -F "= " '{print$2}'`Namelist=`echo "$alllist"|grep "Name"|awk -F "= " '{print$2}'|tr ' ' '-'`#SAS接口自动发现推送aa=`jsonput $Namelist` #json原始格式输出bb=`echo $aa|sed 's/ //g'` #将json去除换行和空格格式化成一行,方便后面推送至zabbixzbx_sender "$2" $bb #推送自动发现json数据#echo "$aa" #echo "$bb"for i in $IDlistdoeth=`echo "$alllist"|grep $i -A $4`ethname=`echo "$eth"|grep "Name"|awk -F"= " '{print $2}'|tr ' ' '-'`ethstatus=`echo "$eth"|grep "Health state"|awk -F"= " '{print $2}'`
# echo "状态输出 $ethname $ethstatus"ww=`echo "$3.[$ethname]"`qq=`echo $ethstatus|tr ' ' '_'`
# echo "监控项原形输出:$ww $qq"zbx_sender $ww $qq#zbx_sender $ww "no"echo "---------"done
}#传输参数:命令、自动发现键值前缀、监控项键值前缀、单个指标信息相关行数(偏移量方便分割)、
pool_putstatuszbx(){cmdstar="uemcli -d $client_ip -u $client_user -p $client_pwd"alllist=`$cmdstar $1`#输出以太网网卡ID列表IDlist=`echo "$alllist"|grep "^[0-9]:.*ID"|awk -F "= " '{print$2}'`Namelist=`echo "$alllist"|grep "Name"|awk -F "= " '{print$2}'|tr ' ' '-'`#SAS接口自动发现推送aa=`jsonput $Namelist` #json原始格式输出bb=`echo $aa|sed 's/ //g'` #将json去除换行和空格格式化成一行,方便后面推送至zabbixzbx_sender "$2" $bb #推送自动发现json数据#echo "$aa" #echo "$bb"for i in $IDlistdoeth=`echo "$alllist"|grep $i -A $4`#echo "$eth"ethname=`echo "$eth"|grep "Name"|awk -F"= " '{print $2}'|tr ' ' '-'`ethstatus=`echo "$eth"|grep "Subscription percent"|awk -F"= " '{print $2}'`
# echo "状态输出 $ethname $ethstatus"ww=`echo "$3.[$ethname]"`qq=`echo $ethstatus|tr ' ' '_'`#echo "监控项原形输出:$ww $qq"zbx_sender $ww $qq#zbx_sender $ww "no"echo "---------"done
}#传输参数:命令、自动发现键值前缀、监控项键值前缀、单个指标信息相关行数(偏移量方便分割)、
#主要用于输出电池SP状态
sp_putstatuszbx(){cmdstar="uemcli -d $client_ip -u $client_user -p $client_pwd"alllist=`$cmdstar $1`#输出以太网网卡ID列表IDlist=`echo "$alllist"|grep "^[0-9]:.*ID"|awk -F "= " '{print$2}'`Namelist=`echo "$alllist"|grep "SP"|awk -F "= " '{print$2}'|tr ' ' '-'`#SAS接口自动发现推送aa=`jsonput $Namelist` #json原始格式输出bb=`echo $aa|sed 's/ //g'` #将json去除换行和空格格式化成一行,方便后面推送至zabbixzbx_sender "$2" $bb #推送自动发现json数据
# echo "$aa"
# echo "$bb"for i in $IDlistdoeth=`echo "$alllist"|grep $i -A $4`ethname=`echo "$eth"|grep "SP"|awk -F"= " '{print $2}'|tr ' ' '-'`ethstatus=`echo "$eth"|grep "Health state"|awk -F"= " '{print $2}'`
# echo "状态输出 $ethname $ethstatus"ww=`echo "$3.[$ethname]"`qq=`echo $ethstatus|tr ' ' '_'`
# echo "监控项原形输出:$ww $qq"zbx_sender $ww $qqecho "---------"done
}#传输参数:命令、自动发现键值前缀、监控项键值前缀、单个指标信息相关行数(偏移量方便分割)、
#主要用于输出电源、存储处理器、硬盘状态
ps_putstatuszbx(){cmdstar="uemcli -d $client_ip -u $client_user -p $client_pwd"alllist=`$cmdstar $1`#输出以太网网卡ID列表IDlist=`echo "$alllist"|grep "^[0-9]*:.*ID"|awk -F "= " '{print$2}'`Namelist=`echo "$alllist"|grep "^[0-9]*:.*ID"|awk -F "= " '{print$2}'|tr ' ' '-'`#SAS接口自动发现推送aa=`jsonput $Namelist` #json原始格式输出bb=`echo $aa|sed 's/ //g'` #将json去除换行和空格格式化成一行,方便后面推送至zabbixzbx_sender "$2" $bb #推送自动发现json数据#echo "$aa" #echo "$bb"for i in $IDlistdoeth=`echo "$alllist"|grep "$i\>" -A $4`ethname=`echo "$eth"|grep "^[0-9]*:.*ID"|awk -F"= " '{print $2}'|tr ' ' '-'`ethstatus=`echo "$eth"|grep "Health state"|awk -F"= " '{print $2}'`#echo "状态输出 $ethname $ethstatus"ww=`echo "$3.[$ethname]"`qq=`echo $ethstatus|tr ' ' '_'`#echo "监控项原形输出:$ww $qq"zbx_sender $ww $qqecho "---------"done
}echo "推送sas接口状态"
putstatuszbx "/net/port/sas show" "sas.list" "sasstatus" 5echo "推送以太网接口状态"
putstatuszbx "/net/port/eth show" "eth.list" "status" 10echo "推送FC接口信息"
putstatuszbx "/net/port/fc show" "fc.list" "fcstatus" 6echo "推送电池状态"
sp_putstatuszbx "/env/bat show" "sp.list" "spstatus" 4echo "推送电源状态"
ps_putstatuszbx "/env/ps show" "ps.list" "psstatus" 5echo "推送存储处理器状态"
ps_putstatuszbx "/env/sp show" "cc.list" "ccstatus" 6echo "推送硬盘状态"
ps_putstatuszbx "/env/disk show" "disk.list" "diskstatus" 10echo "推送存储池容量"
pool_putstatuszbx "/stor/config/pool show -detail" "pool.list" "poolused" 36
以推送以太网接口信息为例说吧
#传输参数:命令、自动发现键值前缀、监控项键值前缀、单个指标信息相关行数(偏移量方便分割)、
putstatuszbx(){cmdstar="uemcli -d $client_ip -u $client_user -p $client_pwd" alllist=`$cmdstar $1`#输出以太网网卡ID列表IDlist=`echo "$alllist"|grep "^[0-9]:.*ID"|awk -F "= " '{print$2}'`Namelist=`echo "$alllist"|grep "Name"|awk -F "= " '{print$2}'|tr ' ' '-'`#SAS接口自动发现推送aa=`jsonput $Namelist` #json原始格式输出bb=`echo $aa|sed 's/ //g'` #将json去除换行和空格格式化成一行,方便后面推送至zabbixzbx_sender "$2" $bb #推送自动发现json数据#echo "$aa" #echo "$bb"#将每个以太网卡状态推送到zabbixfor i in $IDlistdoeth=`echo "$alllist"|grep $i -A $4`ethname=`echo "$eth"|grep "Name"|awk -F"= " '{print $2}'|tr ' ' '-'`ethstatus=`echo "$eth"|grep "Health state"|awk -F"= " '{print $2}'`
# echo "状态输出 $ethname $ethstatus"ww=`echo "$3.[$ethname]"`qq=`echo $ethstatus|tr ' ' '_'`
# echo "监控项原形输出:$ww $qq"zbx_sender $ww $qq#zbx_sender $ww "no"echo "---------"done
}
putstatuszbx "/net/port/eth show" "eth.list" "status" 10 可以看到是调用putstatuszbx这个函数,第一个参数是查询以太网状态的命令,第二个参数是创建自动发现规则时要的键值key,第三个参数是监控项原型的key前缀(为了避免主机监控项key冲突),第四个参数是用于分割文本用的,比如只要从某字符串开头的那行以下的10行内容,grep -A 10 直接可以获取。
重要点:
1、 #SAS接口自动发现推送
aa=`jsonput $Namelist` #json格式输出,必须要json格式
bb=`echo $aa|sed 's/ //g'` #将json去除换行和空格格式化成一行,方便后面推送至zabbix,有空格和换行推送就会报错
zbx_sender "$2" $bb #推送自动发现json数据
注意:echo $a和echo "$aa"是有区别的,区别在于echo "$aa" 原样输出(包含换行),echo $a不会输出换行
2、eth=`echo "$alllist"|grep $i -A $4` 这个就很关键了,虽然命令支持加ID信息查看单独以太网卡的信息,但是我没这样用,为什么?考虑到网络IO延迟问题,我在开始cmdstar="uemcli -d $client_ip -u $client_user -p $client_pwd" 就将所有以太网卡信息获取出来了,我通过分割字符串文本的方式来得到不同卡的信息,就是通过grep -A实现的,这样就只需要一次网络IO就能获取到我要的以太网卡所有信息
json格式这样的,由于我现在没环境所以你懂的,{#PORT}换成{#ETHNAME}就是上面jsonput函数输出的格式
命令获取信息如下:
root@localhost .pip]# uemcli -d ****** -u admin -p **** /net/port/eth show
Storage system address: ****
Storage system port: 443
HTTPS connection1: ID = spa_srmName = SP A Sync Replication Management PortSP = spaProtocols = srmMTU size = 1500Speed = 1 GbpsHealth state = OK (5)Aggregated port ID = NoneFSN port ID = NoneMAC address = F6:60:F0:76:8F:EB2: ID = spb_mgmtName = SP B Management PortSP = spbProtocols = mgmtMTU size = 1500Speed = Health state = OK (5)Aggregated port ID = NoneFSN port ID = NoneMAC address = 00:00:00:00:00:003: ID = spa_mgmtName = SP A Management PortSP = spaProtocols = mgmtMTU size = 1500Speed = 1 GbpsHealth state = OK (5)Aggregated port ID = NoneFSN port ID = NoneMAC address = 08:00:1B:FF:49:0F4: ID = spb_srmName = SP B Sync Replication Management PortSP = spbProtocols = srmMTU size = 1500Speed = Health state = OK (5)Aggregated port ID = NoneFSN port ID = NoneMAC address = 0A:83:23:80:0F:625: ID = spb_eth2Name = SP B Ethernet Port 2SP = spbProtocols = file, net, iscsiMTU size = 1500Speed = Health state = OK (5)Aggregated port ID = NoneFSN port ID = NoneMAC address = 00:60:16:AF:4C:956: ID = spb_eth3Name = SP B Ethernet Port 3SP = spbProtocols = file, net, iscsiMTU size = 1500Speed = Health state = OK (5)Aggregated port ID = NoneFSN port ID = NoneMAC address = 00:60:16:AF:4C:947: ID = spa_eth3Name = SP A Ethernet Port 3SP = spaProtocols = file, net, iscsiMTU size = 1500Speed = Health state = OK (5)Aggregated port ID = NoneFSN port ID = NoneMAC address = 00:60:16:A5:80:288: ID = spa_eth2Name = SP A Ethernet Port 2SP = spaProtocols = file, net, iscsiMTU size = 1500Speed = Health state = OK (5)Aggregated port ID = NoneFSN port ID = NoneMAC address = 00:60:16:A5:80:29
再就是zabbix模板编写了,给你们看截图