netwatch-rs 0.2.0

A modern network traffic monitor for Unix systems, inspired by nload but written in Rust
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::time::{Duration, SystemTime};

#[derive(Debug, Clone)]
pub struct ProcessNetworkInfo {
    pub pid: u32,
    pub name: String,
    pub command: String,
    pub connections: u32,
    pub bytes_sent: u64,
    pub bytes_received: u64,
    pub packets_sent: u64,
    pub packets_received: u64,
    pub established_connections: u32,
    pub listening_ports: u32,
    pub last_updated: SystemTime,
}

impl ProcessNetworkInfo {
    pub fn total_bytes(&self) -> u64 {
        self.bytes_sent + self.bytes_received
    }

    pub fn total_packets(&self) -> u64 {
        self.packets_sent + self.packets_received
    }
}

pub struct ProcessMonitor {
    processes: HashMap<u32, ProcessNetworkInfo>,
    previous_stats: HashMap<u32, ProcessNetworkStats>,
    last_update: SystemTime,
}

#[derive(Debug, Clone)]
pub struct ProcessNetworkStats {
    bytes_sent: u64,
    bytes_received: u64,
    packets_sent: u64,
    packets_received: u64,
    timestamp: SystemTime,
}

impl ProcessMonitor {
    pub fn new() -> Self {
        Self {
            processes: HashMap::new(),
            previous_stats: HashMap::new(),
            last_update: SystemTime::now(),
        }
    }

    pub fn update(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        // Clear existing processes to get fresh data
        self.processes.clear();

        let now = SystemTime::now();

        // Read all process network information
        self.scan_processes()?;

        // Update connection counts
        self.update_connection_counts()?;

        // Calculate network I/O rates
        self.calculate_rates(now)?;

        self.last_update = now;
        Ok(())
    }

    fn scan_processes(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        if let Ok(entries) = fs::read_dir("/proc") {
            for entry in entries.flatten() {
                if let Ok(file_name) = entry.file_name().into_string() {
                    if let Ok(pid) = file_name.parse::<u32>() {
                        if let Some(process_info) = self.read_process_info(pid)? {
                            self.processes.insert(pid, process_info);
                        }
                    }
                }
            }
        } else {
            // macOS fallback - get real process data from system commands
            self.get_real_processes_from_system();
        }
        Ok(())
    }

    fn read_process_info(
        &mut self,
        pid: u32,
    ) -> Result<Option<ProcessNetworkInfo>, Box<dyn std::error::Error>> {
        let proc_path = format!("/proc/{pid}");

        // Check if process directory exists and is accessible
        if !Path::new(&proc_path).exists() {
            return Ok(None);
        }

        // Read process name
        let comm_path = format!("{proc_path}/comm");
        let name = fs::read_to_string(comm_path)
            .unwrap_or_else(|_| format!("process-{pid}"))
            .trim()
            .to_string();

        // Read command line
        let cmdline_path = format!("{proc_path}/cmdline");
        let command = fs::read_to_string(cmdline_path)
            .unwrap_or_else(|_| name.clone())
            .replace('\0', " ")
            .trim()
            .to_string();

        // Read network I/O statistics from /proc/pid/net/dev if available
        let (bytes_sent, bytes_received, packets_sent, packets_received) =
            self.read_process_network_stats(pid).unwrap_or((0, 0, 0, 0));

        let process_info = ProcessNetworkInfo {
            pid,
            name,
            command,
            connections: 0, // Will be updated later
            bytes_sent,
            bytes_received,
            packets_sent,
            packets_received,
            established_connections: 0,
            listening_ports: 0,
            last_updated: SystemTime::now(),
        };

        Ok(Some(process_info))
    }

    fn read_process_network_stats(
        &self,
        pid: u32,
    ) -> Result<(u64, u64, u64, u64), Box<dyn std::error::Error>> {
        // Try to read network statistics from various sources

        // Method 1: Try /proc/pid/net/dev (process-specific network stats)
        let net_dev_path = format!("/proc/{pid}/net/dev");
        if let Ok(content) = fs::read_to_string(net_dev_path) {
            if let Some((bytes_rx, bytes_tx, packets_rx, packets_tx)) = self.parse_net_dev(&content)
            {
                return Ok((bytes_tx, bytes_rx, packets_tx, packets_rx));
            }
        }

        // Method 2: Try /proc/pid/io (process I/O stats)
        let io_path = format!("/proc/{pid}/io");
        if let Ok(content) = fs::read_to_string(io_path) {
            if let Some((read_bytes, write_bytes)) = self.parse_io_stats(&content) {
                // Estimate network I/O as a fraction of total I/O
                // This is a rough approximation
                return Ok((write_bytes / 4, read_bytes / 4, 0, 0));
            }
        }

        // Method 3: Use system-wide stats as fallback
        // Read from /proc/net/netstat for system-wide network stats
        if let Ok(content) = fs::read_to_string("/proc/net/netstat") {
            if let Some((bytes_sent, bytes_received)) = self.parse_netstat(&content) {
                // Distribute proportionally based on number of connections
                // This is a very rough estimate
                return Ok((bytes_sent / 100, bytes_received / 100, 0, 0));
            }
        }

        Ok((0, 0, 0, 0))
    }

    fn parse_net_dev(&self, content: &str) -> Option<(u64, u64, u64, u64)> {
        let mut total_bytes_rx = 0;
        let mut total_bytes_tx = 0;
        let mut total_packets_rx = 0;
        let mut total_packets_tx = 0;

        for line in content.lines().skip(2) {
            // Skip header lines
            let fields: Vec<&str> = line.split_whitespace().collect();
            if fields.len() >= 10 {
                if let (Ok(bytes_rx), Ok(packets_rx), Ok(bytes_tx), Ok(packets_tx)) = (
                    fields[1].parse::<u64>(),
                    fields[2].parse::<u64>(),
                    fields[9].parse::<u64>(),
                    fields[10].parse::<u64>(),
                ) {
                    total_bytes_rx += bytes_rx;
                    total_bytes_tx += bytes_tx;
                    total_packets_rx += packets_rx;
                    total_packets_tx += packets_tx;
                }
            }
        }

        if total_bytes_rx > 0 || total_bytes_tx > 0 {
            Some((
                total_bytes_rx,
                total_bytes_tx,
                total_packets_rx,
                total_packets_tx,
            ))
        } else {
            None
        }
    }

    fn parse_io_stats(&self, content: &str) -> Option<(u64, u64)> {
        let mut read_bytes = 0;
        let mut write_bytes = 0;

        for line in content.lines() {
            if line.starts_with("read_bytes:") {
                if let Some(value) = line.split_whitespace().nth(1) {
                    read_bytes = value.parse().unwrap_or(0);
                }
            } else if line.starts_with("write_bytes:") {
                if let Some(value) = line.split_whitespace().nth(1) {
                    write_bytes = value.parse().unwrap_or(0);
                }
            }
        }

        if read_bytes > 0 || write_bytes > 0 {
            Some((read_bytes, write_bytes))
        } else {
            None
        }
    }

    fn parse_netstat(&self, content: &str) -> Option<(u64, u64)> {
        // Parse system-wide network statistics
        for line in content.lines() {
            if line.starts_with("IpExt:") {
                let fields: Vec<&str> = line.split_whitespace().collect();
                if fields.len() > 6 {
                    if let (Ok(bytes_sent), Ok(bytes_received)) =
                        (fields[5].parse::<u64>(), fields[6].parse::<u64>())
                    {
                        return Some((bytes_sent, bytes_received));
                    }
                }
            }
        }
        None
    }

    fn update_connection_counts(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        // Count connections per process by parsing /proc/net/tcp and /proc/net/udp
        let mut pid_connections: HashMap<u32, (u32, u32, u32)> = HashMap::new(); // (total, established, listening)

        // Parse TCP connections
        if let Ok(content) = fs::read_to_string("/proc/net/tcp") {
            self.count_connections_in_file(&content, &mut pid_connections)?;
        }

        // Parse UDP connections
        if let Ok(content) = fs::read_to_string("/proc/net/udp") {
            self.count_connections_in_file(&content, &mut pid_connections)?;
        }

        // Update process information
        for (pid, (total, established, listening)) in pid_connections {
            if let Some(process) = self.processes.get_mut(&pid) {
                process.connections = total;
                process.established_connections = established;
                process.listening_ports = listening;
            }
        }

        Ok(())
    }

    fn count_connections_in_file(
        &self,
        content: &str,
        pid_connections: &mut HashMap<u32, (u32, u32, u32)>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        for line in content.lines().skip(1) {
            // Skip header
            let fields: Vec<&str> = line.split_whitespace().collect();
            if fields.len() >= 8 {
                // Try to extract PID from inode (this is complex and may not always work)
                // For now, we'll use a simplified approach
                if let Ok(inode) = fields[9].parse::<u64>() {
                    if let Some(pid) = self.find_pid_by_inode(inode) {
                        let (total, established, listening) =
                            pid_connections.entry(pid).or_insert((0, 0, 0));
                        *total += 1;

                        // Check connection state
                        if fields[3] == "01" {
                            // ESTABLISHED
                            *established += 1;
                        } else if fields[3] == "0A" {
                            // LISTEN
                            *listening += 1;
                        }
                    }
                }
            }
        }
        Ok(())
    }

    fn find_pid_by_inode(&self, _inode: u64) -> Option<u32> {
        // This is a simplified implementation
        // In reality, we'd need to scan /proc/*/fd/* to find which process owns this inode
        // For now, return None to avoid complex filesystem scanning
        None
    }

    fn calculate_rates(&mut self, now: SystemTime) -> Result<(), Box<dyn std::error::Error>> {
        // Calculate network I/O rates by comparing with previous measurements
        for (pid, process) in &mut self.processes {
            if let Some(prev_stats) = self.previous_stats.get(pid) {
                let time_diff = now
                    .duration_since(prev_stats.timestamp)
                    .unwrap_or(Duration::from_secs(1));
                let time_secs = time_diff.as_secs_f64();

                if time_secs > 0.0 {
                    // Calculate rates (bytes per second)
                    let bytes_sent_rate =
                        ((process.bytes_sent.saturating_sub(prev_stats.bytes_sent)) as f64
                            / time_secs) as u64;
                    let bytes_received_rate = ((process
                        .bytes_received
                        .saturating_sub(prev_stats.bytes_received))
                        as f64
                        / time_secs) as u64;

                    // Update with calculated rates
                    process.bytes_sent = bytes_sent_rate;
                    process.bytes_received = bytes_received_rate;
                }
            }

            // Store current stats for next calculation
            self.previous_stats.insert(
                *pid,
                ProcessNetworkStats {
                    bytes_sent: process.bytes_sent,
                    bytes_received: process.bytes_received,
                    packets_sent: process.packets_sent,
                    packets_received: process.packets_received,
                    timestamp: now,
                },
            );
        }

        Ok(())
    }

    pub fn get_processes(&self) -> Vec<&ProcessNetworkInfo> {
        let mut processes: Vec<&ProcessNetworkInfo> = self.processes.values().collect();

        // Sort by total network activity (bytes sent + received)
        processes.sort_by(|a, b| {
            let a_total = a.total_bytes();
            let b_total = b.total_bytes();
            b_total.cmp(&a_total)
        });

        processes
    }

    pub fn get_top_network_processes(&self, limit: usize) -> Vec<&ProcessNetworkInfo> {
        let mut processes = self.get_processes();
        processes.truncate(limit);
        processes
    }

    pub fn get_process_stats(&self) -> ProcessNetworkStats {
        let mut stats = ProcessNetworkStats {
            bytes_sent: 0,
            bytes_received: 0,
            packets_sent: 0,
            packets_received: 0,
            timestamp: SystemTime::now(),
        };

        for process in self.processes.values() {
            stats.bytes_sent += process.bytes_sent;
            stats.bytes_received += process.bytes_received;
            stats.packets_sent += process.packets_sent;
            stats.packets_received += process.packets_received;
        }

        stats
    }

    pub fn get_listening_processes(&self) -> Vec<&ProcessNetworkInfo> {
        let mut processes: Vec<&ProcessNetworkInfo> = self
            .processes
            .values()
            .filter(|p| p.listening_ports > 0)
            .collect();

        processes.sort_by(|a, b| b.listening_ports.cmp(&a.listening_ports));
        processes
    }

    fn get_real_processes_from_system(&mut self) {
        use std::process::Command;

        // Use ps and lsof to get real process data with network activity
        if let Ok(output) = Command::new("lsof").args(["-i", "-n", "-P"]).output() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            self.parse_lsof_processes(&stdout);
        } else {
            // Fallback to ps if lsof is not available
            if let Ok(output) = Command::new("ps").args(["-eo", "pid,comm,rss"]).output() {
                let stdout = String::from_utf8_lossy(&output.stdout);
                self.parse_ps_processes(&stdout);
            }
        }
    }

    fn parse_lsof_processes(&mut self, output: &str) {
        let mut process_map: HashMap<u32, (String, String)> = HashMap::new(); // pid -> (name, command)
        let mut process_connections: HashMap<u32, u32> = HashMap::new(); // pid -> total connections
        let mut process_listening: HashMap<u32, u32> = HashMap::new(); // pid -> listening ports
        let mut process_established: HashMap<u32, u32> = HashMap::new(); // pid -> established connections

        for line in output.lines().skip(1) {
            // Skip header
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() < 10 {
                continue;
            }

            let process_name = parts[0].to_string();
            if let Ok(pid) = parts[1].parse::<u32>() {
                // Count total connections per process
                *process_connections.entry(pid).or_insert(0) += 1;

                // Check if this is a listening port or established connection
                // The connection state is in parts[9] like "(LISTEN)" or "(ESTABLISHED)"
                let connection_state = parts.get(9).unwrap_or(&"");
                if connection_state.contains("LISTEN") {
                    *process_listening.entry(pid).or_insert(0) += 1;
                } else if connection_state.contains("ESTABLISHED") {
                    *process_established.entry(pid).or_insert(0) += 1;
                }

                // Store process info if not already seen
                process_map.entry(pid).or_insert_with(|| {
                    let command = process_name.to_string(); // lsof doesn't give full command
                    (process_name, command)
                });
            }
        }

        // Convert to ProcessNetworkInfo
        for (pid, total_connections) in process_connections {
            if let Some((name, command)) = process_map.get(&pid) {
                let listening_ports = process_listening.get(&pid).copied().unwrap_or(0);
                let established_connections = process_established.get(&pid).copied().unwrap_or(0);

                let process_info = ProcessNetworkInfo {
                    pid,
                    name: name.clone(),
                    command: command.clone(),
                    connections: total_connections,
                    bytes_sent: 0, // lsof doesn't provide byte counts
                    bytes_received: 0,
                    packets_sent: 0,
                    packets_received: 0,
                    established_connections,
                    listening_ports,
                    last_updated: SystemTime::now(),
                };
                self.processes.insert(process_info.pid, process_info);
            }
        }
    }

    fn parse_ps_processes(&mut self, output: &str) {
        // Basic fallback - just get running processes without network info
        for line in output.lines().skip(1) {
            // Skip header
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() < 2 {
                continue;
            }

            if let Ok(pid) = parts[0].parse::<u32>() {
                let name = parts[1].to_string();
                let process_info = ProcessNetworkInfo {
                    pid,
                    name: name.clone(),
                    command: name,
                    connections: 0,
                    bytes_sent: 0,
                    bytes_received: 0,
                    packets_sent: 0,
                    packets_received: 0,
                    established_connections: 0,
                    listening_ports: 0,
                    last_updated: SystemTime::now(),
                };
                self.processes.insert(process_info.pid, process_info);
            }
        }
    }
}

impl Default for ProcessMonitor {
    fn default() -> Self {
        Self::new()
    }
}