root/trunk/flowmon-web/charts/top-ports-src.inc.php

Revision 1, 5.1 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 = 'Top Source Ports';
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', 'orange', 'blue', 'purple', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen');
37
38
39     // Grab the data from the database
40     // get top-talkers for the timespan: top 9 hosts plus rest-traffic
41     $q = $dbh->prepare('SELECT PortSrc, SUM(Bytes) AS TrafficSum
42             FROM Flows
43             WHERE TimeStop BETWEEN ? AND ?
44             AND FlowSrc = ?
45             GROUP BY PortSrc
46             ORDER BY TrafficSum 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     // Fill the legend build the WHERE limit
54     $legend = $q->fetchAll(PDO::FETCH_COLUMN, 0);
55     $limit = '';
56     for ($i = 0; $i < count($legend); $i++) {
57         $limit .= @$legend[$i].', ';
58         if (array_key_exists($i, $legend)) {
59             $legend[$i] = $legend[$i];
60         } else {
61             $legend[$i] = '';
62         }
63     }
64     $legend[] = 'rest';
65     $limit = substr($limit, 0, (strlen($limit) - 2));
66
67     // divide the timespan into 5min intervals
68     // query the traffic per port in 5min intervals
69     // Initialize the data_table
70     $data = array();
71     $steps = 0;
72     $rest_table = array();
73     for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) {
74         $time2 = ($time1 + (5 * 60));
75         $steps++;
76         // Add the column header (time) to the data array
77         $data[] = array(strftime('%H:%M', $time1));
78         $q = $dbh->prepare('SELECT PortSrc, SUM(Bytes) AS TrafficSum
79                 FROM Flows
80                 WHERE TimeStop BETWEEN ? AND ?
81                 AND PortSrc IN ('.$limit.')
82                 AND FlowSrc = ?
83                 GROUP BY PortSrc
84                 ORDER BY TrafficSum DESC
85                 LIMIT 0, 9');
86         $q->bindParam(1, $time1, PDO::PARAM_INT);
87         $q->bindParam(2, $time2, PDO::PARAM_INT);
88         $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT);
89         $q->execute();
90         $r = $q->fetchAll();
91         $i = (count($data) - 1);
92         foreach ($r as $row) {
93             $tmpdata[$i][$row[0]] = $row[1];
94         }
95
96         // Get the rest of the aggregated traffic to fill up the bars
97         $q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum
98                 FROM Flows
99                 WHERE TimeStop BETWEEN ? AND ?
100                 AND PortSrc NOT IN ('.$limit.')
101                 AND FlowSrc = ?');
102         $q->bindParam(1, $time1, PDO::PARAM_INT);
103         $q->bindParam(2, $time2, PDO::PARAM_INT);
104         $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT);
105         $q->execute();
106         $rest_table[] = $q->fetch();
107     }
108
109     // Iterate through the legend and fill the data array from the temporary data array
110     // This function makes sure that each top talker is always at the same position
111     // in the multidimensional data table.
112     // This is necessary for phpplot to assign the same color to each host on
113     // sequential columns
114     // $ip is the key for the temp_data lookup
115     foreach ($legend as $port) {
116         for($i = 0; $i < $steps; $i++) {
117             if (array_key_exists($i, $tmpdata)) {
118             // The "column" exists, let's sort the rows
119                 $key = sprintf('%u', $port);
120                 if (array_key_exists($key, $tmpdata[$i])) {
121                     $data[$i][] = $tmpdata[$i][$key];
122                 } else {
123                     $data[$i][] = '';
124                 }
125             } else {
126                 $data[$i][] = '';
127             }
128         }
129     }
130
131
132     // Append the data from the rest_table to the data array
133     for($i = 0; $i < $steps; $i++) {
134         $data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0];
135     }
136
137     // Create the chart
138     $plot = new PHPlot(800, 400);
139     $plot->SetIsInline(True);
140     //$plot->SetPrintImage(False);
141     $plot->SetImageBorderType('plain');
142     $plot->SetDataColors($data_colors);
143
144
145     $plot->SetPlotType('stackedbars');
146     $plot->SetDataType('text-data');
147     $plot->SetDataValues($data);
148
149     $plot->SetTitle($chart_title);
150     $plot->SetYTitle($ytitle);
151     $plot->SetLegend($legend);
152
153     $plot->SetXTickLabelPos('none');
154     $plot->SetXTickPos('none');
155
156     $plot->SetNumberFormat(',', '.');
157     $plot->SetPrecisionY(0);
158
159     $plot->SetOutputFile($output);
160     $plot->DrawGraph();
161
162     chmod($output, 0644);
163     $target dirname($output).'/'.basename($output).'.png';
164     if (rename($output, $target)) {
165         return 'tmp/'.basename($target);
166     } else {
167         return False;
168     }
169 }
170
171
Note: See TracBrowser for help on using the browser.