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
//! rs-crisp-status-reporter Crisp Status Reporter for Rust.

#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
extern crate reqwest;
extern crate sys_info;

use std::cmp::max;
use std::thread;
use std::time::Duration;

use reqwest::header::{Authorization, Basic, Headers, UserAgent};
use reqwest::{Client, RedirectPolicy, StatusCode};
use sys_info::{cpu_num, loadavg, mem_info};

static LOG_NAME: &'static str = "Crisp Status Reporter";
static REPORT_URL: &'static str = "https://report.crisp.watch/v1";

pub struct Reporter<'a> {
    token: &'a str,
    service_id: Option<&'a str>,
    node_id: Option<&'a str>,
    replica_id: Option<&'a str>,
    interval: Duration,
}

pub struct ReporterBuilder<'a> {
    reporter: Reporter<'a>,
}

struct ReporterManager {
    report_url: String,
    replica_id: String,
    interval: Duration,
    client: Client,
}

#[derive(Serialize, Debug)]
struct ReportPayload<'a> {
    replica_id: &'a str,
    interval: u64,
    load: ReportPayloadLoad,
}

#[derive(Serialize, Debug)]
struct ReportPayloadLoad {
    cpu: f32,
    ram: f32,
}

impl<'a> Reporter<'a> {
    pub fn new(token: &'a str) -> ReporterBuilder<'a> {
        ReporterBuilder {
            reporter: Reporter {
                token: token,
                service_id: None,
                node_id: None,
                replica_id: None,
                interval: Duration::from_secs(30),
            },
        }
    }

    pub fn run(&self) -> Result<(), ()> {
        debug!("{}: Will run using URL: {}", LOG_NAME, REPORT_URL);

        // Build HTTP client
        let mut headers = Headers::new();

        headers.set(UserAgent::new(format!(
            "rs-{}/{}",
            env!("CARGO_PKG_NAME"),
            env!("CARGO_PKG_VERSION")
        )));

        headers.set(Authorization(Basic {
            username: "".to_owned(),
            password: Some(self.token.to_owned()),
        }));

        let http_client = Client::builder()
            .timeout(Duration::from_secs(10))
            .redirect(RedirectPolicy::none())
            .gzip(true)
            .enable_hostname_verification()
            .default_headers(headers)
            .build();

        // Build thread manager context?
        match (self.service_id, self.node_id, self.replica_id, http_client) {
            (Some(service_id), Some(node_id), Some(replica_id), Ok(client)) => {
                let manager = ReporterManager {
                    report_url: format!("{}/report/{}/{}/", REPORT_URL, service_id, node_id),
                    replica_id: replica_id.to_owned(),
                    interval: self.interval,
                    client: client,
                };

                // Spawn thread
                thread::Builder::new()
                    .name("crisp-status-reporter".to_string())
                    .spawn(move || manager.run())
                    .or(Err(()))
                    .and(Ok(()))
            }
            _ => Err(()),
        }
    }
}

impl<'a> ReporterBuilder<'a> {
    pub fn build(self) -> Reporter<'a> {
        if self.reporter.service_id.is_none() {
            panic!("missing service_id");
        }
        if self.reporter.node_id.is_none() {
            panic!("missing node_id");
        }
        if self.reporter.replica_id.is_none() {
            panic!("missing replica_id");
        }

        self.reporter
    }

    pub fn service_id(mut self, service_id: &'a str) -> ReporterBuilder<'a> {
        self.reporter.service_id = Some(service_id);

        self
    }

    pub fn node_id(mut self, node_id: &'a str) -> ReporterBuilder<'a> {
        self.reporter.node_id = Some(node_id);

        self
    }

    pub fn replica_id(mut self, replica_id: &'a str) -> ReporterBuilder<'a> {
        self.reporter.replica_id = Some(replica_id);

        self
    }

    pub fn interval(mut self, interval: Duration) -> ReporterBuilder<'a> {
        self.reporter.interval = interval;

        self
    }
}

impl ReporterManager {
    pub fn run(&self) {
        debug!("{}: Now running", LOG_NAME);

        // Schedule first report after 10 seconds
        thread::sleep(Duration::from_secs(10));

        loop {
            if self.report().is_err() == true {
                warn!(
                    "{}: Last report failed, trying again sooner than usual",
                    LOG_NAME
                );

                // Try reporting again after half the interval (this report failed)
                thread::sleep(self.interval / 2);

                self.report().ok();
            }

            thread::sleep(self.interval);
        }
    }

    fn report(&self) -> Result<(), ()> {
        debug!("{}: Will dispatch request", LOG_NAME);

        // Generate report payload
        let payload = ReportPayload {
            replica_id: &self.replica_id,
            interval: self.interval.as_secs(),
            load: ReportPayloadLoad {
                cpu: Self::get_load_cpu(),
                ram: Self::get_load_ram(),
            },
        };

        debug!(
            "{}: Will send request to URL: {} with payload: {:?}",
            LOG_NAME, &self.report_url, payload
        );

        // Submit report payload
        let response = self.client.post(&self.report_url).json(&payload).send();

        match response {
            Ok(response_inner) => {
                let status = response_inner.status();

                if status == StatusCode::Ok {
                    debug!("{}: Request succeeded", LOG_NAME);

                    return Ok(());
                } else {
                    warn!("{}: Got non-OK status code: {}", LOG_NAME, status);
                }
            }
            Err(err) => error!("{}: Failed dispatching request: {}", LOG_NAME, err),
        }

        Err(())
    }

    fn get_load_cpu() -> f32 {
        match (cpu_num(), loadavg()) {
            (Ok(cpu_num_value), Ok(loadavg_value)) => {
                (loadavg_value.one / (max(cpu_num_value, 1) as f64)) as f32
            }
            _ => 0.00,
        }
    }

    fn get_load_ram() -> f32 {
        if let Ok(mem_info_value) = mem_info() {
            1.00 - ((mem_info_value.avail as f32) / (mem_info_value.total as f32))
        } else {
            0.00
        }
    }
}