最近要部署smokeping,想使用nginx的fastcgi的方式來部署,結果perl的執行的效率真是大失所望,就想用zabbix來實作smokeping。
python 環境2.6
參考的zabbix 社群
https://www.zabbix.com/forum/showthread.php?t=31147
實作的原理使用zabbix_sender主動向zabbixserver發送資料,zabbix就能采集到資料啦
檔案清單:
all create_graph.py create_other_screen.py define monitor.py zbxsmokepingprimary zbx_export_templates.xml
檔案說明:
all: 所要監控ip位址池,這個檔案monitor.py和create_graph.py都會在這裡讀取ip,create_graph.py是個多線程的腳本用于發送all的所有ip到zabbixserver,monitor.py去讀取all檔案的ip生成所需要監控的主
define: 定義客戶的ip位址,這個檔案會被create_create_other.py讀取,create_other_screen.py實作讀取define檔案中的ip位址實作自動生成帶客戶screen,手動生成的話會該覺很頭痛
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位址生成的圖檔
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmL1UjMv9EdXJlMZFXUBF0N5RUZ6JXVD9lTx02bpt0dvwlMF9CX5IzLcFDMN9CXyAzcml3dvwVbvNmLvR3YxUjLzM3Lc9CX6MHc0RHaiojIsJye.jpg)
轉載于:https://blog.51cto.com/8831068/1416320