api: Added api routes for eventlog, syslog, alertlog, authlog (#7071)

This commit is contained in:
Neil Lathwood
2017-07-27 19:56:38 +01:00
committed by GitHub
parent cd006d1842
commit fc2041caaf
3 changed files with 161 additions and 1 deletions

View File

@@ -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.
# <a name="api-structure">`Structure`</a> [`top`](#top)
@@ -1857,6 +1862,8 @@ Output:
}
```
## <a name="api-services">`Services`</a> [`top`](#top)
### <a name="api-services-list_services">Function: `list_services`</a> [`top`](#top)
Retrieve all services
@@ -1963,3 +1970,74 @@ Output:
]
}
```
## <a name="api-logs">`Logs`</a> [`top`](#top)
### <a name="api-logs-list_eventlog">Function: `list_eventlog`</a> [`top`](#top)
### <a name="api-logs-list_syslog">Function: `list_syslog`</a> [`top`](#top)
### <a name="api-logs-list_alertlog">Function: `list_alertlog`</a> [`top`](#top)
### <a name="api-logs-list_authlog">Function: `list_authlog`</a> [`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: -> <pptp-something-something-tunnel-something>",
"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"
}
]
}
```

View File

@@ -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');

View File

@@ -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);
}