views:

144

answers:

3

Hi everyone, i'm trying to make a logging function with php for a tool that captures computer information on Windows. Every 15 seconds the a txt file will be produced with information inside. i want to make it run in the background while the user access other pages and not be interrupted.

Here's the code i'm now using

<?php
        $tracker = $_GET['counter'];
        if (isset($tracker) && $tracker == 1)
        {
            $_SESSION['tracker'] = 1;

            $query = "SELECT hostname FROM computers ORDER BY hostname ASC";
            $computer_search = mysql_query($query);
            confirm_query($computer_search);

            while ($computers = mysql_fetch_array($computer_search)){
                $COMP = $computers['hostname'];
        $time = time();
        shell_exec('perl logger.pl ' . $COMP . ' >> logs/' . $COMP . '-' . $time . '.txt');
            }

            redirect_to('index.php?tracker=1');
        }
        elseif (isset($tracker) && $tracker == 0)
        {
            $_SESSION['tracker'] = 0;

            redirect_to('index.php?tracker=0');
        }
    ?>

At first i tried to use

    $query = "SELECT hostname FROM computers ORDER BY hostname ASC";
            $computer_search = mysql_query($query);
            confirm_query($computer_search);

            while ($computers = mysql_fetch_array($computer_search)){
                $COMP = $computers['hostname'];
                $time = time();
                // 1 is the counter entered into the perl script
                shell_exec('nohup perl logger.pl ' . $COMP . ' 1 > /dev/null 2> /dev/null >> logs/' . $COMP . '-' . $time . '.txt');
            }

But the code only captures information of one computer and gets stuck in the loop before capturing the next computer's information. On the other hand, the first code only captures data of all computers, but only once.

As for the logger.pl here's what i did.

    !/usr/bin/perl -w
    use BER;
    use SNMP_util;
    use SNMP_Session;

    $MIB1 = ".1.3.6.1.2.1.25.3.3.1.2"; #Cpu Processors
    $MIB2 = ".1.3.6.1.2.1.1.3"; #System Uptime
    $MIBIPAdd = ".1.3.6.1.2.1.4.20.1.1";

    $HOST = shift;
    # taken out for 1st code
    #$tracker = shift;

    #while ($tracker == 1)
    {
    print "Computer Name: $HOST\n";
    $count = 0;
    # ($MIB1) && ($HOST) || die "Usage: $0 MIB_OID HOSTNAME";
    (@values) = &snmpwalk("$HOST","$MIB1");
    foreach $value (@values)
        {
        $count++;
    if ($value) {
        $goodvalue = &strip_comment($value);
        print "CPU Usage of Processor $count: $goodvalue%\n"; }
        if ($value > 90){
            print "Warning: CPU Usage over 90%! \n"
        }
    else { warn "No response from host :$HOST:\n"; }
}

(@valuesIP) = &snmpwalk("$HOST","$MIBIPAdd");
$ipaddress = &strip_comment($valuesIP[1]);
print "IP Address: $ipaddress \n";

($value) = &snmpwalk("public\@$HOST","$MIB2");
if ($value) {
    $goodvalue = &strip_comment($value);
    print "System Up Time: $goodvalue\n"; }
else { print "No response from host: $HOST\n"; }

print "\n";
sleep(15);
    }

    sub strip_comment
    {
$theline = shift;

$where = index($theline, ":");

if($where eq -1){
    return $theline;
}

$selectedpart = substr($theline, $where);
$goodpart = substr($selectedpart, 1);


return $goodpart;
}
+2  A: 

you should use a cron job, create a script with the piece of code that you want to run every 15 seconds, then add a cron job that will schedule the script to be run every 15 seconds

ovais.tariq
Sorry, forgot to mention that i'm doing it on Windows.
vrerer
Use Windows Scheduled Tasks then.
Blair McMillan
@vrerer windows has scheduling tools as well. check windows manual for `AT` command
Col. Shrapnel
yeah you should then use windows scheduling tool as Col. Shrapnel has mentioned.
ovais.tariq
But i need to turn it on and off via a button on the web site itself.
vrerer
@vrererI'm not so sure you can just "stop" a php script from running via client side code. At best, another call to the server side php script would just run the script again.Now, if you have a dedicated server (i.e. C or Java) running then you could have a bit more "control" of the process by sending it shutdown messages or what not.
KennyCason
A: 

Use sleep(15); in your loop. It will use few resources until the timer expires.

stillstanding
i tried that and it causes the site to loop and then time out, the user is also not able to do anything on the site.
vrerer
you have to `set_time_limit(0)` if you plan to run the program indefinitely!
stillstanding
If you're on a sessions-based system, you'll have to `session_write_close()` before entering the loop. The default file-based session handler locks the session file while a script's actively using it, preventing any other requests from proceeding until the lock's released (script ends and/or session is closed).
Marc B
i would like it to run as a hidden process and not affect anything else. It's a button on the website and when the user click it, it will switch on information logging. i tried to both set_time_limit(0) and session write_close but both don't seem to work, the website does the logging once and then crashes.
vrerer
how about letting us know what the error message is when the program crashed? too little information helps no one.
stillstanding
The page will displayFatal error: Maximum execution time of 60 seconds exceeded in C:\Program_Files\wamp\www\backup of mp_site\autolog.php on line 15the Code:While ($SESSION['tracker'] ==1){while ($computers = mysql_fetch_array($computer_search)){ <--line 15 $COMP = $computers['hostname']; $time = time(); shell_exec('perl logger.pl ' . $COMP . ' >> logs/' . $COMP . '-' . $time . '.txt'); }}I added a While ($SESSION['tracker'] ==1){ to loop the process within the php script and instead of the perl script.
vrerer
where did u insert `set_time_limit(0)` in the first place? it's not having its effect because the error says u exceeded the max execution time
stillstanding
i tried at inside the while loop and at the start right after <?php, but the whole page just keep loading with nothing produced.
vrerer
there's something wrong with your program logic. even if you have a while loop that wakes up every 15 seconds, your program redirects to another page `redirect_to('index.php?tracker=1');`. that terminates your script entirely!
stillstanding