Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. // check if we have at least the minimum required output
  5. // (we need at least 1 argument)
  6. if (count($argv) < 2) {
  7.  
  8. echo <<<USAGE
  9. Usage:
  10. {$argv[0]} <outputfile> <address1>,<snmpcommunity1>,<snmpversion1>,<mib1> <address2>,<snmpcommunity2>,<snmpversion2>,<mib2> ...
  11.  
  12. USAGE;
  13. exit(1);
  14. }
  15.  
  16. // prep the data
  17. $hosts = array();
  18. $output = array();
  19. $output_file = '';
  20. for ($i = 1; $i < count($argv); $i++) {
  21.  
  22. $host = explode(",", $argv[$i]);
  23.  
  24. // we need exactly 4 elements
  25. if (count($host) != 4) {
  26.  
  27. // unless of course we are specifying the output file to write the data to!
  28. if (count($host) == 1) {
  29. $output_file = $argv[$i];
  30. continue;
  31. }
  32.  
  33. echo "{$argv[$i]} IS INVALID. YOU MUST SPECIFY ALL OF: <address>,<snmpcommunity>,<snmpversion>,<mib>n";
  34. exit(1);
  35. }
  36.  
  37. $hosts[] = array(
  38. 'address' => $host[0],
  39. 'snmp_community' => $host[1],
  40. 'snmp_version' => $host[2],
  41. 'mib' => $host[3],
  42. );
  43. }
  44.  
  45. // cycle through each host and gather the data
  46. // this may take a while
  47. foreach($hosts as $host) {
  48.  
  49. $snmpwalk_array = get_snmpwalk_lines($host['address'], $host['snmp_community'], $host['snmp_version'], $host['mib']);
  50. $snmp_array = walk_lines_to_snmp_array($snmpwalk_array);
  51.  
  52. $output[$host['address']] = $snmp_array;
  53. }
  54.  
  55. // convert the output array to json and put it in the file!
  56. $json = json_encode($output);
  57. file_put_contents($output_file, $json);
  58.  
  59. $num_hosts = count($hosts);
  60. echo "OK - {$num_hosts} PROCESSEDn";
  61. exit(0);
  62.  
  63. // format an array in a sane way from snmp walk output
  64. // this will return an array like:
  65. // [oid][type] = 'Counter32'
  66. // [oid][value] = 0011232
  67. // etc.
  68. function walk_lines_to_snmp_array($walk_arr) {
  69.  
  70. $snmp = array();
  71.  
  72. foreach ($walk_arr as $line) {
  73. $oid = convert_snmpwalk_line_to_array($line, $arr);
  74. if ($oid !== false)
  75. $snmp[$oid] = $arr;
  76. }
  77.  
  78. return $snmp;
  79. }
  80.  
  81. // return an array of an executed snmpwalk output
  82. function get_snmpwalk_lines($address, $snmp_community, $snmp_version, $mib) {
  83.  
  84. $cmd = "snmpwalk -c {$snmp_community} -v {$snmp_version} {$address} -m {$mib}";
  85. exec($cmd, $output);
  86.  
  87. return $output;
  88. }
  89.  
  90. // return the oid and pass the array by ref
  91. // or return false on failure
  92. function convert_snmpwalk_line_to_array($line, &$arr) {
  93.  
  94. if (preg_match('/(.*) = (.*): (.*)/', $line, $matches) === 1) {
  95. $arr = array(
  96. 'type' => $matches[2],
  97. 'value' => $matches[3],
  98. );
  99.  
  100. return $matches[1];
  101. }
  102.  
  103. return false;
  104. }
  105.  
  106. define command {
  107. command_name check_multi_snmpwalk
  108. command_line $USER1$/check_multi_snmpwalk.php $ARG1$ $ARG2$ $ARG3$ $ARG4$
  109. }
  110.  
  111. define service {
  112. host_name localhost
  113. service_description Multi SNMP Walk
  114. use local-service
  115. check_command check_multi_snmpwalk!/tmp/jsonfile!192.168.1.10,community,1,all!192.168.1.11,community,2c,all!!
  116. register 1
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement