本篇文章给大家谈谈zabbix的api接口,以及zabbix对接短信接口对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
今天给各位分享zabbix的api接口的知识,其中也会对zabbix对接短信接口进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
如何采用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 | 登录
Zabbix API提供两项主要功能
zabbix的api接口:
使用JSON API 采用JSON-RPC实现。这意味着调用任何函数
zabbix的api接口,都需要发送POST请求,输入输出数据都是以JSON格式。大致工作流如下
zabbix的api接口:
可以采用脚本或者任何"手动"支持JSON RPC
zabbix的api接口的工具来使用API。而首先需要了解的就是如何验证和如何使用验证ID来获取想要的信息。后面的演示会以Python脚本呈现API的基本使用。
login.py

使用Python调用Zabbix API获取SESSIONID
Zabbix API 的使用流程
使用 API 的基本步骤
连接 http://x.x.x.x/api_jsonrpc.php 提供用户名和密码,
并标识 HTTP 头部 Content-Type:application/json,HTTP 方法为 POST。
获取 SESSIONID
通过 SESSIONID 建立后续的连接
提交 POST 数据,格式为 JSON,其中放对应的方法,获取需要的数据。
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 怎么访问api
前端
zabbix的api接口的表现可以跟web一致
zabbix的api接口,甚至不需要编写界面库就能实现很炫的效果
zabbix的api接口,
zabbix的api接口你的整个应用程序可以用cef实现多进程、沙盒安全控制。
如果你只是想做桌面的跨平台,上面给你的答案都能帮你解决。如果web和桌面你都需要考虑,HTML5还是首选。
关于zabbix的api接口和zabbix对接短信接口的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
zabbix的api接口的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于zabbix对接短信接口、zabbix的api接口的信息别忘了在本站进行查找喔。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~