#!/usr/bin/perl -w use strict; use MIME::Lite; # monitor.pl - a program for monitoring critical processes on a Unix server # Copyright 2004, Tex Thompson my $emergency_email = 'contact@mail.com'; my $time_to_sleep = 60; my %vital_processes = ( 'httpd' => '/usr/local/apache/bin/apachectl start', 'pop-before-smtp' => '/etc/init.d/pop-before-smtp.init start', 'postfix' => '/etc/init.d/postfix start', 'mysql' => '/etc/init.d/mysql start', 'ssh' => '/etc/init.d/sshd start', 'syslog' => '/etc/init.d/syslog start' ); while (1) { my @process_listing = `ps ax`; foreach my $process ( keys %vital_processes ) { my @running = grep /$process/, @process_listing; my $num_processes = scalar @running; if ( $num_processes == 0 ) { # try to fix the problem my $time = localtime(); print "Process $process not found at $time!\n"; print "Executing command ",$vital_processes{ $process },"\n"; my $command = $vital_processes{ $process }; my $output = `$command`; # send a notification e-mail my $data = "$process not running at $time!\n"; $data .= "Tried to restart with command\n$command\n"; $data .= "Output:\n$output\n"; my $msg = MIME::Lite->build( From => 'root@biosysadmin.com', To => $emergency_email, Subject => "Emergency: $process down", Type => 'TEXT', Data => $data ); $msg->send; } } sleep( $time_to_sleep ); }