zabbix微信python
A. 監控常用的應用軟體有什麼
常用的有:
1.cactio
實際上cacti不是監控工具,而是個依賴於SNMP的數據採集和數據呈現的工具。但是很多人喜歡用來當監控(因為其功能可以很好的完成這個工作)
功能:數據採集、 保存數據[SQL, txt].
數據展示(rrdtool 繪圖)。
數據分析和報警(很一般)。
2. nagios。
功能:數據報警(報警功能是Nagios的特色功能) [ 故障觸發,故障恢復都可以。
依賴分析報警(能自動的識別到關鍵設備的故障,關聯設備不會報警)。
數據採集(採集的數據是弱項,他只關心警戒位,只關心正常與否的狀態,狀態轉換時可以實現報警,所以它採集的數據不需要保存),當然也有插件彌補這個不足,如PNP4Nagios。
3. zabbix (php)(推薦)
Nagiostcacti整合互相彌補不足!I
nagios和 cacti不適合超大規模的監控、由於大規模的帶寬和網路限制,會導致監控的延遲等問題,所以有很多是 nagios+ cacti整合,但是依然不適合在大規模的環境中,不適合分布式部署, Nagios在大規模中就會出現延遲,失
去 Nagios本事的特色。
那麼 zabbix同時整合了 cacti和 Nagios特點的工具,而且還具有了前兩者不具有的工具,支持分布式等等。
4. 補充工具:
netdata:託管在github上的一款類型zabbix的開源監控工具https:/
/github. com/firehol/netdata
open- falcon:小米公司開源的企業級監控工具(python)(推薦)
Ganglia類似於 zabbix,大型分布式監控系統
開源監控工具對比http://www.oschina.net/news/67525/monitoring-tools
5. 監控軟體數據採集的方式
SNMP 協議。
agent 代理的方式去採集數據。
shell 腳本api 介面
6. 數據展示方式
php html app
7. 數據告警
mail,msm,微信,電話,釘釘機器人
B. 請教各位一個問題zabbix怎樣用shell或者python調用簡訊介面api進行發簡訊
2.填上發簡訊腳本的名稱 zabbix 實戰簡訊報警之調用簡訊介面3.注意zabbix_server.conf裡面的配置,指定腳本放的位置及賦予腳本執行和屬主zabbix
C. 如何採用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()
四,整理數據,匯報成圖形,撰寫技術報告