remove snafu
This commit is contained in:
parent
9e70b37820
commit
fed9a73aa0
|
@ -33,8 +33,8 @@ jsonrpc-core = "11"
|
|||
jsonrpc-http-server = "11"
|
||||
log = "0.4"
|
||||
miniserde = "0.1.15"
|
||||
probes = "0.3"
|
||||
snafu = "0.4"
|
||||
#probes = "0.3"
|
||||
probes = "0.4.1"
|
||||
systemstat = "0.1"
|
||||
|
||||
[dev-dependencies]
|
||||
|
|
|
@ -1,30 +1,43 @@
|
|||
use std::{error, io};
|
||||
use std::io;
|
||||
|
||||
use jsonrpc_core::{types::error::Error, ErrorCode};
|
||||
use probes::ProbeError;
|
||||
use snafu::Snafu;
|
||||
|
||||
pub type BoxError = Box<dyn error::Error>;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
#[snafu(visibility(pub(crate)))]
|
||||
#[derive(Debug)]
|
||||
pub enum StatError {
|
||||
#[snafu(display("Failed to retrieve CPU statistics: {}", source))]
|
||||
ReadCpuStat { source: ProbeError },
|
||||
|
||||
#[snafu(display("Failed to retrieve disk usage statistics: {}", source))]
|
||||
ReadDiskUsage { source: ProbeError },
|
||||
|
||||
#[snafu(display("Failed to retrieve load average statistics: {}", source))]
|
||||
ReadLoadAvg { source: ProbeError },
|
||||
|
||||
#[snafu(display("Failed to retrieve memory statistics: {}", source))]
|
||||
ReadMemStat { source: ProbeError },
|
||||
|
||||
#[snafu(display("Failed to retrieve system uptime: {}", source))]
|
||||
ReadUptime { source: io::Error },
|
||||
}
|
||||
|
||||
impl std::fmt::Display for StatError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match *self {
|
||||
StatError::ReadCpuStat { ref source } => {
|
||||
write!(f, "Failed to retrieve CPU statistics: {}", source)
|
||||
}
|
||||
StatError::ReadDiskUsage { ref source } => {
|
||||
write!(f, "Failed to retrieve disk usage statistics: {}", source)
|
||||
}
|
||||
StatError::ReadLoadAvg { ref source } => {
|
||||
write!(f, "Failed to retrieve load average statistics: {}", source)
|
||||
}
|
||||
StatError::ReadMemStat { ref source } => {
|
||||
write!(f, "Failed to retrieve memory statistics: {}", source)
|
||||
}
|
||||
StatError::ReadUptime { ref source } => {
|
||||
write!(f, "Failed to retrieve system uptime: {}", source)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StatError> for Error {
|
||||
fn from(err: StatError) -> Self {
|
||||
match &err {
|
||||
|
|
|
@ -8,9 +8,9 @@ use jsonrpc_core::{IoHandler, Value};
|
|||
use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation, ServerBuilder};
|
||||
use log::info;
|
||||
|
||||
use crate::error::BoxError;
|
||||
use crate::error::StatError;
|
||||
|
||||
pub fn run() -> Result<(), BoxError> {
|
||||
pub fn run() -> Result<(), StatError> {
|
||||
info!("Starting up.");
|
||||
|
||||
info!("Creating JSON-RPC I/O handler.");
|
||||
|
|
|
@ -2,14 +2,13 @@ use std::result::Result;
|
|||
|
||||
use miniserde::json;
|
||||
use probes::{cpu, disk_usage, load, memory};
|
||||
use snafu::ResultExt;
|
||||
use systemstat::{Platform, System};
|
||||
|
||||
use crate::error::*;
|
||||
use crate::error::StatError;
|
||||
use crate::structs::{CpuStat, CpuStatPercentages, DiskUsage, LoadAverage, MemStat};
|
||||
|
||||
pub fn cpu_stats() -> Result<String, StatError> {
|
||||
let cpu_stats = cpu::proc::read().context(ReadCpuStat)?;
|
||||
let cpu_stats = cpu::proc::read().map_err(|source| StatError::ReadCpuStat { source })?;
|
||||
let s = cpu_stats.stat;
|
||||
let cpu = CpuStat {
|
||||
user: s.user,
|
||||
|
@ -23,7 +22,7 @@ pub fn cpu_stats() -> Result<String, StatError> {
|
|||
}
|
||||
|
||||
pub fn cpu_stats_percent() -> Result<String, StatError> {
|
||||
let cpu_stats = cpu::proc::read().context(ReadCpuStat)?;
|
||||
let cpu_stats = cpu::proc::read().map_err(|source| StatError::ReadCpuStat { source })?;
|
||||
let s = cpu_stats.stat.in_percentages();
|
||||
let cpu = CpuStatPercentages {
|
||||
user: s.user,
|
||||
|
@ -37,7 +36,7 @@ pub fn cpu_stats_percent() -> Result<String, StatError> {
|
|||
}
|
||||
|
||||
pub fn disk_usage() -> Result<String, StatError> {
|
||||
let disks = disk_usage::read().context(ReadDiskUsage)?;
|
||||
let disks = disk_usage::read().map_err(|source| StatError::ReadDiskUsage { source })?;
|
||||
let mut disk_usages = Vec::new();
|
||||
for d in disks {
|
||||
let disk = DiskUsage {
|
||||
|
@ -56,7 +55,7 @@ pub fn disk_usage() -> Result<String, StatError> {
|
|||
}
|
||||
|
||||
pub fn load_average() -> Result<String, StatError> {
|
||||
let l = load::read().context(ReadLoadAvg)?;
|
||||
let l = load::read().map_err(|source| StatError::ReadLoadAvg { source })?;
|
||||
let load_avg = LoadAverage {
|
||||
one: l.one,
|
||||
five: l.five,
|
||||
|
@ -68,7 +67,7 @@ pub fn load_average() -> Result<String, StatError> {
|
|||
}
|
||||
|
||||
pub fn mem_stats() -> Result<String, StatError> {
|
||||
let m = memory::read().context(ReadMemStat)?;
|
||||
let m = memory::read().map_err(|source| StatError::ReadMemStat { source })?;
|
||||
let mem = MemStat {
|
||||
total: m.total(),
|
||||
free: m.free(),
|
||||
|
@ -81,7 +80,9 @@ pub fn mem_stats() -> Result<String, StatError> {
|
|||
|
||||
pub fn uptime() -> Result<String, StatError> {
|
||||
let sys = System::new();
|
||||
let uptime = sys.uptime().context(ReadUptime)?;
|
||||
let uptime = sys
|
||||
.uptime()
|
||||
.map_err(|source| StatError::ReadUptime { source })?;
|
||||
let uptime_secs = uptime.as_secs();
|
||||
let json_uptime = json::to_string(&uptime_secs);
|
||||
|
||||
|
|
Loading…
Reference in New Issue