zabbix自动发现 url 以及对http返回状态码监控实现 告警
2019-06-04 18:39:12
目的:批量监控业务URL的返回状态码,通过zabbix监控判断业务好坏。
1、zabbix安装
请查看此永久链接:
2、编写监控脚本
cd /usr/lib/zabbix/externalscripts #本人存放脚本路径,存放路径可以自定义,这个定义文件在zabbix_server.conf中,及zabbix-server服务配置文件。
vim web_site_code_status.sh
#!/bin/bash
source /etc/profile >/dev/null 2>&1
WEB_SITE_discovery () {
WEB_SITE=($(grep -v "^#" /usr/local/zabbix/etc/zabbix_script.d/WEBURL.txt))
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 () {
#下面注释的是也可以使用域名+端口号
# host=$(echo "$1"|awk -F "/" '{print $3}')
# port=$(echo "$1"|awk -F "/" '{print $3}'|awk -F':' '{print $2}')
# if [[ -z $port ]]
# then
# ntlocal="127.0.0.1"
# else
# ntlocal="127.0.0.1:$port"
# fi
# http_uri=$(echo "$1"|awk -F "/" '{for(i=4;i<=NF;i++) printf "/" $i;printf "\n"}')
# /usr/bin/curl -H "Host:$host" -o /dev/null -s -w %{http_code} $ntlocal${http_uri}
/usr/bin/curl -I "$1" 2> /dev/null | grep 'HTTP' | awk '{print $2}'
}
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
3、创建URL列表
cat /etc/zabbix/zabbix_script.d/web_url_list.txt #这个文件在上面的脚本中定义。
www.baidu.com www.qq.com www.redhat.com https://www.redhat.com/en
https://www/zabbix.com
注意:这里也可以在后面加端口号
4、在zabbix_agentd.conf中添加相关的key
vim /etc/zabbix/zabbix_agentd.conf #客户端配置文件中添加&修改如下内容
UnsafeUserParameters=1 #允许将参数中的所有字符传递给用户定义的参数。
UserParameter=web.site.discovery,/usr/lib/zabbix/externalscripts/web_site_code_status.sh web_site_discovery
UserParameter=web.site.code[*],/usr/lib/zabbix/externalscripts/web_site_code_status.sh web_site_code $1
5、重启zabbix-agent
Linux6:
service zabbix-agent restart
Linux7:
systemctl restart zabbix-agent
6、导入zabbix监控模板文件。
复制如下代码,在本地计算机中创建一个文件:zbx_export_templates.xml (自定义创建):
<?xml version="1.0" encoding="UTF-8"?>3.4 2019-06-04T18:15:17Z http_status_code Automatically discover http_code Automatically discover http_code automatically discover http_status_code http_status_code Disvover http url 0 web.site.discovery 30s 0 0 0 0 0 0 30d http url $1 0 web.site.code[{#SITENAME}] 30s 90d 365d 0 3 0 0 0 0 0 HTTP response status code {Automatically discover http_code:web.site.code[{#SITENAME}].last(#3,15)}>=500 and {Automatically discover http_code:web.site.code[{#SITENAME}].last(,0)} 0 web.site.code {#SITENAME} greater 500 0 0 4 0 0 {Automatically discover http_code:web.site.code[{#SITENAME}].last(#3,15)}<>200 and {Automatically discover http_code:web.site.code[{#SITENAME}].last(,0)} 0 web.site.code {#SITENAME} is not 200 0 0 0 0 0 web.site.code.on {#SITENAME} 900 200 0.0000 100.0000 1 1 0 1 0 0.0000 0.0000 0 0 0 0 0 0 1A7C11 0 2 0 Automatically discover http_code web.site.code[{#SITENAME}] HTTP response status code 100 Continue 101 Switching Protocols 102 Processing 200 OK 201 Created 202 Accepted 203 Non-Authoritative Information 204 No Content 205 Reset Content 206 Partial Content 207 Multi-Status 208 Already Reported 226 IM Used 300 Multiple Choices 301 Moved Permanently 302 Found 303 See Other 304 Not Modified 305 Use Proxy 306 Switch Proxy 307 Temporary Redirect 308 Permanent Redirect/Resume Incomplete 400 Bad Request 401 Unauthorized 402 Payment Required 403 Forbidden 404 Not Found 405 Method Not Allowed 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict 410 Gone 411 Length Required 412 Precondition Failed 413 Payload Too Large 414 Request-URI Too Long 415 Unsupported Media Type 416 Requested Range Not Satisfiable 417 Expectation Failed 418 I'm a Teapot 419 Authentication Timeout 420 Method Failure/Enhance Your Calm 421 Misdirected Request 422 Unprocessable Entity 423 Locked 424 Failed Dependency 426 Upgrade Required 428 Precondition Required 429 Too Many Requests 431 Request Header Fields Too Large 440 Login Timeout 444 No Response 449 Retry With 450 Blocked by Windows Parental Controls 451 Unavailable for Legal Reasons/Redirect 494 Request Header Too Large 495 Cert Error 496 No Cert 497 HTTP to HTTPS 498 Token Expired/Invalid 499 Client Closed Request/Token Required 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported 506 Variant Also Negotiates 507 Insufficient Storage 508 Loop Detected 509 Bandwidth Limit Exceeded 510 Not Extended 511 Network Authentication Required 520 Unknown Error 598 Network Read Timeout Error 599 Network Connect Timeout Error
7、导入模板文件zbx_export_templates.xml
登录zabbix,登录过程跳过;
导入成功后可以看到如下名称的模板:
8、验证查看监控:
至此url监控完毕。
参考链接:https://blog.csdn.net/qq_34857250/article/details/78323780
参考链接:https://www.zabbix.com