diff --git a/html/api_v1.php b/html/api_v1.php
index 18e8cf283..99972ef81 100644
--- a/html/api_v1.php
+++ b/html/api_v1.php
@@ -51,6 +51,9 @@ $app->group('/api', function() use ($app) {
$app->group('/list', function() use ($app) {
$app->get('/devices(/:order)(/:type)(/)', 'authToken', 'list_devices');//api/v1/list/devices (order can be any device column) (types = all, ignored, up, down, disabled)
});
+ $app->group('/add', function() use ($app) {
+ $app->post('/device(/)', 'authToken', 'add_device');//api/v1/add/device (json data needs to be passed)
+ });
});
});
diff --git a/includes/api_functions.inc.php b/includes/api_functions.inc.php
index 7b9e0e9bf..2b0549f32 100644
--- a/includes/api_functions.inc.php
+++ b/includes/api_functions.inc.php
@@ -233,3 +233,68 @@ function list_devices()
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode($output);
}
+
+function add_device()
+{
+ // This will add a device using the data passed encoded with json
+ global $config;
+ $app = \Slim\Slim::getInstance();
+ $data = json_decode(file_get_contents('php://input'), true);
+ // Default status to error and change it if we need to.
+ $status = "error";
+ if(empty($data))
+ {
+ $message = "No information has been provided to add this new device";
+ }
+ elseif(empty($data["hostname"]))
+ {
+ $message = "Missing the device hostname";
+ }
+ $hostname = $data['hostname'];
+ if ($data['port']) { $port = mres($data['port']); } else { $port = $config['snmp']['port']; }
+ if ($data['transport']) { $transport = mres($data['transport']); } else { $transport = "udp"; }
+ if($data['version'] == "v1" || $data['version'] == "v2c")
+ {
+ if ($data['community'])
+ {
+ $config['snmp']['community'] = array($data['community']);
+ }
+ $snmpver = mres($data['version']);
+ }
+ elseif($data['version'] == 'v3')
+ {
+ $v3 = array (
+ 'authlevel' => mres($data['authlevel']),
+ 'authname' => mres($data['authname']),
+ 'authpass' => mres($data['authpass']),
+ 'authalgo' => mres($data['authalgo']),
+ 'cryptopass' => mres($data['cryptopass']),
+ 'cryptoalgo' => mres($data['cryptoalgo']),
+ );
+
+ array_push($config['snmp']['v3'], $v3);
+ $snmpver = "v3";
+ }
+ else
+ {
+ $message = "You haven't specified an SNMP version to use";
+ }
+ if(empty($message))
+ {
+ require_once("functions.php");
+ $result = addHost($hostname, $snmpver, $port, $transport, 1);
+ if($result)
+ {
+ $status = 'ok';
+ $message = 'Device has been added successfully';
+ }
+ else
+ {
+ $messge = "Failed adding $hostname";
+ }
+ }
+
+ $output = array("status" => $status, "message" => $message);
+ $app->response->headers->set('Content-Type', 'application/json');
+ echo json_encode($output);
+}
diff --git a/includes/functions.php b/includes/functions.php
index 38ecec15b..776cf308a 100755
--- a/includes/functions.php
+++ b/includes/functions.php
@@ -252,7 +252,7 @@ function delete_device($id)
return $ret;
}
-function addHost($host, $snmpver, $port = '161', $transport = 'udp')
+function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0')
{
global $config;
@@ -297,7 +297,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp')
foreach ($config['snmp']['v3'] as $v3)
{
$device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3);
- print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
+ if($quiet == '0') { print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... "); }
if (isSNMPable($device))
{
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
@@ -306,10 +306,10 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp')
$device_id = createHost ($host, NULL, $snmpver, $port, $transport, $v3);
return $device_id;
} else {
- print_error("Given hostname does not match SNMP-read hostname ($snmphost)!");
+ if($quiet == '0') {print_error("Given hostname does not match SNMP-read hostname ($snmphost)!"); }
}
} else {
- print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using $snmpver");
+ if($quiet == '0') {print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using $snmpver"); }
}
}
}
@@ -319,7 +319,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp')
foreach ($config['snmp']['community'] as $community)
{
$device = deviceArray($host, $community, $snmpver, $port, $transport, NULL);
- print_message("Trying community $community ...");
+ if($quiet == '0') { print_message("Trying community $community ..."); }
if (isSNMPable($device))
{
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
@@ -328,32 +328,34 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp')
$device_id = createHost ($host, $community, $snmpver, $port, $transport);
return $device_id;
} else {
- print_error("Given hostname does not match SNMP-read hostname ($snmphost)!");
+ if($quiet == '0') { print_error("Given hostname does not match SNMP-read hostname ($snmphost)!"); }
}
} else {
- print_error("No reply on community $community using $snmpver");
+ if($quiet == '0') { print_error("No reply on community $community using $snmpver"); }
}
}
}
else
{
- print_error("Unsupported SNMP Version \"$snmpver\".");
+ if($quiet == '0') { print_error("Unsupported SNMP Version \"$snmpver\"."); }
}
if (!$device_id)
{
// Failed SNMP
- print_error("Could not reach $host with given SNMP community using $snmpver");
+ if($quiet == '0') { print_error("Could not reach $host with given SNMP community using $snmpver"); }
}
} else {
// failed Reachability
- print_error("Could not ping $host"); }
+ if($quiet == '0') { print_error("Could not ping $host"); }
+ }
} else {
// Failed DNS lookup
- print_error("$host looks like an IP address, please use FQDN"); }
+ if($quiet == '0') { print_error("$host looks like an IP address, please use FQDN"); }
+ }
} else {
// found in database
- print_error("Already got host $host");
+ if($quiet == '0') { print_error("Already got host $host"); }
}
return 0;