« Home | So, my favorite field is finally starting to produ... » | Never, under penalty of death*, should you order t... » | My Fall 2002 schedule is now available. All 400 l... » | When I meet someone new, eventually the conversati... » | I apologize for the lack of posts lately. I had t... » | UPDATE: Elaine made it to the list with a birthda... » | People that have wished me a happy birthday (in or... » | I was only awake for about 50 minutes today, my 22... » | "If you could change one thing about yourself what... » | How is this for a crazy workout mix: Bad Religion... »

Here is a little perl script that I wrote for work over the past couple days. Basically, our E10K (named CAD) had a hardware failure and the Oracle database struggled to come back up afterward. The senior UNIX admin was having trouble getting a Korn shell script to correctly report whether or not a process was running. Originally, this perl script would just grep the process list for one specific process and then shoot off an email to a list of admins. Now, it will look for any number of processes and send emails to any number of specified admins. The comments should explain it all. The only line that might need some explanation is this one:
foreach $recipient_number (@{$process_recipient_bridge[$process]}) {
process_recipient_bridge is an array of arrays (or list of lists, if you will). I want to get access to one of those internal lists. To get a single element in an array of arrays, you would use $array[index1][index2]. To get a whole array out, you still use $array[index1], but then you have to cast that as an array: @{$array[index1]}.

If you want to run this on a BSD, OS X or Linux system, make sure that you change "ps -ef" to "ps -ax" and "mailx" to just "mail".

#! /usr/bin/perl
#
#---------------------------------------------------------#
# CAD Process Alert Script #
# ------------------------ #
# #
# Usage: #
# Run as a cron job. The script will verify that #
# specific processes are running on CAD. If any #
# of the processes are not running, emails and #
# pages will be sent to specified admins. There #
# will be no output to log files or the terminal. #
# #
# Configuration: #
# Process list: This is an array of arrays. The #
# syntax is ["process_name", "alert"] for each #
# item. Process name is what the script will #
# look for in the process list. The alert is #
# a description of the problem that will be sent #
# to the admins. #
# #
# Recipient list: This is an array of email #
# accounts that will be sent an email when there #
# is a problem with a process on CAD. Make #
# sure to place a slash ("\") before the "@" #
# sign in all email addresses. #
# Example: "email\@CENSORED" #
# #
# Process to recipient bridge: This is a list #
# of lists. Each list corresponds to a specific #
# process (thus, the first list corresponds to #
# the first process). Said list contains the #
# indices for the email recipients (thus if a #
# list contains [4,7,10], the recipients at the 4, #
# 7, and 10 indices of the recipient list will be #
# sent an email. #
# #
#---------------------------------------------------------#


@process_list = (
["ora_pmon_citrix", "The ora_pmon_citrix process is not running on CAD.\n\n
Please check the status of your Citrix servers"],
["ora_pmon_prod", "The ora_pmon_prod process is not running on CAD.\n\n
Please check the status of your Iman services"],
["LISTENE", "The Oracle listener process is not running on CAD.\n\n
Please check the status of your Citrix servers."],
);

@recipient_list = (
"CENSORED"
);


@process_recipient_bridge = (
[0,1,2,3,5,6,7,8],
[1,6],
[0,1,2,3,5,6,7,8],
);

sub process_check {
my $process = shift(@_);
$process_name = $process_list[$process][0];
$ps_text = `ps -ef | grep $process_name | grep -v grep`;
if ($ps_text =~ /$process_name/) {
return (0);
}
return (1);
}

sub send_alert {
my $process = shift(@_);
$alert = $process_list[$process][1];
foreach $recipient_number (@{$process_recipient_bridge[$process]}) {
$recipient = $recipient_list[$recipient_number];
open (MAIL, "|mailx -s \"CAD Process Check Failed\" $recipient");
print MAIL $alert;
close MAIL;
}
}

for ($lcv=0; $lcv<@process_list; $lcv++) {
if ( process_check($lcv) == 1) {
send_alert($lcv);
}
}