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