whatsup gold perl script to monitor unix processes

D

Danny Jensen

I need to test if certain processes on a unix box were running. I
wanted to use whatsup gold to do the testing. First I needed to go to
the whatsup configure>monitors & services menu to add this tcp/ip port
1555 service with the folowing lines:

Send=psef /dj/myco/rf.monitor\r\n
Expect=~1

the psef above is a command that the unix server executes. The unix
box communicates back a 1 if the test is successful and a 0 if it is
not successful.
The server listens on 1555. I use my unix box as a network perl probe
to the rest of my network to run more complex tests.


Then I wrote this server to run on my unix box:
#!/opt/perl5/bin/perl -w
#================================================================
# program : whatsup server
# by : danny jensen
# date : 4/13/2002
#
use strict;
BEGIN { $ENV{PATH} = '/usr/bin:/bin' }
use IO::Socket;
use IO::Select;
use Carp;

my ($port, $lsock, $sock, $iaddr, $new, $name);
my ($select_sockets, $select_timeout, $line, $good_data);
my $in_line;

# -----------------------------------------
sub s100_psfind {
# ------------------------------------------
my (@psefout);
my $hpux_process;
my $outstr;
my $line;
$hpux_process=$_[0];
$outstr = `UNIX95= ps -eo args `;
#print $outstr;
@psefout=split("\n",$outstr); # easier to deal with array
foreach $line (@psefout) {
# print "line = $line\n";
if ( $line =~ /^$hpux_process/ ) {
return 1;
last;
}
}
return 0;
}

# -----------------------------------------
sub s150_processcommand {
# -----------------------------------------
my $commandline;
my (@cargs);
my $command;
my $arg;
my $stat;
my $smtp_cmd;
my $sock=$_[1];
$commandline=$_[0];
@cargs=split(/\s/,$commandline);
print "commandline = $commandline \n";
$command=$cargs[0];
my $smtp_status_file=$cargs[2];
my $smtp_server=$cargs[1];;
my $smtp_timer=$cargs[3];
my $smtp_to_addr=$cargs[4];
$arg=$cargs[1];
print "parsed cmd $command \n";
print "parsed arg $arg \n";
print "nexline\n";
if ( $command =~ /psef/ ) {
$stat=s100_psfind($arg);
print $sock "$stat\n";
print "inside psef \n";
}
elsif ( $command =~ /ping/ ) {
print "inside ping \n";
print $sock "1\n";
}
elsif ( $command =~ /smtptester/ ) {
print "inside smtptester";

my $smtp_status_file_path='/tmp/smtpstatus/' . $smtp_status_file;
print "looking for status file $smtp_status_file_path\n";
if ( -e ($smtp_status_file_path) ) {
print "found the status file \n";
print $sock "1\n";
}
else {
print $sock "0\n";
}
# launch test
$smtp_cmd="/opt/perl5/bin/perl /uv1/myco/PERL/smtptester
$smtp_server $smtp_status_file $smtp_timer $smtp_to_addr";
$stat=system($smtp_cmd);
}

return;
}


# -----------------------------------------
# Subroutine for fancy output.
# -----------------------------------------
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }

# -----------------------------------------
# Create listen socket
# -----------------------------------------
$port = shift || 1555;
$lsock = IO::Socket::INET->new(Listen => 5,
Reuse => SO_REUSEADDR,
Type => SOCK_STREAM,
LocalPort => $port,
Proto => 'tcp');

logmsg "server started on port $port";

# ---------------------------------------------------------
# Add the listen socket to those being monitored by "select"
# ---------------------------------------------------------
$select_sockets = IO::Select->new($lsock);

$select_timeout = '2'; # Poll every 2 seconds

#$sock;
my @ready = ();

while('1') { # Go into forever loop
#-----------------------------------------
# See if any sockets have data to be read.
#-----------------------------------------
@ready = $select_sockets->can_read($select_timeout);
foreach $sock ( @ready) {
$sock->autoflush();
# print "after auto flush \n";

# ---------------------------------------------------
# See if readable socket is the listen socket and if
# is then accept the new connection and add that socket
# to the "select" list.
# ---------------------------------------------------
if ( $sock == $lsock ) {
print "sock e lsock \n";
$new = $lsock->accept();
$select_sockets->add($new);
$iaddr = $new->peeraddr();
$port = $new->peerport();
$name = gethostbyaddr($iaddr, AF_INET);
# logmsg "Connection established with $name [",
# inet_ntoa($iaddr), "] at port $port";
print $lsock "OK\n";
} else {
# ---------------------------------------------------
# Otherwise a client connection is readable. Read what
# they sent and send them back some stuff. For now
# just remove the client from the select list and close
# the socket. Could leave it open if we want to have
# a more extended conversation with them.
# ---------------------------------------------------
$iaddr = $sock->peeraddr();
$port = $sock->peerport();
$name = gethostbyaddr($iaddr, AF_INET);
$line = undef;
$in_line = undef;
# print $sock "OK\n";
# $sock->recv($line,80);
$line=$sock->getline;
print "this is the line = $line \n";
s150_processcommand($line,$sock);
# logmsg "$name [", inet_ntoa($iaddr),
# "] at port $port sends: $line";
# print $sock "Hello there, $name, it's now ",
# scalar localtime, "\n";
# print $sock "DONE \n";
$select_sockets->remove($sock);
$sock->close();

# ---------------------------------------------------
# This is just a cleanup trap in case the socket is
# closed on the client side. If it is it will "select"
# as readable but there will be no data.
# ---------------------------------------------------
# unless( defined($good_data) ) {
# $select_sockets->remove($sock);
# $sock->close();
# }

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top