zabbix3.2使用fping批量监控ip的连通性

1.在zabbix-agent端安装fping

wget http://www.fping.org/dist/fping-3.16.tar.gz

tar zxvf fping-3.16.tar.gz

cd fping-3.16

./configure --prefix=/usr/local/fping/

make && make install


2.编写自动发现脚本

[root@eus_dlcbs_web01:/usr/local/zabbix_agents_3.2.0/scripts]# cat webip_status.sh 
#!/bin/bash 
# function:monitor tcp connect status from zabbix 
 

web_ip_discovery () { 
WEB_IP=($(cat  /usr/local/zabbix_agents_3.2.0/scripts/webip.txt|grep -v "^#")) 
        printf '{\n' 
        printf '\t"data":[\n' 
for((i=0;i<${#WEB_IP[@]};++i)) 
{ 
num=$(echo $((${#WEB_IP[@]}-1))) 
        if [ "$i" != ${num} ]; 
                then 
        printf "\t\t{ \n" 
        printf "\t\t\t\"{#SITENAME}\":\"${WEB_IP[$i]}\"},\n" 
                else 
                        printf  "\t\t{ \n" 
                        printf  "\t\t\t\"{#SITENAME}\":\"${WEB_IP[$num]}\"}]}\n" 
        fi 
} 
} 
 
web_site_code () { 
/usr/local/fping/sbin/fping $1|grep -c 'alive'
} 
 
case "$1" in
web_ip_discovery) 
web_ip_discovery 
;;
web_site_code) 
web_site_code $2
;; 
*) 
 
echo "Usage:$0 {web_ip_discovery|web_site_code [URL]}" 
;; 
esac


3.配置编写
vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/web_ip_discovery.conf

UserParameter=web.ip.discovery,/usr/local/zabbix_agents_3.2.0/scripts/webip_status.sh web_ip_discovery
UserParameter=web.ip.code[*],/usr/local/zabbix_agents_3.2.0/scripts/webip_status.sh web_site_code $1


[root@eus_dlcbs_web01:/usr/local/zabbix_agents_3.2.0/scripts]# cat webip.txt 
1.1.1.1
2.2.2.2


触发器:
web.ip.code {#SITENAME} is not alive

# 最近5个取值最大值不为1
{ip_monitor:web.ip.code[{#SITENAME},].max(#5)}<>1


报错处理:
Received value [/usr/local/fping/sbin/fping: can't create socket (must run as root?) : Permission denied0] is not suitable for value type [Numeric (unsigned)] and data type [Decimal]


(这里遇到一个问题,在客户端自测脚本是好的,但是到了服务端怎么样都执行不出来fping,因为fping必须由root执行,如果非root会报错,这是权限问题,chmod u+s /usr/local/fping/sbin/fping 就可以解决这个问题。)

[root@eus_dlcbs_web01:/usr/local/zabbix_agents_3.2.0/scripts]# ll /usr/local/fping/sbin/fping 
-rwxr-xr-x 1 root root 87700 Sep 11 20:54 /usr/local/fping/sbin/fping
[root@eus_dlcbs_web01:/usr/local/zabbix_agents_3.2.0/scripts]# chmod u+s /usr/local/fping/sbin/fping 
[root@eus_dlcbs_web01:/usr/local/zabbix_agents_3.2.0/scripts]# ll /usr/local/fping/sbin/fping 
-rwsr-xr-x 1 root root 87700 Sep 11 20:54 /usr/local/fping/sbin/fping



# 触发报警的时候由于只是显示IP,这样不能快速知道是哪台服务器出了问题,所以带上主机名,如下
# 自动发现脚本

[root@eus_dlcbs_web01:/usr/local/zabbix_agents_3.2.0/scripts]# cat webip_status.sh 
#!/bin/bash 
# function:monitor tcp connect status from zabbix 
 

web_ip_discovery () { 
WEB_IP=($(cat  /usr/local/zabbix_agents_3.2.0/scripts/ips.txt|grep -v "^#")) 
        printf '{\n' 
        printf '\t"data":[\n' 
for((i=0;i<${#WEB_IP[@]};++i)) 
{ 
num=$(echo $((${#WEB_IP[@]}-1))) 
        if [ "$i" != ${num} ]; 
                then 
        printf "\t\t{ \n" 
        printf "\t\t\t\"{#SITENAME}\":\"${WEB_IP[$i]}\"},\n" 
                else 
                        printf  "\t\t{ \n" 
                        printf  "\t\t\t\"{#SITENAME}\":\"${WEB_IP[$num]}\"}]}\n" 
        fi 
} 
} 
 
web_site_code () {
ip=`echo $1|awk -F ':' '{print $1}'`
#echo $ip
/usr/local/fping/sbin/fping ${ip}|grep -c 'alive'
} 
 
case "$1" in
web_ip_discovery) 
web_ip_discovery 
;;
web_site_code) 
web_site_code $2
;; 
*) 
 
echo "Usage:$0 {web_ip_discovery|web_site_code [URL]}" 
;; 
esac


# webip.txt

1.1.1.1:cbs_web01
2.2.2.2:cbs_web02

自动发现配置

posted @ 2018-09-13 22:55  reblue520  阅读(600)  评论(0编辑  收藏  举报