0

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;
}
vrerer
  • 31
  • 3

3 Answers3

2

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
  • 2,627
  • 17
  • 12
0

Windows CRON What is the Windows version of cron?

Community
  • 1
  • 1
abel
  • 2,377
  • 9
  • 39
  • 62
0

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

bcosca
  • 17,371
  • 5
  • 40
  • 51
  • 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 Aug 16 '10 at 11:35
  • you have to `set_time_limit(0)` if you plan to run the program indefinitely! – bcosca Aug 16 '10 at 12:29
  • 1
    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 Aug 16 '10 at 15:12
  • 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 Aug 17 '10 at 05:51
  • how about letting us know what the error message is when the program crashed? too little information helps no one. – bcosca Aug 17 '10 at 07:24
  • The page will display Fatal error: Maximum execution time of 60 seconds exceeded in C:\Program_Files\wamp\www\backup of mp_site\autolog.php on line 15 the 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 Aug 17 '10 at 09:49
  • 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 – bcosca Aug 17 '10 at 15:12
  • i tried at inside the while loop and at the start right after – vrerer Aug 18 '10 at 03:46
  • 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! – bcosca Aug 18 '10 at 04:11