Index: trunk/flowmon-web/charts/top-src.inc.php =================================================================== --- trunk/flowmon-web/charts/top-src.inc.php (revision 1) +++ trunk/flowmon-web/charts/top-src.inc.php (revision 4) @@ -21,15 +21,10 @@ # -require_once('phplot-5.0.5/phplot.php'); +require_once('stacked-bars.inc.php'); // Get the data from the table, format it and write it out to a file -function chart_prepare($flowsrc, $starttime, $endtime, $timeframe = False) { +function chart_prepare($flowsrc, $starttime, $endtime, $nslookup = False) { global $date_fmt_str; global $dbh; - - if ($timeframe != False) { - $starttime = (time() - $timeframe); - $endtime = time(); - } // Define the plot properties @@ -37,7 +32,4 @@ $chart_title = $style.' '.$flowsrc.' - '.strftime($date_fmt_str, $starttime).' to '.strftime($date_fmt_str, $endtime); $ytitle = 'Bytes'; - $output = tempnam('tmp/', 'plot'); - $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'purple', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen'); - // Grab the data from the database @@ -55,125 +47,103 @@ $q->execute(); - // Fill the legend build the WHERE limit - $legend = $q->fetchAll(PDO::FETCH_COLUMN, 0); - $limit = ''; - for ($i = 0; $i < count($legend); $i++) { - $limit .= @$legend[$i].', '; - if (array_key_exists($i, $legend)) { - $legend[$i] = long2ip($legend[$i]); - } else { - $legend[$i] = ''; + // Did this return anything at all? + if ($q->rowCount() == 0) { + // No + print('No data for query-period'); + return False; + } else { + // Yes? Carry on! + + // Fill the legend build the WHERE limit + $legend = $q->fetchAll(PDO::FETCH_COLUMN, 0); + $limit = ''; + for ($i = 0; $i < count($legend); $i++) { + $limit .= @$legend[$i].', '; + if (array_key_exists($i, $legend)) { + $legend[$i] = long2ip($legend[$i]); + } else { + $legend[$i] = ''; + } } - } - $legend[] = 'rest'; - $limit = substr($limit, 0, (strlen($limit) - 2)); + $legend[] = 'rest'; + $limit = substr($limit, 0, (strlen($limit) - 2)); - // divide the timespan into 5min intervals - // query the traffic per talker in 5min intervals - // Initialize the data_table - $data = array(); - $steps = 0; - $rest_table = array(); - for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) { - $time2 = ($time1 + (5 * 60)); - $steps++; - // Add the column header (time) to the data array - $data[] = array(strftime('%H:%M', $time1)); - $q = $dbh->prepare('SELECT IPSrc, SUM(Bytes) AS TrafficSum - FROM Flows - WHERE TimeStop BETWEEN ? AND ? - AND IPSrc IN ('.$limit.') - AND FlowSrc = ? - GROUP BY IPSrc - ORDER BY TrafficSum DESC - LIMIT 0, 9'); - $q->bindParam(1, $time1, PDO::PARAM_INT); - $q->bindParam(2, $time2, PDO::PARAM_INT); - $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); - $q->execute(); - $r = $q->fetchAll(); - $i = (count($data) - 1); - foreach ($r as $row) { - $tmpdata[$i][$row[0]] = $row[1]; + // divide the timespan into 5min intervals + // query the traffic per talker in 5min intervals + // Initialize the data_table + $data = array(); + $steps = 0; + $rest_table = array(); + for ($time1 = $starttime; $time1 < $endtime; $time1 += (5 * 60)) { + $time2 = ($time1 + (5 * 60)); + $steps++; + // Add the column header (time) to the data array + $data[] = array(strftime('%H:%M', $time1)); + $q = $dbh->prepare('SELECT IPSrc, SUM(Bytes) AS TrafficSum + FROM Flows + WHERE TimeStop BETWEEN ? AND ? + AND IPSrc IN ('.$limit.') + AND FlowSrc = ? + GROUP BY IPSrc + ORDER BY TrafficSum DESC + LIMIT 0, 9'); + $q->bindParam(1, $time1, PDO::PARAM_INT); + $q->bindParam(2, $time2, PDO::PARAM_INT); + $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); + $q->execute(); + $r = $q->fetchAll(); + $i = (count($data) - 1); + foreach ($r as $row) { + $tmpdata[$i][$row[0]] = $row[1]; + } + + // Get the rest of the aggregated traffic to fill up the bars + $q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum + FROM Flows + WHERE TimeStop BETWEEN ? AND ? + AND IPSrc NOT IN ('.$limit.') + AND FlowSrc = ?'); + $q->bindParam(1, $time1, PDO::PARAM_INT); + $q->bindParam(2, $time2, PDO::PARAM_INT); + $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); + $q->execute(); + $rest_table[] = $q->fetch(); } - // Get the rest of the aggregated traffic to fill up the bars - $q = $dbh->prepare('SELECT SUM(Bytes) AS TrafficSum - FROM Flows - WHERE TimeStop BETWEEN ? AND ? - AND IPSrc NOT IN ('.$limit.') - AND FlowSrc = ?'); - $q->bindParam(1, $time1, PDO::PARAM_INT); - $q->bindParam(2, $time2, PDO::PARAM_INT); - $q->bindParam(3, sprintf('%u', ip2long($flowsrc)), PDO::PARAM_INT); - $q->execute(); - $rest_table[] = $q->fetch(); - } - - // Iterate through the legend and fill the data array from the temporary data array - // This function makes sure that each top talker is always at the same position - // in the multidimensional data table. - // This is necessary for phpplot to assign the same color to each host on - // sequential columns - // $ip is the key for the temp_data lookup - foreach ($legend as $ip) { - for($i = 0; $i < $steps; $i++) { - if (array_key_exists($i, $tmpdata)) { - // The "column" exists, let's sort the rows - $key = sprintf('%u', ip2long($ip)); - if (array_key_exists($key, $tmpdata[$i])) { - $data[$i][] = $tmpdata[$i][$key]; + // Iterate through the legend and fill the data array from the temporary data array + // This function makes sure that each top talker is always at the same position + // in the multidimensional data table. + // This is necessary for phpplot to assign the same color to each host on + // sequential columns + // $ip is the key for the temp_data lookup + foreach ($legend as $ip) { + for($i = 0; $i < $steps; $i++) { + if (array_key_exists($i, $tmpdata)) { + // The "column" exists, let's sort the rows + $key = sprintf('%u', ip2long($ip)); + if (array_key_exists($key, $tmpdata[$i])) { + $data[$i][] = $tmpdata[$i][$key]; + } else { + $data[$i][] = ''; + } } else { $data[$i][] = ''; } - } else { - $data[$i][] = ''; } } - } - // Append the data from the rest_table to the data array - for($i = 0; $i < $steps; $i++) { - $data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0]; - } + // Append the data from the rest_table to the data array + for($i = 0; $i < $steps; $i++) { + $data[$i][(count($data[$i]) - 1)] = $rest_table[$i][0]; + } - // Create the chart, write it to tmp/output.png - $plot = new PHPlot(900, 400); - $plot->SetIsInline(True); - //$plot->SetPrintImage(False); - $plot->SetImageBorderType('plain'); - $plot->SetDataColors($data_colors); + if ($nslookup === True) { + for ($i=0; $i < count($legend); $i++) { + $legend[$i] = gethostbyaddr($legend[$i]).' ('.$legend[$i].')'; + } + } - - $plot->SetPlotType('stackedbars'); - $plot->SetDataType('text-data'); - $plot->SetDataValues($data); - - $plot->SetTitle($chart_title); - $plot->SetYTitle($ytitle); - $plot->SetLegend($legend); - - $plot->SetXTickLabelPos('none'); - $plot->SetXTickPos('none'); - - $plot->SetLegendPixels(760, 40); - $plot->SetMarginsPixels(100, 150, 50, 50); - - - $plot->SetNumberFormat(',', '.'); - $plot->SetPrecisionY(0); - $plot->SetYLabelType('data'); - - $plot->SetOutputFile($output); - $plot->DrawGraph(); - - chmod($output, 0644); - $target = dirname($output).'/'.basename($output).'.png'; - if (rename($output, $target)) { - return 'tmp/'.basename($target); - } else { - return False; + return draw_stackedbar($chart_title, $legend, $ytitle, $data); } } -