6 use FS::Daemon qw(daemonize1 daemonize2 logfile sigint sigterm);
7 use FS::Yori qw(report);
12 &untaint_argv; #what it sounds like (eww)
15 getopts('m:p:', \%opt );
17 my ($machine, @emails) = @ARGV;
18 die &usage unless @emails;
20 warn "starting daemonization (forking)\n" if $DEBUG;
21 daemonize1('freeside-monitor');
22 #logfile( "%%%FREESIDE_LOG%%%/monitorlog.$machine" );
23 logfile( "/usr/local/etc/freeside/monitorlog.$machine" );
25 warn "completing daemonization (detaching))\n" if $DEBUG;
28 my $wantfree = $opt{m} || 1048576;
29 my $wantload = $opt{p} || 5;
31 die 'bogus memory requirement: $wantfree'
32 unless $wantfree && $wantfree =~ /^\d+$/;
34 die 'bogus load requirement: $wantload'
35 unless $wantload && $wantload =~ /^[\d.]+$/;
41 my(undef, $load, undef) = report('load');
42 my($free) = report('freememory');
44 unless( defined($load) && $load < $wantload
45 && defined($free) && $free > $wantfree
46 || ( time() < $last + 1800 && $alerts > 2 ) )
48 warn localtime(). ": $machine has load of $load and $free kB free memory\n";
50 $alerts = 0 if time() > $last + 1800;
52 foreach my $email ( @emails ) {
54 my $message = <<"__MESSAGE__";
55 From: support\@freeside.biz
57 Subject: ALERT - $machine
59 ALERT: $machine has a load of $load and only $free kB free..
63 my $sender = Email::Send->new({mailer => 'SMTP'});
64 $sender->mailer_args([Host => 'mail.freeside.biz']);
65 $sender->send($message);
74 warn "received TERM signal; exiting\n";
78 warn "received INT signal; exiting\n";
82 sleep 30; #too long? too short?
87 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
88 $ARGV[$_] =~ /^([\w\-\/\@\.]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
94 die "Usage:\n\n freeside-monitor [ -pm ] machine email\n";
99 freeside-monitor - Perform some basic load monitoring
103 freeside-monitor [ -p MAXLOAD ] [ -m REQUIRED_FRERMEM ] machine email [ ... ]
107 Load monitoring daemon. Should be running at all times.
109 -p: maximum permitted 5 minute load
111 -m: minimum free vmem in kB
113 machine: a unique name to be used in alert messages
114 email: address(es) to which alerts should be sent