zabbixpythonapi
① 如何采用python zabbix
Zabbix API是基于JSON-RPC 2.0规格,具体实现可以选择任何自己爱好的编程语言,可以采用Perl、Ruby、PHP之类的。
py-zabbixby Alexey Dubkov - Zabbix Moles for Python (PyPI py-zabbix, no python3)
ZabbixPythonApiby Frank Yao - Zabbix API for Python (no python3)
zabbixby gescheit - a Python library (PyPI zabbix-api)
PyZabbixby Luke Cyca - a Python mole (PyPI pyzabbix, depends-on requests)
zabbix_apiby Grigoriy Netsman - scripts for creating and deleting hosts (depends on 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,dateti...
③ 如何获取zabbix以监控的所有机器IP
目的: 获取zabbix中所有监控的机器主机的IP信息
方法1 使用zabbix API 接口 python 程序
方法2 直接使用数据库进行查询 导出(ip 包括monitor和not monitor的机器,还有为删除的一些残留机器ip)
这里使用方法二来介绍
mysql -uUSERNAME -pPASSWORD 登入mysql
mysql> use zabbix 选定操作zabbix 库
mysql> select * from interface limit 1,10; 查看接口信息的表,表中一个字段是IP地址 (hosts 表中有 host 和name 字段,但是没有接口IP 字段)
+-------------+--------+------+------+-------+----------------+-----+-------+
| interfaceid | hostid | main | type | useip | ip | dns | port |
+-------------+--------+------+------+-------+----------------+-----+-------+
| 255 | 10361 | 1 | 1 | 1 | 192.168.213.21 | | 10050 |
| 256 | 10362 | 1 | 1 | 1 | 192.168.213.22 | | 10050 |
| 257 | 10363 | 1 | 1 | 1 | 192.168.213.23 | | 10050 |
| 258 | 10364 | 1 | 1 | 1 | 192.168.213.24 | | 10050 |
| 259 | 10365 | 1 | 1 | 1 | 192.168.213.25 | | 10050 |
| 261 | 10367 | 1 | 1 | 1 | 192.168.213.27 | | 10050 |
| 262 | 10368 | 1 | 1 | 1 | 192.168.213.28 | | 10050 |
| 263 | 10369 | 1 | 1 | 1 | 192.168.213.29 | | 10050 |
| 264 | 10370 | 1 | 1 | 1 | 192.168.213.30 | | 10050 |
| 265 | 10371 | 1 | 1 | 1 | 192.168.213.31 | | 10050 |
+-------------+--------+------+------+-------+----------------+-----+-------+
mysql> select * from interface into outfile '/tmp/zabbix.ip'; 已文本形式导出这个表(注意导出的路径 登入数据库用户必须,对这个路径有写的权限,/tmp权限777)
④ 如何采用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怎样用shell或者python调用短信接口api进行发短信
2.填上发短信脚本的名称 zabbix 实战短信报警之调用短信接口3.注意zabbix_server.conf里面的配置,指定脚本放的位置及赋予脚本执行和属主zabbix
⑥ 如何采用Python zabbix
ZabbixAPI是基于JSON-RPC2.0规格,具体实现可以选择任何自己爱好的编程语言,可以采用Perl、Ruby、PHP之类的。py-zabbixbyAlexeyDubkov-ZabbixMolesforPython(PyPIpy-zabbix,nopython3)ZabbixPythonApibyFrankYao-ZabbixAPIforPython(nopython3)zabbixbygescheit-aPythonlibrary(PyPIzabbix-api)PyZabbixbyLukeCyca-aPythonmole(PyPIpyzabbix,depends-onrequests)zabbix_apibyGrigoriyNetsman-(dependsonzabbix-api)