萬盛學電腦網

 萬盛學電腦網 >> 服務器教程 >> Zabbix與RRDtool繪圖之創建每台主機每張圖的rrd文件

Zabbix與RRDtool繪圖之創建每台主機每張圖的rrd文件

  下面我們一起來看一個關於Zabbix與RRDtool繪圖之創建每台主機每張圖的rrd文件文章,希望這個例子可以給你有幫助。

  RRDtool對於圖形展示有多優秀,想必懂的人都知道。

  兵馬未動糧草先行。搞IT的得手冊先行RRDtool的官方手冊地址http://oss.oetiker.ch/rrdtool/doc/index.en.html 還有ailms整理的“RRDtool簡體中文教程 v1.01” 該教材通俗易懂,先膜拜下!基本看了alims的 那個教程就對RRDtool清楚了。

  我創建每台主機每張rrd文件用的是多線程,遍歷每張圖的時間大概12秒的樣子,千張圖的rrd文件創建大概2分鐘的樣子,主要受硬件的限制,如果

  有更快的硬盤和更多的線程應該會快不少。rrd文件分類參考了cacti的風格,創建的rrd文件按主機ID分文件夾存放(hostid),每張圖的名稱

  由圖形ID和.rrd後綴組成(graphid.rrd)。

Zabbix與RRDtool繪圖之創建每台主機每張圖的rrd文件 三聯

  創建每個rrd文件的py模塊如下,寫的有點笨,有多少個不同的item組成的圖形就得定義不同的創建函數,我找了一個下午都沒有找到更好的傳參方法,先

  就用這個笨方法。記住有幾種DS就需要寫對應的Item函數,下面我只列出Item03,我是不會告訴你們我總共堆了24個一直到Item24

  代碼如下復制代碼

  #!/usr/bin/env python

  #coding=utf-8

  import rrdtool

  def Item01(rrdname, startStamp, DS):

  rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],

  'RRA:AVERAGE:0.5:1:3000',

  'RRA:AVERAGE:0.5:5:4200',

  'RRA:AVERAGE:0.5:24:3800',

  'RRA:AVERAGE:0.5:240:4400',

  'RRA:MAX:0.5:1:3000',

  'RRA:MAX:0.5:5:4200',

  'RRA:MAX:0.5:24:3800',

  'RRA:MAX:0.5:240:4400',

  'RRA:MIN:0.5:1:3000',

  'RRA:MIN:0.5:5:4200',

  'RRA:MIN:0.5:24:3800',

  'RRA:MIN:0.5:240:4400')

  def Item02(rrdname, startStamp, DS):

  rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],

  DS[1],

  'RRA:AVERAGE:0.5:1:3000',

  'RRA:AVERAGE:0.5:5:4200',

  'RRA:AVERAGE:0.5:24:3800',

  'RRA:AVERAGE:0.5:240:4400',

  'RRA:MAX:0.5:1:3000',

  'RRA:MAX:0.5:5:4200',

  'RRA:MAX:0.5:24:3800',

  'RRA:MAX:0.5:240:4400',

  'RRA:MIN:0.5:1:3000',

  'RRA:MIN:0.5:5:4200',

  'RRA:MIN:0.5:24:3800',

  'RRA:MIN:0.5:240:4400')

  def Item03(rrdname, startStamp, DS):

  rrdtool.create(rrdname, '--step', '60', '--start', startStamp, DS[0],

  DS[1], DS[2],

  'RRA:AVERAGE:0.5:1:3000',

  'RRA:AVERAGE:0.5:5:4200',

  'RRA:AVERAGE:0.5:24:3800',

  'RRA:AVERAGE:0.5:240:4400',

  'RRA:MAX:0.5:1:3000',

  'RRA:MAX:0.5:5:4200',

  'RRA:MAX:0.5:24:3800',

  'RRA:MAX:0.5:240:4400',

  'RRA:MIN:0.5:1:3000',

  'RRA:MIN:0.5:5:4200',

  'RRA:MIN:0.5:24:3800',

  'RRA:MIN:0.5:240:4400')

  基本創建rrd文件的模塊寫好了,然後組織數據、調用函數傳遞相應的參數就可以了,下面是我寫的一個多線程程序,同時啟用16個線程工作(CPU線程數的2倍)。可更改線程數適應不同的硬件環境。

  代碼如下復制代碼

  #!/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圖形目錄不存在,則創建

  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的值,後來考慮到api的壓力改成每張圖的最新值(減少了一個for循環變量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()

 

  上面還有更新數據的函數沒有寫完,用pass站位。過多的解釋就不用了,對著相應的注釋大家都應該看的懂。

 
copyright © 萬盛學電腦網 all rights reserved