1 |
<?php |
---|
2 |
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 |
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 |
|
---|
21 |
|
---|
22 |
|
---|
23 |
require_once('phplot-5.0.5/phplot.php'); |
---|
24 |
|
---|
25 |
|
---|
26 |
function chart_prepare($flowsrc, $starttime, $endtime) { |
---|
27 |
global $date_fmt_str; |
---|
28 |
global $dbh; |
---|
29 |
|
---|
30 |
|
---|
31 |
|
---|
32 |
$style = 'Top Source Ports'; |
---|
33 |
$chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime); |
---|
34 |
$ytitle = 'Bytes'; |
---|
35 |
$output = tempnam('tmp/', 'plot'); |
---|
36 |
$data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'purple', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen'); |
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 |
// get top-talkers for the timespan: top 9 hosts plus rest-traffic |
---|
41 |
$q = $dbh->prepare('SELECT PortSrc, SUM(Bytes) AS TrafficSum |
---|
42 |
FROM Flows |
---|
43 |
WHERE TimeStop BETWEEN ? AND ? |
---|
44 |
AND FlowSrc = ? |
---|
45 |
GROUP BY PortSrc |
---|
46 |
ORDER BY TrafficSum DESC |
---|
47 |
LIMIT 0, 9'); |
---|
48 |
$q->bindParam(1, $starttime, PDO::PARAM_INT); |
---|
49 |
$q->bindParam(2, $endtime, PDO::PARAM_INT); |
---|
50 |
$q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); |
---|
51 |
$q->execute(); |
---|
52 |
|
---|
53 |
|
---|
54 |
$legend = $q->fetchAll(PDO::FETCH_COLUMN, 0); |
---|
55 |
$limit = ''; |
---|
56 |
for ($i = 0; $i < count($legend); $i++) { |
---|
57 |
$limit .= @$legend[$i].', '; |
---|
58 |
if (array_key_exists($i, $legend)) { |
---|
59 |
$legend[$i] = $legend[$i]; |
---|
60 |
} else { |
---|
61 |
$legend[$i] = ''; |
---|
62 |
} |
---|
63 |
} |
---|
64 |
$legend[] = 'rest'; |
---|
65 |
$limit = substr($limit, 0, (strlen($limit) - 2)); |
---|
66 |
|
---|
67 |
|
---|
68 |
// query the traffic per port in 5min intervals |
---|
69 |
// Initialize the data_table |
---|
70 |
$data = array(); |
---|
71 |
$steps = 0; |
---|
72 |
$rest_table = array(); |
---|
73 |
for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) { |
---|
74 |
$time2 = ($time1 + (5 * 60)); |
---|
75 |
$steps++; |
---|
76 |
|
---|
77 |
$data[] = array(strftime('%H:%M', $time1)); |
---|
78 |
$q = $dbh->prepare('SELECT PortSrc, SUM(Bytes) AS TrafficSum |
---|
79 |
FROM Flows |
---|
80 |
WHERE TimeStop BETWEEN ? AND ? |
---|
81 |
AND PortSrc IN ('.$limit.') |
---|
82 |
AND FlowSrc = ? |
---|
83 |
GROUP BY PortSrc |
---|
84 |
ORDER BY TrafficSum DESC |
---|
85 |
LIMIT 0, 9'); |
---|
86 |
$q->bindParam(1, $time1, PDO::PARAM_INT); |
---|
87 |
$q->bindParam(2, $time2, PDO::PARAM_INT); |
---|
88 |
$q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); |
---|
89 |
$q->execute(); |
---|
90 |
$r = $q->fetchAll(); |
---|
91 |
$i = (count($data) - 1); |
---|
92 |
foreach ($r as $row) { |
---|
93 |
$tmpdata[$i][$row[0]] = $row[1]; |
---|
94 |
} |
---|
95 |
|
---|
96 |
|
---|
97 |
$q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum |
---|
98 |
FROM Flows |
---|
99 |
WHERE TimeStop BETWEEN ? AND ? |
---|
100 |
AND PortSrc NOT IN ('.$limit.') |
---|
101 |
AND FlowSrc = ?'); |
---|
102 |
$q->bindParam(1, $time1, PDO::PARAM_INT); |
---|
103 |
$q->bindParam(2, $time2, PDO::PARAM_INT); |
---|
104 |
$q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); |
---|
105 |
$q->execute(); |
---|
106 |
$rest_table[] = $q->fetch(); |
---|
107 |
} |
---|
108 |
|
---|
109 |
|
---|
110 |
// This function makes sure that each top talker is always at the same position |
---|
111 |
// in the multidimensional data table. |
---|
112 |
// This is necessary for phpplot to assign the same color to each host on |
---|
113 |
// sequential columns |
---|
114 |
// $ip is the key for the temp_data lookup |
---|
115 |
foreach ($legend as $port) { |
---|
116 |
for($i = 0; $i < $steps; $i++) { |
---|
117 |
if (array_key_exists($i, $tmpdata)) { |
---|
118 |
|
---|
119 |
$key = sprintf('%u', $port); |
---|
120 |
if (array_key_exists($key, $tmpdata[$i])) { |
---|
121 |
$data[$i][] = $tmpdata[$i][$key]; |
---|
122 |
} else { |
---|
123 |
$data[$i][] = ''; |
---|
124 |
} |
---|
125 |
} else { |
---|
126 |
$data[$i][] = ''; |
---|
127 |
} |
---|
128 |
} |
---|
129 |
} |
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 |
for($i = 0; $i < $steps; $i++) { |
---|
134 |
$data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0]; |
---|
135 |
} |
---|
136 |
|
---|
137 |
|
---|
138 |
$plot = new PHPlot(800, 400); |
---|
139 |
$plot->SetIsInline(True); |
---|
140 |
|
---|
141 |
$plot->SetImageBorderType('plain'); |
---|
142 |
$plot->SetDataColors($data_colors); |
---|
143 |
|
---|
144 |
|
---|
145 |
$plot->SetPlotType('stackedbars'); |
---|
146 |
$plot->SetDataType('text-data'); |
---|
147 |
$plot->SetDataValues($data); |
---|
148 |
|
---|
149 |
$plot->SetTitle($chart_title); |
---|
150 |
$plot->SetYTitle($ytitle); |
---|
151 |
$plot->SetLegend($legend); |
---|
152 |
|
---|
153 |
$plot->SetXTickLabelPos('none'); |
---|
154 |
$plot->SetXTickPos('none'); |
---|
155 |
|
---|
156 |
$plot->SetNumberFormat(',', '.'); |
---|
157 |
$plot->SetPrecisionY(0); |
---|
158 |
|
---|
159 |
$plot->SetOutputFile($output); |
---|
160 |
$plot->DrawGraph(); |
---|
161 |
|
---|
162 |
chmod($output, 0644); |
---|
163 |
$target = dirname($output).'/'.basename($output).'.png'; |
---|
164 |
if (rename($output, $target)) { |
---|
165 |
return 'tmp/'.basename($target); |
---|
166 |
} else { |
---|
167 |
return False; |
---|
168 |
} |
---|
169 |
} |
---|
170 |
|
---|
171 |
|
---|