使用Nagios将SNMP信息转储到文件中

时间:2016-12-06 11:00:54

标签: networking monitoring snmp nagios

是否有任何Nagios插件可以捕获来自多个SNMP代理的信息,并会在特定时间间隔内转储snmpwalk信息。 如果信息将以JSON格式转储,那将会更好。

1 个答案:

答案 0 :(得分: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;
}

现在,您可以将其放在名为check_multi_snmpwalk.php的$ USER1 $目录(/ usr / local / nagios / libexec)中的文件中,并确保它的可执行文件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$
}

现在,您应该能够在ARG1中指定要将JSON输出到的文件,然后每个其他参数需要包含主机地址,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
        }

现在你要说&#34;好的,这一切都很棒,但它做了什么?!&#34;

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

  • 从用户那里获取一些信息(我们瞎扯什么?)
  • 为指定的每个主机执行snmpwalk(并保存输出)
  • 将snmpwalk输出转换为易于读取的数组
  • 将每个主机的snmpwalk易于阅读的阵列聚合成一个巨大的阵列
  • 将巨型数组转换为JSON
  • 将JSON写入指定的文件
  • 返回Nagios的OK状态,并显示一条消息,说明我们处理了多少个主机!

一些注意事项:

  • 无论您指定多少个主机,此插件都需要一段时间才能运行,因此您可能需要考虑从cron作业而不是Nagios检查中运行它
  • 此插件不符合我之前链接的插件指南,但它仍然是一个有趣的小项目

希望这有帮助!

相关问题