| 1 | + | use std::io::{self, BufRead}; |
| 2 | + | use std::process::Command; |
| 3 | + | |
1 | 4 | | use clap::Parser; |
2 | 5 | | use netstat2::*; |
3 | | - | use std::io::{self, BufRead}; |
4 | | - | use sysinfo::{PidExt, ProcessExt, SystemExt}; |
5 | | - | use sysinfo::{ProcessRefreshKind, RefreshKind, System}; |
| 6 | + | use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt}; |
6 | 7 | | use websocket::{ClientBuilder, Message, OwnedMessage}; |
7 | 8 | | |
8 | 9 | | mod protocol; |
| skipped 110 lines |
119 | 120 | | /// Execute a custom request payload, use '-' to read from stdin. |
120 | 121 | | #[clap(long)] |
121 | 122 | | custom_payload: Option<String>, |
| 123 | + | /// Variable polling time in milliseconds. |
| 124 | + | #[clap(long, default_value_t = 1000)] |
| 125 | + | poll_interval: u64, |
122 | 126 | | /// Poll variable by name at regular intervals. |
123 | 127 | | #[clap(long)] |
124 | 128 | | poll_variable: Option<String>, |
| 129 | + | /// If specified along with --poll-variable, the variable value will be passed to this command |
| 130 | + | #[clap(long)] |
| 131 | + | poll_command: Option<String>, |
125 | 132 | | } |
126 | 133 | | |
127 | 134 | | fn main() { |
| skipped 118 lines |
246 | 253 | | |
247 | 254 | | let resp = client.recv_message().unwrap(); |
248 | 255 | | if let OwnedMessage::Text(data) = resp { |
249 | | - | println!("{}", data); |
| 256 | + | let result: protocol::responses::ResultMessage = |
| 257 | + | serde_json::from_str(&data).unwrap(); |
| 258 | + | if let Some(ref poll_command) = args.poll_command { |
| 259 | + | println!("passing variable value to {} ...", poll_command); |
| 260 | + | Command::new(poll_command) |
| 261 | + | .arg(&result.result.result.value) |
| 262 | + | .spawn() |
| 263 | + | .unwrap() |
| 264 | + | .wait() |
| 265 | + | .unwrap(); |
| 266 | + | } else { |
| 267 | + | println!("{}", result.result.result.value); |
| 268 | + | } |
250 | 269 | | } else { |
251 | 270 | | println!("got non text message: {:?}", resp); |
252 | 271 | | } |
253 | 272 | | |
254 | | - | std::thread::sleep(std::time::Duration::from_secs(1)); |
| 273 | + | std::thread::sleep(std::time::Duration::from_millis(args.poll_interval)); |
255 | 274 | | } else { |
256 | 275 | | println!("{:?}", client.recv_message().unwrap()); |
257 | 276 | | } |
| skipped 3 lines |