diff options
| author | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-11-27 22:33:42 -0700 |
|---|---|---|
| committer | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-11-27 22:33:42 -0700 |
| commit | 3dd4c3c6f3c876a2128f0153794412d35a37f1bd (patch) | |
| tree | b6158e2693665144a7463ce56032436fdbb61e8e | |
| parent | 0b8779738bf7e673173498f1f0c5610abeb757e7 (diff) | |
| download | varanus-3dd4c3c6f3c876a2128f0153794412d35a37f1bd.tar.gz varanus-3dd4c3c6f3c876a2128f0153794412d35a37f1bd.tar.bz2 varanus-3dd4c3c6f3c876a2128f0153794412d35a37f1bd.zip | |
Minimal functionality now in place
| -rw-r--r-- | src/forecast.rs | 0 | ||||
| -rw-r--r-- | src/main.rs | 88 | ||||
| -rw-r--r-- | varanus.1 | 30 |
3 files changed, 91 insertions, 27 deletions
diff --git a/src/forecast.rs b/src/forecast.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/forecast.rs diff --git a/src/main.rs b/src/main.rs index 0848754..fbb23e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,9 +22,7 @@ * * Remove socket file when daemon exits. * - * Accept command-line arguments to query only part of JSON - * - * Fix delay + * Accept command-line arguments to query only part of JSON. (Maybe also a formatting string?) * */ @@ -76,8 +74,8 @@ struct Cli { daemon: bool, #[arg(short='v', long)] verbose: bool, - #[arg(short='D', long, default_value="100")] - delay: u64, + #[arg(short='D', long, default_value="2.0")] + delay: f64, #[arg(short='s', long, default_value="varanus.sock")] socket: String, } @@ -107,13 +105,22 @@ struct ProcFS { impl ProcFS { } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Default)] struct Memory { + total: u64, + free: u64, } impl Memory { fn new(si: Sysinfo) -> Self { - return Memory{} + let mut mem = Memory::default(); + mem.update(si); + return mem; + } + + fn update(&mut self, si: Sysinfo) { + self.total = si.totalram; + self.free = si.freeram; } } @@ -138,7 +145,10 @@ impl State { } fn update(&mut self) { + let si = Sysinfo::new(); self.asof = SystemTime::now(); + self.uptime = si.uptime; + self.memory.update(si); } } @@ -155,32 +165,56 @@ fn get_state(sockfile: String) -> Result<State> { Ok(serde_json::from_str::<State>(str::from_utf8(&buf)?)?) } +struct Listener { + file: String, + state: Arc<Mutex<State>>, +} + +impl Listener { + fn new(socket: String, state_mutex: &Arc<Mutex<State>>) -> Self { + return Listener { + file: socket, + state: Arc::clone(state_mutex), + } + } + + fn listen(&self) { + let state_mutex = Arc::clone(&self.state); + let sfn = self.file.clone(); + spawn(move || { + let _ = remove_file(&sfn); + let listener = UnixListener::bind(sfn).unwrap(); + loop { + match listener.accept() { + Ok((mut socket,accept)) => { + let json = { + let state = state_mutex.lock().unwrap(); + serde_json::to_string(&*state).unwrap() + }; + socket.write_all(json.as_bytes()).unwrap(); + }, + Err(e) => println!("error: {:?}", e) + } + } + }); + } +} + +impl Drop for Listener { + fn drop(&mut self) { + let _ = remove_file(&self.file); + } +} + fn main() { let args = Cli::parse(); if args.daemon { let start = Instant::now(); - let delay_ms: u64 = args.delay * 1000; // TODO not right + let delay_ms: u64 = (args.delay * 1000.0) as u64; let mut cycle: u64 = 0; let state_mutex = Arc::new(Mutex::new(State::new())); - let listen_thread = { - let state_mutex = Arc::clone(&state_mutex); - spawn(move || { - let _ = remove_file(&args.socket); - let listener = UnixListener::bind(args.socket).unwrap(); - loop { - match listener.accept() { - Ok((mut socket,accept)) => { - let json = { - let state = state_mutex.lock().unwrap(); - serde_json::to_string(&*state).unwrap() - }; - socket.write_all(json.as_bytes()).unwrap(); - }, - Err(e) => println!("error: {:?}", e) - } - } - }); - }; + let listener = Listener::new(args.socket, &state_mutex); + listener.listen(); loop { if args.verbose { println!("{:?} elapsed; updating state...", start.elapsed()); diff --git a/varanus.1 b/varanus.1 new file mode 100644 index 0000000..712b8fd --- /dev/null +++ b/varanus.1 @@ -0,0 +1,30 @@ +.TH VARANUS 1 +.SH NAME +varanus \- description +.SH SYNOPSIS +.SH DESCRIPTION +.SH OPTIONS +.SH AUTHOR +Written by Scott Lawrence (scott [at] ineffectivetheory.com). +.SH BUGS +Report bugs to the author. +.SH COPYRIGHT +Copyright (c) 2024; released under the MIT license: + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. |
