Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > whatsup gold perl script to monitor unix processes

Reply
Thread Tools

whatsup gold perl script to monitor unix processes

 
 
Danny Jensen
Guest
Posts: n/a
 
      06-28-2004
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();
# }

}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controlling processes and what to "feed" other processes Marc Heiler Ruby 1 05-24-2009 05:37 PM
monitor groove with whatsup paul b Computer Support 0 04-22-2005 09:56 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments