[背景]最近,发现如果分析一个log文件还是挺方便的,可如果log文件很多,那不是要做多个首页文件对应每一个log吗?哎,麻烦。今天,我就为解决这个问题,而又再次向我前段时间写的代码就进了扩充,功能是解决在一个页面下显示多个log文件,当然是通过选项进行切换文件的,而这些文件我是通过php指定到目录下面遍历出来的。功能还是很不错的。先来张图,给大家看看。
看到没,根据选择不同的log文件,下面的IP,包大小都会自动的调整的。这个功能的实现,还需要结合javascript功能的实现,当然还有个事件,那就是onchange
好了,说到这里,我还是扔代码给大家吧!
[Code]
index.php
<html>
<head>
<title>IDC状态图</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<mce:style type="text/css"><!--
.style1 {
font-size: 16px;
font-weight: bold;
}
--></mce:style><style type="text/css" mce_bogus="1">.style1 {
font-size: 16px;
font-weight: bold;
}</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function myfunction()
{
location.href="http://log.tiancity.net:8090/graph/test.php?file_path="+document.form1.select1.value;
}
//-->
</SCRIPT>
</head>
<body>
<?php
$i=0;
global $file_path;
$arr_files=array();
$path="/home/wiki/www/dev_tools/graph/log/";
$file_path=$_GET['file_path'];
if(!isset($file_path)){
$file_path ="log/test.log";
}
else
{
$file_path="log/".$file_path;
}
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
array_push($arr_files,$file);
}
}
closedir($handle);
}
$fp = fopen ($file_path, "r");
while(!feof($fp)){
$bruce=fgets($fp);
$pieces = explode(",", $bruce);
if(!is_null($pieces[1])){
$ip_result[$i] = $pieces[1];
}
if(!is_null($pieces[3])){
$size_result[$i]=$pieces[3];
}
$i++;
}
fclose($fp);
$tmp_size_result=array_keys(array_flip($size_result));
$tmp_ip_result=array_keys(array_flip($ip_result));
?>
<form name="form1" method="get" action="result.php">
<p align="center" class="style1"> IDC状态生成图</p>
<table width="300" border="1" align="center" cellpadding="3" cellspacing="3">
<tr><td><strong>文件名:</strong></td><td>
<select name="select1" onChange="myfunction()">
<option>Null</option>
<?php foreach($arr_files as $file_name){ ?>
<option value="<?php echo $file_name ?>"><?php echo $file_name?></option>
<?php } ?>
</select>
</td></tr>
<tr>
<td width="85"><strong>IP:</strong></td>
<td width="188"><select name="ip" id="ip">
<?php
while (list(,$value)= each($tmp_ip_result)){
?>
<option value=<?php echo $value ?>><?php echo $value ?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td><strong>包大小:</strong></td>
<td>
<select name="size" id="size">
<?php while(list(,$value)=each($tmp_size_result)){ ?>
<option value=<?php echo $value ?> ><?php echo $value ?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td><strong>图宽度:</strong></td>
<td>
<INPUT TYPE="text" NAME="img_width" value="1000">
</td>
</tr>
<INPUT TYPE="hidden" NAME="file_name" value="<?php echo $file_path; ?>">
</table>
<p align="center">
<input type="submit" value="生 成">
<input type="reset" name="Submit2" value="重 置">
</p>
</form>
</body>
</html>
result.php
<?php
include ("sgraph/jpgraph.php");
include ("sgraph/jpgraph_line.php");
$src_ip = $_GET["ip"];
$pack_size = $_GET["size"];
$image_width = $_GET["img_width"];
$file_name =$_GET["file_name"];
$i=0;
$fp = fopen ("$file_name", "r");
while (!feof($fp))
{
$bruce=fgets($fp);
$pieces = explode(",", $bruce);
if($src_ip == $pieces[1]){
if($pieces[3]==$pack_size){
$result_min[$i]=$pieces[4];
$result_avg[$i]=$pieces[5];
$result_max[$i]=$pieces[6];
$result_loss[$i]=$pieces[7]+0;
$tmp_time=explode(" ",$pieces[2]);
$tmp1_time=explode(":",$tmp_time[1]);
$result_time[$i]=$tmp1_time[0].":".$tmp1_time[1];
$i++;
}
}
}
fclose($fp);
$graph = new Graph($image_width,300,auto); //创建新的Graph对象
$graph->SetScale("textlin");
$graph->SetShadow(); //设置图像的阴影样式
$graph->img->SetMargin(60,30,30,70); //设置图像边距
$graph->title->Set("IDC状态(Size=$pack_size)"); //设置图像标题
$graph->title->SetMargin(10);
$lineplot1=new LinePlot($result_avg); //创建设置两条曲线对象
$lineplot2=new LinePlot($result_min);
$lineplot3=new LinePlot($result_max);
$lineplot4=new LinePlot($result_loss);
$graph->Add($lineplot1); //将曲线放置到图像上
$graph->Add($lineplot2);
$graph->Add($lineplot3);
$graph->Add($lineplot4);
$graph->xaxis->title->Set("时间段"); //设置坐标轴名称
$graph->yaxis->title->Set("Ping值(ms)");
$graph->xaxis->title->SetMargin(10);
$graph->yaxis->title->SetMargin(15);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置字体
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->SetTickLabels($result_time);
$lineplot1->SetColor("red"); //设置颜色
$lineplot2->SetColor("blue");
$lineplot3->SetColor("green");
$lineplot4->SetColor("black");
$lineplot1->SetLegend("Avg"); //设置图例名称
$lineplot2->SetLegend("Min");
$lineplot3->SetLegend("Max");
$lineplot4->SetLegend("Loss");
$graph->legend->SetLayout(LEGEND_HOR); //设置图例样式和位置
$graph->legend->Pos(0.5,0.96,"center","bottom");
$graph->Stroke(); //输出图像
看到没,根据选择不同的log文件,下面的IP,包大小都会自动的调整的。这个功能的实现,还需要结合javascript功能的实现,当然还有个事件,那就是onchange
好了,说到这里,我还是扔代码给大家吧!
[Code]
index.php
<html>
<head>
<title>IDC状态图</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<mce:style type="text/css"><!--
.style1 {
font-size: 16px;
font-weight: bold;
}
--></mce:style><style type="text/css" mce_bogus="1">.style1 {
font-size: 16px;
font-weight: bold;
}</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function myfunction()
{
location.href="http://log.tiancity.net:8090/graph/test.php?file_path="+document.form1.select1.value;
}
//-->
</SCRIPT>
</head>
<body>
<?php
$i=0;
global $file_path;
$arr_files=array();
$path="/home/wiki/www/dev_tools/graph/log/";
$file_path=$_GET['file_path'];
if(!isset($file_path)){
$file_path ="log/test.log";
}
else
{
$file_path="log/".$file_path;
}
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
array_push($arr_files,$file);
}
}
closedir($handle);
}
$fp = fopen ($file_path, "r");
while(!feof($fp)){
$bruce=fgets($fp);
$pieces = explode(",", $bruce);
if(!is_null($pieces[1])){
$ip_result[$i] = $pieces[1];
}
if(!is_null($pieces[3])){
$size_result[$i]=$pieces[3];
}
$i++;
}
fclose($fp);
$tmp_size_result=array_keys(array_flip($size_result));
$tmp_ip_result=array_keys(array_flip($ip_result));
?>
<form name="form1" method="get" action="result.php">
<p align="center" class="style1"> IDC状态生成图</p>
<table width="300" border="1" align="center" cellpadding="3" cellspacing="3">
<tr><td><strong>文件名:</strong></td><td>
<select name="select1" onChange="myfunction()">
<option>Null</option>
<?php foreach($arr_files as $file_name){ ?>
<option value="<?php echo $file_name ?>"><?php echo $file_name?></option>
<?php } ?>
</select>
</td></tr>
<tr>
<td width="85"><strong>IP:</strong></td>
<td width="188"><select name="ip" id="ip">
<?php
while (list(,$value)= each($tmp_ip_result)){
?>
<option value=<?php echo $value ?>><?php echo $value ?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td><strong>包大小:</strong></td>
<td>
<select name="size" id="size">
<?php while(list(,$value)=each($tmp_size_result)){ ?>
<option value=<?php echo $value ?> ><?php echo $value ?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td><strong>图宽度:</strong></td>
<td>
<INPUT TYPE="text" NAME="img_width" value="1000">
</td>
</tr>
<INPUT TYPE="hidden" NAME="file_name" value="<?php echo $file_path; ?>">
</table>
<p align="center">
<input type="submit" value="生 成">
<input type="reset" name="Submit2" value="重 置">
</p>
</form>
</body>
</html>
result.php
<?php
include ("sgraph/jpgraph.php");
include ("sgraph/jpgraph_line.php");
$src_ip = $_GET["ip"];
$pack_size = $_GET["size"];
$image_width = $_GET["img_width"];
$file_name =$_GET["file_name"];
$i=0;
$fp = fopen ("$file_name", "r");
while (!feof($fp))
{
$bruce=fgets($fp);
$pieces = explode(",", $bruce);
if($src_ip == $pieces[1]){
if($pieces[3]==$pack_size){
$result_min[$i]=$pieces[4];
$result_avg[$i]=$pieces[5];
$result_max[$i]=$pieces[6];
$result_loss[$i]=$pieces[7]+0;
$tmp_time=explode(" ",$pieces[2]);
$tmp1_time=explode(":",$tmp_time[1]);
$result_time[$i]=$tmp1_time[0].":".$tmp1_time[1];
$i++;
}
}
}
fclose($fp);
$graph = new Graph($image_width,300,auto); //创建新的Graph对象
$graph->SetScale("textlin");
$graph->SetShadow(); //设置图像的阴影样式
$graph->img->SetMargin(60,30,30,70); //设置图像边距
$graph->title->Set("IDC状态(Size=$pack_size)"); //设置图像标题
$graph->title->SetMargin(10);
$lineplot1=new LinePlot($result_avg); //创建设置两条曲线对象
$lineplot2=new LinePlot($result_min);
$lineplot3=new LinePlot($result_max);
$lineplot4=new LinePlot($result_loss);
$graph->Add($lineplot1); //将曲线放置到图像上
$graph->Add($lineplot2);
$graph->Add($lineplot3);
$graph->Add($lineplot4);
$graph->xaxis->title->Set("时间段"); //设置坐标轴名称
$graph->yaxis->title->Set("Ping值(ms)");
$graph->xaxis->title->SetMargin(10);
$graph->yaxis->title->SetMargin(15);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置字体
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->SetTickLabels($result_time);
$lineplot1->SetColor("red"); //设置颜色
$lineplot2->SetColor("blue");
$lineplot3->SetColor("green");
$lineplot4->SetColor("black");
$lineplot1->SetLegend("Avg"); //设置图例名称
$lineplot2->SetLegend("Min");
$lineplot3->SetLegend("Max");
$lineplot4->SetLegend("Loss");
$graph->legend->SetLayout(LEGEND_HOR); //设置图例样式和位置
$graph->legend->Pos(0.5,0.96,"center","bottom");
$graph->Stroke(); //输出图像
?>
本文转自hahazhu0634 51CTO博客,原文链接:http://blog.51cto.com/5ydycm/193006,如需转载请自行联系原作者