mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
api: Added api routes for eventlog, syslog, alertlog, authlog (#7071)
This commit is contained in:
committed by
GitHub
co-authored by
GitHub
parent
cd006d1842
commit
fc2041caaf
@@ -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');
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user