erikb

Prev Next TECH TECH
SMS warning on low file quote this [2005-03-23]

This piece of code is mainly intended for our department staff. We have a free (as in free beer) SMS gateway which allows us to email our mobile phones. The email is converted to a SMS message of course. I think many operators have this kind of gateway even if they don't always like to inform customers of it.

This code is meant to run regularly, using your WinXP scheduler or unix cron. You or your sysadmin needs to install the perl package Quota first.

#!/it/sw/gnu/bin/perl
use warnings;
# see http://search.cpan.org/dist/Quota/
use Quota;

$debug = 0;

# what quota to check, change this to your home directory
$path = "/home/USERNAME";

# lower than this and a warning is sent, in kB
$limit = 50000;

# who to notify, using your cell phone SMS gateway
# UU/IT staff, see: http://www.its.uu.se/tjanster/minicall.html
$to = "0123456789\@sms.OPERATOR.COM";

# mailer
$mailprog = '/usr/lib/sendmail';

# must call with arg "check"
if (@ARGV > 0 && $ARGV[0] eq "check") {
  $space = &checkquota();
  if ($debug) { print STDOUT "quota left is $space\n"; }

  if ($space < $limit) {
    &notify($space);
  } else {
    if ($debug) { print STDOUT "safe quota\n"; }
  }
}

sub checkquota() {
  $dev = Quota::getqcarg($path);
  ($bc,$bs,$bh,$bt,$ic,$is,$ih,$it) = Quota::query($dev);
  if ($debug) { print STDOUT "$bc,$bs,$bh,$bt,$ic,$is,$ih,$it"; }

  return ($bs-$bc);
}

sub notify {
  my ($str) = shift (@_);

  open (MAIL, "|$mailprog $to") || die "Can't open $mailprog!\n";
  print MAIL "Subject: $path quota $str\n";
  close (MAIL);
}

Prev Next