Skip to content

Commit

Permalink
Move the plugin to use LibreNMS stored SNMP Community and adapted tes…
Browse files Browse the repository at this point in the history
…ts on errors
  • Loading branch information
PipoCanaja committed May 11, 2018
1 parent b6d3fda commit 0c3734a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
6 changes: 1 addition & 5 deletions InterfaceAdminPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
* Code displayed when loading the plugin
*/

if ($config['InterfaceAdminPlugin_snmp_RW'] != '') {
echo ('The InterfaceAdminPlugin is up and running, and RW community is defined in the config.php');
} else {
echo ('In order to use this Plugin, please define your device SNMP Write community in the config.php file, using the following code : </br> $config["InterfaceAdminPlugin_snmp_RW"] = "MyLittleRWCommunity";');
}
echo ('The InterfaceAdminPlugin is up and running');

?>
4 changes: 2 additions & 2 deletions forms/enable-port.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* Let's do the SNMP Set to the device
*
*/
$res = snmp_set($device[hostname], ".1.3.6.1.2.1.2.2.1.7.$port[ifIndex]", 'i', 1);
$res = snmp_set($device, ".1.3.6.1.2.1.2.2.1.7.$port[ifIndex]", 'i', 1);

/*
* Return the results of the call
Expand All @@ -72,7 +72,7 @@
if ($res == false) {
$message = 'Could not enable '.$port['ifName'].' port';
} elseif ($res == 'community') {
$message = 'Community for WR SNMP not provided in config.php';
$message = 'Community in LibreNMS is probably ReadOnly. No answer from device with current community';
$status = 'error';

} elseif ($res == 'multiple-oid') {
Expand Down
4 changes: 2 additions & 2 deletions forms/shutdown-port.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* Let's do the SNMP Set to the device
*
*/
$res = snmp_set($device[hostname], ".1.3.6.1.2.1.2.2.1.7.$port[ifIndex]", 'i', 2);
$res = snmp_set($device, ".1.3.6.1.2.1.2.2.1.7.$port[ifIndex]", 'i', 2);


/*
Expand All @@ -72,7 +72,7 @@
if ($res == false) {
$message = 'Could not disable '.$port['ifName'].' port';
} elseif ($res == 'community') {
$message = 'Community for WR SNMP not provided in config.php';
$message = 'Community in LibreNMS is probably ReadOnly. No answer from device with current community';
$status = 'error';

} elseif ($res == 'multiple-oid') {
Expand Down
28 changes: 15 additions & 13 deletions include/snmp.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
* the source code distribution for details.
*/


include "../../../includes/snmp.inc.php";

/* function snmp_set($device, $oid, $type, $value, $options = null, $mib = null, $mibdir = null)
*
* Execute SNMP Set on device, using $config['InterfaceAdminPlugin_snmp_RW']
* Execute SNMP Set on device, using default LibreNMS community settings for current device
*/

function snmp_set($device, $oid, $type, $value, $options = null)
Expand All @@ -27,26 +30,25 @@ function snmp_set($device, $oid, $type, $value, $options = null)
return 'multiple-oid';
}

if (!isset($config['InterfaceAdminPlugin_snmp_RW']) || $config['InterfaceAdminPlugin_snmp_RW'] == '') {
//echo ('No SNMP Write Community provided in config file ( $config["InterfaceAdminPlugin_snmp_RW"])');
return 'community';
}

$community = $config['InterfaceAdminPlugin_snmp_RW'];
$snmpAuth = snmp_gen_auth($device);
$value=addslashes($value);
$cmd= "snmpset -v2c $options -c $community $device $oid $type '".$value."'";
$cmd= "snmpset $snmpAuth $options ".' '.$device[transport].':'.$device[hostname].':'.$device[port]." $oid $type '".$value."'";
$data = trim(external_exec($cmd), "\" \n\r");
$res = $data;

recordSnmpStatistic('snmpset', $time_start);
if (preg_match('/(No Such Instance|No Such Object|No more variables left|Authentication failure)/i', $data)) {
return false;
} elseif ($data || $data === '0') {
return $res;
} else {
return false;
} elseif (preg_match('/(No Access)/i', $data)) {
return 'community';
} elseif ($data || $data === '0') {
return $data;
} elseif ($data =='') {
return 'community';
} else {
return false; //$cmd." :". $data.":";
}
return false;
return $data;
}//end snmp_set()

?>

0 comments on commit 0c3734a

Please sign in to comment.