1 | #!/usr/bin/env php |
||
2 | <?php |
||
3 | /* |
||
4 | * This program is free software: you can redistribute it and/or modify |
||
5 | * it under the terms of the GNU General Public License as published by |
||
6 | * the Free Software Foundation, either version 3 of the License, or |
||
7 | * (at your option) any later version. |
||
8 | * |
||
9 | * This program is distributed in the hope that it will be useful, |
||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
12 | * GNU General Public License for more details. |
||
13 | * |
||
14 | * You should have received a copy of the GNU General Public License |
||
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
16 | */ |
||
17 | |||
18 | /** |
||
19 | * libreNMS CLI utility to purge old ports. |
||
20 | * |
||
21 | * @author Maximilian Wilhelm <[email protected]> |
||
22 | * @copyright 2016-2017 LibreNMS, Barbarossa |
||
23 | * @license GPL |
||
24 | */ |
||
25 | |||
26 | use App\Models\Port; |
||
27 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
||
28 | |||
29 | chdir(dirname($argv[0])); |
||
30 | |||
31 | $init_modules = []; |
||
32 | require realpath(__DIR__ . '/..') . '/includes/init.php'; |
||
33 | |||
34 | $opt = getopt('p:f:'); |
||
35 | |||
36 | // Single Port-id given on cmdline? |
||
37 | $port_id = null; |
||
38 | if ($opt['p']) { |
||
39 | $port_id = $opt['p']; |
||
40 | } |
||
41 | |||
42 | // File with port-ids given on cmdline? |
||
43 | $port_id_file = null; |
||
44 | if ($opt['f']) { |
||
45 | $port_id_file = $opt['f']; |
||
46 | } |
||
47 | |||
48 | if (! $port_id && ! $port_id_file || ($port_id && $port_id_file)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
49 | echo $console_color->convert(\LibreNMS\Config::get('project_name') . ' Port purge tool |
||
50 | -p <port_id> Purge single port by it\'s port-id |
||
51 | -f <file> Purge a list of ports, read port-ids from <file>, one on each line. |
||
52 | A filename of - means reading from STDIN. |
||
53 | '); |
||
54 | } |
||
55 | |||
56 | // Purge single port |
||
57 | if ($port_id) { |
||
58 | try { |
||
59 | Port::findOrFail($port_id)->delete(); |
||
60 | } catch (ModelNotFoundException $e) { |
||
61 | echo "Port ID $port_id not found!\n"; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | // Delete multiple ports |
||
66 | if ($port_id_file) { |
||
67 | $fh = null; |
||
68 | if ($port_id_file == '-') { |
||
69 | $fh = STDIN; |
||
70 | } else { |
||
71 | $fh = fopen($port_id_file, 'r'); |
||
72 | if (! $fh) { |
||
0 ignored issues
–
show
|
|||
73 | echo 'Failed to open port-id list "' . $port_id_file . "\": \n"; |
||
74 | exit(1); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | while ($port_id = trim(fgets($fh))) { |
||
79 | try { |
||
80 | Port::findOrFail($port_id)->delete(); |
||
81 | } catch (ModelNotFoundException $e) { |
||
82 | echo "Port ID $port_id not found!\n"; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | if ($fh != STDIN) { |
||
87 | fclose($fh); |
||
88 | } |
||
89 | } |
||
90 |