本篇文章给大家谈谈zabbix api接口,以及zabbix调用api接口进行监控对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
今天给各位分享zabbix api接口的知识,其中也会对zabbix调用api接口进行监控进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
如何采用Python zabbix
一:安装zabbix api 接口,修改zabbix api 调用接口,获取数据、
from zabbix_api import ZabbixAPI
import sys
import datetime
import time
import argparse
def fetch_to_csv(username,password,server,hostname,key,output,datetime1,datetime2,debuglevel):
zapi = ZabbixAPI(server=server, log_level=debuglevel)
try:
zapi.login(username, password)
except:
print "zabbix server is not reachable: %s" % (server)
sys.exit()
host = zapi.host.get({"filter":{"host":hostname}, "output":"extend"})
if(len(host)==0):
print "hostname: %s not found in zabbix server: %s, exit" % (hostname,server)
sys.exit()
else:
hostid=host[0]["hostid"]
print '*' * 100
print key
print '*' * 100
if(key==""):
print '*' * 100
items = zapi.item.get({"filter":{"hostid":hostid} , "output":"extend"})
if(len(items)==0):
print "there is no item in hostname: %s, exit" % (hostname)
sys.exit()
dict={}
for item in items:
dict[str(item['itemid'])]=item['key_']
if (output == ''):
output=hostname+".csv"
f = open(output, 'w')
str1="#key;timestamp;value\n"
if (datetime1=='' and datetime2==''):
for itemid in items:
itemidNr=itemid["itemid"]
str1=str1+itemid["key_"]+";"+itemid["lastclock"]+";"+itemid["lastvalue"]+"\n"
f.write(str1)
print "Only the last value from each key has been fetched, specify t1 or t1 and t2 to fetch more data"
f.close()
elif (datetime1!='' and datetime2==''):
try:
d1=datetime.datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
except:
print "time data %s does not match format Y-m-d H:M:S, exit" % (datetime1)
sys.exit()
timestamp1=time.mktime(d1.timetuple())
timestamp2=int(round(time.time()))
inc=0
history = zapi.history.get({"hostids":[hostid,],"time_from":timestamp1,"time_till":timestamp2, "output":"extend" })
for h in history:
str1=str1+dict[h["itemid"]]+";"+h["clock"]+";"+h["value"]+"\n"
inc=inc+1
f.write(str1)
f.close()
print str(inc) +" records has been fetched and saved into: " + output
elif (datetime1=='' and datetime2!=''):
for itemid in items:
itemidNr=itemid["itemid"]
str1=str1+itemid["key_"]+";"+itemid["lastclock"]+";"+itemid["lastvalue"]+"\n"
f.write(str1)
print "Only the last value from each key has been fetched, specify t1 or t1 and t2 to fetch more data"
f.close()
else:
try:
d1=datetime.datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
except:
print "time data %s does not match format Y-m-d H:M:S, exit" % (datetime1)
sys.exit()
try:
d2=datetime.datetime.strptime(datetime2,'%Y-%m-%d %H:%M:%S')
except:
print "time data %s does not match format Y-m-d H:M:S, exit" % (datetime2)
sys.exit()
timestamp1=time.mktime(d1.timetuple())
timestamp2=time.mktime(d2.timetuple())
inc=0
history =
zapi.history.get({"hostids":[hostid,],"time_from":timestamp1,"time_till":timestamp2,
"output":"extend" })
for h in history:
str1=str1+dict[h["itemid"]]+";"+h["clock"]+";"+h["value"]+"\n"
inc=inc+1
f.write(str1)
f.close()
print str(inc) +" records has been fetched and saved into: " + output
else:
#print "key is: %s" %(key)
itemid = zapi.item.get({"filter":{"key_":key, "hostid":hostid} , "output":"extend"})
if(len(itemid)==0):
print "item key: %s not found in hostname: %s" % (key,hostname)
sys.exit()
itemidNr=itemid[0]["itemid"]
if (output == ''):
output=hostname+".csv"
f = open(output, 'w')
str1="#key;timestamp;value\n"
if (datetime1=='' and datetime2==''):
str1=str1+key+";"+itemid[0]["lastclock"]+";"+itemid[0]["lastvalue"]+"\n"
#f.write(str1)
f.write(str1)
f.close()
print "Only the last value has been fetched, specify t1 or t1 and t2 to fetch more data"
elif (datetime1!='' and datetime2==''):
d1=datetime.datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
timestamp1=time.mktime(d1.timetuple())
timestamp2=int(round(time.time()))
history =
zapi.history.get({"history":itemid[0]["value_type"],"time_from":timestamp1,"time_till":timestamp2,
"itemids":[itemidNr,], "output":"extend" })
inc=0
for h in history:
str1 = str1 + key + ";" + h["clock"] +";"+h["value"] + "\n"
inc=inc+1
f.write(str1)
f.close()
print str(inc) +" records has been fetched and saved into: " + output
elif (datetime1=='' and datetime2!=''):
str1=str1+key+";"+itemid[0]["lastclock"]+";"+itemid[0]["lastvalue"]+"\n"
f.write(str1)
f.close()
print "Only the last value has been fetched, specify t1 or t1 and t2 to fetch more data"
else:
d1=datetime.datetime.strptime(datetime1,'%Y-%m-%d %H:%M:%S')
d2=datetime.datetime.strptime(datetime2,'%Y-%m-%d %H:%M:%S')
timestamp1=time.mktime(d1.timetuple())
timestamp2=time.mktime(d2.timetuple())
history =
zapi.history.get({"history":itemid[0]["value_type"],"time_from":timestamp1,"time_till":timestamp2,
"itemids":[itemidNr,], "output":"extend" })
inc=0
for h in history:
str1 = str1 + key + ";" + h["clock"] +";"+h["value"] + "\n"
inc=inc+1
print str(inc) +" records has been fetched and saved into: " + output
f.write(str1)
f.close()
二:撰写通过key获取一周内的数据
items : 在zabbix 中搜索主机,选择最新数据,找到项目(items),点击进入,能看到机器的所有keys,在负载到程序的items 字典中,程序会循环读取items ,获取数据
#/usr/bin/env python
#-*-coding:UTF-8
import os,sys,time
users=u'admin'
pawd = 'zabbix'
exc_py = '/data/zabbix/fetch_items_to_csv.py'
os.system('easy_install zabbix_api')
os.system('mkdir -p /data/zabbix/cvs/')
if not os.path.exists(exc_py):
os.system("mkdir -p /data")
os.system("wget
http://doc.bonfire-project.eu/R4.1/_static/scripts/fetch_items_to_csv.py
-O /data/zabbix/fetch_items_to_csv.py")
def work():
moniter='192.168.1.1'
ip_list =
['192.168.1.15','192.168.1.13','192.168.1.66','192.168.1.5','192.168.1.7','192.168.1.16','192.168.1.38','192.168.1.2','192.168.1.13','192.168.1.10']
for ip in ip_list:
show_items(moniter,ip )
if __name__ == "__main__":
sc = work()
三:数据采集完毕,进行格式化输出
#!/usr/bin/env python
#-*-coding:utf8-*-
import os,sys,time
workfile = '/home/zabbix/zabbix/sjz/'
def collect_info():
dict_doc = dict()
for i in os.listdir(workfile):
dict_doc[i] = list()
for v in os.listdir('%s%s' %(workfile,i)):
dict_doc[i].append(v)
count = 0
for x,y in dict_doc.items():
for p in y:
fp = '%s/%s/%s' %(workfile,x,p)
op = open(fp,'r').readlines()
np = '%s.txt' %p
os.system( """ cat %s|awk -F";" '{print $3}' %s """ %(fp,np))
count += 1
print count
if __name__ == "__main__":
sc = collect_info()
四,整理数据,汇报成图形,撰写技术报告

zabbix api怎么给host添加proxy
主机
Name:定义item的名称
Type:选择各种接口
key:点击select即可选择zabbix自带的key
Host interface: 选择主机接口。
Type of information:选择类型的信息,分别是 数字(无符号),数字(浮点),字符,日志,文本
Data type:选择数据的类型,分别是布尔值,八进制,十进制,十六进制
Units:单位,如计算百分比后的 %
Use custom multiplier:使用自定义乘法器,将数据乘以一个数值。可用来进行单位转换。zabbix采集数据的单位默认为b
Update interval (in sec):监控项每个多少秒更新一次
New flexible interval:可点击add添加自定义的灵活更新时间。
Keep history (in days):保存历史天数
Keep trends (in days):保存趋势天数
Store value:As is不改变数据值直接存储,Delta(speed per second):保存为(value-prev_time)的计算结果,既当前值减去前一次获取的数据值,除以当前时间戳前去前一次值获取时的时间戳得到的结果;如果当前值小于前一次的值,其将会被丢弃,Delta(simple change):保存为(value-prev_value)的计算结果;
Show value:没折腾明白。。
New application:是否创建新的item集合,并把item加入到新的application中。
Populates host inventory field:你可以选择一个host信息域存放该item的值,如果设置automatic inventory population,则他会自动工作
正确添加保存后,稍等片刻将可以看到数据。
Zabbix监控平台如何通过使用微信接口实现
首先,该操作是非常简单的。而且通过微信辅助邮件报警是一个非常好的选择,现在微信这么普及而且免费。短信报警也是非常好的,只不过都是双刃剑,如果晚上出现网络波动发送报警,那么这个是很头疼的,严重影响睡眠。 下边简单说下zabbix实现微信报警的步骤。
1,申请一个微信企业号。
申请的时候可以自己填写一个XXX工作室之类的名字,这个不影响。具体不做详细阐述,可以参考http://wuhf2015.blog.51cto.com/8213008/1688614说明去申请和设置。我这里创建了两个账号,一个manager01和manager02
2,zabbix server端进行shell脚本设置,这里我们操作的是weixin.sh。
/usr/local/zabbix/alertscripts[root@21yunwei alertscripts]# vim weixin.sh #!/bin/bash CropID='XXXXXXXX'Secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropIDamp;corpsecret=$Secret"Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $4}') PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" function body() { local int AppID=1 local UserID=$1 local PartyID=2 local Msg=$(echo "$@" | cut -d" " -f3-) printf '{\n' printf '\t"touser": "'"$UserID"\"",\n" printf '\t"toparty": "'"$PartyID"\"",\n" printf '\t"msgtype": "text",\n' printf '\t"agentid": "'" $AppID "\"",\n" printf '\t"text": {\n' printf '\t\t"content": "'"$Msg"\""\n" printf '\t},\n' printf '\t"safe":"0"\n' printf '}\n'}/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
chmod +x weixin.sh
脚本是否正常,我们可以先自己执行测试。比如执行:
sh weixin.sh manager01 testsub testcomtent 看下是否微信可以接收到消息,接收到说明成功,如果接受失败,那么设置有问题,需简单之前的步骤是否都操作对了。
3,创建报警媒介、action和设置报警接收
创建报警媒介,这里相信大家都不陌生。我这里创建的情况是:
创建action:报警步骤这里不详细说了,我只是写一点微信报警的内容,其他的都一样。创建action的步骤可以参考《zabbix 如何通过action进行报警配置》
\n报警问题:{TRIGGER.NAME}\n报警主机:{HOSTNAME}\n报警时间:{EVENT.TIME}\n报警状态:{TRIGGER.STATUS}\n报警级别:{TRIGGER.SEVERITY}
加行\n是为了换行。
好了,设置下微信报警接收人员:
保存后测试下效果。
比如我们useradd -M test123,添加以后就可以收到微信报警了。
关于zabbix api接口和zabbix调用api接口进行监控的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
zabbix api接口的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于zabbix调用api接口进行监控、zabbix api接口的信息别忘了在本站进行查找喔。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~