diff --git a/delhost.php b/delhost.php index 2c21d01f7..189881778 100755 --- a/delhost.php +++ b/delhost.php @@ -28,8 +28,7 @@ if ($argv[1]) $id = getidbyname($host); if ($id) { - echo(delete_device($id)); - echo("Removed $host\n"); + echo(delete_device($id)."\n"); } else { echo("Host doesn't exist!\n"); } diff --git a/html/api_v0.php b/html/api_v0.php index e5f8aa71b..2431adb3c 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -30,9 +30,10 @@ $app->setName('api'); $app->group('/api', function() use ($app) { $app->group('/v0', function() use ($app) { $app->group('/devices', function() use ($app) { - $app->get('/:hostname/ports/:ifname/:type', 'authToken', 'get_graph_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName/$type + $app->get('/:hostname', 'authToken', 'get_device');//api/v0/devices/$hostname $app->get('/:hostname/:type', 'authToken', 'get_graph_generic_by_hostname');//api/v0/devices/$hostname/$type $app->get('/:hostname/ports/:ifname', 'authToken', 'get_port_stats_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName + $app->get('/:hostname/ports/:ifname/:type', 'authToken', 'get_graph_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName/$type }); $app->get('/devices', 'authToken', 'list_devices');//api/v0/devices $app->post('/devices', 'authToken', 'add_device');//api/v0/devices (json data needs to be passed) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 8845b26ca..c728a6dd5 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -176,8 +176,8 @@ function authToken(\Slim\Route $route) if($authenticated === false) { - $app->response->setStatus(400); - $output = array("status" => "error", "message" => "API Token is invalid"); + $app->response->setStatus(403); + $output = array("status" => "error", "message" => "API Token is missing or invalid; please supply a valid token"); echo _json_encode($output); $app->stop(); } @@ -247,6 +247,33 @@ function get_graph_generic_by_hostname() require("includes/graphs/graph.inc.php"); } +function get_device() +{ + // return details of a single device + $app = \Slim\Slim::getInstance(); + $app->response->headers->set('Content-Type', 'application/json'); + $router = $app->router()->getCurrentRoute()->getParams(); + $hostname = $router['hostname']; + + require_once("../includes/functions.php"); + + // use hostname as device_id if it's all digits + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + + // find device matching the id + $device = device_by_id_cache($device_id); + if (!$device) { + $app->response->setStatus(404); + $output = array("status" => "error", "message" => "Device $hostname does not exist"); + echo _json_encode($output); + $app->stop(); + } + else { + $output = array("status" => "ok", "devices" => array($device)); + echo _json_encode($output); + } +} + function list_devices() { // This will return a list of devices @@ -299,11 +326,15 @@ function list_devices() function add_device() { // This will add a device using the data passed encoded with json + // FIXME: Execution flow through this function could be improved 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. + // Default status & code to error and change it if we need to. $status = "error"; + $code = 500; + // keep scrutinizer from complaining about snmpver not being set for all execution paths + $snmpver = "v2c"; if(empty($data)) { $message = "No information has been provided to add this new device"; @@ -313,8 +344,8 @@ function add_device() $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"; } + $port = $data['port'] ? mres($data['port']) : $config['snmp']['port']; + $transport = $data['transport'] ? mres($data['transport']) : "udp"; if($data['version'] == "v1" || $data['version'] == "v2c") { if ($data['community']) @@ -339,6 +370,8 @@ function add_device() } else { + $code = 400; + $status = "error"; $message = "You haven't specified an SNMP version to use"; } if(empty($message)) @@ -347,8 +380,9 @@ function add_device() $result = addHost($hostname, $snmpver, $port, $transport, 1); if($result) { - $status = 'ok'; - $message = 'Device has been added successfully'; + $code = 201; + $status = "ok"; + $message = "Device $hostname has been added successfully"; } else { @@ -356,6 +390,7 @@ function add_device() } } + $app->response->setStatus($code); $output = array("status" => $status, "message" => $message); $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); @@ -371,30 +406,44 @@ function del_device() $hostname = $router['hostname']; // Default status to error and change it if we need to. $status = "error"; + $code = 500; if(empty($hostname)) { - $message = "No hostname has been provided to delete this device"; + $message = "No hostname has been provided to delete"; + $output = array("status" => $status, "message" => $message); } - elseif(empty($hostname)) - { - $message = "Missing the device hostname"; - } - if(empty($message)) + else { require_once("../includes/functions.php"); - $device_id = get_device_id($hostname); - $response = delete_device($device_id); - if(empty($response)) - { - $message = "Device couldn't be deleted"; + + // allow deleting by device_id or hostname + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + $device = null; + if ($device_id) { + // save the current details for returning to the client on successful delete + $device = device_by_id_cache($device_id); } - else - { - $message = $response; - $status = "ok"; + if ($device) { + $response = delete_device($device_id); + if(empty($response)) { + // FIXME: Need to provide better diagnostics out of delete_device + $output = array("status" => $status, "message" => "Device deletion failed"); + } + else { + // deletion succeeded - include old device details in response + $code = 200; + $status = "ok"; + $output = array("status" => $status, "message" => $response, "devices" => array($device)); + } + } + else { + // no device matching the name + $code = 404; + $output = array("status" => $status, "message" => "Device $hostname not found"); } } - $output = array("status" => $status, "message" => $message); + + $app->response->setStatus($code); $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); } diff --git a/html/index.php b/html/index.php index 04a47905d..87f272de0 100755 --- a/html/index.php +++ b/html/index.php @@ -288,9 +288,10 @@ if (is_array($pagetitle))
curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices/localhost/ports/eth0/port_bits" > /tmp/graph.pngcurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/ports/eth0/port_bits" > /tmp/graph.pngcurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices/localhost/ports/eth0/port_bits?width=1024&height=768&from=1405457456&to=1405543856" > /tmp/graph.pngcurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/ports/eth0/port_bits?width=1024&height=768&from=1405457456&to=1405543856" > /tmp/graph.pngcurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost" > localhost.jsoncurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices/localhost/device_processor" > /tmp/graph.pngcurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/device_processor" > /tmp/graph.pngcurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices/localhost/device_processor?width=1024&height=768&from=1405457456&to=1405543856" > /tmp/graph.pngcurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/device_processor?width=1024&height=768&from=1405457456&to=1405543856" > /tmp/graph.pngcurl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices/localhost/ports/eth0"curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/ports/eth0"curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices"curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices"curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices?order=hostname&type=all"curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices?order=hostname&type=all"curl -X POST -d '{"hostname":"localhost.localdomain","version":"v1","community":"public"}' \
-H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices"curl -X POST -d '{"hostname":"localhost.localdomain","version":"v0","community":"public"}' \
-H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices"curl -X DELETE -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v1/devices/localhost"curl -X DELETE -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost"