Last active
March 11, 2019 20:27
-
-
Save markuskont/81b014a25b7592574d516f6af78cb41e to your computer and use it in GitHub Desktop.
Babbys first rust program.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate redis; | |
extern crate reqwest; | |
extern crate serde_json; | |
use redis::Commands; | |
use serde_json::{json, Map, Value}; | |
use std::{thread, time}; | |
use std::collections::HashMap; | |
fn main() { | |
let client = | |
redis::Client::open("redis://192.168.144.10/").expect("unable to connect to redis"); | |
let con = client | |
.get_connection() | |
.expect("unable to get redis connection"); | |
let key = "suricata"; | |
let max = 10; | |
let mut bulk = Vec::new(); | |
let mut count = 0; | |
loop { | |
let res: String = match con.lpop(key) { | |
Ok(res) => res, | |
Err(_res) => { | |
let ten_millis = time::Duration::from_millis(10); | |
thread::sleep(ten_millis); | |
continue; | |
} | |
}; | |
let mut v: Value = match serde_json::from_str(&res) { | |
Ok(v) => v, | |
Err(_v) => { | |
println!("ERR: {}", res); | |
continue; | |
} | |
}; | |
let ts = v["timestamp"].to_owned(); | |
if !v.is_object() { | |
continue; | |
} | |
let obj = v.as_object_mut().unwrap(); | |
obj.insert("@timestamp".to_string(), ts.into()); | |
let meta = json!({ | |
"index": { | |
"_index": "suricata-all", | |
"_type": "doc", | |
}, | |
}); | |
let v = json!(obj); | |
bulk.push(meta.to_string()); | |
bulk.push(v.to_string()); | |
count = count + 1; | |
if count % max == 0 { | |
let mut ndbulk = bulk.join("\n"); | |
ndbulk.push_str("\n"); | |
let client = reqwest::Client::new(); | |
match client | |
.post("http://192.168.144.10:9200/_bulk") | |
.body(ndbulk) | |
.header("Content-Type", "application/x-ndjson") | |
.send() | |
{ | |
Ok(mut resp) => { | |
match resp.text() { | |
Ok(text) => { | |
println!("{}", text); | |
} | |
Err(_) => println!("no resp text"), | |
}; | |
} | |
Err(_) => println!("fuck"), | |
}; | |
bulk.clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment