class TrafficAction extends Action
{
// 刷新流量数据
public function create_traffic ()
{
$iplist = S('load_iplist');
$eth_list = array();
for ($i = 0; $i < count($iplist); $i ++)
{
$ip = $iplist[$i];
$interface_list = $this->interface_name($ip);
for ($j = 0; $j < count($interface_list); $j ++)
{
$name = $interface_list[$j];
array_push($eth_list, $ip . "_" . $name . "_traffic-");
$this->create_traffic_graph($ip, $name, "picture/" . $ip . "_" . $name . "_traffic-hour.png", "-4h", $ip . " " . $name . " 四小时流量图");
$this->create_traffic_graph($ip, $name, "picture/" . $ip . "_" . $name . "_traffic-day.png", "-1d", $ip . " " . $name . " 一天内流量图");
$this->create_traffic_graph($ip, $name, "picture/" . $ip . "_" . $name . "_traffic-week.png", "-1w", $ip . " " . $name . " 一周内流量图");
$this->create_traffic_graph($ip, $name, "picture/" . $ip . "_" . $name . "_traffic-month.png", "-1m", $ip . " " . $name . " 一个月内流量图");
$this->create_traffic_graph($ip, $name, "picture/" . $ip . "_" . $name . "_traffic-year.png", "-1y", $ip . " " . $name . " 一年内流量图");
}
}
S('eth_list_cache', $eth_list);
}
// 创建RRD文件
public function create_traffic_rrd ()
{
$ip = $_REQUEST['ip'];
$options = array(
"--step",
"60", // Use a step-size of 5 minutes
"--start",
"-6 months", // this rrd started 6 months ago
"DS:eth_in:COUNTER:600:0:U",
"DS:eth_out:COUNTER:600:0:U",
"RRA:AVERAGE:0.5:1:2880",
"RRA:AVERAGE:0.5:6:1440",
"RRA:AVERAGE:0.5:24:1440",
"RRA:AVERAGE:0.5:144:1440",
"RRA:MIN:0.5:1:2880",
"RRA:MIN:0.5:6:1440",
"RRA:MIN:0.5:24:1440",
"RRA:MIN:0.5:144:1440",
"RRA:MAX:0.5:1:2880",
"RRA:MAX:0.5:6:1440",
"RRA:MAX:0.5:24:1440",
"RRA:MAX:0.5:144:1440",
"RRA:LAST:0.5:1:2880",
"RRA:LAST:0.5:6:1440",
"RRA:LAST:0.5:24:1440",
"RRA:LAST:0.5:144:1440"
);
$interface_list = $this->interface_name($ip);
for ($i = 0; $i < count($interface_list); $i ++)
{
$filename = "../rrdtool/traffic_" . $ip . "_" . $interface_list[$i] . ".rrd";
if (file_exists($filename))
{
echo $filename . " RRD file already exists.";
exit();
}
$ret = rrd_create($filename, $options, count($options));
if (! $ret)
{
echo "Creation error: " . rrd_error() . "
";
}
else
{
echo $filename . " Create Success!
";
}
}
}
// 更新RRD文件
public function update_traffic_rrd ()
{
$iplist = S('load_iplist');
for ($i = 0; $i < count($iplist); $i ++)
{
$ip = $iplist[$i];
$interface_list = $this->interface_name($ip);
$t = time();
for ($j = 0; $j < count($interface_list); $j ++)
{
$name = $interface_list[$j];
$filename = "../rrdtool/traffic_" . $ip . "_" . $name . ".rrd";
$traffic = $this->interface_info($ip, $name);
$eth_in = $traffic[0]['in'];
$eth_out = $traffic[0]['out'];
echo '';
echo $ip . " " . date('Y-m-d H:i:s', time()) . ": " . $name . " In " . $eth_in . " and Out " . $eth_out;
echo "
";
$ret = rrd_update("../rrdtool/traffic_" . $ip . "_" . $name . ".rrd", "$t:$eth_in:$eth_out");
}
}
}
// 创建图像
public function create_traffic_graph ($ip, $interface, $output, $start, $title)
{
$filename = "../rrdtool/traffic_" . $ip . "_" . $interface . ".rrd";
$options = array(
"--slope-mode",
"--start",
$start,
"--end=-60",
"--title=$title",
"--vertical-label=掌众传媒 - 位/秒",
"--rigid",
"--base=1000",
"--height=120",
"--width=480",
"--alt-autoscale-max",
"--lower=0",
"--font=TITLE:10:/usr/share/fonts/chinese/TrueType/msyh.ttf",
"--font=AXIS:8:",
"--font=LEGEND:8:",
"--font=UNIT:8:",
"DEF:a=" . $filename . ":eth_in:AVERAGE",
"DEF:b=" . $filename . ":eth_in:MAX",
"DEF:e=" . $filename . ":eth_in:MIN",
"DEF:g=" . $filename . ":eth_in:LAST",
"DEF:c=" . $filename . ":eth_out:AVERAGE",
"DEF:d=" . $filename . ":eth_out:MAX",
"DEF:f=" . $filename . ":eth_out:MIN",
"DEF:h=" . $filename . ":eth_out:LAST",
"CDEF:cdefa=a,8,*",
"CDEF:cdefe=b,8,*",
"CDEF:cdeff=c,8,*",
"CDEF:cdefg=c,8,*",
"CDEF:cdefba=c,8,*",
"CDEF:cdefbb=d,8,*",
"CDEF:cdefbe=e,8,*",
"CDEF:cdefbf=f,8,*",
"CDEF:cdefbg=g,8,*",
"CDEF:cdefbh=h,8,*",
"AREA:cdefa#00FF00:流入",
"GPRINT:cdefbg:LAST:当前值\:%8.2lf %s",
"GPRINT:cdefa:AVERAGE:平均值\:%8.2lf %s",
"GPRINT:cdefe:MAX:最大值\:%8.2lf %s",
"GPRINT:cdefbe:MIN:最小值\:%8.2lf %s\\r",
"LINE2:cdeff#FF0000:流出",
"GPRINT:cdefbh:LAST:当前值\:%8.2lf %s",
"GPRINT:cdefg:AVERAGE:平均值\:%8.2lf %s",
"GPRINT:cdefbb:MAX:最大值\:%8.2lf %s",
"GPRINT:cdefbf:MIN:最小值\:%8.2lf %s\\r"
);
$ret = rrd_graph($output, $options, count($options));
if (! $ret)
{
echo "Graph error: " . rrd_error() . "\n";
}
}
// 格式化获取到的SNMP数据
function rrd_format ($result)
{
if (! $result)
return false;
if (is_array($result))
$result = array_shift($result);
$result = str_replace('STRING: ', '', $result);
$result = str_replace('INTEGER: ', '', $result);
$result = str_replace('Counter32: ', '', $result);
return $result;
}
// 通过SNMP获取指定IP地址的网络接口名称
public function interface_name ($ip)
{
$name = array();
$result = snmprealwalk($ip, 'public', '1.3.6.1.2.1.2.2.1.2');
foreach ($result as $key => $value)
{
$net_name = $this->rrd_format($value);
if ($net_name == 'eth0' || $net_name == 'eth1' || $net_name == 'eth2' || $net_name == 'eth3' || $net_name == 'em1' || $net_name == 'em2' || $net_name == 'em3' || $net_name == 'em4')
{
$traffic = $this->interface_info($ip, $net_name);
$eth_in = $traffic[0]['in'];
$eth_out = $traffic[0]['out'];
if ($eth_in != 0 || $eth_out != 0)
array_push($name, $net_name);
}
}
return $name;
}
// 通过SNMP获取指定IP地址、网络接口的流量信息
public function interface_info ($ip, $interface)
{
$result = snmprealwalk($ip, 'public', '1.3.6.1.2.1.2.2.1');
foreach ($result as $key => $value)
{
if ($label = strstr($key, 'ifDescr'))
{
$label = explode('.', $label);
$label = $label[1];
if ($name = $value)
{
$name = $this->rrd_format($value);
if (($size = $this->rrd_format($result["IF-MIB::ifInOctets.{$label}"])) != 0 && $name == $interface)
{
$traffic[] = array(
'interface' => $name,
'in' => $size,
'out' => $this->rrd_format($result["IF-MIB::ifOutOctets.{$label}"])
);
}
}
}
}
return $traffic;
}
}
?>