當前位置:首頁 » 編程語言 » zabbixpythonapi

zabbixpythonapi

發布時間: 2022-09-12 21:48:55

① 如何採用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)

熱點內容
手機怎樣給程序加密軟體 發布:2025-01-12 06:47:11 瀏覽:824
地平線最高畫質筆記本要什麼配置才能玩 發布:2025-01-12 06:47:10 瀏覽:369
原神過主線任務腳本 發布:2025-01-12 06:34:51 瀏覽:514
醫保電子密碼在哪裡找到 發布:2025-01-12 06:34:38 瀏覽:349
安卓手機有網卻不能使用怎麼辦 發布:2025-01-12 06:25:20 瀏覽:213
arm存儲器映射 發布:2025-01-12 06:25:12 瀏覽:250
安卓系統個人字典有什麼用 發布:2025-01-12 06:13:37 瀏覽:929
geventpython安裝 發布:2025-01-12 06:13:34 瀏覽:339
放鬆解壓助睡眠直播 發布:2025-01-12 06:13:00 瀏覽:829
車載wince和安卓哪個好用 發布:2025-01-12 05:58:18 瀏覽:840