Nagios php调用bash shell获取服务器状态

 


<?php

require_once(dirname(__FILE__) . '/../componenthelper.inc.php');

nagiosxicustomendpoints_component_init();
function nagiosxicustomendpoints_component_init()
{

    $args = array(
        COMPONENT_NAME =>           "nagiosxicustomendpoints",
        COMPONENT_VERSION =>        "1.0",
        COMPONENT_AUTHOR =>         "Nagios Enterprises, LLC",
        COMPONENT_DESCRIPTION =>    "Demonstrate Nagios XI Custom API Endpoints",
        COMPONENT_TITLE =>          "Nagios XI Custom API Endpoints Example"
        );

    register_component("nagiosxicustomendpoints", $args);

    register_custom_api_callback('com', 'mangosis', 'nagiosxicustomendpoints_awesome_example');

}

function nagiosxicustomendpoints_awesome_example($endpoint, $verb,$args) {

    $xml = '';
    foreach ($args as $key => $arg) {

        $cpu = shell_exec("/usr/local/nagios/libexec/check_nrpe -H ".$arg."  -t 30 -c check_cpu_stats -a '-w 85 -c 95'");
        $ram = shell_exec("/usr/local/nagios/libexec/check_nrpe -H ".$arg." -t 30 -c check_mem -a '-w 20 -c 10'");
        $disk = shell_exec("/usr/local/nagios/libexec/check_nrpe -H ".$arg." -t 30 -c check_disk -a '-w 5 -c 10'");
    }

    $xml = "    <xml>
                    <cpu>$cpu</cpu>
                    <ram>$ram</ram>
                    <disk>$disk</disk>
                    <endpoint>$endpoint</endpoint>
                    <verb>$verb</verb>
                    <arg>$arg</arg>
                </xml>";

    return xml2json::transformXmlStringToJson($xml);

}

?>

附上个人脚本

#!/bin/bash
#
DEV=`df -hP | grep '^/dev/*' | cut -d' ' -f1 | sort`
for I in $DEV
do dev=`df -Ph | grep $I | awk '{print $1}'`
size=`df -Ph | grep $I | awk '{print $2}'`
used=`df -Ph | grep $I | awk '{print $3}'`
free=`df -Ph | grep $I | awk '{print $4}'`
rate=`df -Ph | grep $I | awk '{print $5}'`

echo -e "$I:\tsize:$size\tused:$used\tfree:$free\trate:$rate"
F=`echo $rate | awk -F% '{print $1}'`

done
#!/bin/bash
#

CPU1=`cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`
sleep 1  
CPU2=`cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}'`
IDLE1=`echo $CPU1 | awk '{print $4}'`
IDLE2=`echo $CPU2 | awk '{print $4}'`
CPU1_TOTAL=`echo $CPU1 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
CPU2_TOTAL=`echo $CPU2 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
IDLE=`echo "$IDLE2-$IDLE1" | bc`
CPU_TOTAL=`echo "$CPU2_TOTAL-$CPU1_TOTAL" | bc`
RATE=`echo "scale=4;($CPU_TOTAL-$IDLE)/$CPU_TOTAL*100" | bc | awk '{printf "%.2f",$1}'`

echo "${RATE}%"
#!/bin/bash
#

total=$(free -m | sed -n '2p' | awk '{print $2}')
used=$(free -m | sed -n '2p' | awk '{print $3}')
free=$(free -m | sed -n '2p' | awk '{print $4}')
shared=$(free -m | sed -n '2p' | awk '{print $5}')
buff=$(free -m | sed -n '2p' | awk '{print $6}')
cached=$(free -m | sed -n '2p' | awk '{print $7}')

rate=`echo "scale=2;$used/$total" | bc | awk -F. '{print $2}'`

echo -e "${rate}%

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值