diff --git a/doc/API/Devices.md b/doc/API/Devices.md index 33ec33bc4e..e878778c8b 100644 --- a/doc/API/Devices.md +++ b/doc/API/Devices.md @@ -72,6 +72,37 @@ Output: } ``` +### `discover_device` + +Trigger a discovery of given device. + +Route: `/api/v0/devices/:hostname/discover` + +- hostname can be either the device hostname or id + +Input: + + - + +Example: + +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/discover +``` + +Output: + +```json +{ + "status": "ok", + "result": { + "status": 0, + "message": "Device will be rediscovered" + }, + "count": 2 +} +``` + ### `get_graphs` Get a list of available graphs for a device, this does not include ports. diff --git a/includes/functions.php b/includes/functions.php index 1e6b6da5cd..05f7ee38c8 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -434,6 +434,24 @@ function renamehost($id, $new, $source = 'console') return "Renaming of $host failed\n"; } +function device_discovery_trigger($id) +{ + global $debug; + + if (isCli() === false) { + ignore_user_abort(true); + set_time_limit(0); + } + + $update = dbUpdate(array('last_discovered' => array('NULL')), 'devices', '`device_id` = ?', array($id)); + if (!empty($update) || $update == '0') { + $message = 'Device will be rediscovered'; + } else { + $message = 'Error rediscovering device'; + } + return array('status'=> $update, 'message' => $message); +} + function delete_device($id) { global $debug; diff --git a/includes/html/api_functions.inc.php b/includes/html/api_functions.inc.php index ad42137734..ca2a00757c 100644 --- a/includes/html/api_functions.inc.php +++ b/includes/html/api_functions.inc.php @@ -717,6 +717,23 @@ function get_graphs(\Illuminate\Http\Request $request) }); } +function trigger_device_discovery(\Illuminate\Http\Request $request) +{ + // return details of a single device + $hostname = $request->route('hostname'); + + // 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) { + return api_error(404, "Device $hostname does not exist"); + } + + $ret = device_discovery_trigger($device_id); + return api_success($ret, 'result'); +} + function list_available_health_graphs(\Illuminate\Http\Request $request) { $hostname = $request->route('hostname'); diff --git a/routes/api.php b/routes/api.php index 09dd12d699..b9e9d5a00e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -81,6 +81,7 @@ Route::group(['prefix' => 'v0', 'namespace' => '\App\Api\Controllers'], function // restricted by access Route::group(['prefix' => 'devices'], function () { Route::get('{hostname}', 'LegacyApiController@get_device')->name('get_device'); + Route::get('{hostname}/discover', 'LegacyApiController@trigger_device_discovery')->name('trigger_device_discovery'); Route::get('{hostname}/graphs/health/{type}/{sensor_id?}', 'LegacyApiController@get_graph_generic_by_hostname')->name('get_health_graph'); Route::get('{hostname}/graphs/wireless/{type}/{sensor_id?}', 'LegacyApiController@get_graph_generic_by_hostname')->name('get_wireless_graph'); Route::get('{hostname}/vlans', 'LegacyApiController@get_vlans')->name('get_vlans');