2013年4月2日星期二

Munin is an excellent monitor program,How to use it deeply?

Basic use:

Client
Install:

yum install munin-common
chkconfig --level 234 munin-node on
service munin-node restart

If you have trouble with "yum install munin-node", may be you should add some repository to your yum.


[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://42.96.166.148/dag/redhat/el$releasever/en/$basearch/dag
gpgcheck=0
gpgkey=http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
enabled=1

[epel]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://42.96.166.148/epel/$releasever/$basearch/
gpgcheck=0
enabled=1

Maybe you should add some perl packages:
cpanm Module::Build Time::HiRes Net::SSLeay Net::CIDR File::readBackwards

If you want to change the hostname and ip allow, do the following:
Edit: /etc/munin/munin-node.conf 
hostname XXX

allow ^127\.0\.0\.1$
cidr_allow 192.168.0.0/24

Maybe you should also disable the selinux:
getenforce
setenforce 0

Server:
yum install munin munin-common


Following are some user defined or some other monitors?

About bind9
Step :
edit bind9 config file:

logging {
        channel b_query {
                file "/var/log/bind9/query.log" versions 2 size 1m;
                print-time yes;
                severity info;
        };
        category queries { b_query; };
};


mkdir /var/log/bind9
chown bind:bind /var/log/bind9
/etc/init.d/bind9 restart

ln -s /usr/share/munin/plugins/bind9 /etc/munin/plugins/bind9

File: /etc/munin/plugin-conf.d/munin-node

[bin9]
env.logfile   /var/named/chroot/var/named/logs/query.log
user root




About NetworkUsedToday


cd /etc/munin/plugins/
wget -O NetworkUsedToday http://www.myciscolab.net/sources/NetworkUsedToday.txt
chmod a+x NetworkUsedToday

mkdir -p /etc/munin/plugins/monitor/NetworkUsedToday
cd /etc/munin/plugins/monitor/NetworkUsedToday
wget http://www.myciscolab.net/sources/NetworkUsedToday.tar.gz
tar xzvf NetworkUsedToday.tar.gz
rm -rvf NetworkUsedToday.tar.gz

echo "
[NetworkUsedToday]
user root
" >> /etc/munin/plugin-conf.d/munin-node

service munin-node restart



检测:
/etc/munin/plugins/monitor/NetworkUsedToday/0100.NetworkUsedToday.sh
/bin/bash /etc/munin/plugins/monitor/NetworkUsedToday/5523.NetworkUsedToday.sh ; /bin/bash /etc/munin/plugins/monitor/NetworkUsedToday/NetworkUsedTodayToFile.sh
# 看到8个竖排的0表示正常
/etc/munin/plugins/monitor/NetworkUsedToday/NetworkUsedToday.sh



# Added For Munin Monitor NetworkUsedToday
01 00 * * * /etc/munin/plugins/monitor/NetworkUsedToday/0100.NetworkUsedToday.sh
*/1 * * * * /bin/bash /etc/munin/plugins/monitor/NetworkUsedToday/5523.NetworkUsedToday.sh ; /bin/bash /etc/munin/plugins/monitor/NetworkUsedToday/NetworkUsedTodayToFile.sh


About DiskUsedToday

# 下载脚本
cd /etc/munin/plugins
wget http://www.myciscolab.net/sources/Munin_DiskUsedToday.tar.gz
tar xzvf Munin_DiskUsedToday.tar.gz

# Added For Munin Monitor DiskUsedToday
00 01 * * * /etc/munin/plugins/monitor/DiskUsedToday/DiskUsed.sh > /etc/munin/plugins/monitor/DiskUsedToday/0100.txt
*/1 * * * * /etc/munin/plugins/monitor/DiskUsedToday/DiskUsed.sh > /etc/munin/plugins/monitor/DiskUsedToday/5523.txt && /etc/munin/plugins/monitor/DiskUsedToday/DiskUsedToday.sh > /etc/munin/plugins/monitor/DiskUsedToday/DiskUsedToday.txt

# 执行命令
/bin/bash /etc/munin/plugins/monitor/DiskUsedToday/DiskUsed.sh > /etc/munin/plugins/monitor/DiskUsedToday/0100.txt
/bin/bash /etc/munin/plugins/monitor/DiskUsedToday/DiskUsed.sh > /etc/munin/plugins/monitor/DiskUsedToday/5523.txt && /bin/bash /etc/munin/plugins/monitor/DiskUsedToday/DiskUsedToday.sh > /etc/munin/plugins/monitor/DiskUsedToday/DiskUsedToday.txt

# 编辑文件 /etc/munin/plugin-conf.d/munin-node
echo '
[DiskUsedToday]
user root
' >> /etc/munin/plugin-conf.d/munin-node

# 重启服务
service munin-node restart

# 测试
telnet localhost 4949
list
fetch DiskUsedToday

# 状态监测
9:28

317765680
314755924
3 009 756
找到原因了, df 显示的单位是KB

/etc/munin/plugins/monitor/DiskUsedToday/DiskUsedToday.sh

About NetworkUsedToday2

/etc/munin/plugins/monitor/NetworkUsedToday2/getNICRTInfo.php
#!/usr/bin/php
<?php
// 调用该文件, 将生成 当前 网卡信息到 指定文件, 指定文件需要 通过参数传递
// start or inter
// start 是每天凌晨00:01 的 时候 产生
// inter 是每 4分钟运行一次的结果
// start 保存为 /etc/munin/plugins/monitor/NetworkUsedToday2/ethx.rx.start
// inter 保存为 /etc/munin/plugins/monitor/NetworkUsedToday2/ethx.rx.inter

function usage(){
    echo "***************************************************************************************************\n";
    echo "getNICRTInfo.php [start|inter] \n";
    echo "***************************************************************************************************\n";

}

if ( count($argv) != 2 ) { echo "Parameter number is not right, please give me just a value for this action type for start or inter.\n"; usage(); exit;}

print_r($argv);
if ( $argv[1] === "start" || $argv[1] === "inter") 
  {}
   else
       { echo "You just give me a wrong argument that not \"start\" or \"inter \",check your command , guy.\n" ; usage(); exit;}
// 全局变量
$path = "/etc/munin/plugins/monitor/NetworkUsedToday2/";
$valueType = $argv[1];

// 获得 系统里面一共有几张网卡
function getNICNames() {
    $nicNames = array();
    if ( system("ifconfig eth0 | grep Ethernet | wc -l") ) { echo "Eth0 存在" . "\n"; array_push($nicNames,"eth0"); }
    if ( system("ifconfig eth1 | grep Ethernet | wc -l") ) { echo "Eth1" . "\n"; array_push($nicNames,"eth1"); }
    if ( system("ifconfig eth2 | grep Ethernet | wc -l") ) { echo "Eth2 存在" . "\n"; array_push($nicNames,"eth2"); }
    if ( system("ifconfig eth3 | grep Ethernet | wc -l") ) { echo "Eth3 存在" . "\n"; array_push($nicNames,"eth3"); }
    return $nicNames;

}
$nicNames = getNICNames();

// 传进来网卡名字,然后 吐出 该网卡的 in  和 out 流量
function getNicIncomingFlow($eth,$path,$valueType){
    $flow = system("ifconfig $eth | grep \"RX bytes\""); 
    $flow = trim($flow); 
    $flowArray = explode(" ",$flow);
    // 1 和 6 分别是 RX 和 TX 但是 里面有:
    // 用下面的两行 来分割 : , 取出 rx  和 tx 的value
    $rxArray = explode(":",$flowArray[1]);
    $txArray = explode(":",$flowArray[6]);
    // 下面的两行取出rx 和 tx
    $rx = $rxArray[1];
    $tx = $txArray[1];
    //echo $eth . " " . $rx . " " . $tx . "\n";
    
    // 将数据写到 相应的文件中
   
    $filepath_rx = $path . $eth . "." . "rx." . $valueType;
    $filepath_tx = $path . $eth . "." . "Tx." . $valueType;
    // echo $filepath_rx . "\n";
    // echo $filepath_tx . "\n";
    // php 一个特性真好, file_put_contents 会完完全全覆盖掉文件之前的内容
    file_put_contents($filepath_rx,$rx);
    file_put_contents($filepath_tx,$tx);

}


// 最后的效用
foreach ($nicNames as $eth) {
    getNicIncomingFlow($eth,$path,$valueType);
}
?>

#Crontab
# NetworkUsedToday2 for munin
02 0 * * * /usr/bin/php /etc/munin/plugins/monitor/NetworkUsedToday2/getNICRTInfo.php start
*/3 * * * * /usr/bin/php /etc/munin/plugins/monitor/NetworkUsedToday2/getNICRTInfo.php inter

/etc/munin/plugins/NetworkUsedToday2
#!/usr/bin/php
<?php
// 获得 系统里面一共有几张网卡
function getNICNames() {
    $nicNames = array();
    if ( system("ifconfig eth0 | grep Ethernet | wc -l") ) { array_push($nicNames,"eth0"); }
    if ( system("ifconfig eth1 | grep Ethernet | wc -l") ) { array_push($nicNames,"eth1"); }
    if ( system("ifconfig eth2 | grep Ethernet | wc -l") ) { array_push($nicNames,"eth2"); }
    if ( system("ifconfig eth3 | grep Ethernet | wc -l") ) { array_push($nicNames,"eth3"); }
    return $nicNames;
}
$nicNames = getNICNames();

function getEthFlow($eth) {
    $path="/etc/munin/plugins/monitor/NetworkUsedToday2/";
    $filepath_rx_start = $path . $eth . "." . "rx.start"; $rx_start=file_get_contents($filepath_rx_start);
    $filepath_tx_start = $path . $eth . "." . "Tx.start"; $tx_start=file_get_contents($filepath_tx_start);
    $filepath_rx_inter = $path . $eth . "." . "rx.inter"; $rx_inter=file_get_contents($filepath_rx_inter);
    $filepath_tx_inter = $path . $eth . "." . "Tx.inter"; $tx_inter=file_get_contents($filepath_tx_inter);

    $rx = $rx_inter - $rx_start;
    $tx = $tx_inter - $tx_start;

    echo $eth . "Rx.label " . $rx . "\n";
    echo $eth . "Tx.label " . $tx . "\n";
}

foreach ($nicNames as $eth) {
    getEthFlow($eth);
}

if (count($argv)==1) {exit;}

if ($argv[1]=="autoconf"){
    if ("1") {
        echo "yes";
exit;
    }
    else {
        echo "no";
        exit;
    }
}

if ($argv[1]=="config") {
    echo "graph_title NetworkUsedToday2" . "\n";
    echo "graph_args --base 1000 -l 0" . "\n";
    echo "graph_vlabel number of open inodes" . "\n";
    echo "graph_category others" . "\n";
    echo "graph_info This graph monitors the linux networkusedtoday" . "\n";
    foreach($nicNames as $eth) {
        echo $eth . "Rx.label " . $eth . "RX" . "\n";
        echo $eth . "Tx.label " . $eth . "TX" . "\n";
    }
    exit;
}


?>



InodeUsed
/etc/munin/plugins/InodeUsed

/etc/munin/plugins/InodeUsed

#!/usr/bin/php
<?php
//. $MUNIN_LIBDIR/plugins/plugin.sh

//echo count($argv);
if (count($argv)>1) {

if ($argv[1]=="autoconf"){
    if (file_exists("/tmp/tmp.df1234")) {
        echo yes;
exit;
    }
    else {
        echo no;
        exit;
    }
}

if ($argv[1]=="config") {
    echo "graph_title InodeUsed" . "\n";
    echo "graph_args --base 1000 -l 0" . "\n";
    echo "graph_vlabel number of open inodes" . "\n";
    echo "graph_category others" . "\n";
    echo "graph_info This graph monitors the linux inodeused" . "\n";

    $file_handle = fopen("/tmp/tmp.df1234","r");
    while (!feof($file_handle)){
        $line = fgets($file_handle);
        if ($line=="") {exit;}
        $value=explode(" ",$line);

        echo "$value[0]-used.label $value[0]-used" . "\n";
echo "$value[0]-used.info The number $value[0]-used" . "\n";

        //print_warning $value[0]-used;
//print_critical $value[0]-used;

        echo "$value[0]-max.label $value[0]-max" . "\n";
echo "$value[0]-max.info The number $value[0]-max" . "\n";

        //print_warning $value[0]-max;
//print_critical $value[0]-max;
    }
    fclose($file_handle);
    exit;
}
}

// 如果$argv[1] 没有参数,那么就执行下面的, 把变量和值都打印出来
    $file_handle = fopen("/tmp/tmp.df1234","r");
    while (!feof($file_handle)){
        $line = fgets($file_handle);
        if ($line=="") {exit;}
        $value=explode(" ",$line);

        echo "$value[0]-used.value $value[2]" . "\n";

        echo "$value[0]-max.value $value[1]" . "\n";
    }
    fclose($file_handle);

?>

/etc/munin/plugins/monitor/InodeUsed/InodeUsedCron.php
#!/usr/bin/php
<?php
$srcArray=$argv;

//echo count($srcArray);

$dstArray = array();
for($srcArrayNum=0,$dstArrayNum=0;$srcArrayNum<count($srcArray);$srcArrayNum++){
    if ($srcArrayNum<8){
    }
    else {
        $dstArray[$dstArrayNum]=$srcArray[$srcArrayNum];
        $dstArrayNum++;
    }
}

//print_r($srcArray);
//print_r($dstArray);

$mountPointNum = count($dstArray)/6;
//echo $mountPointNum;

for($num=0;$num<count($dstArray);$num++){
    echo $dstArray[$num] . " ";
    if (fmod($num+1,6)==0){
        echo "\n";
    }
}
?>


Crontab
# Added For Munin Monitor InodeUsed
*/1 * * * * /usr/bin/php /etc/munin/plugins/monitor/InodeUsed/InodeUsedCron.php `df -i` > /tmp/tmp.df1234

/etc/munin/plugin-conf.d/munin-node
[InodeUSed]
user root

测试
service munin-node restart
telnet localhost 4949
list
fetch InodeUSed

[root@AY130322174434205277 plugin-conf.d]# telnet localhost 4949
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
# munin node at localhost
list
DiskUsedToday InodeUsed NetworkUsedToday cpu df df_inode entropy forks fw_packets if_err_eth0 if_err_eth1 if_eth0 if_eth1 interrupts irqstats load memory netstat ntp_kernel_err ntp_kernel_pll_freq ntp_kernel_pll_off ntp_offset open_files open_inodes postfix_mailqueue postfix_mailvolume proc_pri processes swap threads uptime users vmstat
fetch InodeUsed
/dev/xvda1-used.value 28624
/dev/xvda1-max.value 1310720
tmpfs-used.value 1
tmpfs-max.value 127370
/dev/mapper/opensource-data-used.value 1044800
/dev/mapper/opensource-data-max.value 32768000

格式化输出df

[root@AY130322174434205277 DiskUsedToday]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/xvda1            20641404   1071944  18520936   6% /
tmpfs                   509480         0    509480   0% /dev/shm
/dev/mapper/opensource-data
                     516057528 430917088  58926248  88% /opensource
root@10.129.34.118:/opensource
                     2047854435 1284724987 763129449  63% /a

<?php

$fp=fopen("df","r");

while(!feof($fp)) {

    $text=fgets($fp);

    $text=trim($text);

    // 判断连续两个空格的数量,知道 没有两个连续空格
    while ( substr_count($text,"  ")!=0 ) {

        $text=str_replace("  "," ",$text);

    }

    // 这个是为了防止最后一行是空行
    if (strlen($text)==0 ) {

    }
    // 如果这一行没有空格, 那么就表示 df 在这个地方分行了, 我们不打印回车,取而代之的是一个空格
    elseif ( substr_count($text," ")==0 ) {
        echo $text . " ";
    }
    // 其余的情况,我们直接输出,并在末尾加上回车
    else {
        echo $text . "\n";
    }
}
?>

# php a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/xvda1 20641404 1071612 18521268 6% /
tmpfs 509480 0 509480 0% /dev/shm
/dev/mapper/opensource-data 516057528 429950096 59893240 88% /opensource
root@10.129.34.118:/opensource 2047854453 1283413254 764441200 63% /a

没有评论:

发表评论