开发nagios修改配置文件平台

为君一笑 提交于 2020-08-05 00:45:06

[背景]本文的宗旨,是为解决在后期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>\n";
      echo "<td>$file</td>\n";
      echo "  <td>$lastsave</td>\n";
      echo "  <td>$size KB</td>\n";
      echo "  <td><a href=\"?editfile=".urlencode($dir)."/".urlencode($file)."\">[ Edit ] </a></td>\n";
      echo "</tr>\n";
    }
  }
}
@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()







































































































































































如果想了解更多,请关注我们的公众号
公众号ID:opdevos
扫码关注

gongzhouhao.jpg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!