#!/usr/bin/perl -wT # # check diskspace op servers # # syntax: # ./check_disk --host= [--community=] # # jorg@wirelessleiden.nl - 16 april 2004 use strict; use Getopt::Long; use Net::SNMP; use XML::Simple; use Data::Dumper; # # settings # my $hostname = ""; my $community = "public"; my $warning = "88"; my $critical = "93"; my $file; GetOptions("hostname:s" => \$hostname, "community:s" => \$community, "warning:s" => \$warning, "critical:s" => \$critical, "file:s" => \$file); my @config; if (defined $file) { $file =~ /([\/\w\.\-]+)/s; # untaint if (-f $file) { my ($config) = XMLin($file, forcearray=>['partition']); if (defined $config->{'warning'}) { $warning = $config->{'warning'}; } if (defined $config->{'critical'}) { $critical = $config->{'critical'}; } if (defined $config->{'partition'}) { @config = @{$config->{'partition'}}; } } } my $errorcode = 0; my ($session, $error) = Net::SNMP->session(-hostname => $hostname, -community => $community, -timeout => 60, -retries => 1); if (! defined $session) { printf("ERROR: %s.\n", $error); exit 3; # unknown } # # get data # # hrFSMountPoint my $oid = ".1.3.6.1.2.1.25.3.8.1.2"; my $mountpoint = $session->get_table(-baseoid => $oid); if (! defined $mountpoint) { # apperantly no disks... printf("%s.\n", $session->error); exit 3; # unknown } my %mountpoint = %{$mountpoint}; # hrFSType $oid = ".1.3.6.1.2.1.25.3.8.1.4"; my $fstype = $session->get_table(-baseoid => $oid); if (! defined $fstype) { printf("ERROR (hrFSType): %s.\n", $session->error); exit 3; # unknown } my %fstype = %{$fstype}; # hrFSAccess $oid = ".1.3.6.1.2.1.25.3.8.1.5"; my $fsaccess = $session->get_table(-baseoid => $oid); if (! defined $fsaccess) { printf("ERROR (hrFSAccess): %s.\n", $session->error); exit 3; # unknown } my %fsaccess = %{$fsaccess}; # hrStorageDescr $oid = ".1.3.6.1.2.1.25.2.3.1.3"; my $descr = $session->get_table(-baseoid => $oid); if (! defined $descr) { printf("ERROR (hrStorageDescr): %s.\n", $session->error); exit 3; # unknown } my %descr = %{$descr}; # hrStorageSize $oid = ".1.3.6.1.2.1.25.2.3.1.5"; my $size = $session->get_table(-baseoid => $oid); if (! defined $size) { printf("ERROR (hrStorageSize): %s.\n", $session->error); exit 3; # unknown } my %size = %{$size}; # hrStorageUsed $oid = ".1.3.6.1.2.1.25.2.3.1.6"; my $used = $session->get_table(-baseoid => $oid); if (! defined $used) { printf("ERROR (hrStorageUsed): %s.\n", $session->error); exit 3; # unknown } my %used = %{$used}; # # rewrite data # my (%disk, %disk2); while (my ($key, $value) = (each %descr)) { $key =~ s/.*\.(\d+)$/$1/; $value =~ s/\\.*$//; $disk2{$key}{'descr'} = $value; } while (my ($key, $value) = (each %mountpoint)) { $key =~ s/.*\.(\d+)$/$1/; if ($value eq '') { # windows systems don't use 'mountpoint' $disk{$key}{'mountpoint'} = $disk2{$key}{'descr'}; } else { $disk{$key}{'mountpoint'} = $value; } } while (my ($key, $value) = (each %fstype)) { $key =~ s/.*\.(\d+)$/$1/; $value =~ s/.*\.(\d+)$/$1/; # 1=other, 2=unknown, 3=ffs, 9=ntfs, 14=nfs, 23=ext3 if (" 1 2 14 " =~ / $value /) { # ignore other, floppy, cdrom and nfs delete($disk{$key}); } } while (my ($key, $value) = (each %fsaccess)) { $key =~ s/.*\.(\d+)$/$1/; if ($value eq 2) { # ignore read-only devices delete($disk{$key}); } } while (my ($key, $value) = (each %size)) { $key =~ s/.*\.(\d+)$/$1/; if ($value != 0) { # is it a real disk $disk2{$key}{'size'} = $value; } else { delete($disk2{$key}); } } while (my ($key, $value) = (each %used)) { $key =~ s/.*\.(\d+)$/$1/; if (defined $disk2{$key}{'size'}) { $disk2{$key}{'used'} = $value; } } foreach my $key (keys %disk) { foreach my $key2 (keys %disk2) { if (defined $disk2{$key2}{'descr'}) { if ($disk{$key}{'mountpoint'} eq $disk2{$key2}{'descr'}) { $disk{$key}{'size'} = $disk2{$key2}{'size'}; $disk{$key}{'used'} = $disk2{$key2}{'used'}; } } } foreach my $partition (@config) { my %partition = %{$partition}; if ($disk{$key}{'mountpoint'} eq $partition{'mountpoint'}) { $disk{$key}{'warning'} = $partition{'warning'}; $disk{$key}{'critical'} = $partition{'critical'}; } } } undef %disk2; undef @config; # # parse data # my $text = ''; my $rrdinfo = ''; my $boldon = ''; my $boldoff = ''; foreach my $key (sort keys %disk) { if (defined $disk{$key}{'size'}) { my $percent = $disk{$key}{'used'} / $disk{$key}{'size'} * 100; my $rrdname = $disk{$key}{'mountpoint'}; $rrdname =~ s/_/__/g; # _ door 2x _ vervangen $rrdname =~ s/\//_/g; # / door _ vervangen my ($warning2, $critical2); if (defined $disk{$key}{'warning'}) { $warning2 = $disk{$key}{'warning'}; } else { $warning2 = $warning; } if (defined $disk{$key}{'critical'}) { $critical2 = $disk{$key}{'critical'}; } else { $critical2 = $critical; } if ($percent > $critical2) { $boldon = ''; $boldoff = ''; $errorcode = 2; } elsif ($percent > $warning2) { if ($errorcode < 1) { $errorcode = 1; } $boldon = ''; $boldoff = ''; } else { $boldon = ''; $boldoff = ''; } $text .= $boldon . $disk{$key}{'mountpoint'} . " " . sprintf("%.0f", $percent) . "%" . $boldoff . " "; $rrdinfo .= $rrdname . "," . sprintf("%.2f", $percent) . " "; } } chop($text); chop($rrdinfo); print "$text|$rrdinfo\n"; # # exit # (0=ok 1=warning 2=critical 3=unknown 4=dependent) # exit $errorcode;