Index: /trunk/flowmon-dbspooler.py =================================================================== --- /trunk/flowmon-dbspooler.py (revision 5) +++ /trunk/flowmon-dbspooler.py (revision 1) @@ -72,5 +72,5 @@ data = pickle.load(data_file) data_file.close() -# os.unlink(pkl_file) + os.unlink(pkl_file) except: trace = traceback.format_exc() @@ -80,79 +80,67 @@ # Insert each flow into the databse - try: - unix_starttime = '' - sourceid = '' - for flowset in data: - if type(flowset) is dict: - # The header - # save the uptime and the unixtime - unix_starttime = flowset['unixtime'] - ( flowset['uptime'] / 1000) - # Transform the sourceid to the decimal notation - sourceid = int(IPy.IP(flowset['sourceIP']).strDec()) - - # Workaround for MySQL not correctly working with triggers - if DATABASE is 'mysql': - cursor.execute("START TRANSACTION;") - cursor.execute("SELECT FlowSrc FROM FlowSources WHERE FlowSrc = %s", sourceid) - if int(cursor.rowcount) == 0: - # Insert the key - cursor.execute("INSERT INTO FlowSources (FlowSrc) VALUES (%s);", sourceid) - cursor.execute("COMMIT;") - - elif type(flowset) is list: - # flowset - cursor.execute("START TRANSACTION;") - for flow in flowset: - # flow - - # General Workaround for flowprobes which do not send the expected data - # Simply create the lookup key with the value None, this will make the SQL insert succeed - for key in ('FIRST_SWITCHED', 'LAST_SWITCHED', 'PROTOCOL', 'IPV4_SRC_ADDR', 'IPV4_DST_ADDR', - 'INPUT_SNMP', 'OUTPUT_SNMP', 'L4_SRC_PORT', 'L4_DST_PORT', 'SRC_MASK', 'DST_MASK'): - if key not in flow: - try: - flow[key] = None - except: - debug('key>flow: %s > %s' % (pprint.pformat(key), pprint.pformat(flow))) - print key, type(flow) - - # Workaround for flowprobes which do not send a direction keyword - # We can do an educated guess on the flow-direction based on the existance - # of IN_ or OUT_ keys - if 'DIRECTION' not in flow: - flow['DIRECTION'] = None - if 'IN_BYTES' in flow: - flow['DIRECTION'] = 0 - if 'OUT_BYTES' in flow: - flow['DIRECTION'] = 1 - - if flow['DIRECTION'] is 0: - # Ingress flow - bytes = flow['IN_BYTES'] - pkts = flow['IN_PKTS'] - elif flow['DIRECTION'] is 1: - # Egress flow - bytes = flow['OUT_BYTES'] - pkts = flow['OUT_PKTS'] - - cursor.execute("""INSERT INTO Flows (FlowSrc, TimeStart, TimeStop, IPProto, IPSrc, IPDst, IntIn, - IntOut, PortSrc, PortDst, MaskSrc, MaskDst, Direction, Bytes, Pakets) - VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""", - (sourceid, unix_starttime + ( flow['FIRST_SWITCHED'] / 1000 ), - unix_starttime + ( flow['LAST_SWITCHED'] / 1000), flow['PROTOCOL'], IPy.IP(flow['IPV4_SRC_ADDR']).strDec(), - IPy.IP(flow['IPV4_DST_ADDR']).strDec(), flow['INPUT_SNMP'], flow['OUTPUT_SNMP'], - flow['L4_SRC_PORT'], flow['L4_DST_PORT'], flow['SRC_MASK'], flow['DST_MASK'], - flow['DIRECTION'], bytes, pkts)) - - debug("Flow ready to insert: %s:%s -> %s:%s" % (flow['IPV4_SRC_ADDR'], flow['L4_SRC_PORT'], flow['IPV4_DST_ADDR'], flow['L4_DST_PORT'])) - - cursor.execute("COMMIT;") - os.unlink(pkl_file) - except: - trace = traceback.format_exc() - print 'error in', pkl_file - print trace - warning(trace) - return False + unix_starttime = '' + sourceid = '' + for flowset in data: + if type(flowset) is dict: + # The header + # save the uptime and the unixtime + unix_starttime = flowset['unixtime'] - ( flowset['uptime'] / 1000) + # Transform the sourceid to the decimal notation + sourceid = int(IPy.IP(flowset['sourceIP']).strDec()) + + # Workaround for MySQL not correctly working with triggers + if DATABASE is 'mysql': + cursor.execute("""START TRANSACTION;""") + cursor.execute("""SELECT FlowSrc FROM FlowSources WHERE FlowSrc = %s""", sourceid) + if int(cursor.rowcount) == 0: + # Insert the key + cursor.execute("""INSERT INTO FlowSources (FlowSrc) VALUES (%s);""", sourceid) + cursor.execute("""COMMIT;""") + + elif type(flowset) is list: + # flowset + cursor.execute("""START TRANSACTION;""") + for flow in flowset: + # flow + + # General Workaround for flowprobes which do not send the expected data + # Simply create the lookup key with the value None, this will make the SQL insert succeed + for key in ('FIRST_SWITCHED', 'LAST_SWITCHED', 'PROTOCOL', 'IPV4_SRC_ADDR', 'IPV4_DST_ADDR', + 'INPUT_SNMP', 'OUTPUT_SNMP', 'L4_SRC_PORT', 'L4_DST_PORT', 'SRC_MASK', 'DST_MASK'): + if key not in flow: + debug('key>flow: %s > %s' % (pprint.pformat(key), pprint.pformat(flow))) + flow[key] = None + + # Workaround for flowprobes which do not send a direction keyword + # We can do an educated guess on the flow-direction based on the existance + # of IN_ or OUT_ keys + if 'DIRECTION' not in flow: + flow['DIRECTION'] = None + if 'IN_BYTES' in flow: + flow['DIRECTION'] = 0 + if 'OUT_BYTES' in flow: + flow['DIRECTION'] = 1 + + if flow['DIRECTION'] is 0: + # Ingress flow + bytes = flow['IN_BYTES'] + pkts = flow['IN_PKTS'] + elif flow['DIRECTION'] is 1: + # Egress flow + bytes = flow['OUT_BYTES'] + pkts = flow['OUT_PKTS'] + + cursor.execute("""INSERT INTO Flows (FlowSrc, TimeStart, TimeStop, IPProto, IPSrc, IPDst, IntIn, + IntOut, PortSrc, PortDst, MaskSrc, MaskDst, Bytes, Pakets) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""", + (sourceid, unix_starttime + ( flow['FIRST_SWITCHED'] / 1000 ), + unix_starttime + ( flow['LAST_SWITCHED'] / 1000), flow['PROTOCOL'], IPy.IP(flow['IPV4_SRC_ADDR']).strDec(), + IPy.IP(flow['IPV4_DST_ADDR']).strDec(), flow['INPUT_SNMP'], flow['OUTPUT_SNMP'], + flow['L4_SRC_PORT'], flow['L4_DST_PORT'], flow['SRC_MASK'], flow['DST_MASK'], bytes, pkts)) + + debug("Flow ready to insert: %s:%s -> %s:%s" % (flow['IPV4_SRC_ADDR'], flow['L4_SRC_PORT'], flow['IPV4_DST_ADDR'], flow['L4_DST_PORT'])) + + cursor.execute("""COMMIT;""") @@ -163,5 +151,5 @@ # Flush the database - cursor.execute("COMMIT") + cursor.execute("""COMMIT""") # Remove the gamin monitor @@ -240,5 +228,5 @@ except (KeyboardInterrupt, SystemExit): cleanup - sys.exit(0) + pass except: raise Index: /trunk/mysql.sql =================================================================== --- /trunk/mysql.sql (revision 2) +++ /trunk/mysql.sql (revision 1) @@ -29,5 +29,4 @@ `TimeStart` int(11) unsigned NOT NULL default '0', `TimeStop` int(11) unsigned NOT NULL default '0', - `Direction` smallint(11) unsigned NULL default NULL, `Bytes` int(11) unsigned NOT NULL default '0', `Pakets` int(11) unsigned NOT NULL default '0', Index: /trunk/flowmon-web/config.inc.php =================================================================== --- /trunk/flowmon-web/config.inc.php (revision 4) +++ /trunk/flowmon-web/config.inc.php (revision 1) @@ -13,4 +13,5 @@ # Date formatstring $date_fmt_str = '%Y-%m-%d %T+%Z'; +$date_fmt = ('Y-m-d T'); # Chart Styles we support Index: /trunk/flowmon-web/charts/proto-dist.inc.php =================================================================== --- /trunk/flowmon-web/charts/proto-dist.inc.php (revision 4) +++ /trunk/flowmon-web/charts/proto-dist.inc.php (revision 1) @@ -16,4 +16,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# Copyright 2004, 2005 Red Hat, Inc. +# +# AUTHOR: Andreas Thienemann # Index: /trunk/flowmon-web/charts/top-src.inc.php =================================================================== --- /trunk/flowmon-web/charts/top-src.inc.php (revision 4) +++ /trunk/flowmon-web/charts/top-src.inc.php (revision 1) @@ -21,10 +21,15 @@ # -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, $nslookup = False) { +function chart_prepare($flowsrc, $starttime, $endtime, $timeframe = False) { global $date_fmt_str; global $dbh; + + if ($timeframe != False) { + $starttime = (time() - $timeframe); + $endtime = time(); + } // Define the plot properties @@ -32,4 +37,7 @@ $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,103 +55,125 @@ $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 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(); + // 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]; } - // 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 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]; } 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]; + } - if ($nslookup === True) { - for ($i=0; $i < count($legend); $i++) { - $legend[$i] = gethostbyaddr($legend[$i]).' ('.$legend[$i].')'; - } - } + // 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); - return draw_stackedbar($chart_title, $legend, $ytitle, $data); + + $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; } } + 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; } } + Index: /trunk/flowmon-web/charts/top-ports-src.inc.php =================================================================== --- /trunk/flowmon-web/charts/top-ports-src.inc.php (revision 4) +++ /trunk/flowmon-web/charts/top-ports-src.inc.php (revision 1) @@ -21,5 +21,5 @@ # -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 @@ -33,4 +33,6 @@ $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'); @@ -133,6 +135,36 @@ } - return draw_stackedbar($chart_title, $legend, $ytitle, $data); + // Create the chart + $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; + } } Index: /trunk/flowmon-web/charts/top-ports-dst.inc.php =================================================================== --- /trunk/flowmon-web/charts/top-ports-dst.inc.php (revision 4) +++ /trunk/flowmon-web/charts/top-ports-dst.inc.php (revision 1) @@ -21,5 +21,5 @@ # -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 @@ -33,4 +33,7 @@ $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 @@ -132,7 +135,36 @@ } - print_r($data); - return draw_stackedbar($chart_title, $legend, $ytitle, $data); + // Create the chart + $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; + } } Index: /unk/flowmon-web/charts/stacked-bars.inc.php =================================================================== --- /trunk/flowmon-web/charts/stacked-bars.inc.php (revision 4) +++ (revision ) @@ -1,69 +1,0 @@ - -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU Library General Public License as published by -# the Free Software Foundation; version 2 only -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Library General Public License for more details. -# -# You should have received a copy of the GNU Library General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# - -require_once('phplot-5.0.5/phplot.php'); - -// accept data to plot through phplot -function draw_stackedbar($chart_title, $legend, $ytitle, $data) { - - - // Define general plot properties - $data_colors = array('SkyBlue', 'green', 'orange', 'blue', 'purple', 'red', 'violet', 'azure1', 'yellow', 'DarkGreen'); - - // Create the chart, write it to a newly created tmpfile. - $output = tempnam('tmp/', 'plot'); - - $plot = new PHPlot(900, 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->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; - } -} - Index: /trunk/flowmon-web/common.inc.php =================================================================== --- /trunk/flowmon-web/common.inc.php (revision 6) +++ /trunk/flowmon-web/common.inc.php (revision 1) @@ -55,6 +55,6 @@ // HTML Footer function html_footer() { - global $prog_name, $prog_url, $prog_vers, $date_fmt_str; - $foot = '

This page was generated by '.$prog_name.' version '.$prog_vers.' at '.strftime($date_fmt_str).'

+ global $prog_name, $prog_vers, $date_fmt_str; + $foot = '

This page was generated by '.$prog_name.' version '.$prog_vers.' at '.strftime($date_fmt_str).'

@@ -67,5 +67,5 @@ $head = ' - + @@ -89,46 +89,2 @@ return $head; } - -function chartstyle_header() { - - global $chart_styles; - - if (@$_REQUEST['nslookup'] == 'on') { - $nslookup = 'checked="checked"'; - } else { - $nslookup = False; - } - if (@$_REQUEST['rolling'] == 'on') { - $rolling = 'checked="checked"'; - } else { - $rolling = False; - } - - $output = '
-
Back Home flowmon '.$title.'
- - - - - - - -
Start Date: Start Time: End Date: End Time: Chart Style: Nslookup: - Rolling chart: -
- '; - -// '); + print(''); print(''); print(html_footer()); @@ -74,5 +77,4 @@ // We have selected a probe, present with detailes form to get the chart parameters } else if (array_key_exists('action', $_REQUEST) && $_REQUEST['action'] == 'probe_select') { - print(html_header($title = 'FlowMon '.$prog_vers.' - running on '.$_SERVER['HTTP_HOST'], $calendar = True)); print(page_header($title = 'FlowMon Chart Parameters')); @@ -110,26 +112,4 @@ print(html_header($title = 'FlowMon '.$prog_vers.' - running on '.$_SERVER['HTTP_HOST'], $calendar = True)); print(page_header($title = 'FlowMon Traffic Chart')); - - // Initialize defaults which might not exist - if (!@array_key_exists('style', $_REQUEST)) { - $_REQUEST['style'] = 'Top Sources'; - } - if (!@array_key_exists('timeframe', $_REQUEST)) { - $_REQUEST['timeframe'] = '3600'; - } - if (@$_REQUEST['rolling'] == 'on') { - $_REQUEST['startdate'] = strftime('%Y-%m-%d', (time() - $_REQUEST['timeframe'])); - $_REQUEST['starttime'] = strftime('%H:%M', (time() - $_REQUEST['timeframe'])); - $_REQUEST['enddate'] = strftime('%Y-%m-%d'); - $_REQUEST['endtime'] = strftime('%H:%M'); - - $starttime = strtotime($_REQUEST['startdate'].' '.$_REQUEST['starttime']); - $endtime = strtotime($_REQUEST['enddate'].' '.$_REQUEST['endtime']); - } else { - // Sanitize user input - $starttime = strtotime($_REQUEST['startdate'].' '.$_REQUEST['starttime']); - $endtime = strtotime($_REQUEST['enddate'].' '.$_REQUEST['endtime']); - } - print(chartstyle_header($_REQUEST['startdate'], $_REQUEST['starttime'], $_REQUEST['enddate'], $_REQUEST['endtime'])); print('

Statistics

'); @@ -140,16 +120,12 @@ } + // Sanitize user input + $starttime = strtotime($_REQUEST['startdate'].' '.$_REQUEST['starttime']); + $endtime = strtotime($_REQUEST['enddate'].' '.$_REQUEST['endtime']); if (!isvalid_probe($_REQUEST['probe'])) { die("Invalid Probe"); } - // Do we want namelookups? - if (@$_REQUEST['nslookup'] == 'on') { - $nslookup = True; - } else { - $nslookup = False; - } - - $file = chart_prepare($_REQUEST['probe'], $starttime, $endtime, $nslookup=$nslookup); + $file = chart_prepare($_REQUEST['probe'], $starttime, $endtime); print(''); @@ -159,3 +135,29 @@ print(html_footer()); +// Rolling chart requested +} else if (array_key_exists('action', $_REQUEST) && $_REQUEST['action'] == 'rollingchart') { + print(html_header($title = 'FlowMon '.$prog_vers.' - running on '.$_SERVER['HTTP_HOST'], $calendar = False, $refresh = 300)); + print(page_header($title = 'FlowMon Rolling Traffic Chart')); + print('
+

Rolling Statistics (updated every 5min)

'); + + // Import the Chart-Plugin + if (array_key_exists($_REQUEST['style'], $chart_styles)) { + require_once('charts/'.$chart_styles[$_REQUEST['style']]); + } + + // Sanitize user input + if (!isvalid_probe($_REQUEST['probe'])) { + die("Invalid Probe"); + } + + $file = chart_prepare($_REQUEST['probe'], False, False, $_REQUEST['timeframe']); + + print(''); + + print('
'); + + print(html_footer()); + + } Index: /unk/TODO =================================================================== --- /trunk/TODO (revision 7) +++ (revision ) @@ -1,7 +1,0 @@ - * Add Makefile (with funky features such as make mysql to create the DB) - - * Improved Error handling with better debugging output - - * Automatic protocol detection to enable "zeroconf-mode" - - * Dynamic configuration handling, configuration interface via the web.