解析器OpenVPN日志

时间:2016-05-25 17:38:55

标签: php sql

我正在寻找几天如何获取OpenVPN的日志信息并放置一个php页面。我也无法解析该信息。有谁能够帮我?以下是日志的示例:

OpenVPN CLIENT LIST
Updated,Wed Jan  7 16:06:56 2015
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
baba_jaga,1.2.3.4:1194,107322,492621,Wed Jan  7 15:59:23 2015
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
172.16.0.1,baba_jaga,1.2.3.4:1194,Wed Jan  7 16:06:54 2015
GLOBAL STATS
Max bcast/mcast queue length,1
END

我在php中找到了一个代码,但是返回了以下错误:注意:未定义的变量:第42行的C:\ xampp \ htdocs \ z \ app.php中的状态

我知道mysql错误

function parseLog ($log) {
$handle = fopen($log, "r");
$uid = 0;
$status;
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        unset($match);
        if (preg_match("/^Updated,(.+)/", $buffer, $match)) {
            $status['updated'] = $match[1];
        }
        if (preg_match("/^(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(\d+),(\d+),(.+)$/", $buffer, $match)) {
            if ($match[1] <> "Common Name") {
                $cn = $match[1];

                $userlookup[$match[2]] = $uid;
                $status['users'][$uid]['CommonName'] = $match[1];
                $status['users'][$uid]['RealAddress'] = $match[2];
                $status['users'][$uid]['BytesReceived'] = $match[3];
                $status['users'][$uid]['BytesSent'] = $match[4];
                $status['users'][$uid]['Since'] = $match[5];
                $uid++;
            }
        }
        if (preg_match("/^(\d+\.\d+\.\d+\.\d+),(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(.+)$/", $buffer, $match)) {
            if ($match[1] <> "Virtual Address") {
                $address = $match[3];
                $uid = $userlookup[$address];
                $status['users'][$uid]['VirtualAddress'] = $match[1];
                $status['users'][$uid]['LastRef'] = $match[4];
            }
        }
    }
    fclose($handle);
    return $status;
}
function sizeformat($bytesize){
    $i=0;
    while(abs($bytesize) >= 1024){
        $bytesize=$bytesize/1024;
        $i++;
        if($i==4) break;
    }
    $units = array("Bytes","KB","MB","GB","TB");
    $newsize=round($bytesize,2);
    return("$newsize $units[$i]");
}
$stats = parseLog("openvpn-status.txt");
foreach($stats['users'] as $user)
{
if($user['CommonName'] != "UNDEF")
{
   $result = mysql_query("UPDATE stats SET updated =  '".time()."',          VirtualAddress = '".$user['VirtualAddress']."',    BytesSent='".$user['BytesSent']."', BytesReceived='".$user['BytesReceived']."', LastRef='".$user['LastRef']."'  WHERE CommonName='".$user['CommonName']."' AND Since='".$user['Since']."'") or die(mysql_error());
   echo mysql_affected_rows();
   if (mysql_affected_rows()==0) {
      $result = mysql_query("insert into stats (CommonName, RealAddress, BytesReceived, BytesSent, Since, VirtualAddress, LastRef) values ('".$user['CommonName']."', '".$user['RealAddress']."', '".$user['BytesReceived']."', '".$user['BytesSent']."', '".$user['Since']."', '".$user['VirtualAddress']."', '".$user['LastRef']."')");
   }
  }
 }

1 个答案:

答案 0 :(得分:0)

您已经可以访问日志中的信息。如果你想建立一个包含所列信息的php页面,你需要做的就是在php代码中引用服务器日志文件,并以标准方式对其进行服务。如果您希望解析信息,只需编写一个脚本,并设置相应的查询字符串和分隔符。你可以在php中这样做,但我个人更喜欢使用python。