mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
api: Added add_service_for_host endpoint to API (#8113)
* Add add_service_for_host endpoint to API Signed-off-by: Rémy Jacquin <remy@remyj.fr> * Change permissions to allow users to modify device Signed-off-by: Rémy Jacquin <remy@remyj.fr>
This commit is contained in:
committed by
Neil Lathwood
parent
8a9fc7783d
commit
e12e6720f8
@@ -209,6 +209,7 @@ LibreNMS contributors:
|
|||||||
- Rob J. Epping <librenms@renf.us> (robje)
|
- Rob J. Epping <librenms@renf.us> (robje)
|
||||||
- Frank Petrilli <frank@petril.li> (frankpetrilli)
|
- Frank Petrilli <frank@petril.li> (frankpetrilli)
|
||||||
- Joel Kociolek <joel@kociolek.org> (lejoko)
|
- Joel Kociolek <joel@kociolek.org> (lejoko)
|
||||||
|
- Rémy Jacquin <remy@remyj.fr> (remyj38)
|
||||||
|
|
||||||
Observium was written by:
|
Observium was written by:
|
||||||
- Adam Armstrong
|
- Adam Armstrong
|
||||||
|
@@ -106,3 +106,31 @@ Output:
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### `add_service_for_host`
|
||||||
|
|
||||||
|
Add a service for device
|
||||||
|
|
||||||
|
Route: `/api/v0/services/:hostname`
|
||||||
|
|
||||||
|
- id or hostname is the specific device
|
||||||
|
|
||||||
|
Input:
|
||||||
|
|
||||||
|
- type: service type
|
||||||
|
- ip: ip of the service
|
||||||
|
- desc: description for the service
|
||||||
|
- param: parameters for the service
|
||||||
|
- ignore: ignore the service for checks
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```curl
|
||||||
|
curl -X POST -d '{"type":"ping","ip": "192.168.1.10","desc":"test ping","param": "-t 10 -c 5"}' -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/services/192.168.1.10
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"message": "Service ping has been added to device 192.168.1.10 (#10)"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@@ -194,6 +194,7 @@ $app->group(
|
|||||||
'/services',
|
'/services',
|
||||||
function () use ($app) {
|
function () use ($app) {
|
||||||
$app->get('/:hostname', 'authToken', 'list_services')->name('get_service_for_host');
|
$app->get('/:hostname', 'authToken', 'list_services')->name('get_service_for_host');
|
||||||
|
$app->post('/:hostname', 'authToken', 'add_service_for_host')->name('add_service_for_host');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
$app->get('/services', 'authToken', 'list_services')->name('list_services');
|
$app->get('/services', 'authToken', 'list_services')->name('list_services');
|
||||||
|
@@ -1721,3 +1721,52 @@ function validate_column_list($columns, $tableName)
|
|||||||
$app->stop();
|
$app->stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function add_service_for_host()
|
||||||
|
{
|
||||||
|
global $config;
|
||||||
|
$app = \Slim\Slim::getInstance();
|
||||||
|
$router = $app->router()->getCurrentRoute()->getParams();
|
||||||
|
$hostname = $router['hostname'];
|
||||||
|
// use hostname as device_id if it's all digits
|
||||||
|
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||||
|
check_device_permission($device_id);
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
$missing_fields = array();
|
||||||
|
|
||||||
|
// Check if some required fields are empty
|
||||||
|
if (empty($data['type'])) {
|
||||||
|
$missing_fields[] = 'type';
|
||||||
|
}
|
||||||
|
if (empty($data['ip'])) {
|
||||||
|
$missing_fields[] = 'ip';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print error if required fields are missing
|
||||||
|
if (!empty($missing_fields)) {
|
||||||
|
api_error(400, sprintf("Service field%s %s missing: %s.", ((sizeof($missing_fields)>1)?'s':''), ((sizeof($missing_fields)>1)?'are':'is'), implode(', ', $missing_fields)));
|
||||||
|
}
|
||||||
|
if (!filter_var($data['ip'], FILTER_VALIDATE_IP)) {
|
||||||
|
api_error(400, 'service_ip is not a valid IP address.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if service type exists
|
||||||
|
if (!in_array($data['type'], list_available_services())) {
|
||||||
|
api_error(400, "The service " . $data['type'] . " does not exist.\n Available service types: " . implode(', ', list_available_services()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get parameters
|
||||||
|
$service_type = $data['type'];
|
||||||
|
$service_ip = $data['ip'];
|
||||||
|
$service_desc = $data['desc'] ? mres($data['desc']) : '';
|
||||||
|
$service_param = $data['param'] ? mres($data['param']) : '';
|
||||||
|
$service_ignore = $data['ignore'] ? true : false; // Default false
|
||||||
|
|
||||||
|
// Set the service
|
||||||
|
$service_id = add_service($device_id, $service_type, $service_desc, $service_ip, $service_param, (int)$service_ignore);
|
||||||
|
if ($service_id != false) {
|
||||||
|
api_success_noresult(201, "Service $service_type has been added to device $hostname (#$service_id)");
|
||||||
|
} else {
|
||||||
|
api_error(500, 'Failed to add the service');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -16,12 +16,8 @@ if ($_SESSION['userlevel'] < '10') {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
foreach (list_available_services() as $current_service) {
|
||||||
foreach (scandir($config['nagios_plugins']) as $file) {
|
$servicesform .= "<option value='$current_service'>$current_service</option>";
|
||||||
if (substr($file, 0, 6) === 'check_') {
|
|
||||||
$check_name = substr($file, 6);
|
|
||||||
$servicesform .= "<option value='$check_name'>$check_name</option>";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (dbFetchRows('SELECT * FROM `devices` ORDER BY `hostname`') as $device) {
|
foreach (dbFetchRows('SELECT * FROM `devices` ORDER BY `hostname`') as $device) {
|
||||||
|
@@ -306,3 +306,20 @@ function check_service($command)
|
|||||||
|
|
||||||
return array ($status, $response, $metrics);
|
return array ($status, $response, $metrics);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List all available services from nagios plugins directory
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function list_available_services()
|
||||||
|
{
|
||||||
|
global $config;
|
||||||
|
$services = array();
|
||||||
|
foreach (scandir($config['nagios_plugins']) as $file) {
|
||||||
|
if (substr($file, 0, 6) === 'check_') {
|
||||||
|
$services[] = substr($file, 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $services;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user