开发者

Loop a part of PHP code every 15 seconds

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();
    开发者_JS百科            // 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;
}


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


Windows CRON What is the Windows version of cron?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜