����JFIF��x�x����'
Server IP : 78.140.185.180 / Your IP : 3.146.37.183 Web Server : LiteSpeed System : Linux cpanel13.v.fozzy.com 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64 User : builderbox ( 1072) PHP Version : 7.3.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /usr/lib64/nagios/plugins/extra/ |
Upload File : |
#!/usr/bin/env perl use extreme; use File::Slurp; use DBI; use Carp; use Getopt::Long; my ( $warn, $crit, $limits, @w_incidents, @c_incidents ); sub help { print <<EOT; $0 [-w/--warn 1,2 -c/--crit 3,4] Plugin accepts two value for warn and crit to detect insufficient resources in each location: first parameter: the amount of memory left in the location second parameter: the size of the disk Default values: warn: 32,400 crit: 16,200 Allowed formats: --warn 1 --crit 1,2 For any unspecified params, corresponding default will be used EOT exit 3; } GetOptions( 'h|help' => \&help, 'w|warn=s' => \$warn, 'c|crit=s' => \$crit ); help() if $warn && $warn !~ /\d+,?/; help() if $crit && $crit !~ /\d+,?/; $warn //= ''; $crit //= ''; ( $limits->{w}{memory}, $limits->{w}{disk} ) = ( $1, $2 ) if $warn =~ /(\d+),?(\d+)?/; ( $limits->{c}{memory}, $limits->{c}{disk} ) = ( $1, $2 ) if $crit =~ /(\d+),?(\d+)?/; $limits->{w}{memory} //= 32; $limits->{w}{disk} //= 400; $limits->{c}{memory} //= 16; $limits->{c}{disk} //= 200; my $dbh = DBI->connect( 'DBI:mysql:panel' . ';mysql_read_default_file=/root/.my.cnf' . ';mysql_read_default_group=client', undef, undef, { RaiseError => 1, AutoCommit => 1, } ); #get max nodes resources my $sth = $dbh->prepare( 'SELECT name,floor(memory/1024) maxmemory,memory_overallocate,disk_overallocate,floor(disk/1024) maxdisk FROM nodes WHERE public=\'1\';' ) or croak "cannot prepare select statement!: $dbh->errstr()"; $sth->execute() or croak "cannot execute select statement!: $dbh->errstr()"; my $nodes_data = $sth->fetchall_hashref('name'); # change max nodes resources if overallocate for my $node ( keys $nodes_data->%* ) { $nodes_data->{$node}{maxmemory} = $nodes_data->{$node}{maxmemory} * ( $nodes_data->{$node}{memory_overallocate} / 100 + 1 ) if $nodes_data->{$node}{memory_overallocate} > 0; $nodes_data->{$node}{maxdisk} = $nodes_data->{$node}{maxdisk} * ( $nodes_data->{$node}{disk_overallocate} / 100 + 1 ) if $nodes_data->{$node}{disk_overallocate} > 0; } # get servers resources $sth = $dbh->prepare( 'SELECT nodes.name,servers.id,servers.node_id,round (servers.disk/1024) disk,round (servers.memory/1024) memory FROM servers LEFT JOIN nodes ON servers.node_id=nodes.id WHERE nodes.public=\'1\';' ) or die "cannot prepare select statement!: $dbh->errstr()"; $sth->execute or die "cannot execute select statement!: $dbh->errstr()"; my $servers_data = $sth->fetchall_hashref('id'); # count how many mem and disk used per node for my $server ( keys $servers_data->%* ) { $nodes_data->{ $servers_data->{$server}{name} }{memory} += $servers_data->{$server}{memory}; $nodes_data->{ $servers_data->{$server}{name} }{disk} += $servers_data->{$server}{disk}; } my $result; # calculate diff between real resource usage and node limit for my $node ( keys $nodes_data->%* ) { my $location = $2 if $node =~ /([^)]+)\.([^)]+).fozzy.games/; for my $res (qw(memory disk)) { $nodes_data->{$node}{$res} //= '0'; my $diff = $nodes_data->{$node}{"max$res"} - $nodes_data->{$node}{$res}; $result->{$location}{$res} += int $diff; } } #determine critical and warning incidents if any for my $location ( keys $result->%* ) { for my $limit ( keys $result->{$location}->%* ) { my $incident_msg = "$location location has left $result->{$location}{$limit} GB $limit "; push @c_incidents, $incident_msg if $result->{$location}{$limit} <= $limits->{c}{$limit}; push @w_incidents, $incident_msg if $result->{$location}{$limit} <= $limits->{w}{$limit}; } } if (@c_incidents) { say join( "\n", @c_incidents ); exit 2; } if (@w_incidents) { say join( "\n", @w_incidents ); exit 1; } say "OK"; for my $location ( sort keys $result->%* ) { print "$location: left "; for my $limit ( sort keys $result->{$location}->%* ) { print "$result->{$location}{$limit} GB $limit "; } print "\n"; }