1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use actix_web::body::BoxBody;
use actix_web::http::header::ContentType;
use actix_web::http::StatusCode;
use actix_web::{get, Error, HttpResponse, Responder};
use actix_web_lab::__reexports::serde_json;
use askama::Template;
use minify::html::minify;
use crate::stats::StatsResponse;
use crate::Stats;
extern crate minify;
extern crate systemstat;
use std::thread;
use std::time::Duration;
use systemstat::platform::PlatformImpl;
use systemstat::{Platform, System};
#[derive(Template)]
#[template(path = "index.html")]
struct StatsTemplate {
stats: Stats,
}
impl Responder for StatsResponse {
type Body = BoxBody;
fn respond_to(self, _req: &actix_web::HttpRequest) -> HttpResponse<Self::Body> {
let res_body = serde_json::to_string(&self).unwrap();
HttpResponse::Ok()
.content_type(ContentType::json())
.body(res_body)
}
}
pub async fn get_stats_from_linux(sys: PlatformImpl) -> Stats {
match sys.cpu_load_aggregate() {
Ok(cpu) => {
println!("\nMeasuring CPU load...");
thread::sleep(Duration::from_secs(1));
let cpu = cpu.done().unwrap();
println!(
"CPU load: {}% user, {}% nice, {}% system, {}% intr, {}% idle ",
cpu.user * 100.0,
cpu.nice * 100.0,
cpu.system * 100.0,
cpu.interrupt * 100.0,
cpu.idle * 100.0
);
let cpu_usage = format!(
"CPU load: {}% user, {}% nice, {}% system, {}% intr, {}% idle ",
cpu.user * 100.0,
cpu.nice * 100.0,
cpu.system * 100.0,
cpu.interrupt * 100.0,
cpu.idle * 100.0
);
let stats = Stats {
loadavg: "1.0".to_string(),
cpu_usage,
memory_usage: "3.0".to_string(),
};
return stats;
}
Err(x) => println!("\nCPU load: error: {}", x),
}
Stats {
loadavg: "Error".to_string(),
cpu_usage: "Error".to_string(),
memory_usage: "Error".to_string(),
}
}
#[get("")]
pub async fn index_page() -> Result<HttpResponse, Error> {
let sys = System::new();
match sys.cpu_temp() {
Ok(cpu_temp) => println!("\nCPU temp: {}", cpu_temp),
Err(x) => println!("\nCPU temp: {}", x),
}
let stats: Stats = get_stats_from_linux(sys).await;
println!("{:?}", stats);
let stats_html = StatsTemplate { stats };
let html_str = minify(&stats_html.render().unwrap());
Ok(HttpResponse::build(StatusCode::OK)
.content_type("text/html; charset=utf-8")
.body(html_str))
}
#[get("/v1/stats")]
async fn status_get_api() -> impl Responder {
let sys = System::new();
let stats: Stats = get_stats_from_linux(sys).await;
println!("{:?}", stats);
let has_error = stats.cpu_usage == "Error";
let stats_response = StatsResponse {
result: !has_error,
data: stats,
};
let response = serde_json::to_string(&stats_response).unwrap();
HttpResponse::Ok()
.content_type(ContentType::json())
.body(response)
}