Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sleep not sleeping

Status
Not open for further replies.

tommycahir1

Programmer
Feb 20, 2004
34
0
0
IE
hi all i trying to put the cgi script to sleep for 60 seconds but it doesnt appear to work as it calls the save_graph function almost immediately..
any ideas on what i have done wrong..

my $index = 1;
while ($index <= $NoOfIf )
{
my $pid = fork();
die "Cannot fork: $!" unless defined($pid);
if ($pid == 0)
{
# Child proces
recieved_packets($now,$then,$index);
exit(0);
}
$index++;
}
my $graphxlabel = "Time";
my $graphylabel = "No. Of Packets Recieved";
my $graphtitle = "No. Of Packets Recieved On Interface No. ";
my $infile = "indata";
sleep(60);
save_graph($graphtitle,$graphxlabel,$graphylabel,$NoOfIf,$infile);
print_graph;

To explain the code above better would be the recieved packets creates a file possibly multiple files if there are more than one process. then the script should sleep for 60 seconds(due to save file trying to read files that werent created yet).then read these created files and save a graph image then print_graph displays these images to screen ..
 
On some systems, the function sleeps till the "top of the second" so for instance a sleep 1 may sleep anywhere from 0 to 1 second, depending on when in the current second you started sleeping ... You may also be able to use syscall to call the gettimer(2) and settimer(2) routines that some *NIX systems support

Don't really know the relevance of this paragraph, what OS are you running this on?

--Paul
 
Tommy,

If you create a one line script

sleep(10);

How long does it sleep for?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
it sleeps for the set amount of time this bugging me as it works in other areas of the script but not on the code snippet:??????????
 
I wonder if it's sleeping until it gets a signal to say that a child process has finished...

Have you tried using the select() function?
Code:
select(undef, undef, undef, 60);
From the documentation page though:
You can effect a sleep of 250 milliseconds this way:

select(undef, undef, undef, 0.25);
Note that whether select gets restarted after signals (say, SIGALRM) is implementation-dependent.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
tried that mike but it just doesnt work..
maybe its somethging to do with the way im forking processes?
 
that's what I was thinking

if it's the child process finishing that's triggering the wakeup from sleep() you should be able to test this by calling sleep() once more than the number of processes you fork(), try fork()ing just the one process and calling sleep() twice

or I could be talking through my ...... never mind, I could just be wrong

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
This is messy, might it give you a work around for the moment.

Call an indepedent script which creates a stub file (which you pass from your script). This script waits for 60 seconds, and deletes the file.

Meanwhile, back at the ranch, your script sits in a loop waiting for the file to disappear.

Basic semaphore, prone to housekeeping if your scripts break, or are killed on a regular basis, but it does give an extra level of control

Just a thought
--Paul
 
I might also be talking !@£$% but isn't save_graph a subroutine?

if it is - when it is called wouldn't it will bypass the sleep?????


Kind Regards
Duncan
 
i really hope i'm not being a muppet but would this work by calling the [red]&saveGraph[/red] instead...

my $graphxlabel = "Time";
my $graphylabel = "No. Of Packets Recieved";
my $graphtitle = "No. Of Packets Recieved On Interface No. ";
my $infile = "indata";

&saveGraph;
sleep(60);
save_graph($graphtitle,$graphxlabel,$graphylabel,$NoOfIf,$infile);
print_graph;


Kind Regards
Duncan
 
Hi Tommy, any luck?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
nah no luck at all this really confusing me as i have the sleep going in another part of the cgi script ... if you want i can post up the whole script if it any help to you..
 
ok, sounds useful in this instance, and the CGI bit's I don't understand others can help with I'm sure

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
the code is as follows i no that it prob really messy and untidy and that there prob loads way better ways to do stuff in perl so forgive my amatuer attempt at it
<code>
#! /usr/bin/perl -w


use strict;
use POSIX ":sys_wait_h";
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
use Socket;
use Net::SNMP;
use GD::Graph::lines;
use GD::Graph;

our $sysDescr = "1.3.6.1.2.1.1.1.0";
our $sysUpTime = "1.3.6.1.2.1.1.3.0";
our $sysContact = "1.3.6.1.2.1.1.4.0";
our $sysName = "1.3.6.1.2.1.1.5.0";
our $sysLocation = "1.3.6.1.2.1.1.6.0";

our $ifOutUcastPkts = "1.3.6.1.2.1.2.2.1.17.";
our $ifOutNUcastPkts = "1.3.6.1.2.1.2.2.1.18.";
our $ifOutDiscards = "1.3.6.1.2.1.2.2.1.19.";
our $ifOutErrors = "1.3.6.1.2.1.2.2.1.20.";

our $ifInUcastPkt = "1.3.6.1.2.1.2.2.1.11.";
our $ifInNUcastPkt = "1.3.6.1.2.1.2.2.1.12.";
our $ifInDiscard = "1.3.6.1.2.1.2.2.1.13.";
our $ifInError = "1.3.6.1.2.1.2.2.1.14.";
our $ifInUnknownProto = "1.3.6.1.2.1.2.2.1.15.";

our $ifNumber = "1.3.6.1.2.1.2.1.0";

our $snmp_host;
our $snmp_community;
my $choice = param('Graphed');
our $returned = param('InformationList');
our $time = param('Time');
my $address = param ('IPAddressField');

if ($address)
{
$snmp_host = $address;
$snmp_host = inet_ntoa( scalar gethostbyname( $snmp_host ) );

}else
{
errormsgs("please enter an address");
}

my $community = param('CommunityNameField');
if($community)
{
$snmp_community = $community;
}else
{
errormsgs("Please enter a community Name")
}

my $snmp_version = '2c';
my $snmp_port = 161;

my ( $snmp_session, $snmp_error ) = Net::SNMP->session(
-hostname => $snmp_host,
-community => $snmp_community,
-version => $snmp_version,
-port => $snmp_port,
-debug => 0 );

if ( !defined( $snmp_session ) )
{
die "Find it: an error occurred: ", $snmp_error, "\n";
}

sub zombie_reaper {
while ( waitpid( -1, WNOHANG ) > 0 )
{ }
$SIG{CHLD} = \&zombie_reaper;
}
$SIG{CHLD} = \&zombie_reaper;

sub errormsgs
{
my $input = shift(@_);
print header;
print start_html('Error');
print qq(<h1><center>Error</h1></center>);
print qq(<h3><center><p><p>$input\n\n</p></p><center></h3>);
print end_html;
}

sub single_value
{
my $sysOIDs = shift(@_);
my $responsePDU = $snmp_session->get_request( $sysOIDs );
if ( !defined( $responsePDU ) )
{
warn "Find it: ", $snmp_session->error, "\n";
}
print header;
print qq(<html>);
print qq(<head>);
print qq(<title>Findit Results</title>);
print qq(</head>);
print qq(<body bgcolor = lemonchiffon>);
print qq(<h1><center>Find it Results</h1></center><br><br>);
print qq(<center><h3>$returned</h3>),$responsePDU->{$sysOIDs };
print qq(</center>);
print end_html;
}

sub print_arrays
{
my($x) = @_;
print header;
for my $item1 ( @$x )
{
print qq($item1 \n);
}
print qq(<br>);
}
 
sub save_graph
{
my ($title,$xlabel,$ylabel,$IfNum,$fn) = @_;
my $numfile = 1;
my $fnbase = $fn;
my $titlebase = $title;
while ($numfile <= $IfNum)
{
$title = $titlebase;
$fn = $fnbase;
$fn = $fn.$numfile;
my @xarray = ();
my @values = ();
my $max = 0;
my $counter = 0;
open my $file, $fn or die $!;
my $line;
$max = readline($file);

while ($counter != $max)
{
$line = readline($file) or die $!;
chop($line);
@xarray[$counter] = $line;
$line = readline($file) or die $!;
chop($line);
@values[$counter] = $line;
$counter++;
}

close ($file) or die $!;
#unlink $fn;
my @data = (\@xarray,\@values);
$title = $title.$numfile;
my $graph = new GD::Graph::lines(500,300);
$graph->set
(
x_label => $xlabel,
y_label => $ylabel,
title => $title,
x_label_skip => 'auto',
y_label_skip => 'auto',
line_width => 1,
y_tick_number => 8,
x_labels_vertical => 1,
x_label_position => 1/2,
transparent => 0,
);

my $gd_image = $graph->plot(\@data) or die $graph->error;

my $image = "image";
$image = $image.$numfile.".png";
open IMG, ">$image" or die "could not open $image: $!\n";
binmode IMG or die $!;
print IMG $gd_image->png or die $!;
close IMG or die $!;
$numfile++;
}
}

sub print_graph
{
print header;
print qq(<html>);
print qq(<head>);
print qq(<title>Results</title>);
print qq(</head>);
print qq(<body bgcolor = lemonchiffon>);
print qq(<CENTER><H1>Results</h1><CENTER><br><br>);
print qq(<h3><img SRC = " alt = "should be pic ere"></h3>);
print qq(</body>);
print qq(</html>);
}
 

sub recieved_packets
{
my ($innow,$inthen, $index) = @_;

my $ifInUcastPktvar = $ifInUcastPkt.$index;
my $ifInNUcastPktvar = $ifInNUcastPkt.$index;
my $ifInDiscardvar = $ifInDiscard.$index;
my $ifInErrorvar = $ifInError.$index;

my @sysOIDs = ($ifInUcastPktvar, $ifInDiscardvar,$ifInErrorvar,$ifInNUcastPktvar);
my $counter = 0;
my @x_axis_array = ();
my @values_array = ();
my $responsePDU = $snmp_session->get_request( @sysOIDs );
my $ifInUcastPktsno = $responsePDU->{$ifInUcastPktvar};
my $ifInDiscardsno = $responsePDU->{$ifInDiscardvar};
my $ifInErrorsno = $responsePDU->{ $ifInErrorvar};
my $ifInNUcastPktsno = $responsePDU->{$ifInNUcastPktvar};
my $original = $ifInUcastPktsno + $ifInDiscardsno + $ifInErrorsno+ $ifInNUcastPktsno;

while (($innow = time) < $inthen)
{#start while

$responsePDU = $snmp_session->get_request( @sysOIDs );
$ifInUcastPktsno = $responsePDU->{ $ifInUcastPktvar};
$ifInDiscardsno = $responsePDU->{ $ifInDiscardvar};
$ifInErrorsno = $responsePDU->{ $ifInErrorvar};
$ifInNUcastPktsno = $responsePDU->{$ifInNUcastPktvar};

my $current = $ifInUcastPktsno + $ifInDiscardsno + $ifInErrorsno + $ifInNUcastPktsno;
my ($sec,$min,$hour) = gmtime;
my $time = "$hour:$min:$sec";
my $in = $current - $original;
$original = $current;
@x_axis_array[$counter] = $time;
@values_array[$counter] = $in;
$counter++;
sleep(2);
}#end while

my $whileindex = 0;
my $tmp_file = "indata";
$tmp_file = $tmp_file.$index;
open my $tmpfile, ">$tmp_file" or die $!;
print $tmpfile $counter,"\n" or die $!;
while ($whileindex <= $counter)
{
print $tmpfile @x_axis_array[$whileindex],"\n", @values_array[$whileindex],"\n" or die $!;
$whileindex++;
}
close $tmpfile or die $!;
}
 

sub sent_packets
{
my ($outnow,$outthen,$index) = @_;

my $ifOutUcastPktvar = $ifOutUcastPkts.$index;
my $ifOutDiscardvar = $ifOutDiscards.$index;
my $ifOutErrorvar = $ifOutErrors.$index;

my @outx_axis_array;
my @outvalues_array;
#pktsOutIfToNet = (ifOutUcastPkts) - ifOutDiscards - ifOutErrors
my @outsysOIDs = ($ifOutUcastPktvar, $ifOutDiscardvar,$ifOutErrorvar);
my $outcounter = 0;

my $outresponsePDU = $snmp_session->get_request( @outsysOIDs );
my $ifOutUcastPktsno = $outresponsePDU->{$ifOutUcastPktvar};
my $ifOutDiscardsno = $outresponsePDU->{ $ifOutDiscardvar};
my $ifOutErrorsno = $outresponsePDU->{ $ifOutErrorvar};
my $outoriginal = ($ifOutUcastPktsno) - $ifOutDiscardsno - $ifOutErrorsno;

while(($outnow = time) < $outthen)
{#start while time

$outresponsePDU = $snmp_session->get_request( @outsysOIDs );
$ifOutUcastPktsno = $outresponsePDU->{$ifOutUcastPktvar};
$ifOutDiscardsno = $outresponsePDU->{$ifOutDiscardvar};
$ifOutErrorsno = $outresponsePDU->{ $ifOutErrorvar};

my $outcurrent = ($ifOutUcastPktsno) - $ifOutDiscardsno - $ifOutErrorsno;
my ($sec,$min,$hour) = gmtime;
my $time = "$hour:$min:$sec";
my $out = $outcurrent - $outoriginal;
$outoriginal = $outcurrent;

@outx_axis_array[$outcounter] = $time;
@outvalues_array[$outcounter] = $out;
$outcounter++;
sleep(2);
}#end while time

my $outwhileindex = 0;
my $out_tmp_file = "outdata";
$out_tmp_file = $out_tmp_file.$index;
open my $out_tmpfile, ">$out_tmp_file" or die $!;
print $out_tmpfile $outcounter,"\n" or die $!;
while ($outwhileindex <= $outcounter)
{
print $out_tmpfile @outx_axis_array[$outwhileindex],"\n", @outvalues_array[$outwhileindex],"\n" or die $!;
$outwhileindex++;
}
close $out_tmpfile or die $!;

}
 

if($choice eq "Normal")
{
if ($returned eq "Name")
{
single_value($sysName);
}elsif($returned eq "Location")
{
single_value($sysLocation);
}elsif($returned eq "Description")
{
single_value($sysDescr);
}elsif($returned eq "System Contact")
{
single_value($sysContact);
}elsif($returned eq "System Uptime")
{
single_value($sysUpTime);
}elsif($returned eq "No. Packets Recieved" or $returned eq "No. Packets Sent")
{
errormsgs ("Please choose graphical mode for this function");
}

}
elsif($choice eq "Graph")
{#start choice = graph

my $minute = $time;
my $now = time;
my $then = $now + (60 * $minute);

my $responsePDU = $snmp_session->get_request( $ifNumber );
my $NoOfIf = $responsePDU->{$ifNumber};

if($returned eq "No. Packets Recieved")
{#start recieve
my $index = 1;
while ($index <= $NoOfIf )
{
my $pid = fork();
die "Cannot fork: $!" unless defined($pid);
if ($pid == 0)
{
# Child proces
recieved_packets($now,$then,$index);
exit(0);
}
$index++;
}
my $graphxlabel = "Time";
my $graphylabel = "No. Of Packets Recieved";
my $graphtitle = "No. Of Packets Recieved On Interface No. ";
my $infile = "indata";

sleep(60);
save_graph($graphtitle,$graphxlabel,$graphylabel,$NoOfIf,$infile);
print_graph;
#end recieve
}elsif($returned eq "No. Packets Sent")
{#start sent
my $index = 1;

while ($index <= $NoOfIf )
{
my $outpid = fork();
die "Cannot fork: $!" unless defined($outpid);
if ($outpid == 0)
{
# Child proces
sent_packets($now,$then,$index);
exit(0);
}
$index++;
}
sleep(5);
my $outgraphxlabel = "Time";
my $outgraphylabel = "No. Of Packets Sent";
my $outgraphtitle = "No. Of Packets Sent On Interface No. ";
my $outfile = "outdata";
save_graph($outgraphtitle,$outgraphxlabel,$outgraphylabel,$NoOfIf,$outfile);
print_graph;
#end sent
}elsif($returned eq "Name" or $returned eq "Location" or $returned eq "System Contact" or $returned eq "System Uptime ")
{#start text
errormsgs ("Please choose Text mode for this function");
}#end text

}#end chioce graph
$snmp_session->close;
there that it all sorry bout it been so broken up over mulitple replys but could only fit so much in one reply

tanx in advance for any help
 
:-( That's long tommy, I'm not going to be able to look at all of that.

Can you reproduce the problem in a short script?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
hi Mike
thanks for all your help it greatly appreciated i after managing to get a way round the problem by rewriting some of my code

thanking you
tommy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top