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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! Load network informations

use std::collections::HashMap;
use std::fs;
use std::io::{Error, ErrorKind, Result};

/// Struct that contains information about networks
#[derive(Clone, Copy, Debug)]
pub struct NetIOCounters {
    /// Number of bytes sent
    pub bytes_send: u64,

    /// Number of bytes received
    pub bytes_recv: u64,

    /// Number of packets sent
    pub packets_send: u64,

    /// Number of packets received
    pub packets_recv: u64,

    /// Total number of errors while receiving
    pub errin: u64,

    /// Total number of errors while sending
    pub errout: u64,

    /// Total number of incoming packets which were dropped
    pub dropin: u64,

    /// Total number of outgoing packets which were dropped (always 0 on macOS and BSD)
    pub dropout: u64,
}

/// Net counter struct to use nowrap mode
#[derive(Debug, Default)]
pub struct NetIOCountersCollector {
    /// Save the total of counters
    total_net_io_counters: HashMap<String, NetIOCounters>,

    /// Save the values of the last call of net_io_counters
    net_io_counters_last_statement: HashMap<String, NetIOCounters>,
}

impl NetIOCountersCollector {
    /// Reset de cache for net_io_counters in nowrap mode
    pub fn cache_clear(&mut self) {
        self.total_net_io_counters = HashMap::new();
        self.net_io_counters_last_statement = HashMap::new();
    }

    /// Return system-wide network I/O statistics as a Result of NetIOCounters
    ///
    /// If nowrap is true psutil will detect and adjust those numbers
    /// across function calls and add “old value” to “new value” so
    /// that the returned numbers will always be increasing or remain
    /// the same, but never decrease. net_io_counters.cache_clear() can
    /// be used to invalidate the nowrap cache.
    pub fn net_io_counters(&mut self, nowrap: bool) -> Result<NetIOCounters> {
        let total_net_io_counters = self.net_io_counters_pernic(nowrap)?;
        let mut final_net_io_counters = NetIOCounters {
            bytes_send: 0,
            bytes_recv: 0,
            packets_send: 0,
            packets_recv: 0,
            errin: 0,
            errout: 0,
            dropin: 0,
            dropout: 0,
        };
        for (_name, disk_io_counters) in total_net_io_counters {
            final_net_io_counters.bytes_send += disk_io_counters.bytes_send;
            final_net_io_counters.bytes_recv += disk_io_counters.bytes_recv;
            final_net_io_counters.packets_send += disk_io_counters.packets_send;
            final_net_io_counters.packets_recv += disk_io_counters.packets_recv;
            final_net_io_counters.errin += disk_io_counters.errin;
            final_net_io_counters.errout += disk_io_counters.errout;
            final_net_io_counters.dropin += disk_io_counters.dropin;
            final_net_io_counters.dropout += disk_io_counters.dropout;
        }
        Ok(final_net_io_counters)
    }

    /// Return for every network interface of the system the I/O statistics
    /// as Result of vector of String and NetIOCounters tuple.
    ///
    /// If nowrap is true psutil will detect and adjust those numbers
    /// across function calls and add “old value” to “new value” so
    /// that the returned numbers will always be increasing or remain
    /// the same, but never decrease. net_io_counters.cache_clear() can
    /// be used to invalidate the nowrap cache.
    pub fn net_io_counters_pernic(
        &mut self,
        nowrap: bool,
    ) -> Result<HashMap<String, NetIOCounters>> {
        let net_dev = fs::read_to_string("/proc/net/dev")?;
        let mut net_lines: Vec<&str> = net_dev.lines().collect();
        // The two first lines contains no usefull informations
        net_lines.remove(1);
        net_lines.remove(0);
        let mut net_io_counters_vector = HashMap::new();
        for line in net_lines {
            let mut net_infos: Vec<&str> = line.split_whitespace().collect();
            let mut net_infos_u64: Vec<u64> = Vec::new();
            let mut net_name = String::from(net_infos[0]);
            net_name.pop();
            net_infos.remove(0);
            for net_info in net_infos {
                net_infos_u64.push(match net_info.parse::<u64>() {
                    Ok(u64_value) => u64_value,
                    Err(error) => {
                        return Err(Error::new(
                            ErrorKind::InvalidData,
                            format!(
                                "Impossible to parse '{}' in u64, error : {}",
                                net_info, error
                            ),
                        ));
                    }
                });
            }
            net_io_counters_vector.insert(
                net_name,
                NetIOCounters {
                    bytes_send: net_infos_u64[8],
                    bytes_recv: net_infos_u64[0],
                    packets_send: net_infos_u64[9],
                    packets_recv: net_infos_u64[1],
                    errin: net_infos_u64[2],
                    errout: net_infos_u64[10],
                    dropin: net_infos_u64[3],
                    dropout: net_infos_u64[11],
                },
            );
        }
        if nowrap {
            if self.net_io_counters_last_statement.is_empty() {
                self.total_net_io_counters = net_io_counters_vector.clone();
                self.net_io_counters_last_statement = net_io_counters_vector;
                Ok(self.total_net_io_counters.clone())
            } else {
                self.total_net_io_counters = total_net_io_counters(
                    &self.net_io_counters_last_statement,
                    &net_io_counters_vector,
                    &self.total_net_io_counters,
                );
                self.net_io_counters_last_statement = net_io_counters_vector;
                Ok(self.total_net_io_counters.clone())
            }
        } else {
            Ok(net_io_counters_vector)
        }
    }
}

/// Calculate nowrap for NetIOCounters field
fn nowrap(past_value: u64, current_value: u64, total_value: u64) -> u64 {
    const MAX_VALUE: u64 = 4_294_967_296;
    if current_value >= past_value {
        total_value + current_value - past_value
    } else {
        total_value + current_value + MAX_VALUE - past_value
    }
}

/// Calculate per interface the new NetIOCounters after a call of net_io_counters_pernic
fn total_net_io_counters(
    past_net_io_counters: &HashMap<String, NetIOCounters>,
    current_net_io_counters: &HashMap<String, NetIOCounters>,
    total_net_io_counters: &HashMap<String, NetIOCounters>,
) -> HashMap<String, NetIOCounters> {
    let mut final_net_io_counters: HashMap<String, NetIOCounters> = HashMap::new();
    for (name, current_counters) in current_net_io_counters {
        if past_net_io_counters.contains_key(name) && total_net_io_counters.contains_key(name) {
            let past_counters = past_net_io_counters[name];
            let total_counters = total_net_io_counters[name];
            final_net_io_counters.insert(
                name.clone(),
                NetIOCounters {
                    bytes_send: nowrap(
                        past_counters.bytes_send,
                        current_counters.bytes_send,
                        total_counters.bytes_send,
                    ),
                    bytes_recv: nowrap(
                        past_counters.bytes_recv,
                        current_counters.bytes_recv,
                        total_counters.bytes_recv,
                    ),
                    packets_send: nowrap(
                        past_counters.packets_send,
                        current_counters.packets_send,
                        total_counters.packets_send,
                    ),
                    packets_recv: nowrap(
                        past_counters.packets_recv,
                        current_counters.packets_recv,
                        total_counters.packets_recv,
                    ),
                    errin: nowrap(
                        past_counters.errin,
                        current_counters.errin,
                        total_counters.errin,
                    ),
                    errout: nowrap(
                        past_counters.errout,
                        current_counters.errout,
                        total_counters.errout,
                    ),
                    dropin: nowrap(
                        past_counters.dropin,
                        current_counters.dropin,
                        total_counters.dropin,
                    ),
                    dropout: nowrap(
                        past_counters.dropout,
                        current_counters.dropout,
                        total_counters.dropout,
                    ),
                },
            );
        } else {
            final_net_io_counters.insert(name.clone(), *current_counters);
        }
    }
    final_net_io_counters
}

#[cfg(test)]
mod unit_test {
    use super::*;

    #[test]
    fn total_net_io_counters_test() {
        //
        let mut past_net_io_counters = HashMap::new();
        past_net_io_counters.insert(
            String::from("eth0"),
            NetIOCounters {
                bytes_send: 3_000,
                bytes_recv: 10_000_000_000,
                packets_send: 4_000_000_000,
                packets_recv: 3_000_000_000,
                errin: 0,
                errout: 0,
                dropin: 0,
                dropout: 0,
            },
        );

        let mut current_net_io_counters = HashMap::new();
        current_net_io_counters.insert(
            String::from("eth0"),
            NetIOCounters {
                bytes_send: 5_000,
                bytes_recv: 50_000_000_000,  // 64 bits counter never wrap
                packets_send: 1_000_000_000, // 32 bits counter will wrap
                packets_recv: 2_000_000_000, // 32 bits counter will wrap
                errin: 0,
                errout: 0,
                dropin: 0,
                dropout: 0,
            },
        );
        current_net_io_counters.insert(
            String::from("eth1"),
            NetIOCounters {
                bytes_send: 42,
                bytes_recv: 0,
                packets_send: 0,
                packets_recv: 0,
                errin: 0,
                errout: 0,
                dropin: 0,
                dropout: 0,
            },
        );

        let mut total_net_io_counter = HashMap::new();
        total_net_io_counter.insert(
            String::from("eth0"),
            NetIOCounters {
                bytes_send: 3_000,
                bytes_recv: 10_000_000_000,
                packets_send: 4_000_000_000,
                packets_recv: 8_000_000_000, // 32 bits counter already wrap in the past
                errin: 0,
                errout: 0,
                dropin: 0,
                dropout: 0,
            },
        );

        let final_net_io_counters = total_net_io_counters(
            &past_net_io_counters,
            &current_net_io_counters,
            &total_net_io_counter,
        );

        assert_eq!(
            final_net_io_counters[&String::from("eth0")].bytes_send,
            5_000,
        );
        assert_eq!(
            final_net_io_counters[&String::from("eth0")].bytes_recv,
            50_000_000_000,
        );
        assert_eq!(
            final_net_io_counters[&String::from("eth0")].packets_send,
            4_294_967_296 + 1_000_000_000
        );
        assert_eq!(
            final_net_io_counters[&String::from("eth0")].packets_recv,
            8_000_000_000 + 1_294_967_296 + 2_000_000_000,
        );

        assert_eq!(final_net_io_counters[&String::from("eth1")].bytes_send, 42);
    }
}