rsys/
api.rs

1#[cfg(target_os = "linux")]
2use crate::linux::{
3    cpu::{Cores, Processor},
4    mem::Memory,
5    misc::MountPoints,
6    net::Interfaces,
7    ps::{Process, Processes},
8    storage::{DeviceMapper, MultipleDeviceStorage, ScsiCdrom, StorageDevice},
9    Linux,
10};
11#[cfg(target_os = "macos")]
12use crate::macos::Macos;
13#[cfg(target_os = "windows")]
14use crate::windows::Windows;
15
16use crate::{
17    os::{OsImpl, OsImplExt},
18    Result,
19};
20use std::boxed::Box;
21use std::env;
22
23/// Main interface that allows for os-agnostic api.
24pub struct Rsys(Box<dyn OsImpl>, Box<dyn OsImplExt>);
25
26#[cfg(target_os = "linux")]
27impl Default for Rsys {
28    fn default() -> Self {
29        Self(
30            Box::new(Linux::new()) as Box<dyn OsImpl>,
31            Box::new(Linux::new()) as Box<dyn OsImplExt>,
32        )
33    }
34}
35#[cfg(target_os = "macos")]
36impl Default for Rsys {
37    fn default() -> Self {
38        Self(
39            Box::new(Macos::new()) as Box<dyn OsImpl>,
40            Box::new(Macos::new()) as Box<dyn OsImplExt>,
41        )
42    }
43}
44#[cfg(target_os = "windows")]
45impl Default for Rsys {
46    fn default() -> Self {
47        Self(
48            Box::new(Windows::new()) as Box<dyn OsImpl>,
49            Box::new(Windows::new()) as Box<dyn OsImplExt>,
50        )
51    }
52}
53
54impl Rsys {
55    /// Creates a new instance of Rsys
56    pub fn new() -> Self {
57        Self::default()
58    }
59
60    /// Returns a hostname.
61    ///   * **linux**
62    ///     * by reading `/proc/sys/kernel/hostname`
63    ///   * **macos**
64    ///     * by calling `sysctl("kern.hostname")`
65    ///   * **windows**
66    ///     * by calling win32 api `GetComputerNameA`
67    pub fn hostname(&self) -> Result<String> {
68        self.0.hostname()
69    }
70
71    /// Returns time since boot in seconds.
72    ///   * **linux**
73    ///     * by reading `/proc/uptime`
74    ///   * **macos**
75    ///     * by calling `sysctl("kern.boottime")`
76    ///   * **windows**
77    ///     * by calling win32 api `GetTickCount64`
78    pub fn uptime(&self) -> Result<u64> {
79        self.0.uptime()
80    }
81
82    /// Returns operating system. Reexported `env::consts::OS` for convenience.
83    pub fn os(&self) -> String {
84        env::consts::OS.to_string()
85    }
86
87    /// Returns cpu architecture.
88    ///   * **linux** and **macos**
89    ///     * by calling `uname -m`
90    ///   * **windows**
91    ///     * by calling win32 api `GetSystemInfo`
92    pub fn arch(&self) -> Result<String> {
93        self.0.arch()
94    }
95    /// Returns a cpu model name.
96    ///   * **linux**
97    ///     * by reading `/proc/cpuinfo`
98    ///   * **macos**
99    ///     * by calling `sysctl("machdep.cpu.brand_string")`
100    ///   ...
101    pub fn cpu(&self) -> Result<String> {
102        self.0.cpu()
103    }
104
105    /// Returns clock speed of cpu.
106    ///   * **linux**
107    ///     * by reading `/proc/cpuinfo`
108    ///   * **macos**
109    ///     * by calling `sysctl("hw.cpufrequency")`
110    ///   ...
111    pub fn cpu_clock(&self) -> Result<f32> {
112        self.0.cpu_clock()
113    }
114
115    /// Returns cpu cores.
116    ///   * **linux**
117    ///     * by reading `/proc/cpuinfo`
118    ///   * **macos**
119    ///     * by calling `sysctl("hw.physicalcpu")`
120    ///   * **windows**
121    ///     * by determining if cpu is hyperthreaded and calculating from logical cores.
122    pub fn cpu_cores(&self) -> Result<u16> {
123        self.0.cpu_cores()
124    }
125
126    /// Returns logical cpu cores.
127    ///   * **linux**
128    ///     * by reading `/proc/cpuinfo`
129    ///   * **macos**
130    ///     * by calling `sysctl("hw.logicalcpu")`
131    ///   * **windows**
132    ///     * by calling win32 api `GetSystemInfo`
133    pub fn logical_cores(&self) -> Result<u16> {
134        self.0.logical_cores()
135    }
136
137    /// Returns total ram memory.
138    ///   * **linux**
139    ///     * by reading `/proc/meminfo`
140    ///   * **macos**
141    ///     * by calling `sysctl("hw.memsize")`
142    ///   * **windows**
143    ///     * by calling win32 api `GlobalMemoryStatusEx`
144    pub fn memory_total(&self) -> Result<usize> {
145        self.0.memory_total()
146    }
147
148    /// Returns free ram memory.
149    ///   * **linux**
150    ///     * by reading `/proc/meminfo`
151    ///   ...
152    pub fn memory_free(&self) -> Result<usize> {
153        self.0.memory_free()
154    }
155
156    /// Returns total swap size.
157    ///   * **linux**
158    ///     * by reading `/proc/meminfo`
159    ///   * **macos**
160    ///     * by calling `sysctl("hw.swapusage")`
161    ///   * **windows**
162    ///     * by calling win32 api `GlobalMemoryStatusEx`
163    pub fn swap_total(&self) -> Result<usize> {
164        self.0.swap_total()
165    }
166
167    /// Returns free swap size.
168    ///   * **linux**
169    ///     * by reading `/proc/meminfo`
170    ///   * **macos**
171    ///     * by calling `sysctl("hw.swapusage")`
172    ///   * **windows**
173    ///     * by calling win32 api `GlobalMemoryStatusEx`
174    pub fn swap_free(&self) -> Result<usize> {
175        self.0.swap_free()
176    }
177
178    /// Returns a default internet interface.
179    ///   * **linux**
180    ///     * by calling `route` and determining default route
181    ///   * **macos**
182    ///     * by calling `route get default`
183    ///   ...
184    pub fn default_iface(&self) -> Result<String> {
185        self.0.default_iface()
186    }
187
188    /// Returns IP address version 4 of interface iface.
189    ///   * **linux**
190    ///     * by calling `ip address show <iface>`
191    ///   * **macos**
192    ///     * by calling `ipconfig getifaddr <iface>`
193    ///   ...
194    pub fn ipv4(&self, iface: &str) -> Result<String> {
195        self.0.ipv4(iface)
196    }
197
198    /// Returns IP address version 6 of interface iface.
199    pub fn ipv6(&self, iface: &str) -> Result<String> {
200        self.0.ipv6(iface)
201    }
202
203    /// Returns a MAC address of interface iface.
204    ///   * **linux**
205    ///     * by calling `ip address show <iface>`
206    ///   ...
207    pub fn mac(&self, iface: &str) -> Result<String> {
208        self.0.mac(iface)
209    }
210
211    /// Returns a vector of names of network interfaces that are available.
212    ///   * **linux**
213    ///     * by calling `ip address show`
214    ///   ...
215    pub fn interfaces(&self) -> Result<Vec<String>> {
216        self.0.interfaces()
217    }
218
219    /// Returns a domain name.
220    ///   * **linux**
221    ///     * by reading `/proc/sys/kernel/domainname`
222    ///   * **windows**
223    ///     * by calling win32 api `NetWkstaGetInfo`
224    ///   ...
225    pub fn domainname(&self) -> Result<String> {
226        self.0.domainname()
227    }
228
229    #[cfg(target_os = "linux")]
230    /// Parses a StorageDevice object from system. If the provided name
231    /// doesn't start with `sd` returns an error.
232    pub fn stat_block_device(&self, name: &str) -> Result<StorageDevice> {
233        self.1.stat_block_device(name)
234    }
235
236    #[cfg(target_os = "linux")]
237    /// Parses a DeviceMapper object from system. If the provided name
238    /// doesn't start with `dm` returns an error.
239    pub fn stat_scsi_cdrom(&self, name: &str) -> Result<ScsiCdrom> {
240        self.1.stat_scsi_cdrom(name)
241    }
242
243    #[cfg(target_os = "linux")]
244    /// Parses a ScsiCdrom object from system. If the provided name
245    /// doesn't start with `sr` returns an error.
246    pub fn stat_device_mapper(&self, name: &str) -> Result<DeviceMapper> {
247        self.1.stat_device_mapper(name)
248    }
249
250    #[cfg(target_os = "linux")]
251    /// Parses a MultipleDeviceStorage object from system. If the provided name
252    /// doesn't start with `md` returns an error.
253    pub fn stat_multiple_device_storage(&self, name: &str) -> Result<MultipleDeviceStorage> {
254        self.1.stat_multiple_device_storage(name)
255    }
256
257    #[cfg(target_os = "linux")]
258    /// Returns block size of device in bytes
259    pub fn block_size(&self, name: &str) -> Result<i64> {
260        self.1.block_size(name)
261    }
262
263    #[cfg(target_os = "linux")]
264    /// Returns detailed information about memory
265    pub fn memory(&self) -> Result<Memory> {
266        self.1.memory()
267    }
268
269    #[cfg(target_os = "linux")]
270    /// Returns detailed Process information parsed from /proc/[pid]/stat
271    pub fn stat_process(&self, pid: i32) -> Result<Process> {
272        self.1.stat_process(pid)
273    }
274    #[cfg(target_os = "linux")]
275    /// Returns a list of pids read from /proc
276    pub fn pids(&self) -> Result<Vec<i32>> {
277        self.1.pids()
278    }
279    #[cfg(target_os = "linux")]
280    /// Returns all processes currently seen in /proc parsed as Processes
281    pub fn processes(&self) -> Result<Processes> {
282        self.1.processes()
283    }
284    #[cfg(target_os = "linux")]
285    /// Returns kernel version of host os.
286    pub fn kernel_version(&self) -> Result<String> {
287        self.1.kernel_version()
288    }
289    #[cfg(target_os = "linux")]
290    /// Returns MountPoints read from /proc/mounts
291    pub fn mounts(&self) -> Result<MountPoints> {
292        self.1.mounts()
293    }
294    #[cfg(target_os = "linux")]
295    /// Returns virtual Cores of host cpu
296    pub fn cores(&self) -> Result<Cores> {
297        self.1.cores()
298    }
299    #[cfg(target_os = "linux")]
300    /// Returns a Processor object containing gathered information about host cpu
301    pub fn processor(&self) -> Result<Processor> {
302        self.1.processor()
303    }
304    #[cfg(target_os = "linux")]
305    /// Returns network interfaces on host os
306    pub fn ifaces(&self) -> Result<Interfaces> {
307        self.1.ifaces()
308    }
309}