開發nagios修改配置檔案平臺

科技小先鋒發表於2017-11-15
[背景]本文的宗旨,是為解決在後期Nagios新增監控物件而煩的管理員做的工具。當然,也是為我減輕工作,而開發的。雖然說,我們可以寫指令碼來解決,但如果是那種每天會新增一兩個IP來做監控,那麼我們是不是每天都要登入伺服器,修改配置檔案,再測試,重啟。感覺不是很方便,如果有一個web介面來操作,那視覺肯定又不一樣,與是我就想到了要開發這款工具,來為自己或者大家來解決現在的問題,可能會有人說,現在的這樣的圖形工具太多了,是的,我也測試了一款,但感覺不是很理想,把我原先的配置檔案全打亂了,而且在新增監控物件的時候,N多引數要填寫,那個煩啊!~今天,這款工具,只要平臺支援php,基本就可以使用了。喜不喜歡,在與能不能讓你的工作更方便,不過,我已經準備用它了。呼呼….
[程式碼]
<?php
/*——————————————–
Codz By Badboy
MSN:hahazhu0634@live.cn
Author: Badboy
Date:2009/15/08
———————————————-
*/


$nagios_action =$_GET[“nagiosaction”];
$editfile = $_GET[“editfile”];

if($nagios_action == “test”){
        echo “<pre>”;
        echo “<font color=blue>”;
        system(`/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg`);
        echo “</font>”;
        echo “</pre>”;
}elseif($nagios_action == “restart”){
        echo “<pre>”;
        echo “<font color=blue>”;
        system(`/etc/rc.d/init.d/nagios restart`);
        echo “</font>”;
        echo “</pre>”;
}
//編輯檔案
if ($editfile) {
  $content=basename($editfile);
  if(empty($_POST[`newcontent`])){
    echo `<table><tr>`;
    echo `<form action=”” method=”post”>`;
    echo `</tr>`;   
    $fp=@fopen(“$content”,”r”);
    $data=@fread($fp,filesize($content));
    echo `<tr>`;
    echo `<textarea name=”newcontent” cols=”80″ rows=”20″ >`;
    echo $data;
    @fclose($fp);
    echo `</textarea></tr>`;
   echo `<input type=”submit” value=”Edition document”/></form></table>`;
  }
   if (!empty($_POST[`newcontent`]))
    {
       $fp=@fopen(“$content”,”w+”);
       echo ($result=@fwrite($fp,$_POST[`newcontent`]))?”<font color=red>The edit document succeeds!Good Luck!</font>”:”<font color
=blue>The edit document is fail!</font>”;
       @fclose($fp);
    }
}

?>
<html>
<title>Modified Nagios config file</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″;>
<STYLE type=”text/css”>
body {font-family: “sans-serif”, “serif”; font-size: 12px;}
BODY { background-color:#A2B5CD }
a:link {color: #BFEFFF; text-decoration: none}
a:visited {color: #080808; text-decoration: none}
a:hover {color: #FFFFFF; text-decoration: underline}
input {font-family: “sans-serif”, “serif”;font-size: 12px;}
td {font-family: “sans-serif”, “Verdana”; font-size: 12px;}
.title {font-family: “Verdana”, “Tahoma”;font-size: 20px;font-weight: bold; color=black}
</STYLE>
</head>
<body>
<table width=”100%”  cellspacing=”1″ cellpadding=”3″>
  <tr>
    <td class=”title” align=”center”>Modified Nagios –by Badboy</td>
  </tr>
</table>
<hr>
<table width=”100%”  cellspacing=”1″ cellpadding=”3″>
  <tr>
  <td>
  Operating system:
  <?php 
  echo PHP_OS;?></td><td>Server name:<?echo $_SERVER[`SERVER_NAME`];?><td>Server IP:<?echo gethostbyname($_SERVER[`SERVER_NAME`]);?>
</tr><tr></td><td>Server time:<?echo date(“Y年m月d日 h:i:s”,time());?></td><td>Server port :<?echo $_SERVER[`SERVER_PORT`];
  ?>
  </td></tr>
</table>
<hr>
當前目錄:
<?php
$dir=$_GET[`dir`];
if (!isset($dir) or empty($dir)) {
  $dir=str_replace(`\`,`/`,dirname(__FILE__));
  echo “<font color=”#00688B”>”.$dir.”</font>”;
} else {
 
  echo “<font color=”#00688B”>”.$dir.”</font>”;
}
?><br>
動作:
<a href=”?#”>[Refresh]</a>
<a href=”?nagiosaction=test”> [Test]</a>
<a href=”?nagiosaction=restart”>[Restart]</a>
<hr>
<table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″>
          <tr>
            <td><b>Filename</b></td>
            <td><b>Filedate</b></td>
            <td><b>Filesize</b></td>
            <td><b>Fileoperates</b></td>
          </tr>

<?php
//列印檔案
$dirs=@opendir($dir);
while ($file=@readdir($dirs)) {
  $b=”$dir/$file”;
  $a=@is_dir($b);
  if($a==”0″){
    $aa=explode(“.”,$file);
    if ($aa[1] == cfg ){
      $size=@filesize(“$dir/$file”)/1024;
      $lastsave=@date(“Y-n-d H:i:s”,filectime(“$dir/$file”));
      echo “<tr>
“;

      echo “<td>$file</td>
“;

      echo ”  <td>$lastsave</td>
“;

      echo ”  <td>$size KB</td>
“;

      echo ”  <td><a href=”?editfile=”.urlencode($dir).”/”.urlencode($file).””>[ Edit ] </a></td>
“;

      echo “</tr>
“;

    }
  }
}
@closedir($dirs);
?></table>
<hr>
<table align=”center”><tr><td>
<h6>Copyright (C) 2009 All Rights Reserved
</td></tr></table>

附上一款備份nagios配置檔案的指令碼

#!/usr/bin/env python
“`Backup config files  of nagios“`

import glob
import os 
import time
import shutil

nagios_path=”config file of nagios”
bak_path=”/backup/nagios/”
DATEFORMAT=”%Y%m%d”

class nagios(object):
        pass

def copy():
        cp_nagios = nagios()

        os.chdir(nagios_path) 
        filelist=glob.glob(`*.cfg`)
        datedir=time.strftime(DATEFORMAT,time.localtime())
        dirname=bak_path+datedir
        if not os.path.exists(dirname):
                os.mkdir(dirname)
                for file in filelist:
                        sfile=nagios_path+file
                        shutil.copy(sfile,dirname)

if __name__ == `__main__`:

        copy()



本文轉自hahazhu0634 51CTO部落格,原文連結:http://blog.51cto.com/5ydycm/192391,如需轉載請自行聯絡原作者


相關文章