Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
3241 rexy 1
#!/usr/bin/env php
2
<?php
3
include_once implode(\DIRECTORY_SEPARATOR, [__DIR__, '..', 'vendor', 'autoload.php']);
4
 
5
use mbolli\nfsen_ng\common\Config;
6
use mbolli\nfsen_ng\common\Debug;
7
use mbolli\nfsen_ng\common\Import;
8
 
9
$d = Debug::getInstance();
10
try {
11
    Config::initialize();
12
} catch (Exception $e) {
13
    $d->log('Fatal: ' . $e->getMessage(), \LOG_ALERT);
14
    exit;
15
}
16
 
17
if ($argc < 2 || in_array($argv[1], ['--help', '-help', '-h', '-?'], true)) {
18
    ?>
19
 
20
    This is the command line interface to nfsen-ng.
21
 
22
    Usage:
23
    <?php echo $argv[0]; ?> [options] import
24
    <?php echo $argv[0]; ?> start|stop|status
25
 
26
    Options:
27
    -v  Show verbose output
28
    -p  Import ports data
29
    -ps Import ports data per source
30
    -f  Force overwriting database and start at the beginning
31
 
32
    Commands:
33
    import  - Import existing nfdump data to nfsen-ng.
34
    Notice: If you have existing nfcapd files, better do this overnight.
35
    start   - Start the daemon for continuous reading of new data
36
    stop    - Stop the daemon
37
    status  - Get the daemon's status
38
 
39
    Examples:
40
    <?php echo $argv[0]; ?> -f import
41
    Imports fresh data for sources
42
 
43
    <?php echo $argv[0]; ?> -s -p import
44
    Imports data for ports only
45
 
46
    <?php echo $argv[0]; ?> start
47
    Start the daemon
48
 
49
    <?php
50
} else {
51
    $folder = __DIR__;
52
    $pidfile = $folder . '/nfsen-ng.pid';
53
 
54
    if (in_array('import', $argv, true)) {
55
        // import 3 years of data if available
56
 
57
        $d->log('CLI: Starting import', \LOG_INFO);
58
        $start = new DateTime();
59
        $start->setDate(date('Y') - 3, (int) date('m'), (int) date('d'));
60
        $i = new Import();
61
        if (in_array('-v', $argv, true)) {
62
            $i->setVerbose(true);
63
        }
64
        if (in_array('-p', $argv, true)) {
65
            $i->setProcessPorts(true);
66
        }
67
        if (in_array('-ps', $argv, true)) {
68
            $i->setProcessPortsBySource(true);
69
        }
70
        if (in_array('-f', $argv, true)) {
71
            $i->setForce(true);
72
        }
73
        $i->start($start);
74
    } elseif (in_array('start', $argv, true)) {
75
        // start the daemon
76
 
77
        $d->log('CLI: Starting daemon...', \LOG_INFO);
78
        $pid = exec('nohup `which php` ' . $folder . '/listen.php > /dev/null 2>&1 & echo $!', $op, $exit);
79
        var_dump($exit);
80
        // todo: get exit code of background process. possible at all?
81
        switch ((int) $exit) {
82
            case 128:
83
                echo 'Unexpected error opening or locking lock file. Perhaps you don\'t have permission to write to the lock file or its containing directory?';
84
                break;
85
            case 129:
86
                echo 'Another instance is already running; terminating.';
87
                break;
88
            default:
89
                echo 'Daemon running, pid=' . $pid;
90
                break;
91
        }
92
        echo \PHP_EOL;
93
    } elseif (in_array('stop', $argv, true)) {
94
        // stop the daemon
95
 
96
        if (!file_exists($pidfile)) {
97
            echo 'Not running' . \PHP_EOL;
98
            exit;
99
        }
100
        $pid = file_get_contents($pidfile);
101
        $d->log('CLI: Stopping daemon', \LOG_INFO);
102
        exec('kill ' . $pid);
103
        unlink($pidfile);
104
 
105
        echo 'Stopped.' . \PHP_EOL;
106
    } elseif (in_array('status', $argv, true)) {
107
        // print the daemon status
108
 
109
        if (!file_exists($pidfile)) {
110
            echo 'Not running' . \PHP_EOL;
111
            exit;
112
        }
113
        $pid = file_get_contents($pidfile);
114
        exec('ps -p ' . $pid, $op);
115
        if (!isset($op[1])) {
116
            echo 'Not running' . \PHP_EOL;
117
        } else {
118
            echo 'Running: ' . $pid . \PHP_EOL;
119
        }
120
    }
121
}