Skip to content

Commit

Permalink
Add optional serde feature
Browse files Browse the repository at this point in the history
  • Loading branch information
adamflott committed Jun 27, 2023
1 parent 8f7857f commit 0d02bd7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
name = "statgrab"
authors = [ "Adam Flott <adam@adamflott.com>" ]
description = "Rust API to libstatgrab"
version = "0.9.6"
version = "0.9.7"
edition = "2021"
repository = "https://github.com/adamflott/statgrab-rs"
license = "MIT OR Apache-2.0"
rust-version = "1.60"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libstatgrab-sys = "~1.0.2"
chrono = "0.4.22"
chrono = { version = "0.4.22", features = ["serde"], default-features = true }
serde = { version = "1.0.164", optional = true }
serde_derive = { version = "1.0.125", optional = true }
serde_with = { version = "3.0.0", optional = true }

[features]
default = []
serde = ["dep:serde", "serde/derive", "serde_with/chrono_0_4"]
45 changes: 45 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use chrono::Duration;

#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};

#[cfg(feature = "serde")]
use serde_with::{serde_as, As, DurationSeconds};

pub struct SGHandle {}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -59,6 +65,7 @@ pub enum SGError {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum HostState {
UnknownConfiguration,
PhysicalHost,
Expand All @@ -67,6 +74,7 @@ pub enum HostState {
HardwareVirtualized,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct HostInfo {
pub os_name: String,
Expand All @@ -78,10 +86,13 @@ pub struct HostInfo {
pub host_state: HostState,
pub ncpus: u32,
pub maxcpus: u32,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub uptime: Duration,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct CPUStats {
pub user: u64,
Expand All @@ -97,16 +108,19 @@ pub struct CPUStats {
pub syscalls: u64,
pub interrupts: u64,
pub soft_interrupts: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum CPUPercentSource {
EntireCPUPercent,
LastDiffCPUPercent,
NewDiffCPUPercent,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct CPUPercents {
pub user: f64,
Expand All @@ -115,26 +129,32 @@ pub struct CPUPercents {
pub iowait: f64,
pub swap: f64,
pub nice: f64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub time_taken: Duration,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct MemStats {
pub total: u64,
pub free: u64,
pub used: u64,
pub cache: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct LoadStats {
pub min1: f64,
pub min5: f64,
pub min15: f64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct UserStats {
pub login_name: String,
Expand All @@ -143,19 +163,24 @@ pub struct UserStats {
pub device: String,
pub hostname: String,
pub pid: i32, // TODO check if there is some appropriate for pid_t,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub login_time: Duration,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct SwapStats {
pub total: u64,
pub used: u64,
pub free: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum FilesystemDeviceType {
Unknown,
Regular,
Expand All @@ -166,6 +191,7 @@ pub enum FilesystemDeviceType {
AllTypes,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct FilesystemStats {
pub device_name: String,
Expand All @@ -187,17 +213,21 @@ pub struct FilesystemStats {
pub free_blocks: u64,
pub used_blocks: u64,
pub avail_blocks: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct DiskIOStats {
pub disk_name: String,
pub read_bytes: u64,
pub write_bytes: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct NetworkIOStats {
pub interface_name: String,
Expand All @@ -208,40 +238,48 @@ pub struct NetworkIOStats {
pub ierrors: u64,
pub oerrors: u64,
pub collisions: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum IfaceDuplexType {
Full,
Half,
Unknown,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum IfaceUpdownType {
Down,
Up,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct NetworkIfaceStats {
pub interface_name: String,
pub speed: u64,
pub factor: u64,
pub duplex: IfaceDuplexType,
pub up: IfaceUpdownType,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct PageStats {
pub pages_pagein: u64,
pub pages_pageout: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum ProcessState {
Running,
Sleeping,
Expand All @@ -250,6 +288,7 @@ pub enum ProcessState {
Unknown,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct ProcessStats {
pub process_name: String,
Expand All @@ -267,19 +306,24 @@ pub struct ProcessStats {
pub involuntary_context_switches: u64,
pub proc_size: u64,
pub proc_resident: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub start_time: Duration,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub time_spent: Duration,
pub cpu_percent: f64,
pub nice: i32,
pub state: ProcessState,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum ProcessCountSource {
Entire,
Last,
}

#[cfg_attr(feature = "serde", serde_as, derive(Deserialize, Serialize))]
#[derive(Debug, Clone)]
pub struct ProcessCount {
pub total: u64,
Expand All @@ -288,6 +332,7 @@ pub struct ProcessCount {
pub stopped: u64,
pub zombie: u64,
pub unknown: u64,
#[cfg_attr(feature="serde", serde(with = "As::<DurationSeconds<f64>>"))]
pub systime: Duration,
}

Expand Down

0 comments on commit 0d02bd7

Please sign in to comment.