Changeset 4 for trunk/flowmon-web/charts
- Timestamp:
- 12/05/08 10:18:51 (16 years ago)
- Files:
-
- trunk/flowmon-web/charts/proto-dist.inc.php (modified) (1 diff)
- trunk/flowmon-web/charts/stacked-bars.inc.php (added)
- trunk/flowmon-web/charts/top-dst.inc.php (modified) (2 diffs)
- trunk/flowmon-web/charts/top-ports-dst.inc.php (modified) (3 diffs)
- trunk/flowmon-web/charts/top-ports-src.inc.php (modified) (3 diffs)
- trunk/flowmon-web/charts/top-src.inc.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/flowmon-web/charts/proto-dist.inc.php
r1 r4 16 16 # along with this program; if not, write to the Free Software 17 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 18 # 22 19 trunk/flowmon-web/charts/top-dst.inc.php
r1 r4 21 21 # 22 22 23 require_once(' phplot-5.0.5/phplot.php');23 require_once('stacked-bars.inc.php'); 24 24 25 25 // Get the data from the table, format it and write it out to a file 26 function chart_prepare($flowsrc, $starttime, $endtime ) {26 function chart_prepare($flowsrc, $starttime, $endtime, $timeframe = False) { 27 27 global $date_fmt_str; 28 28 global $dbh; 29 29 30 31 30 // Define the plot properties 32 $style = 'Top Destination';31 $style = 'Top Sources'; 33 32 $chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime); 34 33 $ytitle = 'Bytes'; 35 $output = tempnam('tmp/', 'plot');36 $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'purple', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen');37 38 34 39 35 // Grab the data from the database … … 51 47 $q->execute(); 52 48 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] = long2ip($legend[$i]); 60 } else { 61 $legend[$i] = ''; 49 // Did this return anything at all? 50 if ($q->rowCount() == 0) { 51 // No 52 print('No data for query-period'); 53 return False; 54 } else { 55 // Yes? Carry on! 56 57 // Fill the legend build the WHERE limit 58 $legend = $q->fetchAll(PDO::FETCH_COLUMN, 0); 59 $limit = ''; 60 for ($i = 0; $i < count($legend); $i++) { 61 $limit .= @$legend[$i].', '; 62 if (array_key_exists($i, $legend)) { 63 $legend[$i] = long2ip($legend[$i]); 64 } else { 65 $legend[$i] = ''; 66 } 62 67 } 63 } 64 $legend[] = 'rest'; 65 $limit = substr($limit, 0, (strlen($limit) - 2)); 68 $legend[] = 'rest'; 69 $limit = substr($limit, 0, (strlen($limit) - 2)); 66 70 67 // divide the timespan into 5min intervals 68 // query the traffic per talker 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 IPDst, SUM(Bytes) AS TrafficSum 79 FROM Flows 80 WHERE TimeStop BETWEEN ? AND ? 81 AND IPDst IN ('.$limit.') 82 AND FlowSrc = ? 83 GROUP BY IPDst 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]; 71 // divide the timespan into 5min intervals 72 // query the traffic per talker in 5min intervals 73 // Initialize the data_table 74 $data = array(); 75 $steps = 0; 76 $rest_table = array(); 77 for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) { 78 $time2 = ($time1 + (5 * 60)); 79 $steps++; 80 // Add the column header (time) to the data array 81 $data[] = array(strftime('%H:%M', $time1)); 82 $q = $dbh->prepare('SELECT IPDst, SUM(Bytes) AS TrafficSum 83 FROM Flows 84 WHERE TimeStop BETWEEN ? AND ? 85 AND IPDst IN ('.$limit.') 86 AND FlowSrc = ? 87 GROUP BY IPDst 88 ORDER BY TrafficSum DESC 89 LIMIT 0, 9'); 90 $q->bindParam(1, $time1, PDO::PARAM_INT); 91 $q->bindParam(2, $time2, PDO::PARAM_INT); 92 $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); 93 $q->execute(); 94 $r = $q->fetchAll(); 95 $i = (count($data) - 1); 96 foreach ($r as $row) { 97 $tmpdata[$i][$row[0]] = $row[1]; 98 } 99 100 // Get the rest of the aggregated traffic to fill up the bars 101 $q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum 102 FROM Flows 103 WHERE TimeStop BETWEEN ? AND ? 104 AND IPDst NOT IN ('.$limit.') 105 AND FlowSrc = ?'); 106 $q->bindParam(1, $time1, PDO::PARAM_INT); 107 $q->bindParam(2, $time2, PDO::PARAM_INT); 108 $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); 109 $q->execute(); 110 $rest_table[] = $q->fetch(); 94 111 } 95 112 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 IPDst 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 $ip) { 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', ip2long($ip)); 120 if (array_key_exists($key, $tmpdata[$i])) { 121 $data[$i][] = $tmpdata[$i][$key]; 113 // Iterate through the legend and fill the data array from the temporary data array 114 // This function makes sure that each top talker is always at the same position 115 // in the multidimensional data table. 116 // This is necessary for phpplot to assign the same color to each host on 117 // sequential columns 118 // $ip is the key for the temp_data lookup 119 foreach ($legend as $ip) { 120 for($i = 0; $i < $steps; $i++) { 121 if (array_key_exists($i, $tmpdata)) { 122 // The "column" exists, let's sort the rows 123 $key = sprintf('%u', ip2long($ip)); 124 if (array_key_exists($key, $tmpdata[$i])) { 125 $data[$i][] = $tmpdata[$i][$key]; 126 } else { 127 $data[$i][] = ''; 128 } 122 129 } else { 123 130 $data[$i][] = ''; 124 131 } 125 } else {126 $data[$i][] = '';127 132 } 128 133 } 129 }130 134 131 135 132 // Append the data from the rest_table to the data array133 for($i = 0; $i < $steps; $i++) {134 $data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0];135 }136 // Append the data from the rest_table to the data array 137 for($i = 0; $i < $steps; $i++) { 138 $data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0]; 139 } 136 140 137 // Create the chart, write it to tmp/output.png 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; 141 return draw_stackedbar($chart_title, $legend, $ytitle, $data); 168 142 } 169 143 } 170 trunk/flowmon-web/charts/top-ports-dst.inc.php
r1 r4 21 21 # 22 22 23 require_once(' phplot-5.0.5/phplot.php');23 require_once('stacked-bars.inc.php'); 24 24 25 25 // Get the data from the table, format it and write it out to a file … … 33 33 $chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime); 34 34 $ytitle = 'Bytes'; 35 $output = tempnam('tmp/', 'plot');36 $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'purple', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen');37 38 35 39 36 // Grab the data from the database … … 135 132 } 136 133 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); 134 print_r($data); 135 return draw_stackedbar($chart_title, $legend, $ytitle, $data); 143 136 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 137 } 170 138 trunk/flowmon-web/charts/top-ports-src.inc.php
r1 r4 21 21 # 22 22 23 require_once(' phplot-5.0.5/phplot.php');23 require_once('stacked-bars.inc.php'); 24 24 25 25 // Get the data from the table, format it and write it out to a file … … 33 33 $chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime); 34 34 $ytitle = 'Bytes'; 35 $output = tempnam('tmp/', 'plot');36 $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'purple', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen');37 35 38 36 … … 135 133 } 136 134 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); 135 return draw_stackedbar($chart_title, $legend, $ytitle, $data); 143 136 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 137 } 170 138 trunk/flowmon-web/charts/top-src.inc.php
r1 r4 21 21 # 22 22 23 require_once(' phplot-5.0.5/phplot.php');23 require_once('stacked-bars.inc.php'); 24 24 25 25 // Get the data from the table, format it and write it out to a file 26 function chart_prepare($flowsrc, $starttime, $endtime, $ timeframe= False) {26 function chart_prepare($flowsrc, $starttime, $endtime, $nslookup = False) { 27 27 global $date_fmt_str; 28 28 global $dbh; 29 30 if ($timeframe != False) {31 $starttime = (time() - $timeframe);32 $endtime = time();33 }34 29 35 30 // Define the plot properties … … 37 32 $chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime); 38 33 $ytitle = 'Bytes'; 39 $output = tempnam('tmp/', 'plot');40 $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'purple', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen');41 42 34 43 35 // Grab the data from the database … … 55 47 $q->execute(); 56 48 57 // Fill the legend build the WHERE limit 58 $legend = $q->fetchAll(PDO::FETCH_COLUMN, 0); 59 $limit = ''; 60 for ($i = 0; $i < count($legend); $i++) { 61 $limit .= @$legend[$i].', '; 62 if (array_key_exists($i, $legend)) { 63 $legend[$i] = long2ip($legend[$i]); 64 } else { 65 $legend[$i] = ''; 49 // Did this return anything at all? 50 if ($q->rowCount() == 0) { 51 // No 52 print('No data for query-period'); 53 return False; 54 } else { 55 // Yes? Carry on! 56 57 // Fill the legend build the WHERE limit 58 $legend = $q->fetchAll(PDO::FETCH_COLUMN, 0); 59 $limit = ''; 60 for ($i = 0; $i < count($legend); $i++) { 61 $limit .= @$legend[$i].', '; 62 if (array_key_exists($i, $legend)) { 63 $legend[$i] = long2ip($legend[$i]); 64 } else { 65 $legend[$i] = ''; 66 } 66 67 } 67 } 68 $legend[] = 'rest'; 69 $limit = substr($limit, 0, (strlen($limit) - 2)); 68 $legend[] = 'rest'; 69 $limit = substr($limit, 0, (strlen($limit) - 2)); 70 70 71 // divide the timespan into 5min intervals 72 // query the traffic per talker in 5min intervals 73 // Initialize the data_table 74 $data = array(); 75 $steps = 0; 76 $rest_table = array(); 77 for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) { 78 $time2 = ($time1 + (5 * 60)); 79 $steps++; 80 // Add the column header (time) to the data array 81 $data[] = array(strftime('%H:%M', $time1)); 82 $q = $dbh->prepare('SELECT IPSrc, SUM(Bytes) AS TrafficSum 83 FROM Flows 84 WHERE TimeStop BETWEEN ? AND ? 85 AND IPSrc IN ('.$limit.') 86 AND FlowSrc = ? 87 GROUP BY IPSrc 88 ORDER BY TrafficSum DESC 89 LIMIT 0, 9'); 90 $q->bindParam(1, $time1, PDO::PARAM_INT); 91 $q->bindParam(2, $time2, PDO::PARAM_INT); 92 $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); 93 $q->execute(); 94 $r = $q->fetchAll(); 95 $i = (count($data) - 1); 96 foreach ($r as $row) { 97 $tmpdata[$i][$row[0]] = $row[1]; 71 // divide the timespan into 5min intervals 72 // query the traffic per talker in 5min intervals 73 // Initialize the data_table 74 $data = array(); 75 $steps = 0; 76 $rest_table = array(); 77 for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) { 78 $time2 = ($time1 + (5 * 60)); 79 $steps++; 80 // Add the column header (time) to the data array 81 $data[] = array(strftime('%H:%M', $time1)); 82 $q = $dbh->prepare('SELECT IPSrc, SUM(Bytes) AS TrafficSum 83 FROM Flows 84 WHERE TimeStop BETWEEN ? AND ? 85 AND IPSrc IN ('.$limit.') 86 AND FlowSrc = ? 87 GROUP BY IPSrc 88 ORDER BY TrafficSum DESC 89 LIMIT 0, 9'); 90 $q->bindParam(1, $time1, PDO::PARAM_INT); 91 $q->bindParam(2, $time2, PDO::PARAM_INT); 92 $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); 93 $q->execute(); 94 $r = $q->fetchAll(); 95 $i = (count($data) - 1); 96 foreach ($r as $row) { 97 $tmpdata[$i][$row[0]] = $row[1]; 98 } 99 100 // Get the rest of the aggregated traffic to fill up the bars 101 $q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum 102 FROM Flows 103 WHERE TimeStop BETWEEN ? AND ? 104 AND IPSrc NOT IN ('.$limit.') 105 AND FlowSrc = ?'); 106 $q->bindParam(1, $time1, PDO::PARAM_INT); 107 $q->bindParam(2, $time2, PDO::PARAM_INT); 108 $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); 109 $q->execute(); 110 $rest_table[] = $q->fetch(); 98 111 } 99 112 100 // Get the rest of the aggregated traffic to fill up the bars 101 $q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum 102 FROM Flows 103 WHERE TimeStop BETWEEN ? AND ? 104 AND IPSrc NOT IN ('.$limit.') 105 AND FlowSrc = ?'); 106 $q->bindParam(1, $time1, PDO::PARAM_INT); 107 $q->bindParam(2, $time2, PDO::PARAM_INT); 108 $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); 109 $q->execute(); 110 $rest_table[] = $q->fetch(); 111 } 112 113 // Iterate through the legend and fill the data array from the temporary data array 114 // This function makes sure that each top talker is always at the same position 115 // in the multidimensional data table. 116 // This is necessary for phpplot to assign the same color to each host on 117 // sequential columns 118 // $ip is the key for the temp_data lookup 119 foreach ($legend as $ip) { 120 for($i = 0; $i < $steps; $i++) { 121 if (array_key_exists($i, $tmpdata)) { 122 // The "column" exists, let's sort the rows 123 $key = sprintf('%u', ip2long($ip)); 124 if (array_key_exists($key, $tmpdata[$i])) { 125 $data[$i][] = $tmpdata[$i][$key]; 113 // Iterate through the legend and fill the data array from the temporary data array 114 // This function makes sure that each top talker is always at the same position 115 // in the multidimensional data table. 116 // This is necessary for phpplot to assign the same color to each host on 117 // sequential columns 118 // $ip is the key for the temp_data lookup 119 foreach ($legend as $ip) { 120 for($i = 0; $i < $steps; $i++) { 121 if (array_key_exists($i, $tmpdata)) { 122 // The "column" exists, let's sort the rows 123 $key = sprintf('%u', ip2long($ip)); 124 if (array_key_exists($key, $tmpdata[$i])) { 125 $data[$i][] = $tmpdata[$i][$key]; 126 } else { 127 $data[$i][] = ''; 128 } 126 129 } else { 127 130 $data[$i][] = ''; 128 131 } 129 } else {130 $data[$i][] = '';131 132 } 132 133 } 133 }134 134 135 135 136 // Append the data from the rest_table to the data array137 for($i = 0; $i < $steps; $i++) {138 $data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0];139 }136 // Append the data from the rest_table to the data array 137 for($i = 0; $i < $steps; $i++) { 138 $data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0]; 139 } 140 140 141 // Create the chart, write it to tmp/output.png 142 $plot = new PHPlot(900, 400); 143 $plot->SetIsInline(True); 144 //$plot->SetPrintImage(False); 145 $plot->SetImageBorderType('plain'); 146 $plot->SetDataColors($data_colors); 141 if ($nslookup === True) { 142 for ($i=0; $i < count($legend); $i++) { 143 $legend[$i] = gethostbyaddr($legend[$i]).' ('.$legend[$i].')'; 144 } 145 } 147 146 148 149 $plot->SetPlotType('stackedbars'); 150 $plot->SetDataType('text-data'); 151 $plot->SetDataValues($data); 152 153 $plot->SetTitle($chart_title); 154 $plot->SetYTitle($ytitle); 155 $plot->SetLegend($legend); 156 157 $plot->SetXTickLabelPos('none'); 158 $plot->SetXTickPos('none'); 159 160 $plot->SetLegendPixels(760, 40); 161 $plot->SetMarginsPixels(100, 150, 50, 50); 162 163 164 $plot->SetNumberFormat(',', '.'); 165 $plot->SetPrecisionY(0); 166 $plot->SetYLabelType('data'); 167 168 $plot->SetOutputFile($output); 169 $plot->DrawGraph(); 170 171 chmod($output, 0644); 172 $target = dirname($output).'/'.basename($output).'.png'; 173 if (rename($output, $target)) { 174 return 'tmp/'.basename($target); 175 } else { 176 return False; 147 return draw_stackedbar($chart_title, $legend, $ytitle, $data); 177 148 } 178 149 } 179