From 8d414d7b8fdcad0466f413419d37edc1ebf922d0 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Thu, 25 May 2017 06:47:48 -0400 Subject: journal: Get unit log --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 31 ++++++++++++++++++++++++++----- src/systemd/journal.rs | 10 ++++++++++ src/systemd/mod.rs | 1 + 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/systemd/journal.rs diff --git a/Cargo.lock b/Cargo.lock index db77859..9abbf50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ dependencies = [ "iron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "iron-sessionstorage 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "router 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "staticfile 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 4581d3a..8661e7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ env_logger = "*" router = "*" horrorshow = "*" staticfile = "*" +regex = "*" diff --git a/src/main.rs b/src/main.rs index b4a8f38..f96ebdc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ // CSRF protection // https://github.com/heartsucker/iron-csrf +#[macro_use] extern crate iron; #[macro_use] extern crate log; @@ -9,6 +10,7 @@ extern crate env_logger; extern crate systemhttp; extern crate iron_sessionstorage; extern crate staticfile; +extern crate regex; use iron_sessionstorage::traits::*; use iron_sessionstorage::SessionStorage; @@ -19,8 +21,10 @@ use iron::headers::ContentType; use iron::{Iron, Request, IronResult, Response, Chain}; use router::Router; use systemhttp::systemd::unit; +use systemhttp::systemd::journal; use systemhttp::render; use staticfile::Static; +use regex::Regex; struct Aaa(String); @@ -36,8 +40,7 @@ impl iron_sessionstorage::Value for Aaa { } } - -fn hello(r: &mut Request) -> IronResult { +fn overview(r: &mut Request) -> IronResult { let mut _value = match try!(r.session().get::()) { Some(aaa) => aaa, None => Aaa("".to_owned()), @@ -73,13 +76,31 @@ fn hello(r: &mut Request) -> IronResult { render::system_status(&units_by_section)))) } -fn main() { +fn journal(r: &mut Request) -> IronResult { + let unit = iexpect!(r.extensions + .get::() + .unwrap() + .find("unit"), status::BadRequest); + info!("got here"); + let re = Regex::new(r"[-_\w\d]*").unwrap(); + if !re.is_match(unit) { + return Ok(Response::with( + (status::BadRequest, + format!("Unit ({}) does not match {}", + unit, re)))); + } + info!("got here"); + Ok(Response::with((status::Ok, + itry!(journal::get_log(unit, 100))))) +} +fn main() { env_logger::init().unwrap(); let secret = b"secret2".to_vec(); let router = router!( - root: get "/" => hello, - name: get "/:name" => hello, + root: get "/" => overview, + name: get "/:name" => overview, + journal: get "/journal/:unit" => journal, css: get "/static/main.css" => Static::new(""), ); let mut chain = Chain::new(router); diff --git a/src/systemd/journal.rs b/src/systemd/journal.rs new file mode 100644 index 0000000..86791ab --- /dev/null +++ b/src/systemd/journal.rs @@ -0,0 +1,10 @@ +use std::process::Command; +use std::io; + +pub fn get_log(unit: &str, lines: i32) -> io::Result { + let status = try!(Command::new("journalctl") + .args(&["-u", unit, + "-n", &lines.to_string()]) + .output()); + Ok(String::from_utf8_lossy(&status.stdout).into_owned()) +} diff --git a/src/systemd/mod.rs b/src/systemd/mod.rs index a5acbce..bc1a9a1 100644 --- a/src/systemd/mod.rs +++ b/src/systemd/mod.rs @@ -1 +1,2 @@ pub mod unit; +pub mod journal; -- cgit v1.2.3