Skip to content

Instantly share code, notes, and snippets.

@rushipkar90
Created September 25, 2015 16:26
Show Gist options
  • Save rushipkar90/a2d36219308f5e926b21 to your computer and use it in GitHub Desktop.
Save rushipkar90/a2d36219308f5e926b21 to your computer and use it in GitHub Desktop.
check-server-bandwidth.pl
#!/usr/local/cpanel/3rdparty/perl/514/bin/perl
use RRDs;
use Getopt::Long;
my $server;
my $direction;
GetOptions(
"server=s" => \$server,
"direction=s" => \$direction,
);
if (!$server || ($direction ne 'TX' && $direction ne 'RX')) {
print "Usage: $0 --server=hpXX.hostpapa.com --direction=(TX|RX)\n";
exit;
}
my @parts = split('\.', $server);
my $nagios_server = shift(@parts);
my $rrd_directory = '/var/lib/munin/hostpapa.com/';
my @plugins;
if ($direction eq 'TX') {
@plugins = qw(if_eth0-up-d if_eth1-up-d);
} else {
@plugins = qw(if_eth0-down-d if_eth1-down-d);
}
my $total_bandwidth = 0;
my $limit = '20';
my $warn = '15';
for my $plugin (@plugins) {
my $rrd_file = "$rrd_directory/$server-$plugin.rrd";
my $ctime = time;
my $rrdres = 300;
my $retrieve_time = $ctime - $rrdres;
my ($start,$step,$names,$data) = RRDs::fetch $rrd_file, "MAX", "-s", $retrieve_time;
my $server_bandwidth = 0;
for my $line (@$data) {
for my $val (@$line) {
if ($val > 0) {
$server_bandwidth = $val;
}
}
}
$total_bandwidth += $server_bandwidth;
}
my $total_mbits = sprintf("%.2f", ($total_bandwidth * 8) / 1000000);
if (int$total_mbits > $limit) {
#system(qq!echo -e "$nagios_server\tBandwidth - $direction\t2\tCRITICAL - $total_mbits Mbit"!);
system(qq!POSIXLY_CORRECT=1;/bin/echo -e "$direction CRITICAL - $total_mbits Mbit"!); exit 2;
} elsif ($total_mbits > $warn) {
#system(qq!echo -e "$nagios_server\tBandwidth - $direction\t1\tWARNING - $total_mbits Mbit"!);
system(qq!POSIXLY_CORRECT=1;/bin/echo -e "$direction WARNING - $total_mbits Mbit"!); exit 1;
} else {
#system(qq!echo -e "$nagios_server\tBandwidth - $direction\t0\tOK - $total_mbits Mbit"!);
system(qq!POSIXLY_CORRECT=1;/bin/echo -e "$direction OK - $total_mbits Mbit"!); exit 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment