zabbix 监控CDN带宽
1
2
3
4
5
6
7
8
9
10
我这边使用的是网宿的CDN做加速,然后有一堆的接口可以调用单独查询;
网宿提供所有频道一起查询;cdn上面都是钱,稍微监控还是非常有必要的。
api信息格式:
https://myview.chinanetcenter.com/api/bandwidth-channel.action?u=xxxx&p=xxxx&cust=xxx&date=xxxx&channel=xxxxxx;xxxxx&isExactMatch=false®ion=xxxx&isp=xxxx&resultType=xxxx&format=xxx&startdate=xxxx&enddate=xxxx
说明:
u 和p 是必选项,p是cdn后台设置的myview密码;其他可以默认或者不选;
channel:频道信息;不填默认是全部。
isp:运营商带宽;默认是所有。
startdate和enddate: 查询的时间;不选默认是全部,而这个时间也有一个规律,就是年月和时间之前用%20转码:比如(2013-01-01%2010:10就是 2013-01-01 10:10)
其api文档可以咨询客服。
1
1、先导入bs 查看返回数据结构:
1
开始查看数据脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# cat check_cndbindwaith.py
#coding=utf-8
import urllib,urllib2
from bs4 import BeautifulSoup
import datetime
import sys
username = "xxx"
password = "xxxx"
now_time=datetime.datetime.now()
starttime=(now_time - datetime.timedelta(seconds=300)).strftime('%Y-%m-%d %H:%M')
starttimeformat = starttime.split()+"%20"+starttime.split()
endtime=(datetime.datetime.now()).strftime('%Y-%m-%d %H:%M')
endtimtformat = endtime.split()+"%20"+endtime.split()
url = "https://myview.chinanetcenter.com/api/bandwidth-channel.action?u=%s&p=%s&startdate=%s&enddate=%s" %(username,password,starttimeformat,endtimtformat)
try:
html = urllib2.urlopen(url, timeout=5)
except urllib2.HTTPError as err:
print str(err)
soup = BeautifulSoup(html)
print soup
1
二、查看结果并取值:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# python check_cndbindwaith.py
markup_type=markup_type))
<?xml version="1.0" encoding="utf-8"?><html><body><provider name="ChinaNetCenter" resulttype="1" type="bandwidth-channel">
<bandwidth time="2016-01-26 15:35:00">0.00</bandwidth>
</channel>
</date>
</provider></body></html>
备注:我们要取的是bandwidth的值。然后通过观察发现有时候脚本返回两个值。而我们zabbix
应该只要一个返回值。
三、zabbix 脚本:
#coding=utf-8
import urllib,urllib2
from bs4 import BeautifulSoup
import datetime
import sys
def cdn():
username = "xxx"
password = "xxxx"
now_time=datetime.datetime.now()
starttime=(now_time - datetime.timedelta(seconds=300)).strftime('%Y-%m-%d %H:%M')
starttimeformat = starttime.split()+"%20"+starttime.split()
endtime=(datetime.datetime.now()).strftime('%Y-%m-%d %H:%M')
endtimtformat = endtime.split()+"%20"+endtime.split()
data = []
url = "https://myview.chinanetcenter.com/api/bandwidth-channel.action?u=%s&p=%s&startdate=%s&enddate=%s" %(username,password,starttimeformat,endtimtformat)
try:
html = urllib2.urlopen(url, timeout=5)
except urllib2.HTTPError as err:
print str(err)
soup = BeautifulSoup(html)
for key in soup.find_all("bandwidth"):
data.append(key.get_text())
for i in data:
if i.startswith("0") and not i.startswith("1"):
return 0
else:
return int(i.split("."))
if __name__ == "__main__":
print cdn()
四、zabbix agent编写脚本并且收集数据:
1
2
# vim /usr/local/zabbix/etc/zabbix_agentd.conf
UserParameter=cdn,/usr/bin/python /usr/local/zabbix/scripts/check_cdnbindwaitch.py
五、添加item:
六:出图和触发器根据自己需要进行添加:
页:
[1]