mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-31 04:23:40 +00:00
add endpoint for dmesg logs
This commit is contained in:
committed by
Aiden McClelland
parent
39a2685506
commit
659af734eb
@@ -77,6 +77,7 @@ pub fn main_api() -> Result<(), RpcError> {
|
|||||||
|
|
||||||
#[command(subcommands(
|
#[command(subcommands(
|
||||||
system::logs,
|
system::logs,
|
||||||
|
system::kernel_logs,
|
||||||
system::metrics,
|
system::metrics,
|
||||||
shutdown::shutdown,
|
shutdown::shutdown,
|
||||||
shutdown::restart,
|
shutdown::restart,
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ fn deserialize_string_or_utf8_array<'de, D: serde::de::Deserializer<'de>>(
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum LogSource {
|
pub enum LogSource {
|
||||||
|
Kernel,
|
||||||
Service(&'static str),
|
Service(&'static str),
|
||||||
Container(PackageId),
|
Container(PackageId),
|
||||||
}
|
}
|
||||||
@@ -139,37 +140,42 @@ pub async fn fetch_logs(
|
|||||||
cursor: Option<String>,
|
cursor: Option<String>,
|
||||||
before_flag: bool,
|
before_flag: bool,
|
||||||
) -> Result<LogResponse, Error> {
|
) -> Result<LogResponse, Error> {
|
||||||
let limit = limit.unwrap_or(50);
|
let mut cmd = Command::new("journalctl");
|
||||||
let limit_formatted = format!("-n{}", limit);
|
|
||||||
|
|
||||||
let mut args = vec!["--output=json", "--output-fields=MESSAGE", &limit_formatted];
|
let limit = limit.unwrap_or(50);
|
||||||
let id_formatted = match id {
|
|
||||||
|
cmd.arg("--output=json");
|
||||||
|
cmd.arg("--output-fields=MESSAGE");
|
||||||
|
cmd.arg(format!("-n{}", limit));
|
||||||
|
match id {
|
||||||
|
LogSource::Kernel => {
|
||||||
|
cmd.arg("-k");
|
||||||
|
}
|
||||||
LogSource::Service(id) => {
|
LogSource::Service(id) => {
|
||||||
args.push("-u");
|
cmd.arg("-u");
|
||||||
id.to_owned()
|
cmd.arg(id);
|
||||||
}
|
}
|
||||||
LogSource::Container(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 cursor_formatted = format!("--after-cursor={}", cursor.clone().unwrap_or("".to_owned()));
|
||||||
let mut get_prev_logs_and_reverse = false;
|
let mut get_prev_logs_and_reverse = false;
|
||||||
if cursor.is_some() {
|
if cursor.is_some() {
|
||||||
args.push(&cursor_formatted);
|
cmd.arg(&cursor_formatted);
|
||||||
if before_flag {
|
if before_flag {
|
||||||
get_prev_logs_and_reverse = true;
|
get_prev_logs_and_reverse = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if get_prev_logs_and_reverse {
|
if get_prev_logs_and_reverse {
|
||||||
args.push("--reverse");
|
cmd.arg("--reverse");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut child = Command::new("journalctl")
|
let mut child = cmd.stdout(Stdio::piped()).spawn()?;
|
||||||
.args(args)
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.spawn()?;
|
|
||||||
let out = BufReader::new(
|
let out = BufReader::new(
|
||||||
child
|
child
|
||||||
.stdout
|
.stdout
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use tokio::sync::RwLock;
|
|||||||
use tracing::instrument;
|
use tracing::instrument;
|
||||||
|
|
||||||
use crate::context::RpcContext;
|
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::logs::{display_logs, fetch_logs, LogResponse, LogSource};
|
||||||
use crate::shutdown::Shutdown;
|
use crate::shutdown::Shutdown;
|
||||||
use crate::util::serde::{display_serializable, IoFormat};
|
use crate::util::serde::{display_serializable, IoFormat};
|
||||||
@@ -31,6 +31,21 @@ pub async fn logs(
|
|||||||
.await?)
|
.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)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct MetricLeaf<T> {
|
pub struct MetricLeaf<T> {
|
||||||
value: T,
|
value: T,
|
||||||
|
|||||||
Reference in New Issue
Block a user