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