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

Revision 4, 4.3 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 # Copyright 2004, 2005 Red Hat, Inc.
19 #
20 # AUTHOR: Andreas Thienemann <athienem@redhat.com>
21 #
22
23 require_once('stacked-bars.inc.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 Destination Ports';
33     $chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime);
34     $ytitle = 'Bytes';
35
36     // Grab the data from the database
37     // get top-talkers for the timespan: top 9 hosts plus rest-traffic
38     $q = $dbh->prepare('SELECT PortDst, SUM(Bytes) AS TrafficSum
39             FROM Flows
40             WHERE TimeStop BETWEEN ? AND ?
41             AND FlowSrc = ?
42             GROUP BY PortDst
43             ORDER BY TrafficSum 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     // Fill the legend build the WHERE limit
51     $legend = $q->fetchAll(PDO::FETCH_COLUMN, 0);
52     $limit = '';
53     for ($i = 0; $i < count($legend); $i++) {
54         $limit .= @$legend[$i].', ';
55         if (array_key_exists($i, $legend)) {
56             $legend[$i] = $legend[$i];
57         } else {
58             $legend[$i] = '';
59         }
60     }
61     $legend[] = 'rest';
62     $limit = substr($limit, 0, (strlen($limit) - 2));
63
64     // divide the timespan into 5min intervals
65     // query the traffic per port in 5min intervals
66     // Initialize the data_table
67     $data = array();
68     $steps = 0;
69     $rest_table = array();
70     for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) {
71         $time2 = ($time1 + (5 * 60));
72         $steps++;
73         // Add the column header (time) to the data array
74         $data[] = array(strftime('%H:%M', $time1));
75         $q = $dbh->prepare('SELECT PortDst, SUM(Bytes) AS TrafficSum
76                 FROM Flows
77                 WHERE TimeStop BETWEEN ? AND ?
78                 AND PortDst IN ('.$limit.')
79                 AND FlowSrc = ?
80                 GROUP BY PortDst
81                 ORDER BY TrafficSum DESC
82                 LIMIT 0, 9');
83         $q->bindParam(1, $time1, PDO::PARAM_INT);
84         $q->bindParam(2, $time2, PDO::PARAM_INT);
85         $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT);
86         $q->execute();
87         $r = $q->fetchAll();
88         $i = (count($data) - 1);
89         foreach ($r as $row) {
90             $tmpdata[$i][$row[0]] = $row[1];
91         }
92
93         // Get the rest of the aggregated traffic to fill up the bars
94         $q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum
95                 FROM Flows
96                 WHERE TimeStop BETWEEN ? AND ?
97                 AND PortDst NOT IN ('.$limit.')
98                 AND FlowSrc = ?');
99         $q->bindParam(1, $time1, PDO::PARAM_INT);
100         $q->bindParam(2, $time2, PDO::PARAM_INT);
101         $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT);
102         $q->execute();
103         $rest_table[] = $q->fetch();
104     }
105
106     // Iterate through the legend and fill the data array from the temporary data array
107     // This function makes sure that each top talker is always at the same position
108     // in the multidimensional data table.
109     // This is necessary for phpplot to assign the same color to each host on
110     // sequential columns
111     // $ip is the key for the temp_data lookup
112     foreach ($legend as $port) {
113         for($i = 0; $i < $steps; $i++) {
114             if (array_key_exists($i, $tmpdata)) {
115             // The "column" exists, let's sort the rows
116                 $key = sprintf('%u', $port);
117                 if (array_key_exists($key, $tmpdata[$i])) {
118                     $data[$i][] = $tmpdata[$i][$key];
119                 } else {
120                     $data[$i][] = '';
121                 }
122             } else {
123                 $data[$i][] = '';
124             }
125         }
126     }
127
128
129     // Append the data from the rest_table to the data array
130     for($i = 0; $i < $steps; $i++) {
131         $data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0];
132     }
133
134     print_r($data);
135     return draw_stackedbar($chart_title, $legend, $ytitle, $data);
136
137 }
138
139
Note: See TracBrowser for help on using the browser.