use components for fail2ban jails list (#6221)

* add get_fail2ban_jails functions

* now do component stuff for fail2ban for tracking jails

* now use get_fail2ban_jails for getting a list of jails

* readd the accidentally deleted update app bit

* white space cleanup

* Update fail2ban component code

* remove unneeded variable
This commit is contained in:
VVelox 2017-03-29 22:54:02 -05:00 committed by Tony Murray
parent 3ac4e3c8d5
commit acd11955d2
4 changed files with 94 additions and 15 deletions

View File

@ -374,4 +374,24 @@ class Component
return true;
}
/**
* Get the component id for the first component in the array
* Only set $device_id if using the array from getCompenents(), which is keyed by device_id
*
* @param array $component_array
* @param int $device_id
* @return int the component id
*/
public function getFirstComponentID($component_array, $device_id = null)
{
if (!is_null($device_id) && isset($component_array[$device_id])) {
$component_array = $component_array[$device_id];
}
foreach ($component_array as $id => $array) {
return $id;
}
return -1;
}
}

View File

@ -1490,6 +1490,31 @@ function get_disks($device)
return dbFetchRows('SELECT * FROM `ucd_diskio` WHERE device_id = ? ORDER BY diskio_descr', array($device));
}
/**
* Get the fail2ban jails for a device... just requires the device ID
* an empty return means either no jails or fail2ban is not in use
* @param $device_id
* @return array
*/
function get_fail2ban_jails($device_id)
{
$options=array(
'filter' => array(
'type' => array('=', 'fail2ban'),
),
);
$component=new LibreNMS\Component();
$f2bc=$component->getComponents($device_id, $options);
if (isset($f2bc[$device_id])) {
$id = $component->getFirstComponentID($f2bc, $device_id);
return json_decode($f2bc[$device_id][$id]['jails']);
}
return array();
}
// takes the device array and app_id
function get_disks_with_smart($device, $app_id)
{

View File

@ -25,14 +25,7 @@ foreach ($graphs as $key => $text) {
echo '</div>';
}
$baseName=rrd_name($device['hostname'], array('app', 'fail2ban', $app['app_id']), '-');
$jails=array();
$jailGlob=$baseName.'*.rrd';
foreach (glob($jailGlob) as $jailrrd) {
$jail=str_replace($baseName, '', $jailrrd);
$jail=str_replace('.rrd', '', $jail);
$jails[]=$jail;
}
$jails=get_fail2ban_jails($device['device_id']);
foreach ($jails as $jail) {
$graph_type = 'fail2ban_jail';

View File

@ -2,18 +2,20 @@
use LibreNMS\RRD\RrdDefinition;
echo "fail2ban";
$name = 'fail2ban';
$app_id = $app['app_id'];
$options = '-O qv';
$mib = 'NET-SNMP-EXTEND-MIB';
$oid = 'nsExtendOutputFull.8.102.97.105.108.50.98.97.110';
$f2b = snmp_walk($device, $oid, $options, $mib);
$new_component = snmp_walk($device, $oid, $options, $mib);
update_application($app, $new_component);
update_application($app, $f2b);
$bannedStuff = explode("\n", $f2b);
$bannedStuff = explode("\n", $new_component);
$banned=$bannedStuff[0];
$total_banned=$bannedStuff[0];
$firewalled=$bannedStuff[1];
$rrd_name = array('app', $name, $app_id);
@ -22,7 +24,7 @@ $rrd_def = RrdDefinition::make()
->addDataset('firewalled', 'GAUGE', 0);
$fields = array(
'banned' =>$banned,
'banned' =>$total_banned,
'firewalled' => $firewalled,
);
@ -30,13 +32,17 @@ $tags = array('name' => $name, 'app_id' => $app_id, 'rrd_def' => $rrd_def, 'rrd_
data_update($device, 'app', $tags, $fields);
$int=2;
$jails=array();
while (isset($bannedStuff[$int])) {
list( $jail, $banned )=explode(" ", $bannedStuff[$int]);
list($jail, $banned) = explode(" ", $bannedStuff[$int]);
if (isset($jail) && isset($banned)) {
$jails[] = $jail;
$rrd_name = array('app', $name, $app_id, $jail);
$rrd_def = RrdDefinition::make()->addDataset('banned', 'GAUGE', 0);
$fields = array('banned' =>$banned);
$fields = array('banned' => $banned);
$tags = array('name' => $name, 'app_id' => $app_id, 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name);
data_update($device, 'app', $tags, $fields);
@ -44,3 +50,38 @@ while (isset($bannedStuff[$int])) {
$int++;
}
//
// component processing for fail2ban
//
$device_id=$device['device_id'];
$options=array(
'filter' => array(
'type' => array('=', 'fail2ban'),
),
);
$component = new LibreNMS\Component();
$f2b_components = $component->getComponents($device_id, $options);
// if no jails, delete fail2ban components
if (empty($jails)) {
if (isset($f2b_components[$device_id])) {
foreach ($f2b_components[$device_id] as $component_id => $_unused) {
$component->deleteComponent($component_id);
}
}
} else {
if (isset($f2b_components[$device_id])) {
$f2bc = $f2b_components[$device_id];
} else {
$f2bc = $component->createComponent($device_id, 'fail2ban');
}
$id = $component->getFirstComponentID($f2bc);
$f2bc[$id]['label'] = 'Fail2ban Jails';
$f2bc[$id]['jails'] = json_encode($jails);
$component->setComponentPrefs($device_id, $f2bc);
}