-
-
- from pyzabbix import ZabbixAPI
- from datetime import datetime
- import time
- import sys
-
- class zabbix(object):
- def __init__(self, hostname,columns,name):
self.hostname = hostname
- self.columns = columns
- self.dynamic = 0
- self.name = name
- self.server = ‘http://10.0.16.10/zabbix’
- self.user = ‘admin’
- self.passwd = ‘zabbix’
- def __login(self):
- zapi = ZabbixAPI(self.server)
- zapi.login(self.user,self.passwd)
- return zapi
-
- def __get_host(self):
- list_host=self.__login().host.get(output=’extend‘,filter={‘host’:self.hostname,})
-
- return list_host[0][‘hostid’]
-
- def __get_item(self):
- list_item=self.__login().item.get(output=’extend‘,hostids=self.__get_host())
- itemids=[]
- for x in list_item:
-
- itemids.append(x[‘itemid’])
-
- return itemids
-
- def __get_history(self):
- self.values={}
- for history in self.__get_item():
- list_history=self.__login().history.get(output=’extend‘,itemids=history,limit=1)
-
-
- for point in list_history:
-
- if self.values.has_key(history):
- self.values[history]=self.values[history]+int(point[‘value’])
- else:
- self.values[history]=int(point[‘value’])
-
-
-
- def __get_graph(self):
- graphids=[]
- list_graph=self.__login().graph.get(output=’extend‘,filter={“host”:self.hostname})
- for x in list_graph:
-
- graphids.append(x[‘graphid’])
- print graphids
- return graphids
-
-
- def __get_graphitem(self):
- have_value_graph=[]
- for x in self.__get_graph():
-
- list_graphitem=self.__login().graphitem.get(output=’extend‘,graphids=x)
-
- have_value=[]
- for b in list_graphitem:
- number=len(list_graphitem)
- if b[‘itemid’] in self.values.keys():
- if self.values[b[‘itemid’]] != 0:
-
- have_value.append(self.values[b[‘itemid’]])
- if len(have_value) > 0:
- have_value_graph.append(x)
-
- x = 0
- y = 0
- graph_list=[]
- for graph in have_value_graph:
-
-
- graph_list.append({
- “resourcetype”:’0′,
- “resourceid”: graph,
- “width”: “500”,
- “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 __create_screen(self):
- graphids=self.__get_graphitem()
- 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 __exists_screen(self):
- list_exists=self.__login().screen.exists(name=self.name)
- if list_exists:
- print ‘%s is exists’ % self.name
- sys.exit(1)
-
- def __exists_host(self):
- list_exists=self.__login().host.exists(host=self.hostname)
- if not list_exists:
- print “%s is not exists” % self.hostname
-
- def main(self):
- self.__exists_host()
- self.__exists_screen()
-
-
- self.__get_history()
-
-
- self.__create_screen()
-
- if __name__ == ‘__main__‘:
- from optparse import OptionParser
- parser = OptionParser()
- parser.add_option(‘-G’, dest=’graphname’,
- help=’Zabbix Host Graph to create screen from‘)
- parser.add_option(‘-H’, dest=’hostname’,
- help=’Zabbix Host to create screen from‘)
- parser.add_option(‘-c’, dest=’columns’, type=int,
- help=’number of columns in the screen’)
- options,args=parser.parse_args()
-
-
-
-
- a=zabbix(hostname=options.hostname,columns=options.columns,name=options.graphname)
- a.main()