需求:
现在有大量 url 需要监控, 形式如 http://www.baidu.com , 要求 url 状态不为 200 即报警并且获得响应时间 (url 可改成自己应用里的 url)
需求详细分析:
大量的 url, 且 url 经常变化, 现在监控用的是 zabbix, 如果手动添加模板, 会造成大量重复工作, 如果利用脚本 + mail, 无法图形呈现
解决方案:
zabbix 有 discovery 功能, 即可轻松解决此问题
首先我们找一个随便找一个 zabbix 客户端来实现接下来要做的功能
首先我们从头来, 我们来装一下 zabbix 客户端
- rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/zabbix-release-3.4-1.el6.noarch.rpm
- yum -y install zabbix-agent
- vim 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 /etc/zabbix/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_time_total () {
- /usr/bin/curl -o /dev/null -s -w %{time_total} http://$1
- }
- 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_time_total)
- web_time_total $2
- ;;
- web_site_code)
- web_site_code $2
- ;;
- *)
- echo "Usage:$0 {web_site_discovery|web_site_code [URL]}"
- ;;
- esac
/opt/scripts/WEB.txt 在脚本路径创建 WEB.txt 文件, 文件内容为要监控 url, 格式如下 (添加自己的实际 url):
- http://www.baidu.com
- http://www.sina.com.cn
- http://www.163.com 10.10.10.10
- http://www.sohu.com 115.23.16.97:80
- http://www.111.com
在 zabbix 客户端加配置文件:
- vim /etc/zabbix/zabbix_agentd.d/web_site_discovery.conf
- UserParameter=web.site.discovery,/etc/zabbix/scripts/web_site_code_status.sh web_site_discovery
- UserParameter=web.site.code[*],/etc/zabbix/scripts/web_site_code_status.sh web_site_code $1 $2
测试是否正常:
- $ zabbix_get -s 10.0.0.109 -k web.site.discovery
- {
- "data":[
- {
- "{#SITENAME}":"www.baidu.com"},
- {
- "{#SITENAME}":"www.sina.com.cn"},
- {
- "{#SITENAME}":"www.****.com"},
- {
- "{#SITENAME}":"www.****.com"}]}
- $ zabbix_get -s 10.0.0.109 -k web.site.code[www.163.com]
- 200
在 zabbix server web 上添加
点击 add
然后去创建发现规则
现在开始创建 item
到此监控完毕!
补充:
curl 监控站点响应时间
监控站点首页下载时间:
- curl -o /dev/null -s -w %{time_total} http://www.miotour.com
- curl -o /dev/null -s -w %{http_code} http://www.miotour.com
- curl -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.miotour.com
结果: 2.547
-s 静默输出; 没有 - s 的话就是下面的情况, 这是在脚本等情况下不需要的信息
- [ec2-user@ip-10-122-250-19 ~]$ curl -o /dev/null -w %{time_total} http://www.miotour.com
- % Total % Received % Xferd Average Speed Time Time Time Current
- Dload Upload Total Spent Left Speed
- 100 67770 0 67770 0 0 19228 0 :: 0:00:03 :: 20705
结果: 3.524
监控首页各项时间指标:
curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} http://www.miotour.com
结果: 0.244: 1.044: 2.672
时间指标解释 :
time_connect 建立到服务器的 TCP 连接所用的时间
time_starttransfer 在发出请求之后, Web 服务器返回数据的第一个字节所用的时间
time_total 完成请求所用的时间
在 发出请求之后, Web 服务器处理请求并开始发回数据所用的时间是
(time_starttransfer) - (time_connect)0.244 = 0.8 秒
来源: http://www.bubuko.com/infodetail-2509943.html