Site-script to send an email alert to an address when the AKIPS hard drive space hits a configured threshold.

  • Updated

The script will run every min/hour to send an email to the configured email address if the disk space hits a threshold.

If you would like the script to run every hour, then you would name the script as per the help:

  • sched_Nm_             Every N minutes (e.g. sched_15m_do_stuff)
  • sched_Nh_             Every N hours (e.g. sched_1h_do_more_stuff)
  • sched_HHMM_      Run at a scheduled time (e.g. sched_0930_report)

Notes:

  • fill out the “mail_to” value
  • fill out the “free_disk_space_threshold” value
  • fill out the “subject” value
sub custom_low_disk_space_alert
{
   my $mail_to = ''; # e.g. email.address@example.com
   my $profile = ''; # e.g. NetEng
   my $free_disk_space_threshold = '149G'; # e.g 100M 1.5G 6.45T
   my $subject = "Disk space"; # subject message

   my $zpool_list  = `zpool list -o free`;
   my %metric_prefix = ('M', 1000000, 'G', 1000000000, 'T', 1000000000000);
   my $free_result = '';
   my $threshold_metric_prefix = '';
   my $free_threshold;
   my $free_metric_prefix = '';
   my $free;
   my @bottom_data;
   my @body;
   my @zpool_results;

   # `zpool list -o free` output example
   # FREE
   # 59.9G

   @zpool_results = split('\n', $zpool_list);
   $free_result = $zpool_results[1];

   #The below line that is commented is for overriding what the zpool is sending us so you can set it to whatever you like to test.
   #$free_result = "100M"; 

   $free_metric_prefix = $free_result;
   $free_metric_prefix =~ tr/A-Za-z//dc;
   $free = $free_result;
   $free =~ s/[^0-9.]//g;
   $free = $free * $metric_prefix{$free_metric_prefix};

   $threshold_metric_prefix = $free_disk_space_threshold;
   $threshold_metric_prefix =~ tr/A-Za-z//dc;
   $free_threshold = $free_disk_space_threshold;
   $free_threshold =~ s/[^0-9.]//g;
   $free_threshold = $free_threshold * $metric_prefix{$threshold_metric_prefix};

   if ($free <= $free_threshold) {
      push @body, join(" ", "Free disk space is:", $free_result, "\nYou exceeded your threshold of:", $free_disk_space_threshold);

      # Send to a single email address
      if ($mail_to ne "") {
         mail ({ subject => $subject, to => $mail_to, body => \@body });
      }

      # Send to all email addresses in a profile
      if ($profile ne "") {
         for my $addr (config_get_emails ($profile)) {
             mail ({ subject => $subject, to => $addr, body => \@body });
         }
      }
   }
#Uncomment the below print for testing, to see what is sent in the email.
#print @body;   
}

Was this article helpful?

/

Comments

0 comments

Please sign in to leave a comment.