#!/usr/bin/env python #coding =utf-8 From Zabbixget import Zabbix From time import CTime Import threading Import Createsub Import OS Import Time, DateTime def rrd_create (Grinfo): #如果主机存放rrd图形目录不存在, you create Bashdir = "/opt/rrd/" Path = Bashdir + grinfo[0][' HostID '] + "/" If not os.path.exists (path): Os.makedirs (PATH) Rrdname = str (path + grinfo[0][' Graphid '] + '. Rrd ') Timedaysago = (Datetime.datetime.now ()-Datetime.timedelta (days = 730)) Startstamp = str (int (time.mktime (Timedaysago.timetuple ())) DS = [] For sub in Grinfo: dstmp = str (' DS: ' + sub[' itemid '] + ': Gauge:120:0:u ') Ds.append (DSTMP) If Len (DS) = = 1:createsub. ITEM01 (Rrdname, Startstamp, DS) Elif len (DS) = = 2:createsub. ITEM02 (Rrdname, Startstamp, DS) Elif len (DS) = = 3:createsub. Item03 (Rrdname, Startstamp, DS) Elif len (DS) = = 4:createsub. Item04 (Rrdname, Startstamp, DS) Elif len (DS) = = 5:createsub. ITEM05 (Rrdname, Startstamp, DS) Elif len (DS) = = 6:createsub. Item06 (Rrdname, Startstamp, DS) Elif len (DS) = = 7:createsub. Item07 (Rrdname, Startstamp, DS) Elif len (DS) = = 8:createsub. Item08 (Rrdname, Startstamp, DS) Elif len (DS) = = 9:createsub. Item09 (Rrdname, Startstamp, DS) Elif len (DS) = = 10:createsub. ITEM10 (Rrdname, Startstamp, DS) Elif len (DS) = = 11:createsub. ITEM11 (Rrdname, Startstamp, DS) Elif len (DS) = = 12:createsub. ITEM12 (Rrdname, Startstamp, DS) Elif len (DS) = = 13:createsub. Item13 (Rrdname, Startstamp, DS) Elif len (DS) = = 14:createsub. Item14 (Rrdname, Startstamp, DS) Elif len (DS) = = 15:createsub. Item15 (Rrdname, Startstamp, DS) Elif len (DS) = = 16:createsub. ITEM16 (Rrdname, Startstamp, DS) Elif len (DS) = = 17:createsub. Item17 (Rrdname, Startstamp, DS) Elif len (DS) = = 18:createsub. Item18 (Rrdname, Startstamp, DS) Elif len (DS) = = 19:createsub. Item19 (Rrdname, Startstamp, DS) Elif len (DS) = = 20:createsub. ITEM20 (Rrdname, Startstamp, DS) Elif len (DS) = = 21:createsub. Item21 (Rrdname, Startstamp, DS) Elif len (DS) = = 22:createsub. ITEM22 (Rrdname, Startstamp, DS) Elif len (DS) = = 23:createsub. ITEM23 (Rrdname, Startstamp, DS) Elif len (DS) = = 24:createsub. Item24 (Rrdname, Startstamp, DS) def rrd_update (Rrdfile, data): Pass Def hosts_get (): Global ZB ZB = Zabbix () Hostsname = Zb.hostsid_get () Return Hostsname #遍历每台主机每张图的每个项目的最新10的值, and then take into account the pressure of the API to change to the latest value of each graph (reduced by A For loop variable items) def items_get (host): Bashdir = "/opt/rrd/" Graphsname = Zb.hostgraph_get (Host.keys ()) For graph in Graphsname: Grapitem = Zb.graphitems_get (graph[' Graphid ') Rrdfile = Bashdir + host.values () [0] + "/" + str (graph[' Graphid ']) + '. Rrd ' If Os.path.isfile (rrdfile): data = [] For item in Grapitem: TMP = {' itemid ': item[' itemid '], ' lastvalue ': item[' Lastvalue ']} Data.append (TMP) Rrd_update (rrdfile, data) Print data Else Grinfo = [] For item in Grapitem: TMP = {' HostID ': host.values () [0], ' graphid ': graph[' Graphid '], ' itemid ': item[' itemid ']} Grinfo.append (TMP) Rrd_create (Grinfo) def main (): Threads = [] Keys = Hosts_get () Numkey = Len (keys) loop = 0 For I in range (0, Numkey, 16): Nkeys = Range (loop*16, (loop+1) *16, 1) For I in Nkeys: If I >= Numkey: Break Else t = Threading. Thread (Target=items_get, args= (keys[i)) Threads.append (t) For I in Nkeys: If I >= Numkey: Break Else Threads[i].start () For I in Nkeys: If I >= Numkey: Break Threads[i].join () loop = loop + 1 if __name__ = = "__main__": Main () |