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 warn "free is $free and wantfree is $wantfree\n" if $DEBUG > 1;
45 warn "load is $load and wantload is $wantload\n" if $DEBUG > 1;
46 warn "last is $last\n" if $DEBUG > 1;
48 unless( defined($load) && $load < $wantload
49 && defined($free) && $free > $wantfree
50 || ( time() < $last + 1800 && $alerts > 2 ) )
52 warn localtime(). ": $machine has load of $load and $free kB free memory\n";
54 $alerts = 0 if time() > $last + 1800;
56 foreach my $email ( @emails ) {
58 my $message = <<"__MESSAGE__";
59 From: support\@freeside.biz
61 Subject: ALERT - $machine
63 ALERT: $machine has a load of $load and only $free kB free..
67 my $sender = Email::Send->new({mailer => 'SMTP'});
68 $sender->mailer_args([Host => 'mail.freeside.biz']);
69 $sender->send($message);
78 warn "received TERM signal; exiting\n";
82 warn "received INT signal; exiting\n";
86 sleep 30; #too long? too short?
91 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
92 $ARGV[$_] =~ /^([\w\-\/\@\.]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
98 die "Usage:\n\n freeside-monitor [ -pm ] machine email\n";
103 freeside-monitor - Perform some basic load monitoring
107 freeside-monitor [ -p MAXLOAD ] [ -m REQUIRED_FRERMEM ] machine email [ ... ]
111 Load monitoring daemon. Should be running at all times.
113 -p: maximum permitted 5 minute load
115 -m: minimum free vmem in kB
117 machine: a unique name to be used in alert messages
118 email: address(es) to which alerts should be sent