Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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} PROCESSEDn";
- 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;
- }
- define command {
- command_name check_multi_snmpwalk
- command_line $USER1$/check_multi_snmpwalk.php $ARG1$ $ARG2$ $ARG3$ $ARG4$
- }
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement