Index: trunk/flowmon-web/charts/top-dst.inc.php =================================================================== --- trunk/flowmon-web/charts/top-dst.inc.php (revision 4) +++ trunk/flowmon-web/charts/top-dst.inc.php (revision 1) @@ -21,15 +21,19 @@ # -require_once('stacked-bars.inc.php'); +require_once('phplot-5.0.5/phplot.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) { global $date_fmt_str; global $dbh; + // Define the plot properties - $style = 'Top Sources'; + $style = 'Top Destination'; $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 @@ -47,97 +51,120 @@ $q->execute(); - // 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)); - // 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)); - - // 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 IPDst, SUM(Bytes) AS TrafficSum - FROM Flows - WHERE TimeStop BETWEEN ? AND ? - AND IPDst IN ('.$limit.') - AND FlowSrc = ? - GROUP BY IPDst - 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 IPDst 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(); + // 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 IPDst, SUM(Bytes) AS TrafficSum + FROM Flows + WHERE TimeStop BETWEEN ? AND ? + AND IPDst IN ('.$limit.') + AND FlowSrc = ? + GROUP BY IPDst + 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]; } - // 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][] = ''; - } + // 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 IPDst 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]; } 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]; + } - return draw_stackedbar($chart_title, $legend, $ytitle, $data); + // Create the chart, write it to tmp/output.png + $plot = new PHPlot(800, 400); + $plot->SetIsInline(True); + //$plot->SetPrintImage(False); + $plot->SetImageBorderType('plain'); + $plot->SetDataColors($data_colors); + + + $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->SetNumberFormat(',', '.'); + $plot->SetPrecisionY(0); + + $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; } } +