add endpoint for dmesg logs

This commit is contained in:
Aiden McClelland
2022-03-16 13:26:28 -05:00
committed by Aiden McClelland
parent 39a2685506
commit 659af734eb
3 changed files with 37 additions and 15 deletions

View File

@@ -77,6 +77,7 @@ pub fn main_api() -> Result<(), RpcError> {
#[command(subcommands(
system::logs,
system::kernel_logs,
system::metrics,
shutdown::shutdown,
shutdown::restart,

View File

@@ -106,6 +106,7 @@ fn deserialize_string_or_utf8_array<'de, D: serde::de::Deserializer<'de>>(
#[derive(Debug)]
pub enum LogSource {
Kernel,
Service(&'static str),
Container(PackageId),
}
@@ -139,37 +140,42 @@ pub async fn fetch_logs(
cursor: Option<String>,
before_flag: bool,
) -> Result<LogResponse, Error> {
let limit = limit.unwrap_or(50);
let limit_formatted = format!("-n{}", limit);
let mut cmd = Command::new("journalctl");
let mut args = vec!["--output=json", "--output-fields=MESSAGE", &limit_formatted];
let id_formatted = match id {
let limit = limit.unwrap_or(50);
cmd.arg("--output=json");
cmd.arg("--output-fields=MESSAGE");
cmd.arg(format!("-n{}", limit));
match id {
LogSource::Kernel => {
cmd.arg("-k");
}
LogSource::Service(id) => {
args.push("-u");
id.to_owned()
cmd.arg("-u");
cmd.arg(id);
}
LogSource::Container(id) => {
format!("CONTAINER_NAME={}", DockerAction::container_name(&id, None))
cmd.arg(format!(
"CONTAINER_NAME={}",
DockerAction::container_name(&id, None)
));
}
};
args.push(&id_formatted);
let cursor_formatted = format!("--after-cursor={}", cursor.clone().unwrap_or("".to_owned()));
let mut get_prev_logs_and_reverse = false;
if cursor.is_some() {
args.push(&cursor_formatted);
cmd.arg(&cursor_formatted);
if before_flag {
get_prev_logs_and_reverse = true;
}
}
if get_prev_logs_and_reverse {
args.push("--reverse");
cmd.arg("--reverse");
}
let mut child = Command::new("journalctl")
.args(args)
.stdout(Stdio::piped())
.spawn()?;
let mut child = cmd.stdout(Stdio::piped()).spawn()?;
let out = BufReader::new(
child
.stdout

View File

@@ -8,7 +8,7 @@ use tokio::sync::RwLock;
use tracing::instrument;
use crate::context::RpcContext;
use crate::disk::util::{get_available, get_percentage, get_used};
use crate::disk::util::{get_available, get_used};
use crate::logs::{display_logs, fetch_logs, LogResponse, LogSource};
use crate::shutdown::Shutdown;
use crate::util::serde::{display_serializable, IoFormat};
@@ -31,6 +31,21 @@ pub async fn logs(
.await?)
}
#[command(rename = "kernel-logs", display(display_logs))]
pub async fn kernel_logs(
#[arg] limit: Option<usize>,
#[arg] cursor: Option<String>,
#[arg] before_flag: Option<bool>,
) -> Result<LogResponse, Error> {
Ok(fetch_logs(
LogSource::Kernel,
limit,
cursor,
before_flag.unwrap_or(false),
)
.await?)
}
#[derive(Serialize, Deserialize)]
pub struct MetricLeaf<T> {
value: T,