Code:
# cat space_check.pl
#!/usr/bin/perl
require "ctime.pl";
require "stat.pl";
open(HTYPE,"/etc/host-type");
$TYPE=<HTYPE>;
chop $TYPE;
$DATE=&ctime(time);
while (true)
{
$UNIX=`uname`;
chop $UNIX;
if ($UNIX ne SunOS) {
print "$UNIX system - not catered for\n";
exit;
}
# If the environment variable of LIMIT is set then it is assumed that the script is run in warning mode
# not alert mode. Warning mode means output to console, Alert mode output to syslog
$WLIMIT=$ENV{'LIMIT'};
if ($WLIMIT eq "") {
system("logger -p user.err MARK");
$DLIMIT=90;
}
else
{$DLIMIT=$WLIMIT;}
&filesystem;
&inodes;
&swap;
exit;
}
sub filesystem
{
open (DF,"(df -F ufs -k;df -F vxfs -k)|grep -v cdrom|");
while ($line = <DF>)
{
chop $line;
($filesystem, $size, $used, $avail, $capacity, $mountp) = split(' ',$line,6);
if ($filesystem ne "Filesystem")
{
$LIMIT="0\n";
open (LF,"${mountp}/.limit");
$LIMIT=<LF>;
chop $LIMIT;
if ($LIMIT==0) {
# if length is 1 then it is the root "/" file system
if (length($mountp)==1)
{ $LIMIT=95; }
else
{ $LIMIT=$DLIMIT; }
}
else
{
if ($WLIMIT ne "" ) {
printf "Filesystem %-40.40s has a limit file containing %s\n", $mountp,$LIMIT;
$LIMIT=$DLIMIT;
}
}
if (index($LIMIT,"m") > 0)
{
chop($LIMIT);
$cap=($avail / 1000);
if ($cap < $LIMIT) {
&get_owner;
$MESSAGE=sprintf "Filesystem %-40.40s has less than minimum space allowed, actual %d MB minimum is %d MB\n", $mountp,$cap,$LIMIT;
&logging($MESSAGE);
}
}
else
{
$cap=($used / $size) * 100;
if ($cap > $LIMIT) {
&get_owner;
$MESSAGE=sprintf "Filesystem %-40.40s is %2.2f full, limit is %2.2f, space available is %d kb\n", $mountp,$cap,$LIMIT,$avail;
&logging($MESSAGE);
}
}
}
}
}
sub inodes
{
$LIMIT=95.3;
$MIN=5000;
open (DF,"(df -F ufs -g;df -F vxfs -g)|grep -v cdrom|");
while ($line = <DF>)
{
@field = split(' ',$line);
$mountp=$field[0];
$line = <DF>;
@field = split(' ',$line);
$avail=$field[6];
$line = <DF>;
@field = split(' ',$line);
$left=$field[0];
$line = <DF>;
$line = <DF>;
$used=$avail-$left;
if ($left < $MIN) {
$MESSAGE=sprintf "Filesystem %-40.40s is below min inodes %6d left, minimum is %6d\n", $mountp,$left,$MIN;
&logging($MESSAGE);
}
}
}
sub swap
{
$HIGH=90;
$LOW=80;
open(SF,"swap -s|tr -d k|");
$line=<SF>;
chop $line;
@swapf = split(' ',$line);
$size=$swapf[8]+$swapf[10];
$used=$swapf[8];
$cap=($used / $size) * 100;
# if ($cap > $HIGH)
# { print "Problem Swap space is $cap full\n"; }
# elsif ($cap > $LOW)
# { print "Warning Swap space is $cap full\n"; }
if ($cap > $HIGH) {
$SWAPMESSAGE=sprintf "Swap Space is $cap full\n";
system("logger -p user.err '(SPACE)' Please call WIPRO UNIX SUPPORT $SWAPMESSAGE"); }
}
sub logging
{
if ($WLIMIT eq "" ) {
if (&check_if_we_report) {
if (&checklast)
{ system("logger -p user.err '(SPACE)' Please call WIPRO $CALLOUT $MESSAGE"); }
}
}
else
{ print $MESSAGE; }
}
sub check_if_we_report
{
open(HTYPE,"/etc/host-type");
$TYPE=<HTYPE>;
chop $TYPE;
$DATE=&ctime(time);
($dayofweek,$month,$day,$time,$tz,$year) = split(' ',$DATE,6);
($hh,$mm,$ss) = split(':',$time,3);
if ($TYPE ne DEV)
{return 1;}
else {
if ($hh >= 8) {
if ($hh < 17) {
if ($dayofweek ne Sat) {
if ($dayofweek ne Sun) {
return 1;
}
}
}
}
}
return 0;
}
sub checklast
{
return 1;
$now=time;
$mountp=~ s/\//./g;
$TAG="/tmp/.tag$mountp";
&Stat($TAG);
# $delay=3600;
$delay=1;
$diff=$now - $st_mtime;
if ($diff > $delay)
{
system("touch $TAG");
return 1;
}
return 0;
}
sub get_owner
{
if ($mountp eq "/" || $mountp eq "/rootbackup")
{
$CALLOUT="UNIX team";
}
else
{
open (LS,"ls -ld $mountp|");
$LINE=<LS>;
($one, $two, $OWNER, $four) = split(' ',$LINE,4);
$CALLOUT="Application support";
if ($OWNER eq sybase) {$CALLOUT="Sybase team"};
if ($OWNER eq oracle) {$CALLOUT="Oracle team"};
open (OF,"${mountp}/.callowner");
$FSOWNER=<OF>;
chop $FSOWNER;
if ($FSOWNER ne "") {
$CALLOUT=$FSOWNER
}
}
return 0;
}
0:0:0 /utl/sh