-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathvirt.rs
170 lines (150 loc) · 5.64 KB
/
virt.rs
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
//! Local virtual machine state.
//!
//! # Configuration
//!
//! Key | Values | Default
//! ----|--------|--------
//! uri | URI of the hypervisor | qemu:///system
//! `interval` | Update interval, in seconds. | `5`
//! `format` | A string to customise the output of this block. See below for available placeholders. | `" $icon $running.eng(w:1) "`
//! `uri` | The path to the virtualization domain.
//!
//! Key | Value | Type | Unit
//! ----------|----------------------------------------|--------|-----
//! `active` | Virtual machines running on the host | Number | -
//! `inactive` | Virtual machines stopped on the host | Number | -
//! `total` | Virtual machines in total the host | Number | -
//! `memory_active | Total memory used by running virtual machines | Number | -
//! `memory_max` | Total memory used by virtual machines of any state | Number | -
//! `cpu_active` | Total cpu cores used by the virtual machines | Number | -
//! `cpu_inactive` | Total cpu used by stopped virtual machines | Number | -
//!
//! # Example
//!
//! ```toml
//! [[block]]
//! block = "virt"
//! uri = "qemu:///system"
//! interval = 2
//! format = " $icon $active/$total ($memory $active@$cpu_active) "
//! ```
//!
use super::prelude::*;
use virt::connect::Connect;
use virt::error::Error;
use virt::sys;
#[derive(Deserialize, Debug, SmartDefault)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
#[default("qemu:///system".into())]
pub uri: ShellString,
pub format: FormatConfig,
#[default(5.into())]
pub interval: Seconds,
}
pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let format = config.format.with_default("$icon $active/$total")?;
let flags: sys::virConnectListAllDomainsFlags = sys::VIR_CONNECT_LIST_DOMAINS_ACTIVE | sys::VIR_CONNECT_LIST_DOMAINS_INACTIVE;
let uri: &str = "qemu:///system";
loop {
println!("Connecting to hypervisor '{}'", &uri);
let mut con = match Connect::open(uri) {
Ok(c) => c,
Err(e) => panic!("No connection to hypervisor: {}", e),
};
let info: LibvirtInfo = LibvirtInfo::new(&mut con, flags)
.await
.unwrap();
let mut widget = Widget::new().with_format(format.clone());
widget.set_values(map!(
// "icon" => Value::icon("".to_string()),
// "total" => Value::number(virt_active_doms + virt_inactive_doms),
"running" => Value::number(info.active),
"stopped" => Value::number(info.inactive),
// "paused" => Value::number(virt_inactive_domains),
"total" => Value::number(info.total),
));
api.set_widget(widget)?;
disconnect(&mut con);
select! {
_ = sleep(config.interval.0) => (),
_ = api.wait_for_update_request() => (),
}
}
}
#[derive(Deserialize, Debug, SmartDefault)]
#[serde(deny_unknown_fields, default)]
struct LibvirtInfo {
#[serde(rename = "VMActive")]
active: u32,
#[serde(rename = "VMInactive")]
inactive: u32,
#[serde(rename = "VMTotal")]
total: u32,
#[serde(rename = "MemoryActive")]
memory_active: u32,
#[serde(rename = "MemoryInactive")]
memory_max: u32,
#[serde(rename = "CPUActive")]
cpu_active: u32,
#[serde(rename = "CPUInactive")]
cpu_inactive: u32,
}
fn disconnect(con: &mut Connect) {
if let Err(e) = con.close() {
panic!("Failed to disconnect from hypervisor: {}", e);
}
println!("Disconnected from hypervisor");
}
impl LibvirtInfo {
async fn new(con: &mut Connect, flags: sys::virConnectListAllDomainsFlags) -> Result<Self, Error> {
println!("Connected to hypervisor");
match con.get_uri() {
Ok(u) => println!("Connected to hypervisor at '{}'", u),
Err(e) => {
disconnect(con);
panic!("Failed to get URI for hypervisor connection: {}", e);
}
};
println!("Getting information about domains");
if let Ok(virt_active_domains) = con.num_of_domains() {
if let Ok(virt_inactive_domains) = con.num_of_defined_domains() {
if let Ok(domains) = con.list_all_domains(flags) {
let mut info = LibvirtInfo {
active: virt_active_domains,
inactive: virt_inactive_domains,
total: virt_active_domains + virt_inactive_domains,
memory_active: 0,
memory_max: 0,
cpu_active: 0,
cpu_inactive: 0,
};
for domain in domains {
if let Ok(domain_info) = domain.get_info() {
info.memory_max += domain_info.max_mem as u32;
if domain.is_active().unwrap_or(false) {
info.memory_active += domain_info.memory as u32;
info.cpu_active += domain_info.nr_virt_cpu;
} else {
info.cpu_inactive += domain_info.nr_virt_cpu;
}
}
}
Ok(info)
}
else {
disconnect(con);
Err(Error::last_error())
}
}
else {
disconnect(con);
Err(Error::last_error())
}
}
else {
disconnect(con);
Err(Error::last_error())
}
}
}