diff --git a/doc/API/API-Docs.md b/doc/API/API-Docs.md index d6104cfd10..9f7c5e38df 100644 --- a/doc/API/API-Docs.md +++ b/doc/API/API-Docs.md @@ -65,7 +65,12 @@ source: API/API-Docs.md - [`services`](#api-services) - [`list_services`](#api-services-list_services) - [`get_service_for_host`](#api-services-get_service_for_host) - + - [`logs`](#api-logs) + - [`list_eventlog`](#api-logs-list_eventlog) + - [`list_syslog`](#api-logs-list_syslog) + - [`list_alertlog`](#api-logs-list_alertlog) + - [`list_authlog`](#api-logs-list_authlog) + Describes the API structure. # `Structure` [`top`](#top) @@ -1857,6 +1862,8 @@ Output: } ``` +## `Services` [`top`](#top) + ### Function: `list_services` [`top`](#top) Retrieve all services @@ -1963,3 +1970,74 @@ Output: ] } ``` + +## `Logs` [`top`](#top) + +### Function: `list_eventlog` [`top`](#top) +### Function: `list_syslog` [`top`](#top) +### Function: `list_alertlog` [`top`](#top) +### Function: `list_authlog` [`top`](#top) + +All the `list_*logs` calls are aliased to `list_logs`. + +Retrieve all logs or logs for a specific device. + +Route: /api/v0/logs/eventlog/:hostname +Route: /api/v0/logs/syslog/:hostname +Route: /api/v0/logs/alertlog/:hostname +Route: /api/v0/logs/authlog/:hostname + + - id or hostname is the specific device + +Input: + + - start: The page number to request. + - limit: The limit of results to be returned. + - from: The date and time to search from. + - to: The data and time to search to. + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/logs/eventlog/:hostname +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/logs/syslog/:hostname?limit=20 +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/logs/eventlog/:hostname?limit=20&start=5&from=2017-07-22%2023:00:00 +``` + +Output: +```text +{ + "status": "ok", + "err-msg": "", + "count": 5, + "total": "15", + "logs": [ + { + "hostname": "localhost", + "sysName": "web01.1.novalocal", + "event_id": "10050349", + "host": "279", + "device_id": "279", + "datetime": "2017-07-22 19:57:47", + "message": "ifAlias: -> ", + "type": "interface", + "reference": "NULL", + "username": "", + "severity": "3" + }, + .... + { + "hostname": "localhost", + "sysName": "web01.1.novalocal", + "event_id": "10050353", + "host": "279", + "device_id": "279", + "datetime": "2017-07-22 19:57:47", + "message": "ifHighSpeed: -> 0", + "type": "interface", + "reference": "NULL", + "username": "", + "severity": "3" + } + ] +} +``` \ No newline at end of file diff --git a/html/api_v0.php b/html/api_v0.php index 4d423e1cbc..1da549cdb8 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -168,6 +168,15 @@ $app->group( ); $app->get('/services', 'authToken', 'list_services')->name('list_services'); // End Service + $app->group( + '/logs', + function () use ($app) { + $app->get('/eventlog(/:hostname)', 'authToken', 'list_logs')->name('list_eventlog'); + $app->get('/syslog(/:hostname)', 'authToken', 'list_logs')->name('list_syslog'); + $app->get('/alertlog(/:hostname)', 'authToken', 'list_logs')->name('list_alertlog'); + $app->get('/authlog(/:hostname)', 'authToken', 'list_logs')->name('list_authlog'); + } + ); } ); $app->get('/v0', 'authToken', 'show_endpoints'); diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index d15aabeb4b..19e4712615 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -1568,3 +1568,76 @@ function list_services() $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); } + +function list_logs() +{ + global $config; + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $type = $app->router()->getCurrentRoute()->getName(); + $hostname = $router['hostname']; + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + if ($type === 'list_eventlog') { + $table = 'eventlog'; + $timestamp = 'datetime'; + } elseif ($type === 'list_syslog') { + $table = 'syslog'; + $timestamp = 'timestamp'; + } elseif ($type === 'list_alertlog') { + $table = 'alert_log'; + $timestamp = 'time_logged'; + } elseif ($type === 'list_authlog') { + $table = 'authlog'; + $timestamp = 'datetime'; + } else { + $table = 'eventlog'; + $timestamp = 'datetime'; + } + + $message = ''; + $status = 'ok'; + $code = 200; + $start = mres($_GET['start']) ?: 1; + $limit = mres($_GET['limit']) ?: 50; + $from = mres($_GET['from']); + $to = mres($_GET['to']); + + $count_query = 'SELECT COUNT(*)'; + $full_query = "SELECT `devices`.`hostname`, `devices`.`sysName`, `$table`.*"; + + $param = array(); + $query = " FROM $table LEFT JOIN `devices` ON `$table`.`device_id`=`devices`.`device_id` WHERE 1"; + + if (is_numeric($device_id)) { + $query .= " AND `devices`.`device_id` = ?"; + $param[] = $device_id; + } + + if ($from) { + $query .= " AND $timestamp >= ?"; + $param[] = $from; + } + + if ($to) { + $query .= " AND $timestamp <= ?"; + $param[] = $to; + } + + $count_query = $count_query . $query; + $count = dbFetchCell($count_query, $param); + $full_query = $full_query . $query . " ORDER BY $timestamp ASC LIMIT $start,$limit"; + $logs = dbFetchRows($full_query, $param); + + $limited_count = count($logs); + $output = array( + 'status' => $status, + 'err-msg' => $message, + 'count' => $limited_count, + 'total' => $count, + 'logs' => $logs, + ); + + $app->response->setStatus($code); + $app->response->headers->set('Content-Type', 'application/json'); + echo _json_encode($output); +}