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

Revision 1, 2.6 kB (checked in by ixs, 16 years ago)

initial checkin of RH revision

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 # Copyright 2004, 2005 Red Hat, Inc.
19 #
20 # AUTHOR: Andreas Thienemann <athienem@redhat.com>
21 #
22
23 require_once('phplot-5.0.5/phplot.php');
24
25 // Get the data from the table, format it and write it out to a file
26 function chart_prepare($flowsrc, $starttime, $endtime) {
27     global $date_fmt_str;
28     global $dbh;
29
30
31     // Define the plot properties
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     // Grab the data from the database
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     // Build the data table and the legend
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     // Create the chart
65     $plot = new PHPlot(800, 400);
66     $plot->SetIsInline(True);
67     //$plot->SetPrintImage(False);
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
Note: See TracBrowser for help on using the browser.