Zabbix 配置监控url返回值
添加配置文件
cd /etc/zabbix/zabbix_agentd.d
vim web_check.conf
UserParameter=urlcheck.discovery,/home/shell/web_site_code_status.sh web_site_discovery
UserParameter=urlcheck.code[*],/home/shell/web_site_code_status.sh web_site_code $1 $2 $3 $4
#重启
systemctl restart zabbix-agent
脚本
cd /home
mkdir shell
vim
#!/bin/bash
url_discovery() {
WEB_SITE=($(grep -v "^#" /url/*/*.txt))
printf '{\n'
printf '\t"data":[\n'
for((i=0;i<${#WEB_SITE[@]};++i))
{
num=$(echo $((${#WEB_SITE[@]}-1)))
tmp=${WEB_SITE[$i]}
arr=(${tmp//\|\|/ })
name=${arr[0]}
url=${arr[1]}
code=${arr[2]}
text=${arr[3]}
if [[ "$i" != ${num} ]]
then
printf "\t\t{ \n"
printf "\t\t\t\"{#WEBNAME}\":\"${name}\",\n"
printf "\t\t\t\"{#WEBURL}\":\"${url}\",\n"
printf "\t\t\t\"{#WEBCODE}\":\"${code}\"\n\t\t},\n"
# printf "\t\t\t\"{#WEBCODE}\":\"${code}\",\n"
# printf "\t\t\t\"{#WEBTEXT}\":\"${text}\"\n\t\t},\n"
else
printf "\t\t{ \n"
printf "\t\t\t\"{#WEBNAME}\":\"${name}\",\n"
printf "\t\t\t\"{#WEBURL}\":\"${url}\",\n"
printf "\t\t\t\"{#WEBCODE}\":\"${code}\"\n\t\t}"
# printf "\t\t\t\"{#WEBCODE}\":\"${code}\",\n"
# printf "\t\t\t\"{#WEBTEXT}\":\"${text}\"\n\t\t}"
printf "\n\t]\n}\n"
fi
}
}
function get_url_code(){
# tmp=`curl --head --location --connect-timeout 10 --write-out %{http_code} --silent --output /dev/null "$1"`
#tmp=`curl --write-out %{http_code} --silent --connect-timeout 20 --no-keepalive --output /dev/null "$1"`
tmp=`curl -Is -m 10 -w %{http_code} -o /dev/null "$1"`
code=`echo $tmp`
#判断$3是否为空
text=$3
if [ -z "$text" ];then
textline=1
else
textline=`echo $tmp | grep $3 | wc -l`
fi
#判断是否为数字
expr $code + 0 1>/dev/null 2>&1
if [ $? -ne 0 ];then
code=0
fi
#汇总判断
if [ $code -eq $2 -a $textline -gt 0 ];then
echo 正常
else
echo 异常
fi
}
case "$1" in
web_site_discovery)
url_discovery
;;
web_site_code)
#url,code,text
get_url_code $2 $3 $4
;;
*)
echo "Usage:$0 {web_site_discovery|web_site_code URL NAME}"
;;
esac
下载
#模板
wget https://aalrl.com/upload/2021/07/zbx_export_templates-e452df6cb85f469495a9844676c81fb3.xml
#脚本
wget https://aalrl.com/upload/2021/07/web_site_code_status-e35878b0f6cf40d3a55d15a9f519f377.sh