summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-25 06:47:48 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-25 06:47:48 -0400
commit8d414d7b8fdcad0466f413419d37edc1ebf922d0 (patch)
treefe3b3717bba6ae9fc05a2e8ff78c7a0aeaa3e691 /src
parent652f6ad4da9918410780ce9f3187f3d3d1f9278c (diff)
journal: Get unit log
Diffstat (limited to 'src')
-rw-r--r--src/main.rs31
-rw-r--r--src/systemd/journal.rs10
-rw-r--r--src/systemd/mod.rs1
3 files changed, 37 insertions, 5 deletions
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<Response> {
+fn overview(r: &mut Request) -> IronResult<Response> {
let mut _value = match try!(r.session().get::<Aaa>()) {
Some(aaa) => aaa,
None => Aaa("".to_owned()),
@@ -73,13 +76,31 @@ fn hello(r: &mut Request) -> IronResult<Response> {
render::system_status(&units_by_section))))
}
-fn main() {
+fn journal(r: &mut Request) -> IronResult<Response> {
+ let unit = iexpect!(r.extensions
+ .get::<Router>()
+ .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<String> {
+ 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;