最近要部署smokeping,想使用nginx的fastcgi的方式來部署,結果perl的執行的效率真是大失所望,就想用zabbix來實現smokeping。php
python 環境2.6python
參考的zabbix 社區nginx
https://www.zabbix.com/forum/showthread.php?t=31147shell
實現的原理使用zabbix_sender主動向zabbixserver發送數據,zabbix就能採集到數據啦api
文件清單:服務器
all create_graph.py create_other_screen.py define monitor.py zbxsmokepingprimary zbx_export_templates.xml多線程
文件說明: app
all: 所要監控ip地址池,這個文件monitor.py和create_graph.py都會在這裏讀取ip,create_graph.py是個多線程的腳本用於發送all的全部ip到zabbixserver,monitor.py去讀取all文件的ip生成所須要監控的主ide
define: 定義客戶的ip地址,這個文件會被create_create_other.py讀取,create_other_screen.py實現讀取define文件中的ip地址實現自動生成帶客戶screen,手動生成的話會該覺很頭痛url
zbxsmokepingprimary: 是外部的執行的腳本須要在zabbix_server設置
zbx_export_templates.xml 模版文件
設置的選項:
zabbix_server:
開啓這個選項ExternalScripts=/usr/local/share/zabbix/externalscripts
zbxsmokepingprimary
ZBXSERVER:zabbix服務器的ip地址
FPING:fping的位置
ZBXSENDER:zabbix_sender的位置
zabbix主機監控有個類型爲external check 選項會去執行 zbxsmokepingprimary這個腳本,這個腳本須要有執行的權限
monitor.py和create_other_screen.py須要修改的zabbix url地址,賬號和密碼
create_graph.py:
num_threads=10 設置初始化的線程個數,ip多的話初始線程能夠跟ip個數相等
軟件包:
supervisor:python的程序,監控腳本,使之一直運行,supervisor須要執行的文件爲create_graph.py,這個就至關於守護進程來使用
pyzabbix:python鏈接zabbix的api
monitor.py 代碼:
#!/usr/bin/env python import pyzabbix class zabbix(object): def __init__(self): self.all=open('all') def _login(self): zapi= pyzabbix.ZabbixAPI('url') #your zabbix url zapi.login('user','password') # you zabbix user and password the user have write privilege return zapi def _get_template(self): temp=self._login().template.get(output='extend',filter={'host':'Template_SmokePrimary'}) return temp[0]['templateid'] def _get_group(self): if not self._login().hostgroup.exists(name='ping_check'): self._login().hostgroup.create(name='ping_check') group=self._login().hostgroup.get(output='extend',filter={'name':'ping_check'}) return group[0]['groupid'] def _create_host(self): for x in self.all: if not self._login().host.exists(host=x.strip()): try: self._login().host.create( host=x.strip(), interfaces={"type":1, "main":1, "useip":1, "ip":"127.0.0.1","dns":"","port":"10050"}, groups={"groupid":str(self._get_group())}, templates={"templateid":str(self._get_template())}, ) except Exception,e: print e def main(self): # self._get_group() # self._get_template() self._create_host() if __name__ == '__main__': a=zabbix() a.main()
create_other_screen.py代碼:
#!/usr/bin/env python import pyzabbix import re class zabbix(object): def __init__(self): self.define=open('define') self.dynamic=0 self.columns=2 for x in self.define: self.name=x.strip() break #the first line is you screen's name def _login(self): zapi= pyzabbix.ZabbixAPI('url') #you zabbix server url zapi.login('admin','zabbix') #your zabbix user and password return zapi def _get_host(self): host_ids=[] for x in self.define: if not re.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',x.strip()): continue if self._login().host.exists(host=x.strip()): hosts=self._login().host.get(filter={'host':[x.strip()]}) host_ids.append(hosts[0]['hostid']) return host_ids def _get_graph(self): graph_ids=[] for x in self._get_host(): graph=self._login().graph.get(hostids=x) graph_ids.append(graph[0]['graphid']) graph_list=[] x = 0 y = 0 for graph in sorted(graph_ids): graph_list.append({ "resourcetype":'0', "resourceid": graph, "width": "600", "height": "100", "x": str(x), "y": str(y), "colspan": "0", "rowspan": "0", "elements": "0", "valign": "0", "halign": "0", "style": "0", "url": "", "dynamic": str(self.dynamic) }) x += 1 if x == int(self.columns): x = 0 y += 1 return graph_list def _get_screen(self): id=self._login().screen.get(output='extend',filter={'name':self.name}) return id[0]['screenid'] def _create_screen(self): if self._login().screen.exists(name=self.name): self._login().screen.delete(str(self._get_screen())) #return 0 graphids=self._get_graph() columns = int(self.columns) if len(graphids) % self.columns == 0: vsize = len(graphids) / self.columns else: vsize = (len(graphids) / self.columns) + 1 self._login().screen.create(name=self.name,hsize=self.columns,vsize=vsize,screenitems=graphids) def main(self): # self._get_graph() # self._get_screen() self._create_screen() if __name__ == '__main__': a=zabbix() a.main()
create_graph.py 代碼
#!/usr/bin/env python import subprocess from threading import Thread from Queue import Queue import time f=open('all') d=[] for x in f: d.append('/usr/local/share/zabbix/externalscripts/zbxsmokepingprimary %s 10 1000 32 %s' % (x.strip(),x.strip())) queue=Queue() def do(i,q): while True: a=q.get() ret=subprocess.call(a,shell=True) q.task_done() num_threads=10 #init thread number for i in range(num_threads): worker = Thread(target=do,args=(i,queue)) worker.setDaemon(True) worker.start() for x in d: queue.put(x) queue.join() time.sleep(3)
我這邊對於主機在screen中沒有排序,每一個人的需求可能不一樣
下面附上腳本文件,軟件包須要本身去下載。。。。。
下面是我在百度和163域名解析到的ip地址生成的圖片