added support for "priority" values
This commit is contained in:
parent
af56e7d67a
commit
4fdc0da24c
1 changed files with 16 additions and 6 deletions
22
src/main.rs
22
src/main.rs
|
@ -50,6 +50,7 @@ async fn main() -> Result<(), Error> {
|
||||||
tokio::spawn(connection);
|
tokio::spawn(connection);
|
||||||
|
|
||||||
client.execute("create table if not exists log(
|
client.execute("create table if not exists log(
|
||||||
|
prio smallint,
|
||||||
rcv_ip inet,
|
rcv_ip inet,
|
||||||
rcv_date timestamptz,
|
rcv_date timestamptz,
|
||||||
date timestamptz,
|
date timestamptz,
|
||||||
|
@ -61,7 +62,7 @@ async fn main() -> Result<(), Error> {
|
||||||
let client = Arc::new(client);
|
let client = Arc::new(client);
|
||||||
|
|
||||||
let insert_statement = client
|
let insert_statement = client
|
||||||
.prepare("insert into log(rcv_ip, rcv_date, date, daemon, message) values ($1, $2, $3, $4, $5)").await?;
|
.prepare("insert into log(prio, rcv_ip, rcv_date, date, daemon, message) values ($1, $2, $3, $4, $5, $6)").await?;
|
||||||
|
|
||||||
let mut listener = TcpListener::bind(&options.addr).await
|
let mut listener = TcpListener::bind(&options.addr).await
|
||||||
.map_err(|e| format!("could not bind to {} with error {:?}", &options.addr, e))?;
|
.map_err(|e| format!("could not bind to {} with error {:?}", &options.addr, e))?;
|
||||||
|
@ -100,9 +101,9 @@ async fn handle_peer(stream: TcpStream, peer: SocketAddr, db: Arc<Client>, inser
|
||||||
loop {
|
loop {
|
||||||
match lines.next().await.transpose()? {
|
match lines.next().await.transpose()? {
|
||||||
Some(line) => {
|
Some(line) => {
|
||||||
let (now, date, service, log) = parse_line(&line)?;
|
let (prio, now, date, service, log) = parse_line(&line)?;
|
||||||
if !blacklist.contains(service) {
|
if !blacklist.contains(service) {
|
||||||
db.execute(&insert_statement, &[&ip, &now, &date, &service, &log]).await?;
|
db.execute(&insert_statement, &[&prio, &ip, &now, &date, &service, &log]).await?;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => break,
|
None => break,
|
||||||
|
@ -118,7 +119,15 @@ async fn handle_peer(stream: TcpStream, peer: SocketAddr, db: Arc<Client>, inser
|
||||||
* , log entry
|
* , log entry
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
fn parse_line(line: &'_ str) -> Result<(DateTime<Local>, DateTime<FixedOffset>, &'_ str, &'_ str), Error> {
|
fn parse_line(line: &'_ str) -> Result<(i16, DateTime<Local>, DateTime<FixedOffset>, &'_ str, &'_ str), Error> {
|
||||||
|
let mut prio_and_remainder = line.splitn(2, '>');
|
||||||
|
let prio = prio_and_remainder.next().ok_or("log did not contain priority")?;
|
||||||
|
let prio = &prio[1..];
|
||||||
|
let prio = prio.parse()
|
||||||
|
.map_err(|e| format!("could not parse priority {}: {}",prio, e))?;
|
||||||
|
|
||||||
|
|
||||||
|
let line = prio_and_remainder.next().expect("splitn should always return a second part");
|
||||||
let (date, line) = line.split_at(16);
|
let (date, line) = line.split_at(16);
|
||||||
|
|
||||||
// we need to prepend the current year and timezone, as that is not stated in the logfile
|
// we need to prepend the current year and timezone, as that is not stated in the logfile
|
||||||
|
@ -126,7 +135,8 @@ fn parse_line(line: &'_ str) -> Result<(DateTime<Local>, DateTime<FixedOffset>,
|
||||||
let mut base = format!("{}", now.format("%Y %z "));
|
let mut base = format!("{}", now.format("%Y %z "));
|
||||||
base.push_str(date);
|
base.push_str(date);
|
||||||
|
|
||||||
let date = DateTime::parse_from_str(&base, "%Y %z %b %e %H:%M:%S ")?;
|
let date = DateTime::parse_from_str(&base, "%Y %z %b %e %H:%M:%S ").
|
||||||
|
map_err(|e| format!("could not parse {}{} {}", date, line, e))?;
|
||||||
|
|
||||||
let mut parts = line.splitn(2, ':');
|
let mut parts = line.splitn(2, ':');
|
||||||
|
|
||||||
|
@ -135,5 +145,5 @@ fn parse_line(line: &'_ str) -> Result<(DateTime<Local>, DateTime<FixedOffset>,
|
||||||
let service = service_parts.next().ok_or("could not split pid from service")?.trim();
|
let service = service_parts.next().ok_or("could not split pid from service")?.trim();
|
||||||
|
|
||||||
let log = parts.next().ok_or("could not parse logfile")?.trim();
|
let log = parts.next().ok_or("could not parse logfile")?.trim();
|
||||||
Ok((now, date, service, log))
|
Ok((prio, now, date, service, log))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue