Last active
February 25, 2019 22:28
-
-
Save maxux/a98973d76cf83675d7e383ff46fa6d7e to your computer and use it in GitHub Desktop.
Dashboard Python/Rust comparaison
This file contains hidden or 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
import requests | |
import redis | |
import time | |
import json | |
RTINFO_ENDPOINT = "http://clea.maxux.net:8089/json" | |
# This class is in another file for reusability | |
class DashboardSlave(): | |
def __init__(self, name): | |
self.name = name | |
self.redis = redis.Redis() | |
self.payload = {} | |
def publish(self): | |
self.redis.publish("dashboard", json.dumps({"id": self.name, "payload": self.payload})) | |
def sleep(self, seconds): | |
time.sleep(seconds) | |
slave = DashboardSlave("rtinfo") | |
while True: | |
print("[+] rtinfo: fetching") | |
try: | |
response = requests.get(RTINFO_ENDPOINT) | |
slave.payload = response.json() | |
print("[+] rtinfo: %d hosts found" % len(slave.payload['rtinfo'])) | |
slave.publish() | |
except Exception as e: | |
print(e) | |
slave.sleep(1) |
This file contains hidden or 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
use redis::Commands; | |
use serde_json::json; | |
use std::{thread, time}; | |
const RTINFO_ENDPOINT: &str = "http://clea.maxux.net:8089/json"; | |
const REDIS_TARGET: &str = "redis://127.0.0.1/"; | |
fn redis_init() -> redis::Connection { | |
let client = redis::Client::open(REDIS_TARGET).unwrap(); | |
return client.get_connection().unwrap(); | |
} | |
fn rtinfo_fetch() -> std::string::String { | |
reqwest::get(RTINFO_ENDPOINT).unwrap().text().unwrap() | |
} | |
fn dashboard_encode(payload: std::string::String) -> std::string::String { | |
// parse json | |
let rtinfo: serde_json::Value = serde_json::from_str(&payload).unwrap(); | |
if rtinfo["rtinfo"].is_array() == false { | |
panic!("malformed json from rtinfo"); | |
} | |
let hosts = rtinfo["rtinfo"].as_array().unwrap(); | |
println!("[+] rtinfo: {} hosts found", hosts.len()); | |
// encapsulate json into dashboard frame | |
let converted = json!({ | |
"id": "rtinfo", | |
"payload": rtinfo | |
}); | |
// dumps json | |
return converted.to_string(); | |
} | |
fn redis_publish(con: &redis::Connection, payload: std::string::String) { | |
let _: () = con.publish("dashboard", payload).unwrap(); | |
} | |
fn main() { | |
let onesec = time::Duration::new(1, 0); | |
let client = redis_init(); | |
loop { | |
println!("[+] rtinfo: fetching"); | |
let payload = rtinfo_fetch(); | |
let converted = dashboard_encode(payload); | |
redis_publish(&client, converted); | |
// waiting next fetch | |
thread::sleep(onesec); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment