Due to network problems, Zabbix with the Web module can not use, background research and Development 2b, always update the formal environment installation package, leading to problems, always give them to wipe the buttocks, said this matter, they do not cooperate, now the problem, very cool, this pot I say no back, find the Pycurl This module write a monitor.
1234567891011121314151617181920212223242526 |
c = pycurl.Curl()
#创建一个curl对象
c.setopt(pycurl.CONNECTTIMEOUT, 5)
#连接的等待时间,设置为0则不等待
c.setopt(pycurl.TIMEOUT, 5)
#请求超时时间
c.setopt(pycurl.NOPROGRESS, 0)
#是否屏蔽下载进度条,非0则屏蔽
c.setopt(pycurl.MAXREDIRS, 5)
#指定HTTP重定向的最大数
c.setopt(pycurl.FORBID_REUSE, 1)
#完成交互后强制断开连接,不重用
c.setopt(pycurl.FRESH_CONNECT,1)
#强制获取新的连接,即替代缓存中的连接
c.setopt(pycurl.DNS_CACHE_TIMEOUT,60)
#设置保存DNS信息的时间,默认为120秒
c.setopt(pycurl.URL,
"http://www.baidu.com"
)
#指定请求的URL
c.setopt(pycurl.USERAGENT,
"Mozilla/5.2 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50324)"
)
#配置请求HTTP头的User-Agent
c.setopt(pycurl.HEADERFUNCTION, getheader)
#将返回的HTTP HEADER定向到回调函数getheader
c.setopt(pycurl.WRITEFUNCTION, getbody)
#将返回的内容定向到回调函数getbody
c.setopt(pycurl.WRITEHEADER, fileobj)
#将返回的HTTP HEADER定向到fileobj文件对象
c.setopt(pycurl.WRITEDATA, fileobj)
#将返回的HTML内容定向到fileobj文件对象
c.getinfo(pycurl.HTTP_CODE)
#返回的HTTP状态码
c.getinfo(pycurl.TOTAL_TIME)
#传输结束所消耗的总时间
c.getinfo(pycurl.NAMELOOKUP_TIME)
#DNS解析所消耗的时间
c.getinfo(pycurl.CONNECT_TIME)
#建立连接所消耗的时间
c.getinfo(pycurl.PRETRANSFER_TIME)
#从建立连接到准备传输所消耗的时间
c.getinfo(pycurl.STARTTRANSFER_TIME)
#从建立连接到传输开始消耗的时间
c.getinfo(pycurl.REDIRECT_TIME)
#重定向所消耗的时间
c.getinfo(pycurl.SIZE_UPLOAD)
#上传数据包大小
c.getinfo(pycurl.SIZE_DOWNLOAD)
#下载数据包大小
c.getinfo(pycurl.SPEED_DOWNLOAD)
#平均下载速度
c.getinfo(pycurl.SPEED_UPLOAD)
#平均上传速度
c.getinfo(pycurl.HEADER_SIZE)
#HTTP头部大小
|
The code is as follows:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
#!/usr/bin/env python
# __*__coding:utf8__*__
#Author:wangpengtai
#Blog:http://wangpengtai.blog.51cto.com/
import
pycurl
import
sys
import
StringIO
#引用该模块的原因是:使用pycurl后会打印出页面内容,我们不需要看到这个内容,只需要获取页面反馈信息就行了,只能将其写入缓存中,目前没找到好办法,学艺不精,不会使用重定向写到os.devnull中,无奈初次下策。。。
#开始使用的是写入临时文件,但是会有权限问题,导致zabbix无法获取到数据。
class WebStatus(
object
):
def
__init__(
self
, url):
self
.url
=
url
self
.curl
=
pycurl.Curl()
self
.string
=
StringIO.StringIO()
# 连接等待时间,0则不等待
self
.curl.setopt(pycurl.CONNECTTIMEOUT,
5
)
# 超时时间
self
.curl.setopt(pycurl.TIMEOUT,
5
)
# 下载进度条,非0则屏蔽
self
.curl.setopt(pycurl.NOPROGRESS,
1
)
# 指定HTTP重定向最大次数
self
.curl.setopt(pycurl.MAXREDIRS,
5
)
# 完成交互后强制断开连接,不重用
self
.curl.setopt(pycurl.FORBID_REUSE,
1
)
# 设置DNS信息保存时间,默认为120秒
self
.curl.setopt(pycurl.DNS_CACHE_TIMEOUT,
60
)
# 设置请求的Url
self
.curl.setopt(pycurl.URL,
self
.url)
self
.curl.setopt(pycurl.WRITEFUNCTION,
self
.string.write)
#将页面内容写入缓存
self
.curl.perform()
def
request_value(
self
):
data
=
{
"Http_code"
:
self
.curl.getinfo(pycurl.HTTP_CODE),
"Speed_download"
:
self
.curl.getinfo(pycurl.SPEED_DOWNLOAD),
"Connect_time"
:
self
.curl.getinfo(pycurl.CONNECT_TIME),
"Total_time"
:
self
.curl.getinfo(pycurl.TOTAL_TIME),
"Dnslookup_time"
:
self
.curl.getinfo(pycurl.NAMELOOKUP_TIME),
"Redirect_time"
:
self
.curl.getinfo(pycurl.REDIRECT_TIME),
"Redirect_count"
:
self
.curl.getinfo(pycurl.REDIRECT_COUNT)
}
return
data
def
__end__(
self
):
#释放内存和连接,做一个有始有终,有责任心的运维狗
self
.string.close()
self
.curl.close()
if
__name__
=
=
"__main__"
:
Usage
=
"""
Usage: python web_monitor.py url [Http_code|Speed_download|Connect_time|Total_time|Dnslookup_time|Redirect_time|Redirect_count]
"""
try
:
url
=
sys.argv[
1
]
request
=
sys.argv[
2
]
try
:
s
=
WebStatus(url)
try
:
print
s.request_value()[request]
except
KeyError:
print
"Make sure 2nd argument is right!"
except
pycurl.error:
print
"Make sure the url is right or reachable!"
except
IndexError:
print "Must be 2 arguments given!%s"
%
Usage
|
Verification: Www.baidu.com has always been the method and ingredient of the object board for my test (tapping)
Second, configure the Zabbix to customize the monitoring
This is relatively flexible, you can find a machine dedicated to monitoring, only need to configure the following on this machine to monitor multiple URLs.
A template can be configured in the Zabbix interface to hang it on the machine.
1. Write the code to the following directory and add the executable permission
1234 |
[[email protected] scripts] # pwd /etc/zabbix/scripts [[email protected] scripts] # vim web_monitor.py [[email protected] scripts] # chmod +x web_monitor.py |
2, Configuration zabbix_agentd.conf
12 |
[[email protected] scripts] # cat /etc/zabbix_agentd.conf UserParameter=web[*], /etc/zabbix/scripts/web_monitor .py $1 $2 |
3. Restart Zabbix-agentd
1 |
[[email protected] scripts] # service zabbix-agentd restart |
Third, configure Zabbix monitoring
Directly, the subsequent addition of the free play, a lot of return value can be out of the picture, can do trigger alarm and so on. Not much to describe.
Zabbix Monitor Web page status using Pycurl module