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 = 'Protocol Distribution'; |
---|
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', 'blue', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen'); |
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 |
// get all protocols |
---|
41 |
$q = $dbh->prepare('SELECT IPProto, SUM(Bytes) AS TotalBytes |
---|
42 |
FROM Flows |
---|
43 |
WHERE TimeStop BETWEEN ? AND ? |
---|
44 |
AND FlowSrc = ? |
---|
45 |
GROUP BY IPProto |
---|
46 |
ORDER BY TotalBytes 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 |
$r = $q->fetchAll(); |
---|
54 |
|
---|
55 |
|
---|
56 |
$data = array(); |
---|
57 |
$legend = array(); |
---|
58 |
foreach($r as $row) { |
---|
59 |
$proto = getprotobynumber($row[0]); |
---|
60 |
$legend[] = $proto; |
---|
61 |
$data[] = array($proto, $row[1]); |
---|
62 |
} |
---|
63 |
|
---|
64 |
|
---|
65 |
$plot = new PHPlot(800, 400); |
---|
66 |
$plot->SetIsInline(True); |
---|
67 |
|
---|
68 |
$plot->SetImageBorderType('plain'); |
---|
69 |
$plot->SetDataColors($data_colors); |
---|
70 |
|
---|
71 |
|
---|
72 |
$plot->SetPlotType('pie'); |
---|
73 |
$plot->SetDataType('text-data-single'); |
---|
74 |
$plot->SetDataValues($data); |
---|
75 |
|
---|
76 |
$plot->SetTitle($chart_title); |
---|
77 |
$plot->SetLegend($legend); |
---|
78 |
|
---|
79 |
$plot->SetOutputFile($output); |
---|
80 |
$plot->DrawGraph(); |
---|
81 |
|
---|
82 |
chmod($output, 0644); |
---|
83 |
$target = dirname($output).'/'.basename($output).'.png'; |
---|
84 |
if (rename($output, $target)) { |
---|
85 |
return 'tmp/'.basename($target); |
---|
86 |
} else { |
---|
87 |
return False; |
---|
88 |
} |
---|
89 |
} |
---|
90 |
|
---|
91 |
|
---|