2017-02-24 11:50:33 -06:00
|
|
|
<?php
|
|
|
|
|
|
2018-05-25 21:16:16 -05:00
|
|
|
use LibreNMS\Exceptions\JsonAppException;
|
2019-04-03 21:49:15 -05:00
|
|
|
use LibreNMS\Exceptions\JsonAppParsingFailedException;
|
2017-02-24 11:50:33 -06:00
|
|
|
use LibreNMS\RRD\RrdDefinition;
|
|
|
|
|
|
|
|
|
|
$name = 'fail2ban';
|
|
|
|
|
|
2018-05-25 21:16:16 -05:00
|
|
|
try {
|
|
|
|
|
$f2b = json_app_get($device, $name);
|
|
|
|
|
} catch (JsonAppParsingFailedException $e) {
|
|
|
|
|
// Legacy script, build compatible array
|
|
|
|
|
$legacy = explode("\n", $e->getOutput());
|
|
|
|
|
$f2b = [
|
|
|
|
|
'data' => [
|
|
|
|
|
'total' => array_shift($legacy), // total was first line in legacy app
|
2020-09-21 15:43:38 +02:00
|
|
|
'jails' => [],
|
|
|
|
|
],
|
2018-05-25 21:16:16 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($legacy as $jail_data) {
|
2020-09-21 15:43:38 +02:00
|
|
|
[$jail, $banned] = explode(' ', $jail_data);
|
2018-05-25 21:16:16 -05:00
|
|
|
if (isset($jail) && isset($banned)) {
|
|
|
|
|
$f2b['data']['jails'][$jail] = $banned;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (JsonAppException $e) {
|
2020-09-21 15:43:38 +02:00
|
|
|
echo PHP_EOL . $name . ':' . $e->getCode() . ':' . $e->getMessage() . PHP_EOL;
|
|
|
|
|
update_application($app, $e->getCode() . ':' . $e->getMessage(), []); // Set empty metrics and error message
|
|
|
|
|
|
2018-05-25 21:16:16 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2017-02-24 11:50:33 -06:00
|
|
|
|
2018-07-13 17:08:00 -05:00
|
|
|
$f2b = $f2b['data'];
|
2017-02-24 11:50:33 -06:00
|
|
|
|
2018-05-25 21:16:16 -05:00
|
|
|
$metrics = [];
|
2017-02-24 11:50:33 -06:00
|
|
|
|
2022-07-22 16:01:55 -05:00
|
|
|
$rrd_name = ['app', $name, $app->app_id];
|
2017-02-24 11:50:33 -06:00
|
|
|
$rrd_def = RrdDefinition::make()
|
|
|
|
|
->addDataset('banned', 'GAUGE', 0)
|
|
|
|
|
->addDataset('firewalled', 'GAUGE', 0);
|
|
|
|
|
|
2018-08-16 11:11:03 -05:00
|
|
|
$fields = ['banned' => $f2b['total']];
|
|
|
|
|
$metrics['total'] = $fields; // don't include legacy ds in db
|
|
|
|
|
$fields['firewalled'] = 'U'; // legacy ds
|
2017-02-24 11:50:33 -06:00
|
|
|
|
2022-07-22 16:01:55 -05:00
|
|
|
$tags = ['name' => $name, 'app_id' => $app->app_id, 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name];
|
2017-02-24 11:50:33 -06:00
|
|
|
data_update($device, 'app', $tags, $fields);
|
|
|
|
|
|
2022-07-22 16:01:55 -05:00
|
|
|
$jails = [];
|
2018-05-25 21:16:16 -05:00
|
|
|
foreach ($f2b['jails'] as $jail => $banned) {
|
2022-07-22 16:01:55 -05:00
|
|
|
$rrd_name = ['app', $name, $app->app_id, $jail];
|
2018-05-25 21:16:16 -05:00
|
|
|
$rrd_def = RrdDefinition::make()->addDataset('banned', 'GAUGE', 0);
|
2020-09-21 15:43:38 +02:00
|
|
|
$fields = ['banned' => $banned];
|
2017-02-24 11:50:33 -06:00
|
|
|
|
2018-05-25 21:16:16 -05:00
|
|
|
$metrics["jail_$jail"] = $fields;
|
2022-07-22 16:01:55 -05:00
|
|
|
$tags = ['name' => $name, 'app_id' => $app->app_id, 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name];
|
2018-05-25 21:16:16 -05:00
|
|
|
data_update($device, 'app', $tags, $fields);
|
2017-03-29 22:54:02 -05:00
|
|
|
|
2022-07-22 16:01:55 -05:00
|
|
|
$jails[] = $jail;
|
|
|
|
|
}
|
2017-03-29 22:54:02 -05:00
|
|
|
|
2022-07-22 16:01:55 -05:00
|
|
|
// check for added or removed jails
|
|
|
|
|
$old_jails = $app->data['jails'] ?? [];
|
|
|
|
|
$added_jails = array_diff($jails, $old_jails);
|
|
|
|
|
$removed_jails = array_diff($old_jails, $jails);
|
|
|
|
|
|
|
|
|
|
// if we have any jail changes, save and log
|
|
|
|
|
if (count($added_jails) > 0 || count($removed_jails) > 0) {
|
|
|
|
|
$app->data = ['jails' => $jails]; // save jails list
|
|
|
|
|
$log_message = 'Fail2ban Jail Change:';
|
|
|
|
|
$log_message .= count($added_jails) > 0 ? ' Added ' . implode(',', $added_jails) : '';
|
|
|
|
|
$log_message .= count($removed_jails) > 0 ? ' Removed ' . implode(',', $added_jails) : '';
|
|
|
|
|
log_event($log_message, $device, 'application');
|
2017-03-29 22:54:02 -05:00
|
|
|
}
|
2022-07-22 16:01:55 -05:00
|
|
|
|
|
|
|
|
update_application($app, 'ok', $metrics);
|