国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Linux > 正文

Zabbix與RRDtool繪圖篇之圖形屬性歸檔

2024-08-27 23:58:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

每臺(tái)主機(jī)每張圖的rrd文件已經(jīng)創(chuàng)建好了,并且定時(shí)每分鐘添加從ZabbixAPI取得的監(jiān)控?cái)?shù)據(jù)到rrd文件,接下來(lái)就得進(jìn)行圖形展示了,在圖形 展示前需要將圖形歸類,不然請(qǐng)求一臺(tái)主機(jī)的圖形后還得扒開(kāi)海量的監(jiān)控圖尋找相關(guān)的應(yīng)用監(jiān)控圖(如nginx、php、redis等).

我按要求分了兩大類主機(jī)硬件類和應(yīng)用信息類,主機(jī)類主要展示主與機(jī)器相關(guān)的監(jiān)控圖如網(wǎng)絡(luò)、硬盤、CPU負(fù)載、內(nèi)存等,應(yīng)用信息大類又細(xì)分為Nginx_PHP、MySQL、Redis等.

實(shí)現(xiàn)這些圖形的分類第一步,數(shù)據(jù)庫(kù)表的設(shè)計(jì)models.

  1. from django.db import models, connection 
  2. # Create your models here. 
  3. ################################################# 
  4. class DrawTreeManager(models.Manager): 
  5.     def getclass(self, sql): 
  6.         cursor = connection.cursor() 
  7.         cursor.execute(sql) 
  8.         return cursor.fetchall() 
  9. class DrawTree(models.Model): 
  10.     classname = models.CharField(max_length=64) 
  11.     hostname = models.CharField(max_length=64) 
  12.     hostid = models.CharField(max_length=8) 
  13.     graphid = models.CharField(max_length=8) 
  14.     graphname = models.CharField(max_length=128) 
  15.     draw = models.CharField(max_length=2) 
  16.     type = models.CharField(max_length=2) 
  17.     objects = DrawTreeManager() 
  18.     def __unicode__(self): 
  19.         return self.hostname 
  20. ################################################ 
  21. class DrawGraphsManager(models.Manager): 
  22.     def getdata(self, sql): 
  23.         cursor = connection.cursor() 
  24.         cursor.execute(sql) 
  25.         return cursor.fetchall() 
  26. class DrawGraphs(models.Model): 
  27.     graphid = models.CharField(max_length=8) 
  28.     itemid = models.CharField(max_length=8) 
  29.     itemname = models.CharField(max_length=128) 
  30.     units = models.CharField(max_length=16, null=True, blank=True) 
  31.     objects = DrawGraphsManager() 
  32.     def __unicode__(self): 
  33.         return self.graphid 
  34. ################################################# 
  35. class DrawDefManager(models.Manager): 
  36.     def getdata(self, sql): 
  37.         cursor = connection.cursor() 
  38.         cursor.execute(sql) 
  39.         return cursor.fetchall() 
  40. class DrawDef(models.Model): 
  41.     graphid = models.CharField(max_length=8) 
  42.     cols = models.CharField(max_length=256, null=True, blank=True) 
  43.     types = models.CharField(max_length=256, null=True, blank=True) 
  44.     objects = DrawDefManager() 
  45.     def __unicode__(self): 
  46.         return self.graphid 

DrawGraphs表用來(lái)存放每張圖的所需的信息圖形id(graphid)、每張圖形包含的item的id和item的名字(itemid、itemname),根據(jù)一個(gè)graphid時(shí)能夠得到這張圖形的所有itemid、itemname、unit(item的計(jì)數(shù)單位),itemid主要是用來(lái)沖rrd文件中取對(duì)應(yīng)DS的數(shù)據(jù)(rrd文件中的DS名我是用的itemid),itemname主要顯示圖形下面布告牌最大值、最小值神馬的,unit就是Y軸顯示的單位了.

DrawTree表主要用來(lái)存放圖形的層疊導(dǎo)航信息的,遍歷所有的圖形,根據(jù)圖形名的關(guān)鍵字將每幅圖歸檔,classname字段存放每組的組名,硬件數(shù)據(jù)的組名取Zabbix的默認(rèn)分組,應(yīng)用數(shù)據(jù)的分組是我自定義的分組如Nginx_PHP、Redis、MySQL等分組,hostname字段對(duì)應(yīng)主機(jī)名、hostid對(duì)應(yīng)主機(jī)id、graphname對(duì)應(yīng)圖形名、graphid對(duì)應(yīng)圖形id,draw對(duì)應(yīng)圖形是否顯示的標(biāo)志位(1顯示、0不顯示),type代表圖形的分類(0代表硬件信息、1代表Nginx_PHP、2代表MySQL以此類推.

DrawDef表用來(lái)存放自定義圖形顯示所需的信息,graphid字段為需要自定義顯示圖形的圖形id,cols代表需要自定義圖形的顏色代碼每種顏色用分號(hào)隔開(kāi),types對(duì)應(yīng)需要自定義的繪圖方法如線性(LINE1、LINE2、LINE3)、區(qū)塊(AREA)等.

更新DrawTree與跟新DrawGraphs記錄是同時(shí)進(jìn)行的(為了精簡(jiǎn)代碼和邏輯),views相關(guān)的代碼是這樣的:

  1. def zabbixindex(request): 
  2.     if request.method == 'GET'
  3.         for key in request.GET: 
  4.             value = request.GET[key
  5.             break 
  6.         if value == 'check'
  7.             zbx = Zabbix() 
  8.             groupinfo = zbx.group_get() 
  9.             hostsinfo = zbx.hostsid_get() 
  10.             for host in groupinfo: 
  11.                 groupid = host['groupid'
  12.                 groupname = host['groupname'
  13.                 hostid = host['hostid'
  14.                 hostname = '' 
  15.                 for i in hostsinfo: 
  16.                     if i.values()[0] == hostid: 
  17.                         hostname = i.keys()[0] 
  18.                         break 
  19.                 hostgraphs = zbx.hostgraph_get(hostname) 
  20.                 for graph in hostgraphs: 
  21.                     graphid = graph['graphid'
  22.                     graphname = graph['name'
  23.                     #不存在的圖形才更新 
  24.                     #圖形展示分類 
  25.                     flag = DrawTree.objects.filter(graphid=graphid) 
  26.                     if not flag: 
  27.                         #更新zabbixapp_drawtree表 
  28.                         draw = '1' 
  29.                         if 'PHP-FPM' in graphname or 'Nginx' in graphname: 
  30.                             type = '1' 
  31.                             classname = 'Nginx_PHP' 
  32.                         elif 'MySQL' in graphname or 'InnoDB' in graphname: 
  33.                             type = '2' 
  34.                             classname = 'MySQL' 
  35.                         elif 'Redis' in graphname: 
  36.                             type = '3' 
  37.                             classname = 'Redis' 
  38.                         elif 'Memcached' in graphname: 
  39.                             type = '4' 
  40.                             classname = 'Memcached' 
  41.                         elif 'MongoDB' in graphname: 
  42.                             type = '5' 
  43.                             classname = 'MongoDB' 
  44.                         else
  45.                             classname = groupname 
  46.                             type = '0' 
  47.                         DrawTree.objects.create(classname=classname, hostname=hostname, hostid=hostid, graphid=graphid, graphname=graphname, 
  48.                                                 draw=draw, type=type) 
  49.                         #更新zabbixapp_drawgraphs表 
  50.                         itemsinfo = zbx.graphitems_get(graphid) 
  51.                         units = itemsinfo[0]['units'
  52.                         for item in itemsinfo: 
  53.                             itemid = item['itemid'
  54.                             itemname = item['name'
  55.                             DrawGraphs.objects.create(graphid=graphid, itemid=itemid, itemname=itemname, units=units) 
  56.             return HttpResponseRedirect("/graph/zabbix/"
  57.         #非get或post請(qǐng)求部分,tree 
  58.         ######################## 
  59.         sql_0 = "select distinct classname from zabbixapp_drawtree where type='0'" 
  60.         type_0 = DrawTree.objects.getclass(sql_0) 
  61.         typelist0 = [] 
  62.         for i in type_0: 
  63.             sql = "select distinct hostname,hostid from zabbixapp_drawtree where classname="+"'"+i[0]+"'"+" and type='0'" 
  64.             chosts = DrawTree.objects.getclass(sql) 
  65.             tmplist = [] 
  66.             for chost in chosts: 
  67.                 tmp = {'hostname':chost[0], 'hostid':chost[1]} 
  68.                 tmplist.append(tmp) 
  69.             typelist0.append({'type':i[0].replace(" ",'-'), 'host':tmplist}) 
  70.         ######################## 
  71.         sql_1 = "select distinct classname from zabbixapp_drawtree where type!='0'" 
  72.         type_1 = DrawTree.objects.getclass(sql_1) 
  73.         typelist1 = [] 
  74.         for i in type_1: 
  75.             sql = "select distinct hostname,hostid,type from zabbixapp_drawtree where classname="+"'"+i[0]+"'" 
  76.             chosts = DrawTree.objects.getclass(sql) 
  77.             tmplist = [] 
  78.             for chost in chosts: 
  79.                 tmp = {'hostname':chost[0], 'hostid':chost[1]} 
  80.                 tmplist.append(tmp) 
  81.             typelist1.append({'type':i[0], 'host':tmplist, 'tflag': chost[2]}) 
  82.     #value = typelist1  --Vevb.com 
  83.     avg = {'privatetitle''Zabbix監(jiān)控?cái)?shù)據(jù)展示''STATIC_URL''/static''list0':typelist0, 'list1':typelist1} 
  84.     return render_to_response('zabbixapp/zabbixindex.html'avg

該視圖函數(shù)渲染的模板zabbixindex.html寫的比較簡(jiǎn)單,代碼是這樣的:

  1. {% extends "base.html" %} 
  2. {% block title %}{{ privatetitle }}{% endblock %} 
  3. {% block thercss %} 
  4. {% endblock %} 
  5. {% block otherjs %} 
  6. {% endblock %} 
  7. {% block content %} 
  8. <div class="container"
  9.     <div class="row"
  10.     <div class="col-md-3"
  11.         <button type="button" class="btn btn-primary active"
  12.             <span class="glyphicon glyphicon-tasks"></span> 硬件數(shù)據(jù) </button> 
  13.     {% for i in list0 %} 
  14.     <div> 
  15.     <a href="#{{i.type}}" data-toggle="collapse"><i class="glyphicon glyphicon-hdd"></i>{{i.type}}</a> 
  16.     <ul id="{{i.type}}" class="collapse"
  17.         {% for tmp in i.host %} 
  18.         <li><a  target="draw" href="/graph/zbdraw/?type=0&hostid={{tmp.hostid}}">{{tmp.hostname}}</a></li> 
  19.         {% endfor %} 
  20.     </ul> 
  21.     </div> 
  22.     {% endfor %} 
  23.         <button type="button" class="btn btn-primary active"
  24.             <span class="glyphicon glyphicon-folder-open"></span> 應(yīng)用數(shù)據(jù) </button> 
  25.     {% for i in list1 %} 
  26.     <div> 
  27.     <a href="#{{i.type}}" data-toggle="collapse"><i class="glyphicon glyphicon-usd"></i>{{i.type}}</a> 
  28.     <ul id="{{i.type}}" class="collapse"
  29.         {% for tmp in i.host %} 
  30.         <li><a target="draw" href="/graph/zbdraw/?type={{i.tflag}}&hostid={{tmp.hostid}}">{{tmp.hostname}}</a></li> 
  31.         {% endfor %} 
  32.     </ul> 
  33.     </div> 
  34.     {% endfor %} 
  35.     <p></p> 
  36.     <div align="left"
  37.         <a href="/graph/zabbix/?action=check" class="btn btn-info" role="button">新增圖形檢測(cè)</a> 
  38.     </div> 
  39.     </div> 
  40. <script type="text/javascript"
  41.     //iframe高度自適應(yīng) 
  42.     function autoHeight(){ 
  43.         var iframe = document.getElementById("draw"); 
  44.         if(iframe.Document){//ie自有屬性 
  45.             iframe.style.height = iframe.Document.documentElement.scrollHeight; 
  46.         }else if(iframe.contentDocument){//ie,firefox,chrome,opera,safari 
  47.             iframe.height = iframe.contentDocument.body.offsetHeight ; 
  48.         } 
  49.     } 
  50. </script> 
  51.     <div class="col-md-9"
  52.                 <iframe name="draw" id="draw" frameborder="0" scrolling="no" style="width:99%;border:none" onload="autoHeight();"></iframe> 
  53.     </div> 
  54.     </div> 
  55. </div> 
  56. {% endblock %} 

從ZabbixAPI取圖形的各種屬性值,經(jīng)過(guò)適當(dāng)?shù)姆诸惒僮骱髮⒎诸悢?shù)據(jù)在本地落地,然后取落地后的分類數(shù)據(jù)將硬件和業(yè)務(wù)信息分類展示出來(lái).

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 皮山县| 博爱县| 嘉荫县| 长治县| 双桥区| 铅山县| 固安县| 正阳县| 汝州市| 噶尔县| 宁强县| 孟村| 南和县| 吴江市| 阿拉尔市| 龙里县| 偃师市| 伊川县| 丹凤县| 延吉市| 裕民县| 舒城县| 班玛县| 海淀区| 福安市| 荃湾区| 盐城市| 徐闻县| 甘德县| 平远县| 无为县| 青州市| 扬州市| 秀山| 马龙县| 乳源| 明溪县| 通江县| 依兰县| 南安市| 宜宾市|