Skip to content

Commit 8053ab0

Browse files
committed
print raw metrics as json
1 parent 8161064 commit 8053ab0

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ sudo cp target/release/macmon /usr/local/bin
6565
Usage: macmon [OPTIONS] [COMMAND]
6666

6767
Commands:
68-
raw Print raw metrics data instead of TUI
69-
help Print this message or the help of the given subcommand(s)
68+
raw Print metrics in JSON format – can be used for piping
69+
debug Print debug information
70+
help Print this message or the help of the given subcommand(s)
7071

7172
Options:
7273
-i, --interval <INTERVAL> Update interval in milliseconds [default: 1000]

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use std::error::Error;
1111

1212
#[derive(Debug, Subcommand)]
1313
enum Commands {
14-
/// Print raw metrics data instead of TUI
14+
/// Print metrics in JSON format – can be used for piping
1515
Raw,
1616

17-
/// Print raw metrics data instead of TUI
17+
/// Print debug information
1818
Debug,
1919
}
2020

@@ -40,7 +40,9 @@ fn main() -> Result<(), Box<dyn Error>> {
4040
let mut sampler = Sampler::new()?;
4141

4242
loop {
43-
println!("{:?}", sampler.get_metrics(msec)?);
43+
let doc = sampler.get_metrics(msec)?;
44+
let doc = serde_json::to_string(&doc)?;
45+
println!("{}", doc);
4446
}
4547
}
4648
Some(Commands::Debug) => debug::print_debug()?,

src/metrics.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core_foundation::dictionary::CFDictionaryRef;
2+
use serde::Serialize;
23

34
use crate::sources::{
45
cfio_get_residencies, cfio_watts, libc_ram, libc_swap, IOHIDSensors, IOReport, SocInfo, SMC,
@@ -12,21 +13,21 @@ const GPU_FREQ_DICE_SUBG: &str = "GPU Performance States";
1213

1314
// MARK: Structs
1415

15-
#[derive(Debug, Default)]
16+
#[derive(Debug, Default, Serialize)]
1617
pub struct TempMetrics {
1718
pub cpu_temp_avg: f32, // Celsius
1819
pub gpu_temp_avg: f32, // Celsius
1920
}
2021

21-
#[derive(Debug, Default)]
22+
#[derive(Debug, Default, Serialize)]
2223
pub struct MemMetrics {
2324
pub ram_total: u64, // bytes
2425
pub ram_usage: u64, // bytes
2526
pub swap_total: u64, // bytes
2627
pub swap_usage: u64, // bytes
2728
}
2829

29-
#[derive(Debug, Default)]
30+
#[derive(Debug, Default, Serialize)]
3031
pub struct Metrics {
3132
pub temp: TempMetrics,
3233
pub memory: MemMetrics,

0 commit comments

Comments
 (0)