473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

whatsup gold perl script to monitor unix processes

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();
# }

}
}
}
Jul 19 '05 #1
0 6417

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bob | last post by:
why is perl referred to as a CGI script and not php, php has the facility to execute backticked execute commands that enable the programmer to use operating system facilities as well ? why...
52
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
7
by: dcrespo | last post by:
Hi to all, I'd like to have an app monitor that gets rid of another app, in the way that if it closes unspectedly, the app monitor just wake it up one more time, and viceversa. I mean: ...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
6
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation...
1
by: jonathan184 | last post by:
how to monitor and find out if files test1_* and test2_* files were sent in an hour and if not send an email This is on a unix system basically I got a cronjob that runs every sec polling a ftp dir...
2
by: statmatics | last post by:
Hello: (I posted related thread about a month ago. New information here.) My ISP tells me that the Perl script I run on their server creates more than 30 processes simultaneously during peak...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.