-
Notifications
You must be signed in to change notification settings - Fork 0
/
start
68 lines (56 loc) · 1.62 KB
/
start
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
#!/usr/bin/env php
<?php
/**
* @author Guilherme Almeida Girardi <guilhermeagirardi@gmail.com>
* @copyright Copyright (c) 2021, Ejetar
* @version 1.0.0
*
* This is the main script of the package, it is responsible for running nethogs tracemode
* for X time interval.
*
* If you do not enter the output path, /var/log/nethogs/YYYY-MM-DD_H.log" will be used
* by default.
*
* The default interval is set to 3600 seconds, that is, one hour.
*
* The default mode chosen for nethogs tracemode is 2, that is, result in bytes.
*
* Return "OK" at the end of the run, if all goes well.
*/
//Load Composer
require_once 'vendor/autoload.php';
use Spatie\Async\Pool;
$menu = new Commando\Command();
//Output path
$menu->option()
->require()
->default("/var/log/bandwidth/".date("Y-m-d_H").".log")
->describedAs('Output path.');
//Interval in seconds
$menu->option('i')
->aka('interval')
->default(3600)
->must(function(int $seconds) {
return is_int($seconds);
})
->describedAs('Capture time in seconds.');
$menu->option('v')
->aka('view-mode')
->must(function($value) {
return in_array($value, [0,1,2,3]);
})
->default(2)
->describedAs('View mode (0 = KB/s, 1 = total KB, 2 = total B, 3 = total MB).');
$last = realpath('./src/last');
$PIDFile = '/tmp/bandwidth-recorder.pid';
$cmd = 'echo $$ > '.$PIDFile.'; /usr/sbin/nethogs -t -v '.$menu['view-mode'].' | '.$last.' '.$menu[0];
$pool = Pool::create()
->timeout((int) $menu['interval']);
$pool
->add(function() use($cmd) {
shell_exec($cmd);
});
$pool->wait();
//kills the process tree
shell_exec("pkill -TERM -P ".file_get_contents($PIDFile));
exit(0);