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('stacked-bars.inc.php'); |
---|
24 |
|
---|
25 |
|
---|
26 |
function chart_prepare($flowsrc, $starttime, $endtime) { |
---|
27 |
global $date_fmt_str; |
---|
28 |
global $dbh; |
---|
29 |
|
---|
30 |
|
---|
31 |
|
---|
32 |
$style = 'Top Destination Ports'; |
---|
33 |
$chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime); |
---|
34 |
$ytitle = 'Bytes'; |
---|
35 |
|
---|
36 |
|
---|
37 |
// get top-talkers for the timespan: top 9 hosts plus rest-traffic |
---|
38 |
$q = $dbh->prepare('SELECT PortDst, SUM(Bytes) AS TrafficSum |
---|
39 |
FROM Flows |
---|
40 |
WHERE TimeStop BETWEEN ? AND ? |
---|
41 |
AND FlowSrc = ? |
---|
42 |
GROUP BY PortDst |
---|
43 |
ORDER BY TrafficSum DESC |
---|
44 |
LIMIT 0, 9'); |
---|
45 |
$q->bindParam(1, $starttime, PDO::PARAM_INT); |
---|
46 |
$q->bindParam(2, $endtime, PDO::PARAM_INT); |
---|
47 |
$q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); |
---|
48 |
$q->execute(); |
---|
49 |
|
---|
50 |
|
---|
51 |
$legend = $q->fetchAll(PDO::FETCH_COLUMN, 0); |
---|
52 |
$limit = ''; |
---|
53 |
for ($i = 0; $i < count($legend); $i++) { |
---|
54 |
$limit .= @$legend[$i].', '; |
---|
55 |
if (array_key_exists($i, $legend)) { |
---|
56 |
$legend[$i] = $legend[$i]; |
---|
57 |
} else { |
---|
58 |
$legend[$i] = ''; |
---|
59 |
} |
---|
60 |
} |
---|
61 |
$legend[] = 'rest'; |
---|
62 |
$limit = substr($limit, 0, (strlen($limit) - 2)); |
---|
63 |
|
---|
64 |
|
---|
65 |
// query the traffic per port in 5min intervals |
---|
66 |
// Initialize the data_table |
---|
67 |
$data = array(); |
---|
68 |
$steps = 0; |
---|
69 |
$rest_table = array(); |
---|
70 |
for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) { |
---|
71 |
$time2 = ($time1 + (5 * 60)); |
---|
72 |
$steps++; |
---|
73 |
|
---|
74 |
$data[] = array(strftime('%H:%M', $time1)); |
---|
75 |
$q = $dbh->prepare('SELECT PortDst, SUM(Bytes) AS TrafficSum |
---|
76 |
FROM Flows |
---|
77 |
WHERE TimeStop BETWEEN ? AND ? |
---|
78 |
AND PortDst IN ('.$limit.') |
---|
79 |
AND FlowSrc = ? |
---|
80 |
GROUP BY PortDst |
---|
81 |
ORDER BY TrafficSum DESC |
---|
82 |
LIMIT 0, 9'); |
---|
83 |
$q->bindParam(1, $time1, PDO::PARAM_INT); |
---|
84 |
$q->bindParam(2, $time2, PDO::PARAM_INT); |
---|
85 |
$q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); |
---|
86 |
$q->execute(); |
---|
87 |
$r = $q->fetchAll(); |
---|
88 |
$i = (count($data) - 1); |
---|
89 |
foreach ($r as $row) { |
---|
90 |
$tmpdata[$i][$row[0]] = $row[1]; |
---|
91 |
} |
---|
92 |
|
---|
93 |
|
---|
94 |
$q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum |
---|
95 |
FROM Flows |
---|
96 |
WHERE TimeStop BETWEEN ? AND ? |
---|
97 |
AND PortDst NOT IN ('.$limit.') |
---|
98 |
AND FlowSrc = ?'); |
---|
99 |
$q->bindParam(1, $time1, PDO::PARAM_INT); |
---|
100 |
$q->bindParam(2, $time2, PDO::PARAM_INT); |
---|
101 |
$q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); |
---|
102 |
$q->execute(); |
---|
103 |
$rest_table[] = $q->fetch(); |
---|
104 |
} |
---|
105 |
|
---|
106 |
|
---|
107 |
// This function makes sure that each top talker is always at the same position |
---|
108 |
// in the multidimensional data table. |
---|
109 |
// This is necessary for phpplot to assign the same color to each host on |
---|
110 |
// sequential columns |
---|
111 |
// $ip is the key for the temp_data lookup |
---|
112 |
foreach ($legend as $port) { |
---|
113 |
for($i = 0; $i < $steps; $i++) { |
---|
114 |
if (array_key_exists($i, $tmpdata)) { |
---|
115 |
|
---|
116 |
$key = sprintf('%u', $port); |
---|
117 |
if (array_key_exists($key, $tmpdata[$i])) { |
---|
118 |
$data[$i][] = $tmpdata[$i][$key]; |
---|
119 |
} else { |
---|
120 |
$data[$i][] = ''; |
---|
121 |
} |
---|
122 |
} else { |
---|
123 |
$data[$i][] = ''; |
---|
124 |
} |
---|
125 |
} |
---|
126 |
} |
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 |
for($i = 0; $i < $steps; $i++) { |
---|
131 |
$data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0]; |
---|
132 |
} |
---|
133 |
|
---|
134 |
print_r($data); |
---|
135 |
return draw_stackedbar($chart_title, $legend, $ytitle, $data); |
---|
136 |
|
---|
137 |
} |
---|
138 |
|
---|
139 |
|
---|