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
286
287
288
289
290
291
292
293
#[cfg(target_os = "linux")]
use crate::linux::{
    Cores, DeviceMapper, Linux, Memory, MountPoints, MultipleDeviceStorage, Process, Processes, Processor, ScsiCdrom,
    StorageDevice,
};
#[cfg(target_os = "macos")]
use crate::macos::Macos;
#[cfg(target_os = "windows")]
use crate::windows::Windows;

use crate::{
    os::{OsImpl, OsImplExt},
    Result,
};
use std::boxed::Box;
use std::env;

/// Main interface that allows for os-agnostic api.
pub struct Rsys(Box<dyn OsImpl>, Box<dyn OsImplExt>);

impl Rsys {
    #[cfg(target_os = "linux")]
    /// Creates a new instance of Rsys
    pub fn new() -> Self {
        Self(
            Box::new(Linux::new()) as Box<dyn OsImpl>,
            Box::new(Linux::new()) as Box<dyn OsImplExt>,
        )
    }

    #[cfg(target_os = "macos")]
    /// Creates a new instance of Rsys
    pub fn new() -> Self {
        Self(
            Box::new(Macos::new()) as Box<dyn OsImpl>,
            Box::new(Macos::new()) as Box<dyn OsImplExt>,
        )
    }

    #[cfg(target_os = "windows")]
    /// Creates a new instance of Rsys
    pub fn new() -> Self {
        Self(
            Box::new(Windows::new()) as Box<dyn OsImpl>,
            Box::new(Windows::new()) as Box<dyn OsImplExt>,
        )
    }

    /// Returns a hostname.
    ///   * **linux**
    ///     * by reading `/proc/sys/kernel/hostname`
    ///   * **macos**
    ///     * by calling `sysctl("kern.hostname")`
    ///   * **windows**
    ///     * by calling win32 api `GetComputerNameA`
    pub fn hostname(&self) -> Result<String> {
        self.0.hostname()
    }

    /// Returns time since boot in seconds.
    ///   * **linux**
    ///     * by reading `/proc/uptime`
    ///   * **macos**
    ///     * by calling `sysctl("kern.boottime")`
    ///   * **windows**
    ///     * by calling win32 api `GetTickCount64`
    pub fn uptime(&self) -> Result<u64> {
        self.0.uptime()
    }

    /// Returns operating system. Reexported `env::consts::OS` for convenience.
    pub fn os(&self) -> String {
        env::consts::OS.to_string()
    }

    /// Returns cpu architecture.
    ///   * **linux** and **macos**
    ///     * by calling `uname -m`
    ///   * **windows**
    ///     * by calling win32 api `GetSystemInfo`
    pub fn arch(&self) -> Result<String> {
        self.0.arch()
    }
    /// Returns a cpu model name.
    ///   * **linux**
    ///     * by reading `/proc/cpuinfo`
    ///   * **macos**
    ///     * by calling `sysctl("machdep.cpu.brand_string")`
    ///   ...
    pub fn cpu(&self) -> Result<String> {
        self.0.cpu()
    }

    /// Returns clock speed of cpu.
    ///   * **linux**
    ///     * by reading `/proc/cpuinfo`
    ///   * **macos**
    ///     * by calling `sysctl("hw.cpufrequency")`
    ///   ...
    pub fn cpu_clock(&self) -> Result<f32> {
        self.0.cpu_clock()
    }

    /// Returns cpu cores.
    ///   * **linux**
    ///     * by reading `/proc/cpuinfo`
    ///   * **macos**
    ///     * by calling `sysctl("hw.physicalcpu")`
    ///   * **windows**
    ///     * by determining if cpu is hyperthreaded and calculating from logical cores.
    pub fn cpu_cores(&self) -> Result<u16> {
        self.0.cpu_cores()
    }

    /// Returns logical cpu cores.
    ///   * **linux**
    ///     * by reading `/proc/cpuinfo`
    ///   * **macos**
    ///     * by calling `sysctl("hw.logicalcpu")`
    ///   * **windows**
    ///     * by calling win32 api `GetSystemInfo`
    pub fn logical_cores(&self) -> Result<u16> {
        self.0.logical_cores()
    }

    /// Returns total ram memory.
    ///   * **linux**
    ///     * by reading `/proc/meminfo`
    ///   * **macos**
    ///     * by calling `sysctl("hw.memsize")`
    ///   * **windows**
    ///     * by calling win32 api `GlobalMemoryStatusEx`
    pub fn memory_total(&self) -> Result<usize> {
        self.0.memory_total()
    }

    /// Returns free ram memory.
    ///   * **linux**
    ///     * by reading `/proc/meminfo`
    ///   ...
    pub fn memory_free(&self) -> Result<usize> {
        self.0.memory_free()
    }

    /// Returns total swap size.
    ///   * **linux**
    ///     * by reading `/proc/meminfo`
    ///   * **macos**
    ///     * by calling `sysctl("hw.swapusage")`
    ///   * **windows**
    ///     * by calling win32 api `GlobalMemoryStatusEx`
    pub fn swap_total(&self) -> Result<usize> {
        self.0.swap_total()
    }

    /// Returns free swap size.
    ///   * **linux**
    ///     * by reading `/proc/meminfo`
    ///   * **macos**
    ///     * by calling `sysctl("hw.swapusage")`
    ///   * **windows**
    ///     * by calling win32 api `GlobalMemoryStatusEx`
    pub fn swap_free(&self) -> Result<usize> {
        self.0.swap_free()
    }

    /// Returns a default internet interface.
    ///   * **linux**
    ///     * by calling `route` and determining default route
    ///   * **macos**
    ///     * by calling `route get default`
    ///   ...
    pub fn default_iface(&self) -> Result<String> {
        self.0.default_iface()
    }

    /// Returns IP address version 4 of interface iface.
    ///   * **linux**
    ///     * by calling `ip address show <iface>`
    ///   * **macos**
    ///     * by calling `ipconfig getifaddr <iface>`
    ///   ...
    pub fn ipv4(&self, iface: &str) -> Result<String> {
        self.0.ipv4(iface)
    }

    /// Returns IP address version 6 of interface iface.
    pub fn ipv6(&self, iface: &str) -> Result<String> {
        self.0.ipv6(iface)
    }

    /// Returns a MAC address of interface iface.
    ///   * **linux**
    ///     * by calling `ip address show <iface>`
    ///   ...
    pub fn mac(&self, iface: &str) -> Result<String> {
        self.0.mac(iface)
    }

    /// Returns a vector of names of network interfaces that are available.
    ///   * **linux**
    ///     * by calling `ip address show`
    ///   ...
    pub fn interfaces(&self) -> Result<Vec<String>> {
        self.0.interfaces()
    }

    /// Returns a domain name.
    ///   * **linux**
    ///     * by reading `/proc/sys/kernel/domainname`
    ///   * **windows**
    ///     * by calling win32 api `NetWkstaGetInfo`
    ///   ...
    pub fn domainname(&self) -> Result<String> {
        self.0.domainname()
    }

    #[cfg(target_os = "linux")]
    /// Parses a StorageDevice object from system. If the provided name
    /// doesn't start with `sd` returns an error.
    pub fn stat_block_device(&self, name: &str) -> Result<StorageDevice> {
        self.1.stat_block_device(name)
    }

    #[cfg(target_os = "linux")]
    /// Parses a DeviceMapper object from system. If the provided name
    /// doesn't start with `dm` returns an error.
    pub fn stat_scsi_cdrom(&self, name: &str) -> Result<ScsiCdrom> {
        self.1.stat_scsi_cdrom(name)
    }

    #[cfg(target_os = "linux")]
    /// Parses a ScsiCdrom object from system. If the provided name
    /// doesn't start with `sr` returns an error.
    pub fn stat_device_mapper(&self, name: &str) -> Result<DeviceMapper> {
        self.1.stat_device_mapper(name)
    }

    #[cfg(target_os = "linux")]
    /// Parses a MultipleDeviceStorage object from system. If the provided name
    /// doesn't start with `md` returns an error.
    pub fn stat_multiple_device_storage(&self, name: &str) -> Result<MultipleDeviceStorage> {
        self.1.stat_multiple_device_storage(name)
    }

    #[cfg(target_os = "linux")]
    /// Returns block size of device in bytes
    pub fn block_size(&self, name: &str) -> Result<i64> {
        self.1.block_size(name)
    }

    #[cfg(target_os = "linux")]
    /// Returns detailed information about memory
    pub fn memory(&self) -> Result<Memory> {
        self.1.memory()
    }

    #[cfg(target_os = "linux")]
    /// Returns detailed Process information parsed from /proc/[pid]/stat
    pub fn stat_process(&self, pid: i32) -> Result<Process> {
        self.1.stat_process(pid)
    }
    #[cfg(target_os = "linux")]
    /// Returns a list of pids read from /proc
    pub fn pids(&self) -> Result<Vec<i32>> {
        self.1.pids()
    }
    #[cfg(target_os = "linux")]
    /// Returns all processes currently seen in /proc parsed as Processes
    pub fn processes(&self) -> Result<Processes> {
        self.1.processes()
    }
    #[cfg(target_os = "linux")]
    /// Returns kernel version of host os.
    pub fn kernel_version(&self) -> Result<String> {
        self.1.kernel_version()
    }
    #[cfg(target_os = "linux")]
    /// Returns MountPoints read from /proc/mounts
    pub fn mounts(&self) -> Result<MountPoints> {
        self.1.mounts()
    }
    #[cfg(target_os = "linux")]
    /// Returns virtual Cores of host cpu
    pub fn cores(&self) -> Result<Cores> {
        self.1.cores()
    }
    #[cfg(target_os = "linux")]
    /// Returns a Processor object containing gathered information about host cpu
    pub fn processor(&self) -> Result<Processor> {
        self.1.processor()
    }
}