aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorScott Lawrence <scott+git@ineffectivetheory.com>2024-11-26 23:23:39 -0700
committerScott Lawrence <scott+git@ineffectivetheory.com>2024-11-26 23:23:39 -0700
commit0b8779738bf7e673173498f1f0c5610abeb757e7 (patch)
treee5a9c165789176c03e53957ab840f478d9263cc3
parentb5ad4356a26dbe9b28df2422cbd65fd182e6094a (diff)
downloadvaranus-0b8779738bf7e673173498f1f0c5610abeb757e7.tar.gz
varanus-0b8779738bf7e673173498f1f0c5610abeb757e7.tar.bz2
varanus-0b8779738bf7e673173498f1f0c5610abeb757e7.zip
Calling sysinfo
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs56
3 files changed, 61 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 064dbdc..1304ff4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -98,6 +98,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]]
+name = "cty"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
+
+[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -204,6 +210,7 @@ name = "varanus"
version = "0.1.0"
dependencies = [
"clap",
+ "cty",
"serde",
"serde_json",
]
diff --git a/Cargo.toml b/Cargo.toml
index e503a55..c9a0fe0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,5 +7,6 @@ edition = "2021"
[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
+cty = { version = "0.2.2" }
serde = { version = "1.0.203", features = ["derive"] }
serde_json = { version = "1.0" }
diff --git a/src/main.rs b/src/main.rs
index cd5df09..0848754 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,6 +18,16 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
+/* TODO
+ *
+ * Remove socket file when daemon exits.
+ *
+ * Accept command-line arguments to query only part of JSON
+ *
+ * Fix delay
+ *
+ */
+
use std::error;
use std::fs::remove_file;
use std::io::{Read,Write};
@@ -30,6 +40,36 @@ use std::time::{Duration,Instant,SystemTime};
use clap::Parser;
use serde::{Serialize,Deserialize};
+#[derive(Default)]
+#[repr(C)]
+struct Sysinfo {
+ uptime: cty::c_long,
+ loads: [cty::c_ulong; 3],
+ totalram: cty::c_ulong,
+ freeram: cty::c_ulong,
+ sharedram: cty::c_ulong,
+ bufferram: cty::c_ulong,
+ totalswap: cty::c_ulong,
+ freeswap: cty::c_ulong,
+ procs: cty::c_ushort,
+ totalhigh: cty::c_ulong,
+ freehigh: cty::c_ulong,
+ mem_unit: cty::c_uint,
+ /*char _f[20-2*sizeof(long)-sizeof(int)];*/
+}
+
+extern "C" {
+ fn sysinfo(si: *mut Sysinfo) -> cty::c_int;
+}
+
+impl Sysinfo {
+ fn new() -> Self {
+ let mut si = Sysinfo::default();
+ unsafe { sysinfo(&mut si) };
+ return si;
+ }
+}
+
#[derive(Parser)]
struct Cli {
#[arg(short='d', long)]
@@ -61,11 +101,18 @@ struct Process {
}
#[derive(Debug, Serialize, Deserialize)]
+struct ProcFS {
+}
+
+impl ProcFS {
+}
+
+#[derive(Debug, Serialize, Deserialize)]
struct Memory {
}
impl Memory {
- fn new() -> Self {
+ fn new(si: Sysinfo) -> Self {
return Memory{}
}
}
@@ -73,6 +120,7 @@ impl Memory {
#[derive(Debug, Serialize, Deserialize)]
struct State {
asof: SystemTime,
+ uptime: i64,
power: Power,
memory: Memory,
}
@@ -80,10 +128,12 @@ struct State {
impl State {
fn new() -> Self {
let asof = SystemTime::now();
+ let si = Sysinfo::new();
return State {
asof: asof,
+ uptime: si.uptime,
power: Power::new(),
- memory: Memory::new()
+ memory: Memory::new(si)
}
}
@@ -109,7 +159,7 @@ fn main() {
let args = Cli::parse();
if args.daemon {
let start = Instant::now();
- let delay_ms: u64 = args.delay * 1000;
+ let delay_ms: u64 = args.delay * 1000; // TODO not right
let mut cycle: u64 = 0;
let state_mutex = Arc::new(Mutex::new(State::new()));
let listen_thread = {