Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oxlog tool and library #4810

Merged
merged 19 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"dev-tools/crdb-seed",
"dev-tools/omdb",
"dev-tools/omicron-dev",
"dev-tools/oxlog",
"dev-tools/thing-flinger",
"dev-tools/xtask",
"dns-server",
Expand Down Expand Up @@ -92,6 +93,7 @@ default-members = [
"dev-tools/crdb-seed",
"dev-tools/omdb",
"dev-tools/omicron-dev",
"dev-tools/oxlog",
"dev-tools/thing-flinger",
# Do not include xtask in the list of default members, because this causes
# hakari to not work as well and build times to be longer.
Expand Down
18 changes: 18 additions & 0 deletions dev-tools/oxlog/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "oxlog"
version = "0.1.0"
edition = "2021"
license = "MPL-2.0"

[dependencies]
anyhow.workspace = true
camino.workspace = true
chrono.workspace = true
clap.workspace = true
uuid.workspace = true

# Disable doc builds by default for our binaries to work around issue
# rust-lang/cargo#8373. These docs would not be very useful anyway.
[[bin]]
name = "oxlog"
andrewjstone marked this conversation as resolved.
Show resolved Hide resolved
doc = false
121 changes: 121 additions & 0 deletions dev-tools/oxlog/src/bin/oxlog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Tool for discovering oxide related logfiles on sleds

use clap::{Args, Parser, Subcommand};
use oxlog::{Filter, LogFile, Zones};

#[derive(Debug, Parser)]
#[command(version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}

#[derive(Debug, Subcommand)]
enum Commands {
/// List all zones found on the filesystem
Zones,

/// List logs for a given service
Logs {
// The name of the zone
zone: String,

/// The name of the service to list logs for
service: Option<String>,

/// Print available metadata
#[arg(short, long)]
metadata: bool,

#[command(flatten)]
filter: FilterArgs,
},
}

#[derive(Args, Debug)]
#[group(required = true, multiple = true)]
struct FilterArgs {
/// Print only the current log file
#[arg(short, long)]
current: bool,

/// Print only the archived log files
#[arg(short, long)]
archived: bool,

// Print only the extra log files
#[arg(short, long)]
extra: bool,
}

fn main() -> Result<(), anyhow::Error> {
let cli = Cli::parse();

match cli.command {
Commands::Zones => {
for zone in Zones::load()?.zones.keys() {
println!("{zone}");
}
Ok(())
}
Commands::Logs { zone, service, metadata, filter } => {
let zones = Zones::load()?;
let filter = Filter {
current: filter.current,
archived: filter.archived,
extra: filter.extra,
};
let print_metadata = |f: &LogFile| {
println!(
"{}\t{}\t{}",
f.path,
f.size.map_or_else(|| "-".to_string(), |s| s.to_string()),
f.modified
.map_or_else(|| "-".to_string(), |s| s.to_rfc3339())
);
};

let logs = zones.zone_logs(&zone, filter);
for (svc_name, mut svc_logs) in logs {
if let Some(service) = &service {
if svc_name != service.as_str() {
continue;
}
}
svc_logs.archived.sort();
if filter.current {
if let Some(current) = &svc_logs.current {
if metadata {
print_metadata(current);
} else {
println!("{}", current.path);
}
}
}
if filter.archived {
for f in &svc_logs.archived {
if metadata {
print_metadata(f);
} else {
println!("{}", f.path);
}
}
}
if filter.extra {
for f in &svc_logs.extra {
if metadata {
print_metadata(f);
} else {
println!("{}", f.path);
}
}
}
}
Ok(())
}
}
}
Loading
Loading