|
| 1 | +// Copyright 2015-2017 Intecture Developers. |
| 2 | +// |
| 3 | +// Licensed under the Mozilla Public License 2.0 <LICENSE or |
| 4 | +// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied, |
| 5 | +// modified, or distributed except according to those terms. |
| 6 | + |
| 7 | +use erased_serde::Serialize; |
| 8 | +use errors::*; |
| 9 | +use ExecutableProvider; |
| 10 | +use host::*; |
| 11 | +use pnet::datalink::interfaces; |
| 12 | +use std::{env, process, str}; |
| 13 | +use target::{default, linux}; |
| 14 | +use telemetry::{Cpu, Os, OsFamily, OsPlatform, Telemetry, TelemetryProvider, serializable}; |
| 15 | + |
| 16 | +pub struct Debian; |
| 17 | + |
| 18 | +#[derive(Serialize, Deserialize)] |
| 19 | +pub enum RemoteProvider { |
| 20 | + Available, |
| 21 | + Load, |
| 22 | +} |
| 23 | + |
| 24 | +impl <'de>ExecutableProvider<'de> for RemoteProvider { |
| 25 | + fn exec(&self, host: &Host) -> Result<Box<Serialize>> { |
| 26 | + match *self { |
| 27 | + RemoteProvider::Available => Ok(Box::new(Debian::available(host))), |
| 28 | + RemoteProvider::Load => { |
| 29 | + let t: serializable::Telemetry = Debian::load(host)?.into(); |
| 30 | + Ok(Box::new(t)) |
| 31 | + }, |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl TelemetryProvider for Debian { |
| 37 | + fn available(host: &Host) -> bool { |
| 38 | + if host.is_local() { |
| 39 | + cfg!(target_os="linux") |
| 40 | + } else { |
| 41 | + unimplemented!(); |
| 42 | + // let r = RemoteProvider::Available; |
| 43 | + // self.host.send(r).chain_err(|| ErrorKind::RemoteProvider("Telemetry", "available"))?; |
| 44 | + // let t: Telemetry = self.host.recv()?; |
| 45 | + // Ok(t) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + fn load(host: &Host) -> Result<Telemetry> { |
| 50 | + if host.is_local() { |
| 51 | + let cpu_vendor = linux::cpu_vendor()?; |
| 52 | + let cpu_brand = linux::cpu_brand_string()?; |
| 53 | + let (version_str, version_maj, version_min) = version()?; |
| 54 | + |
| 55 | + Ok(Telemetry { |
| 56 | + cpu: Cpu { |
| 57 | + vendor: cpu_vendor, |
| 58 | + brand_string: cpu_brand, |
| 59 | + cores: linux::cpu_cores()?, |
| 60 | + }, |
| 61 | + fs: default::fs().chain_err(|| "could not resolve telemetry data")?, |
| 62 | + hostname: default::hostname()?, |
| 63 | + memory: linux::memory().chain_err(|| "could not resolve telemetry data")?, |
| 64 | + net: interfaces(), |
| 65 | + os: Os { |
| 66 | + arch: env::consts::ARCH.into(), |
| 67 | + family: OsFamily::Linux, |
| 68 | + platform: OsPlatform::Centos, |
| 69 | + version_str: version_str, |
| 70 | + version_maj: version_maj, |
| 71 | + version_min: version_min, |
| 72 | + version_patch: 0 |
| 73 | + }, |
| 74 | + }) |
| 75 | + } else { |
| 76 | + unimplemented!(); |
| 77 | + // let r = RemoteProvider::Load; |
| 78 | + // self.host.send(r).chain_err(|| ErrorKind::RemoteProvider("Telemetry", "load"))?; |
| 79 | + // let t: Telemetry = self.host.recv()?; |
| 80 | + // Ok(t) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn version() -> Result<(String, u32, u32)> { |
| 86 | + let out = process::Command::new("lsb_release") |
| 87 | + .arg("-sr") |
| 88 | + .output() |
| 89 | + .chain_err(|| ErrorKind::SystemCommand("lsb_release"))?; |
| 90 | + let version_str = str::from_utf8(&out.stdout) |
| 91 | + .chain_err(|| ErrorKind::SystemCommandOutput("lsb_release"))? |
| 92 | + .trim() |
| 93 | + .to_owned(); |
| 94 | + let (maj, min) = { |
| 95 | + let mut parts = version_str.split('.'); |
| 96 | + let errstr = format!("Expected OS version format `u32.u32`, got: '{}'", version_str); |
| 97 | + ( |
| 98 | + parts.next().ok_or(&*errstr)?.parse().chain_err(|| ErrorKind::SystemCommandOutput("sw_vers"))?, |
| 99 | + parts.next().ok_or(&*errstr)?.parse().chain_err(|| ErrorKind::SystemCommandOutput("sw_vers"))? |
| 100 | + ) |
| 101 | + }; |
| 102 | + Ok((version_str, maj, min)) |
| 103 | +} |
0 commit comments