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