開發nagios修改配置檔案平臺
[背景]本文的宗旨,是為解決在後期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__`:
[程式碼]
<?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,如需轉載請自行聯絡原作者
相關文章
- 做nagios分散式需要修改的配置檔案iOS分散式
- Nagios配置檔案詳解iOS
- Nagios之批量修改nrpe配置檔案並自動重啟iOS
- Git修改配置檔案Git
- phpMyadmin配置檔案修改PHP
- 修改Oracle的配置檔案Oracle
- 用程式修改配置檔案
- C#Winform快速開發平臺與軟體配置平臺C#ORM
- 開放式的檔案管理平臺
- Nagios伺服器端配置檔案詳解(2)iOS伺服器
- 綠茵面家平臺開發(開發平臺)
- NFT交易平臺定製開發|NFT交易平臺專案搭建
- LeaRun快速開發平臺,快速開發.net/java專案Java
- WebStrom 安裝教程_配置檔案修改Web
- 如何開發直播平臺,直播平臺開發需要CDN嗎
- transfer 全平臺Windows linux mac配置檔案轉換工具WindowsLinuxMac
- Cacti+Nagios監控平臺完美整合iOS
- python 修改jenkins的配置檔案PythonJenkins
- 透過命令列修改nacos配置檔案命令列
- 利用IDEA進行JNI開發:生成Windows平臺下的dll檔案IdeaWindows
- PbootCMS後臺檔案修改路徑位置boot
- Web快速開發平臺,基於二次開發平臺Web
- PHP開發laravel框架的.env檔案配置PHPLaravel框架
- 直播平臺開發經驗分享——直播平臺搭建環境配置詳解及分析
- 1.4檔案操作之修改程式配置檔案小應用
- JNPF快速開發平臺-簡單快速高效開發java專案Java
- 直播平臺開發,滾動條樣式的修改和更換
- 直播平臺軟體開發,簡單易修改的彈框元件元件
- 直播平臺開發,css溢位之後捲軸樣式修改CSS
- OA極速配置工具,LR智慧化快速開發平臺
- 1.PHP微信公眾平臺開發(一) 配置介面PHP
- java開發微信公眾平臺(一)-- 伺服器配置Java伺服器
- 直播平臺開發,載入網頁、html檔案顯示載入進度網頁HTML
- 力軟快速開發平臺:方便快捷的Web開發平臺Web
- Nagios+Cacti監控平臺完美整合部署iOS
- VUE打包後配置配置檔案修改請求url方法及webpack打包的檔案生成同名檔案方法VueWeb
- Java程式碼修改yml配置檔案屬性Java
- mysql my.ini配置檔案修改無效MySql