nagios分組出圖代碼實現講解[1]

來源:互聯網
上載者:User

標籤:nagios分組出圖代碼實現講解[1]

一、背景

    鑒於zabbix和cacti的服務分組及映像呈現功能,公司領導以業務最大化穩定為目標導向,對營運部提出“監控系統按服務分組出圖即時展現”的功能,營運部屬於公司的技術保證支撐部門,面對合理的需求,只能是Yes。

 

二、需求

     監控系統按服務分組出圖即時展現

 

三、軟體環境

[[email protected] ~]# cat /etc/redhat-release

CentOS release 6.7 (Final)

[[email protected] ~]# uname -r

2.6.32-573.el6.x86_64

lamp環境及nagios基礎環境的軟體版本如所示:

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/8C/C6/wKiom1h3Tnnix0A4AABo9eP3-Eo391.png-wh_500x0-wm_3-wmp_4-s_1392348390.png" title="nagios基礎環境所用軟體版本" alt="wKiom1h3Tnnix0A4AABo9eP3-Eo391.png-wh_50" />

圖1 nagios基礎環境所用軟體版本

四、需求分析及實現

   4.1分組功能

      我們現在使用的監控系統是nagios,眾所周知,nagios預設是帶hosts和servers分組功能的,如:

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/8C/C3/wKioL1h3TqyCIiBoAAAYTagn8PY642.png-wh_500x0-wm_3-wmp_4-s_3151465377.png" title="nagios預設的分組功能" alt="wKioL1h3TqyCIiBoAAAYTagn8PY642.png-wh_50" />

圖2 nagios預設的分組功能

    在此看到的分組菜單跟nagios4.1.1預設版本稍有差別,這個介面是我們修改過的樣式,感興趣的可以試用下。

    主機分組3所示。

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8C/C6/wKiom1h3TtvjvMxQAAEczCwzPGY157.png-wh_500x0-wm_3-wmp_4-s_4020091558.png" title="nagios主機分組" alt="wKiom1h3TtvjvMxQAAEczCwzPGY157.png-wh_50" />

圖3 nagios主機分組

    圖3中我們看到的分組相比修改之前多了一列(choice),不錯,這一列是如何增加的,是我們接下來要詳細講解的。

     nagios主機分組頁面和服務分組頁面是由status.cgi頁面控制的,所以我們要增加選擇列,必須修改nagios的源碼包,找到status.c檔案,修改並重新編譯,然後覆蓋現有的status.cgi.

    4.1.1修改ngios4.1.1源碼包

[[email protected] nagios-4.1.1]# ls cgi/status.c

cgi/status.c

[[email protected] nagios-4.1.1]# vimcgi/status.c +3598    #修改3598行為如下所示

/*choice add by xuekun 2016-12-23*/

printf("<thclass=‘status‘>choice</th>");       /*這一列是新加的,複選框列*/

printf("<thclass=‘status‘>Host</th><th>Status</th>");

printf("<th>Services</th><thclass=‘status‘>Actions</th>\n");

 [[email protected]]# vim cgi/status.c +3628    #複選框全選代碼,服務下拉式功能表代碼

/*choice all Begin by xuekun 2016-12-23 */

       printf("<tr>\n");

       printf("<td>\n");

       printf("<input type=\"checkbox\"id=\"check_%s\" name=\"all_%s\"onclick=\"check_all(this,‘%s‘)\" class=\"chec

kbox\"/>\n", hstgrp->group_name,hstgrp->group_name, hstgrp->group_name);

       printf("</td>\n");

       printf("<td>\n");

       printf("<select name=‘groupsrv‘>\n");

       printf("<option value=‘Current_Load‘selected=‘‘>Load</option>\n");

       printf("<optionvalue=‘Disk_Partition‘>Disk</option>\n");

       printf("<option value=‘PING‘>PING</option>\n");

       printf("<optionvalue=‘iostat‘>iostat</option>\n");

       printf("<optionvalue=‘Check_traffic‘>traffic</option>\n");

       printf("<optionvalue=‘check_tcp_stat‘>tcp_stat</option></select>\n");

       printf("</td>\n");

       printf("<td><input type=‘submit‘value=‘View‘/></td>\n");

       printf("<td></td>\n");

       printf("<td></td>\n");

       printf("</tr>\n");

       /*choice all Begin by xuekun 2016-12-23 */

    上述兩處代碼的添加,主要解決了新增複選框列和服務下拉式功能表的問題,接下來我們還要通過jquery實現複選框的全選,代碼的實現在544行的document_footer函數中,遵循web設計規則,將js放在頁面的最後,提高頁面載入速度。

void document_footer(void) {

 

       if(embedded == TRUE)

                return;

 

       /* include user SSI footer */

       include_ssi_files(STATUS_CGI, SSI_FOOTER);

       printf("<script type=\"text/javascript\">\n");

       printf("function check_all(obj,flag) \n");

       printf("{var is_checked =obj.checked;\n");

       printf("var checkboxs = $(\"input[flag=‘\"+ flag+\"‘]\");\n");

       printf("console.log(is_checked);\n");

       printf("checkboxs.each(function(){\n");

       printf("var cur_box = $(this);\n");

       printf("cur_box.prop(\"checked\", is_checked);\n");

       printf("});} </script>\n");

       printf("</body>\n");

       printf("</html>\n");

 

       return;

}

以上代碼修改編寫完畢,接下來要做的就是重新編譯nagios,命令如下

[[email protected] nagios-4.1.1]# make all      #執行make all命令重新編譯nagios

nagios編譯完畢,我們要將編譯好的status.cgi替換掉/usr/local/nagios/sbin/目錄的status.cgi,替換之前記得先將nagios停掉,具體操作如下:

[[email protected] nagios-4.1.1]# /etc/init.d/nagiosstop      #停掉nagios

[[email protected]]# cp /usr/local/nagios/sbin/status.cgi /home/xuekun/status.cgi_bak  #備份原有檔案

[[email protected] nagios-4.1.1]# /bin/cpcgi/status.cgi /usr/local/nagios/sbin/status.cgi  #替換原有status.cgi

執行完上述命令,F5重新整理nagios 訪問頁面,然後點擊“hostgroup”串連,看看是不是有新發現了呢!!!

祝你好運!

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/8C/C3/wKioL1h3TxTQ7B31AAEczCwzPGY479.png-wh_500x0-wm_3-wmp_4-s_1683530318.png" title="nagios主機分組" alt="wKioL1h3TxTQ7B31AAEczCwzPGY479.png-wh_50" />

本文出自 “北京開源人Linux營運實戰” 部落格,請務必保留此出處http://bdkyr.blog.51cto.com/7961566/1891535

nagios分組出圖代碼實現講解[1]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.