2016-12-06 48 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列表」是什麼意思? –