Demand:
There are a number of URLs that need to be monitored, in the form of http://www.baidu.com, requiring a URL state of 200 to alert and get response time (URLs can be changed to URLs in their app).
Detailed Requirements Analysis:
A large number of URLs, and the URL changes frequently, now monitoring with Zabbix, if manually add a template, will cause a lot of duplication, if the use of script +mail, can not be graphically rendered
Solution:
Zabbix has discovery function to solve this problem easily
First, let's find a Zabbix client to implement the next function.
First we start from the beginning, let's install the Zabbix client
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
The script content is:
#!/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 "$" in
Web_site_discovery)
Web_site_discovery
;;
Web_time_total)
Web_time_total
;;
Web_site_code)
Web_site_code
;;
*)
echo "usage:$0 {Web_site_discovery|web_site_code [URL]}"
;;
Esac
/opt/scripts/web.txt Create the WEB.txt file in the script path, the file content is to monitor the URL, the format is as follows (add your own actual 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
In the Zabbix Client plus config file:
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
Test is normal:
$ 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
Adding on the Zabbix server Web
Click Add
Then go to create the discovery rule
Start creating Item Now
The monitoring is complete!
Add:
Curl Monitors site Response time
Monitoring site homepage Download time:
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
Results: 2.547
-s silent output, without the-s word is the following situation, which is not required in the case of scripts and other information.
[[email protected] ~]$ Curl-o/dev/null-w '%{time_total} ' http://www.miotour.com
% total% Received% xferd Average speed Time Time current
Dload Upload Total spent
100 67770 0 67770 0 0 19228 0–:–:–0:00:03–:–:–20705
Results: 3.524
Monitoring the first page of the time indicators:
Curl-o/dev/null-s-W '%{time_connect}:%{time_starttransfer}:%{time_total} ' http://www.miotour.com
Results: 0.244:1.044:2.672
Time Indicator Explanation:
Time_connect the time it took to establish a TCP connection to the server
Time_starttransfer the time it takes for the WEB server to return the first byte of data after making a request
Time_Total the time it took to complete the request
After the request is made, the time it takes for the WEB server to process the request and start sending the data back is
(Time_starttransfer)-(time_connect) 0.244 = 0.8 seconds
System Operation Engineer: Li Chao
Examples of URLs in Zabbix self-discovery monitoring app