通过zabbix批量监控 web url,脚本借鉴其他博友的,测试完全ok。自定义的key需要一台服务器部署,这边是放在zabbix_server端。
监控脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# cat web_site_code_status.sh
#!/bin/bash
# function:monitor tcp connect status from zabbix
source
/etc/bashrc
>
/dev/null
2>&1
source
/etc/profile
>
/dev/null
2>&1
#/usr/bin/curl -o /dev/null -s -w %{http_code} http://$1/
web_site_discovery () {
WEB_SITE=($(
cat
/usr/local/zabbix/share/zabbix/alertscripts/WEB
.txt|
grep
-
v
"^#"
))
printf
'{\n'
printf
'\t"data":[\n'
for
((i=0;i<${
#WEB_SITE[@]};++i))
{
num=$(
echo
$((${
#WEB_SITE[@]}-1)))
if
[
"$i"
!= ${num} ];
then
printf
"\t\t{ \n"
printf
"\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$i]}\"},\n"
else
printf
"\t\t{ \n"
printf
"\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$num]}\"}]}\n"
fi
}
}
web_site_code () {
/usr/bin/curl
-o
/dev/null
-s -w %{http_code} http:
//
$1
}
case
"$1"
in
web_site_discovery)
web_site_discovery
;;
web_site_code)
web_site_code $2
;;
*)
echo
"Usage:$0 {web_site_discovery|web_site_code [URL]}"
;;
esac
|
zabbix_agentd.conf文件添加如下2行:
1
2
3
|
cat
/usr/local/zabbix/etc/zabbix_agentd
.conf
UserParameter=web.site.discovery,
/usr/local/zabbix/share/zabbix/alertscripts/web_site_code_status
.sh web_site_discovery
UserParameter=web.site.code[*],
/usr/local/zabbix/share/zabbix/alertscripts/web_site_code_status
.sh web_site_code $1
|
脚本调用的web url文件,竖列写:
1
2
3
|
cat
/usr/local/zabbix/share/zabbix/alertscripts/WEB
.txt
www.baidu.com
www.qq.com
|
重启zabbix_agentd服务:
1
|
/etc/init
.d
/zabbix_agentd
restart
|
通过zabbix_get去获取自定义的key:
1
2
|
/usr/local/zabbix/bin/zabbix_get
-p 10050 -s 10.10.254.25 -k web.site.discovery
/usr/local/zabbix/bin/zabbix_get
-p 10050 -s 10.10.254.25 -k web.site.code[www.baidu.com]
|
具体的操作过程如下:
创建发现规则:
过滤配置:
监控原型:
触发器原型:
本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1886671