我试图用一个工具来捕获Windows上的计算机信息,使用PHP的日志记录功能。每15秒将会生成一个txt文件,其中包含信息。我想让它在后台运行,而用户访问其他页面而不被打断。每15秒循环一部分PHP代码
这里是我现在正在使用
<?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');
}
?>
起初的代码我试图用
$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');
}
但代码只捕获一台计算机的信息,并卡在环捕获前下一台计算机的信息。另一方面,第一个代码只能捕获所有计算机的数据,但只能捕获一次。
至于logger.pl这是我做的。
!/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;
}
你使用的是cron吗? – dmp 2010-08-16 08:57:02
你的perl shebang行是错误的。 – Ether 2010-08-16 18:55:01
嗨,以太,那我该用什么? 我尝试没有-w,但不起作用。 – vrerer 2010-08-17 06:25:41