2016-12-06 49 views

回答

0

由于有充分的文件记录plugin development guidelines,这是你可以轻松建立自己的东西!

现在我们来构建一个。假设我们有2个IP地址,192.168.1.10和192.168.1.11。我们将使用PHP构建一个简单的插件,但理想情况下,您可以使用任何您喜欢的语言编写它。

这个插件不会完全符合指南,但它应该给你一个不错的起点!现在

#!/usr/bin/php 
<?php 

// check if we have at least the minimum required output 
// (we need at least 1 argument) 
if (count($argv) < 2) { 

    echo <<<USAGE 
Usage: 
{$argv[0]} <outputfile> <address1>,<snmpcommunity1>,<snmpversion1>,<mib1> <address2>,<snmpcommunity2>,<snmpversion2>,<mib2> ... 

USAGE; 
    exit(1); 
} 

// prep the data 
$hosts = array(); 
$output = array(); 
$output_file = ''; 
for ($i = 1; $i < count($argv); $i++) { 

    $host = explode(",", $argv[$i]); 

    // we need exactly 4 elements 
    if (count($host) != 4) { 

     // unless of course we are specifying the output file to write the data to! 
     if (count($host) == 1) { 
      $output_file = $argv[$i]; 
      continue; 
     } 

     echo "{$argv[$i]} IS INVALID. YOU MUST SPECIFY ALL OF: <address>,<snmpcommunity>,<snmpversion>,<mib>\n"; 
     exit(1); 
    } 

    $hosts[] = array(
     'address'  => $host[0], 
     'snmp_community' => $host[1], 
     'snmp_version' => $host[2], 
     'mib'   => $host[3], 
     ); 
} 

// cycle through each host and gather the data 
// this may take a while 
foreach($hosts as $host) { 

    $snmpwalk_array = get_snmpwalk_lines($host['address'], $host['snmp_community'], $host['snmp_version'], $host['mib']); 
    $snmp_array = walk_lines_to_snmp_array($snmpwalk_array); 

    $output[$host['address']] = $snmp_array; 
} 

// convert the output array to json and put it in the file! 
$json = json_encode($output); 
file_put_contents($output_file, $json); 

$num_hosts = count($hosts); 
echo "OK - {$num_hosts} PROCESSED\n"; 
exit(0); 

// format an array in a sane way from snmp walk output 
// this will return an array like: 
// [oid][type] = 'Counter32' 
// [oid][value] = 0011232 
// etc. 
function walk_lines_to_snmp_array($walk_arr) { 

    $snmp = array(); 

    foreach ($walk_arr as $line) { 
     $oid = convert_snmpwalk_line_to_array($line, $arr); 
     if ($oid !== false) 
      $snmp[$oid] = $arr; 
    } 

    return $snmp; 
} 

// return an array of an executed snmpwalk output 
function get_snmpwalk_lines($address, $snmp_community, $snmp_version, $mib) { 

    $cmd = "snmpwalk -c {$snmp_community} -v {$snmp_version} {$address} -m {$mib}"; 
    exec($cmd, $output); 

    return $output; 
} 

// return the oid and pass the array by ref 
// or return false on failure 
function convert_snmpwalk_line_to_array($line, &$arr) { 

    if (preg_match('/(.*) = (.*): (.*)/', $line, $matches) === 1) { 
     $arr = array(
      'type' => $matches[2], 
      'value' => $matches[3], 
      ); 

     return $matches[1]; 
    } 

    return false; 
} 

,你可以在你的$ USER1 $目录(在/ usr /本地/ nagios的/ libexec目录)命名check_multi_snmpwalk.php把这个文件,并确保它的可执行chmod +x /usr/local/nagios/libexec/check_multi_snmpwalk.php

最后,我们需要做的就是定义一个命令让Nagios来捡起它并使用它!像下面的内容就足够了:

define command { 
     command_name        check_multi_snmpwalk 
     command_line        $USER1$/check_multi_snmpwalk.php $ARG1$ $ARG2$ $ARG3$ $ARG4$ 
} 

现在你应该可以指定你想要的JSON要输出的文件中ARG1,然后对方参数必须包含主机地址,SNMP社区, snmp版本和你想要走的mib。

因此,举例来说:

define service { 
     host_name      localhost 
     service_description    Multi SNMP Walk 
     use        local-service 
     check_command     check_multi_snmpwalk!/tmp/jsonfile!192.168.1.10,community,1,all!192.168.1.11,community,2c,all!! 
     register      1 
     } 

现在你说:“好吧,那是所有伟大的,但它有什么作用?”

我很高兴你问了!这就是它的作用:

  • 拼抢从用户(什么是我们SNMP走?)一些输入
  • 执行的snmpwalk的(和保存输出)作为指定
  • 每个主机的snmpwalk的输出转换一个易于阅读的阵列
  • 汇总每个主机的运行snmpwalk易于阅读数组转换成一个巨大的阵
  • 巨型数组转换为JSON
  • 编写JSON到指定的文件
  • 通过消息说明我们处理了多少个主机,为Nagios返回OK状态!

的几个注意事项:

  • 这个插件将需要一段时间来,不管你有多少台主机指定运行,所以你可能要考虑从cron作业,而不是Nagios的运行它检查
  • 这个插件不符合我联系到前面的插件指引,但它仍然是一个有趣的小项目

希望这有助于!

+0

感谢@Nagios支持。真的很感谢你的回应。但是,由于少量可扩展性挑战,我将在C(多线程)中使用相同的概念。请建议如果C多线程将是一个很好的选择来监控太多接近10万个)设备。 –

+0

请建议C多线程来创建API列表将是一个很好的选择来监视太多(可能接近0.1万)的设备。这些API将获得CPU负载,内存使用,温度e.t.c。再次感谢。 –

+0

我不认为我明白你的问题,但如果你基本上问:“就性能和可伸缩性而言,以上代码的多线程C版本会更好吗?”那么答案是“可能是的”。 “创建API列表”是什么意思? –