1 |
<?php |
---|
2 |
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 |
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 |
require_once('phplot-5.0.5/phplot.php'); |
---|
21 |
|
---|
22 |
|
---|
23 |
function chart_prepare($flowsrc, $starttime, $endtime) { |
---|
24 |
global $date_fmt_str; |
---|
25 |
global $dbh; |
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 |
$style = 'Protocol Distribution'; |
---|
30 |
$chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime); |
---|
31 |
$ytitle = 'Bytes'; |
---|
32 |
$output = tempnam('tmp/', 'plot'); |
---|
33 |
$data_colors = array('SkyBlue', 'green', 'blue', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen'); |
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 |
// get all protocols |
---|
38 |
$q = $dbh->prepare('SELECT IPProto, SUM(Bytes) AS TotalBytes |
---|
39 |
FROM Flows |
---|
40 |
WHERE TimeStop BETWEEN ? AND ? |
---|
41 |
AND FlowSrc = ? |
---|
42 |
GROUP BY IPProto |
---|
43 |
ORDER BY TotalBytes 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 |
$r = $q->fetchAll(); |
---|
51 |
|
---|
52 |
|
---|
53 |
$data = array(); |
---|
54 |
$legend = array(); |
---|
55 |
foreach($r as $row) { |
---|
56 |
$proto = getprotobynumber($row[0]); |
---|
57 |
$legend[] = $proto; |
---|
58 |
$data[] = array($proto, $row[1]); |
---|
59 |
} |
---|
60 |
|
---|
61 |
|
---|
62 |
$plot = new PHPlot(800, 400); |
---|
63 |
$plot->SetIsInline(True); |
---|
64 |
|
---|
65 |
$plot->SetImageBorderType('plain'); |
---|
66 |
$plot->SetDataColors($data_colors); |
---|
67 |
|
---|
68 |
|
---|
69 |
$plot->SetPlotType('pie'); |
---|
70 |
$plot->SetDataType('text-data-single'); |
---|
71 |
$plot->SetDataValues($data); |
---|
72 |
|
---|
73 |
$plot->SetTitle($chart_title); |
---|
74 |
$plot->SetLegend($legend); |
---|
75 |
|
---|
76 |
$plot->SetOutputFile($output); |
---|
77 |
$plot->DrawGraph(); |
---|
78 |
|
---|
79 |
chmod($output, 0644); |
---|
80 |
$target = dirname($output).'/'.basename($output).'.png'; |
---|
81 |
if (rename($output, $target)) { |
---|
82 |
return 'tmp/'.basename($target); |
---|
83 |
} else { |
---|
84 |
return False; |
---|
85 |
} |
---|
86 |
} |
---|
87 |
|
---|
88 |
|
---|