1
0
mirror of https://github.com/librenms/librenms-agent.git synced 2024-05-09 09:54:52 +00:00

now requires cron usage

This commit is contained in:
VVelox
2017-02-20 03:49:50 -06:00
committed by GitHub
parent d90f3e8792
commit c367e9ff9d

View File

@ -2,42 +2,82 @@
# Add this to your snmpd.conf as below.
# extend fail2ban /etc/snmp/fail2ban
#
# Then add to your cron tab...
# */3 * * * * /root/snmp-extends/fail2ban.pl -u
#make sure this path is correct
my $f2bc="/usr/local/bin/fail2ban-client";
# The cache file to use.
my $cache='/var/cache/fail2ban';
##
## you should not have to touch anything below this
##
use strict;
use warnings;
use Getopt::Std;
#gets a list of jails
my $jailsOutput=`$f2bc status`;
my @jailsOutputA=split(/\n/, $jailsOutput);
my ( $jailsS )=grep( /Jail\ list/, @jailsOutputA );
$jailsS=~s/.*\://;
$jailsS=~s/\s//g;
my @jails=split(/\,/, $jailsS);
$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub main::VERSION_MESSAGE {
print "fail2ban-client SNMP extend 0.0.0\n";
};
#process jail
my $int=0;
my $total=0;
my $toReturn='';
while(defined($jails[$int])){
#get the total for this jail
my $jailStatusOutput=`fail2ban-client status $jails[$int]`;
my @jailStatusOutputA=split(/\n/, $jailStatusOutput);
my ( $jailTotal )=grep(/Currently\ banned\:/, @jailStatusOutputA);
$jailTotal=~s/.*\://;
$jailTotal=~s/\s//g;
#tally the total and add this jail to the list
$total=$total+$jailTotal;
$toReturn=$toReturn.$jails[$int].' '.$jailTotal."\n";
$int++;
sub main::HELP_MESSAGE {
print "\n".
"-u Update '".$cache."'\n";
}
print $total."\n".$toReturn;
#gets the options
my %opts=();
getopts('u', \%opts);
if (defined( $opts{u} )){
#gets a list of jails
my $jailsOutput=`$f2bc status`;
my @jailsOutputA=split(/\n/, $jailsOutput);
my ( $jailsS )=grep( /Jail\ list/, @jailsOutputA );
$jailsS=~s/.*\://;
$jailsS=~s/\s//g;
my @jails=split(/\,/, $jailsS);
#process jail
my $int=0;
my $total=0;
my $toReturn='';
while(defined($jails[$int])){
#get the total for this jail
my $jailStatusOutput=`fail2ban-client status $jails[$int]`;
my @jailStatusOutputA=split(/\n/, $jailStatusOutput);
my ( $jailTotal )=grep(/Currently\ banned\:/, @jailStatusOutputA);
$jailTotal=~s/.*\://;
$jailTotal=~s/\s//g;
#tally the total and add this jail to the list
$total=$total+$jailTotal;
$toReturn=$toReturn.$jails[$int].' '.$jailTotal."\n";
$int++;
}
open(my $writefh, ">", $cache) or die "Can't open '".$cache."'";
print $writefh $total."\n".$toReturn;
close($writefh);
exit 0;
}
my $old='';
open(my $readfh, "<", $cache) or die "Can't open '".$cache."'";
# if this is over 2048, something is most likely wrong
read($readfh , $old , 10240);
close($readfh);
print $old;