root/trunk/flowmon-web/charts/proto-dist.inc.php

Revision 4, 2.5 kB (checked in by ixs, 16 years ago)

Additional changes, nslookup support, added time selection to the chart itself...

Line 
1 <?php
2 #
3 # Copyright (C) 2008 Red Hat, Inc.
4 # Author: Andreas Thienemann <athienem@redhat.com>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Library General Public License as published by
8 # the Free Software Foundation; version 2 only
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Library General Public License for more details.
14 #
15 # You should have received a copy of the GNU Library General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #
19
20 require_once('phplot-5.0.5/phplot.php');
21
22 // Get the data from the table, format it and write it out to a file
23 function chart_prepare($flowsrc, $starttime, $endtime) {
24     global $date_fmt_str;
25     global $dbh;
26
27
28     // Define the plot properties
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     // Grab the data from the database
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     // Build the data table and the legend
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     // Create the chart
62     $plot = new PHPlot(800, 400);
63     $plot->SetIsInline(True);
64     //$plot->SetPrintImage(False);
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
Note: See TracBrowser for help on using the browser.