diff --git a/LibreNMS/Component.php b/LibreNMS/Component.php index d9fa977458..ae42fa600e 100644 --- a/LibreNMS/Component.php +++ b/LibreNMS/Component.php @@ -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; + } } diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index ba0221833f..4cf133d48b 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -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) { diff --git a/html/pages/device/apps/fail2ban.inc.php b/html/pages/device/apps/fail2ban.inc.php index 17e98c44b8..c3694f2fb4 100644 --- a/html/pages/device/apps/fail2ban.inc.php +++ b/html/pages/device/apps/fail2ban.inc.php @@ -25,14 +25,7 @@ foreach ($graphs as $key => $text) { echo ''; } -$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'; diff --git a/includes/polling/applications/fail2ban.inc.php b/includes/polling/applications/fail2ban.inc.php index 327a033852..765e8d30f9 100644 --- a/includes/polling/applications/fail2ban.inc.php +++ b/includes/polling/applications/fail2ban.inc.php @@ -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); +}