api2/node/syslog.rs: implement dump journal
This commit is contained in:
parent
4f9a726833
commit
0441886842
@ -8,12 +8,71 @@ use serde_json::{json, Value};
|
||||
use std::sync::Arc;
|
||||
use lazy_static::lazy_static;
|
||||
use crate::tools::common_regex;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
fn dump_journal(
|
||||
start: Option<u64>,
|
||||
limit: Option<u64>,
|
||||
since: Option<&str>,
|
||||
until: Option<&str>,
|
||||
service: Option<&str>,
|
||||
) -> Result<(u64, Vec<Value>), Error> {
|
||||
|
||||
let mut args = vec!["-o", "short", "--no-pager"];
|
||||
|
||||
if let Some(service) = service { args.push("--unit"); args.push(service); }
|
||||
if let Some(since) = since { args.push("--since"); args.push(since); }
|
||||
if let Some(until) = until { args.push("--until"); args.push(until); }
|
||||
|
||||
let mut lines: Vec<Value> = vec![];
|
||||
let mut limit = limit.unwrap_or(50);
|
||||
let start = start.unwrap_or(0);
|
||||
let mut count: u64 = 0;
|
||||
|
||||
let mut child = Command::new("/bin/journalctl")
|
||||
.args(&args)
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
use std::io::{BufRead,BufReader};
|
||||
|
||||
if let Some(ref mut stdout) = child.stdout {
|
||||
for line in BufReader::new(stdout).lines() {
|
||||
count += 1;
|
||||
if count < start { continue };
|
||||
if limit <= 0 { continue };
|
||||
|
||||
let line = line?; // fixme: nom utf8??
|
||||
lines.push(json!({ "n": count, "t": line }));
|
||||
|
||||
limit -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
let status = child.wait()?; // fixme: check status
|
||||
|
||||
// HACK: ExtJS store.guaranteeRange() does not like empty array
|
||||
// so we add a line
|
||||
if count == 0 {
|
||||
count += 1;
|
||||
lines.push(json!({ "n": count, "t": "no content"}));
|
||||
}
|
||||
|
||||
Ok((count, lines))
|
||||
}
|
||||
|
||||
fn get_syslog(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
||||
|
||||
let result = json!({});
|
||||
let (count, lines) = dump_journal(
|
||||
param["start"].as_u64(),
|
||||
param["limit"].as_u64(),
|
||||
param["since"].as_str(),
|
||||
param["until"].as_str(),
|
||||
param["service"].as_str());
|
||||
|
||||
Ok(result)
|
||||
//fixme: $restenv->set_result_attrib('total', $count);
|
||||
|
||||
Ok(json!(lines))
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
|
Loading…
Reference in New Issue
Block a user