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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use sysinfo::{CpuExt, DiskExt, DiskType, NetworkExt, RefreshKind, System, SystemExt};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SystemInfo {
pub name: String,
pub uptime: u64,
pub boot_time: u64,
pub cpu_usage: f32,
pub total_memory: u64,
pub used_memory: u64,
pub received_bytes: u64,
pub transmitted_bytes: u64,
pub total_received_bytes: u64,
pub total_transmitted_bytes: u64,
pub ssd_disk_total: u64,
pub ssd_disk_avail: u64,
pub hdd_disk_total: u64,
pub hdd_disk_avail: u64,
}
impl Default for SystemInfo {
fn default() -> Self {
let sys = System::new();
let uptime = sys.uptime() * 1000 * 1000;
let boot_time = cyfs_base::unix_time_to_bucky_time(sys.boot_time() * 1000 * 1000);
Self {
name: "".to_owned(),
uptime,
boot_time,
cpu_usage: 0.0,
total_memory: 0,
used_memory: 0,
received_bytes: 0,
transmitted_bytes: 0,
total_received_bytes: 0,
total_transmitted_bytes: 0,
ssd_disk_total: 0,
ssd_disk_avail: 0,
hdd_disk_total: 0,
hdd_disk_avail: 0,
}
}
}
struct SystemInfoManagerInner {
running: bool,
last_access_time: Instant,
max_idle_time: Duration,
info_inner: SystemInfo,
handler: System,
}
impl SystemInfoManagerInner {
pub fn new() -> Self {
let r = RefreshKind::new()
.with_networks()
.with_networks_list()
.with_memory()
.with_cpu(sysinfo::CpuRefreshKind::new().with_cpu_usage())
.with_disks()
.with_disks_list();
let handler = System::new_with_specifics(r);
let mut info_inner = SystemInfo::default();
let s = System::new();
info_inner.name = match s.host_name() {
Some(name) => {
let trim = '\0';
if name.ends_with(trim) {
name[..name.len() - 1].to_owned()
} else {
name
}
}
None => "MY PC".to_owned(),
};
info!("os name: {:?}", info_inner.name);
Self {
running: false,
last_access_time: Instant::now(),
max_idle_time: Duration::from_secs(15),
info_inner,
handler,
}
}
pub fn check_idle(&mut self) {
let now = Instant::now();
if now - self.last_access_time >= self.max_idle_time {
info!(
"system info extend max idle duration, now will stop: last_access={:?}",
self.last_access_time
);
self.running = false;
}
}
pub fn refresh(&mut self) {
self.handler.refresh_all();
self.update_memory();
self.update_cpu();
self.update_network();
self.update_disks();
}
fn update_memory(&mut self) {
self.info_inner.total_memory = self.handler.total_memory();
self.info_inner.used_memory = self.handler.used_memory();
}
fn update_disks(&mut self) {
self.info_inner.hdd_disk_total = 0;
self.info_inner.hdd_disk_avail = 0;
self.info_inner.ssd_disk_total = 0;
self.info_inner.ssd_disk_avail = 0;
for disk in self.handler.disks() {
if disk.is_removable() {
continue;
}
match disk.type_() {
DiskType::HDD => {
self.info_inner.hdd_disk_total += disk.total_space();
self.info_inner.hdd_disk_avail += disk.available_space();
}
DiskType::SSD => {
self.info_inner.ssd_disk_total += disk.total_space();
self.info_inner.ssd_disk_avail += disk.available_space();
}
DiskType::Unknown(_) => {
}
}
}
}
fn update_cpu(&mut self) {
self.info_inner.cpu_usage = self.handler.global_cpu_info().cpu_usage();
}
fn update_network(&mut self) {
let networks = self.handler.networks();
let mut received_bytes = 0;
let mut transmitted_bytes = 0;
let mut total_received_bytes = 0;
let mut total_transmitted_bytes = 0;
for (interface_name, network) in networks {
if interface_name
.find("Hyper-V Virtual Ethernet Adapter")
.is_some()
{
continue;
}
if interface_name.find("VMware").is_some() {
continue;
}
if network.mac_address().is_unspecified() {
warn!("will ignore unspecified addr network interface: {}", interface_name);
continue;
}
received_bytes += network.received();
transmitted_bytes += network.transmitted();
total_received_bytes += network.total_received();
total_transmitted_bytes += network.total_transmitted();
}
self.info_inner.received_bytes = received_bytes;
self.info_inner.transmitted_bytes = transmitted_bytes;
self.info_inner.total_received_bytes = total_received_bytes;
self.info_inner.total_transmitted_bytes = total_transmitted_bytes;
}
}
#[derive(Clone)]
pub struct SystemInfoManager(Arc<Mutex<SystemInfoManagerInner>>);
impl SystemInfoManager {
fn new() -> Self {
Self(Arc::new(Mutex::new(SystemInfoManagerInner::new())))
}
pub async fn get_system_info(&self) -> SystemInfo {
if !self.0.lock().unwrap().running {
self.start();
async_std::task::sleep(Duration::from_secs(2)).await;
}
let mut item = self.0.lock().unwrap();
item.last_access_time = Instant::now();
item.info_inner.clone()
}
pub fn start(&self) {
let start = {
let mut item = self.0.lock().unwrap();
if !item.running {
item.running = true;
true
} else {
false
}
};
if !start {
info!("system info already in refreshing!");
return;
}
info!("start refresh system info...");
let this = self.clone();
async_std::task::spawn(async move { this.run_refresh().await });
}
async fn run_refresh(&self) {
loop {
{
let mut item = self.0.lock().unwrap();
item.check_idle();
if !item.running {
break;
}
item.refresh();
}
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
}
}
pub fn stop(&self) {
let mut item = self.0.lock().unwrap();
if item.running {
item.running = false;
info!("will stop refresh system info!");
} else {
info!("refresh system info stopped already!");
}
}
}
lazy_static::lazy_static! {
pub static ref SYSTEM_INFO_MANAGER: SystemInfoManager = SystemInfoManager::new();
}