bandwidthd-php: PHP files to graph bandwidthd data in a postgresql
authorJean-Michel Lacroix <redacted>
Thu, 14 Jul 2016 17:38:05 +0000 (13:38 -0400)
committerJean-Michel Lacroix <redacted>
Fri, 29 Jul 2016 23:33:10 +0000 (19:33 -0400)
database
Signed-off-by: Jean-Michel Lacroix <redacted>
bandwidthd-php is a set of PHP files that allows to graph bandwidthd
data stored in a postgresql database.  This bandwidthd data can be
generated using bandwidthd-pgsql in OpenWRT or LEDE or with
bandwidthd on different OS (Windows, Linux for example).

Addition of a config file and an init file.
Modification of the Makefile to take in account the changes above
Signed-off-by: Jean-Michel Lacroix <redacted>
utils/bandwidthd-php/Makefile [new file with mode: 0644]
utils/bandwidthd-php/files/bandwidthd-php.config [new file with mode: 0644]
utils/bandwidthd-php/files/bandwidthd-php.init [new file with mode: 0755]
utils/bandwidthd-php/files/details.php [new file with mode: 0644]
utils/bandwidthd-php/files/footer.php [new file with mode: 0644]
utils/bandwidthd-php/files/graph.php [new file with mode: 0644]
utils/bandwidthd-php/files/include.php [new file with mode: 0644]
utils/bandwidthd-php/files/index.php [new file with mode: 0644]
utils/bandwidthd-php/files/legend.gif [new file with mode: 0644]
utils/bandwidthd-php/files/logo.gif [new file with mode: 0644]

diff --git a/utils/bandwidthd-php/Makefile b/utils/bandwidthd-php/Makefile
new file mode 100644 (file)
index 0000000..29a19ec
--- /dev/null
@@ -0,0 +1,51 @@
+#
+# Copyright (C) 2006-2016 OpenWrt.org
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=bandwidthd-php
+PKG_VERSION:=2.0.1
+PKG_RELEASE:=1
+
+PKG_MAINTAINER:=Jean-Michel Lacroix <lacroix@lepine-lacroix.info>
+
+PKG_LICENSE:=GPL-2.0
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/bandwidthd-php
+  SECTION:=utils
+  CATEGORY:=Utilities
+  DEPENDS:=+libpcre +libxml2 +php5 +php5-cgi +php5-mod-pgsql +php5-mod-gd
+  TITLE:=PHP files to graph bandwidthd data in a postgresql database
+  URL:=http://bandwidthd.sourceforge.net/
+endef
+
+define Package/bandwidthd-php/description
+  PHP files to graph bandwidthd data in a postgresql database
+endef
+
+define Build/Compile
+endef
+
+define Package/bandwidthd-php/install
+       $(INSTALL_DIR) $(1)/www/phphtdocs
+       $(INSTALL_DATA) ./files/legend.gif $(1)/www/phphtdocs/
+       $(INSTALL_DATA) ./files/logo.gif $(1)/www/phphtdocs/
+       $(INSTALL_DATA) ./files/details.php $(1)/www/phphtdocs/
+       $(INSTALL_DATA) ./files/footer.php $(1)/www/phphtdocs/
+       $(INSTALL_DATA) ./files/graph.php $(1)/www/phphtdocs/
+       $(INSTALL_DATA) ./files/include.php $(1)/www/phphtdocs/
+       $(INSTALL_DATA) ./files/index.php $(1)/www/phphtdocs/
+       ln -s /var/etc/bandwidthd-php.conf $(1)/www/phphtdocs/config.conf
+       $(INSTALL_DIR) $(1)//etc/config
+       $(INSTALL_CONF) ./files/bandwidthd-php.config $(1)/etc/config/bandwidthd-php
+       $(INSTALL_DIR) $(1)/etc/init.d
+       $(INSTALL_BIN) ./files/bandwidthd-php.init $(1)/etc/init.d/bandwidthd-php
+endef
+
+$(eval $(call BuildPackage,bandwidthd-php))
diff --git a/utils/bandwidthd-php/files/bandwidthd-php.config b/utils/bandwidthd-php/files/bandwidthd-php.config
new file mode 100644 (file)
index 0000000..3bad494
--- /dev/null
@@ -0,0 +1,7 @@
+config bandwidthd-php
+       option dflt_width       '900'
+       option dflt_height      '256'
+       option dflt_interval    'INT_DAILY'
+       option host             '127.0.0.1'
+       option user             'postgres'
+       option dbname           'bandwidthd'
diff --git a/utils/bandwidthd-php/files/bandwidthd-php.init b/utils/bandwidthd-php/files/bandwidthd-php.init
new file mode 100755 (executable)
index 0000000..f3a7c67
--- /dev/null
@@ -0,0 +1,75 @@
+#!/bin/sh /etc/rc.common
+# Copyright (C) 2006-2016 OpenWrt.org
+
+START=99
+
+USE_PROCD=1
+
+CONFIGNAME="bandwidthd-php"
+CONFIGFILE="/var/etc/bandwidthd-php.conf"
+
+config_cb() {
+       local cfg_type="$1"
+       local cfg_name="$2"
+       case "$cfg_type" in
+               $CONFIGNAME)
+                       append cfgs "$cfg_name"
+               ;;
+       esac
+}
+
+export_number() {
+       local option="$1"
+       local section="$2"
+       local _loctmp
+       paramstr=""
+       config_get _loctmp "$section" "$option"
+       if [ -n "$_loctmp" ]; then
+               paramstr="${_loctmp}"
+       fi
+}
+
+export_string() {
+       local option="$1"
+       local section="$2"
+       local _loctmp
+       paramstr=""
+       config_get _loctmp "$section" "$option"
+       if [ -n "$_loctmp" ]; then
+               paramstr="${_loctmp}"
+       fi
+}
+
+service_triggers()
+{
+        procd_add_reload_trigger $CONFIGNAME
+}
+
+
+start_service() {
+       local conffile="<?php\n// auto-generated config file from /etc/config/${CONFIGNAME}\n"
+        rm -f $CONFIGFILE
+        touch $CONFIGFILE
+
+       config_load $CONFIGNAME
+       for cfg in $cfgs; do
+               export_number dflt_width $cfg
+               conffile="${conffile}define(\"DFLT_WIDTH\", ${paramstr:-"900"});\n"
+
+               export_number dflt_height $cfg
+                       conffile="${conffile}define(\"DFLT_HEIGHT\", ${paramstr:-"256"});\n"
+
+               export_string dflt_interval $cfg
+               conffile="${conffile}define(\"DFLT_INTERVAL\", ${paramstr:-"INT_DAILY"});\n\n"'$db_connect_string = "host='
+
+               export_string host $cfg
+               conffile="${conffile}${paramstr:-"127.0.0.1"} user="
+
+               export_string user $cfg
+               conffile="${conffile}${paramstr:-"postgres"} dbname="
+
+               export_string dbname $cfg
+               conffile="${conffile}${paramstr:-"bandwidthd"}"'"'"\n?>"
+               [ -n "$conffile" ] && echo -e "$conffile" >>$CONFIGFILE
+       done
+}
diff --git a/utils/bandwidthd-php/files/details.php b/utils/bandwidthd-php/files/details.php
new file mode 100644 (file)
index 0000000..d2f0786
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+include("include.php");
+?>
+<html>
+<center>
+<img src=logo.gif>
+<?php
+if (isset($_GET['sensor_name']))
+    $sensor_name = $_GET['sensor_name'];
+else
+    {
+    echo "<br>Please provide a sensor_name";
+    exit(1);
+    }
+
+if (isset($_GET['ip']))
+    $ip = $_GET['ip'];
+else
+    {
+    echo "<br>Please provide an ip address";
+    exit(1);
+    }
+                                                                                                                             
+echo "<h3>";
+if (strpos($ip, "/") === FALSE)
+       echo "$ip - ".gethostbyaddr($ip)."</h3>";
+else
+       echo "Total - $ip</h3>";
+
+$db = ConnectDb();
+
+if ($ip == "0.0.0.0/0")
+       {
+    $rxtable = "bd_rx_total_log";
+       $txtable = "bd_tx_total_log";
+       }
+else
+       {
+    $rxtable = "bd_rx_log";
+       $txtable = "bd_tx_log";
+       }
+
+$sql = "select rx.scale as rxscale, tx.scale as txscale, tx.total+rx.total as total, tx.total as sent,
+rx.total as received, tx.tcp+rx.tcp as tcp, tx.udp+rx.udp as udp,
+tx.icmp+rx.icmp as icmp, tx.http+rx.http as http,
+tx.p2p+rx.p2p as p2p, tx.ftp+rx.ftp as ftp
+from
+                                                                                                                             
+(SELECT ip, max(total/sample_duration)*8 as scale, sum(total) as total, sum(tcp) as tcp, sum(udp) as udp, sum(icmp) as icmp,
+sum(http) as http, sum(p2p) as p2p, sum(ftp) as ftp
+from sensors, $txtable
+where sensor_name = '$sensor_name'
+and sensors.sensor_id = ".$txtable.".sensor_id
+and ip <<= '$ip'
+group by ip) as tx,
+                                                                                                                             
+(SELECT ip, max(total/sample_duration)*8 as scale, sum(total) as total, sum(tcp) as tcp, sum(udp) as udp, sum(icmp) as icmp,
+sum(http) as http, sum(p2p) as p2p, sum(ftp) as ftp
+from sensors, $rxtable
+where sensor_name = '$sensor_name'
+and sensors.sensor_id = ".$rxtable.".sensor_id
+and ip <<= '$ip'
+group by ip) as rx
+                                                                                                                             
+where tx.ip = rx.ip;";
+//echo "</center><pre>$sql</pre><center>";exit(0);
+$result = pg_query($sql);
+echo "<table width=100% border=1 cellspacing=0><tr><td>Ip<td>Name<td>Total<td>Sent<td>Received<td>tcp<td>udp<td>icmp<td>http<td>p2p<td>ftp";
+$r = pg_fetch_array($result);
+echo "<tr><td>";
+if (strpos($ip, "/") === FALSE)
+       echo "$ip<td>".gethostbyaddr($ip);
+else
+       echo "Total<td>$ip";
+echo fmtb($r['total']).fmtb($r['sent']).fmtb($r['received']).
+       fmtb($r['tcp']).fmtb($r['udp']).fmtb($r['icmp']).fmtb($r['http']).
+    fmtb($r['p2p']).fmtb($r['ftp']);
+echo "</table></center>";
+
+echo "<center><h4>Daily</h4></center>";
+echo "Send:<br><img src=graph.php?ip=$ip&sensor_name=".$sensor_name."&table=$txtable&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+echo "<img src=legend.gif><br>";
+echo "Receive:<br><img src=graph.php?ip=$ip&sensor_name=".$sensor_name."&table=$rxtable&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+echo "<img src=legend.gif><br>";
+
+echo "<center><h4>Weekly</h4></center>";
+echo "Send:<br><img src=graph.php?interval=".INT_WEEKLY."&ip=$ip&sensor_name=$sensor_name&table=$txtable&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+echo "<img src=legend.gif><br>";
+echo "Receive:<br><img src=graph.php?interval=".INT_WEEKLY."&ip=$ip&sensor_name=$sensor_name&table=$rxtable&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+echo "<img src=legend.gif><br>";
+
+echo "<center><h4>Monthly</h4></center>";
+echo "Send:<br><img src=graph.php?interval=".INT_MONTHLY."&ip=$ip&sensor_name=$sensor_name&table=$txtable&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+echo "<img src=legend.gif><br>";
+echo "Receive:<br><img src=graph.php?interval=".INT_MONTHLY."&ip=$ip&sensor_name=$sensor_name&table=$rxtable&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+echo "<img src=legend.gif><br>";
+
+echo "<center><h4>Yearly</h4></center>";
+echo "Send:<br><img src=graph.php?interval=".INT_YEARLY."&ip=$ip&sensor_name=$sensor_name&table=$txtable&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+echo "<img src=legend.gif><br>";
+echo "Receive:<br><img src=graph.php?interval=".INT_YEARLY."&ip=$ip&sensor_name=$sensor_name&table=$rxtable&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+echo "<img src=legend.gif><br>";
diff --git a/utils/bandwidthd-php/files/footer.php b/utils/bandwidthd-php/files/footer.php
new file mode 100644 (file)
index 0000000..b342825
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+echo("Page load completed in ". (time() - $starttime) ." seconds"); 
+?>
diff --git a/utils/bandwidthd-php/files/graph.php b/utils/bandwidthd-php/files/graph.php
new file mode 100644 (file)
index 0000000..b978177
--- /dev/null
@@ -0,0 +1,457 @@
+<?php
+require("include.php");
+
+// Returns x location of any given timestamp
+function ts2x($ts)
+       {
+       global $timestamp, $width, $interval;
+       return(($ts-$timestamp)*(($width-XOFFSET) / $interval) + XOFFSET);
+       }
+
+// If we have multiple IP's in a result set we need to total the average of each IP's samples
+function AverageAndAccumulate()
+       {
+       global $Count, $total, $icmp, $udp, $tcp, $ftp, $http, $p2p, $YMax;
+       global $a_total, $a_icmp, $a_udp, $a_tcp, $a_ftp, $a_http, $a_p2p;
+       
+       foreach ($Count as $key => $number)
+       {
+           $total[$key] /= $number;
+       $icmp[$key] /= $number;
+       $udp[$key] /= $number;
+       $tcp[$key] /= $number;
+       $ftp[$key] /= $number;
+       $http[$key] /= $number;
+       $p2p[$key] /= $number;
+       }
+
+       foreach ($Count as $key => $number) 
+               {
+               $a_total[$key] += $total[$key];
+               $a_icmp[$key] += $icmp[$key];
+               $a_udp[$key] += $udp[$key];
+               $a_tcp[$key] += $tcp[$key];
+               $a_ftp[$key] += $ftp[$key];
+               $a_http[$key] += $http[$key];
+               $a_p2p[$key] += $p2p[$key];
+
+               if ($a_total[$key] > $YMax)
+                       $YMax = $a_total[$key];
+               }
+       
+       unset($GLOBALS['total'], $GLOBALS['icmp'], $GLOBALS['udp'], $GLOBALS['tcp'], $GLOBALS['ftp'], $GLOBALS['http'], $GLOBALS['p2p'], $GLOBALS['Count']);
+
+       $total = array();
+       $icmp = array();
+       $udp = array();
+       $tcp = array();
+       $ftp = array();
+       $http = array();
+       $p2p = array();
+       $Count = array();
+       }
+
+$db = ConnectDb();
+
+// Get parameters
+
+if (isset($_GET['width']))
+    $width = $_GET['width'];
+else
+       $width = DFLT_WIDTH;
+
+if (isset($_GET['height']))
+    $height = $_GET['height'];
+else
+       $height = DFLT_HEIGHT;
+
+if (isset($_GET['interval']))
+    $interval = $_GET['interval'];
+else
+       $interval = DFLT_INTERVAL;
+
+if (isset($_GET['ip']))
+    $ip = $_GET['ip'];
+else
+       exit(1);
+
+if (isset($_GET['sensor_name']))
+       $sensor_name = $_GET['sensor_name'];
+else
+       exit(1);
+
+if (isset($_GET['timestamp']))
+    $timestamp = $_GET['timestamp'];
+else
+       $timestamp = time() - $interval + (0.05*$interval);
+
+if (isset($_GET['table']))
+    $table = $_GET['table'];
+else
+       $table = "bd_rx_log";
+
+if (isset($_GET['yscale']))
+    $yscale = $_GET['yscale'];
+
+$total = array();
+$icmp = array();
+$udp = array();
+$tcp = array();
+$ftp = array();
+$http = array();
+$p2p = array();
+$Count = array();
+
+// Accumulator
+$a_total = array();
+$a_icmp = array();
+$a_udp = array();
+$a_tcp = array();
+$a_ftp = array();
+$a_http = array();
+$a_p2p = array();
+
+$sql = "select *, extract(epoch from timestamp) as ts from sensors, $table where sensors.sensor_id = ".$table.".sensor_id and ip <<= '$ip' and sensor_name = '$sensor_name' and timestamp > $timestamp::abstime and timestamp < ".($timestamp+$interval)."::abstime order by ip;";
+//echo $sql."<br>"; exit(1);
+$result = pg_query($sql);
+
+// The SQL statement pulls the data out of the database ordered by IP address, that way we can average each
+// datapoint for each IP address to provide smoothing and then toss the smoothed value into the accumulator
+// to provide accurate total traffic rate.
+
+while ($row = pg_fetch_array($result))
+       {
+       if ($row['ip'] != $last_ip)
+               {
+               AverageAndAccumulate();
+               $last_ip = $row['ip'];
+               }
+
+       $x = ($row['ts']-$timestamp)*(($width-XOFFSET)/$interval)+XOFFSET;
+       $xint = (int) $x;
+
+       //echo "xint: ".$xint."<br>";
+       $Count[$xint]++;
+                                                                                                                             
+       if ($row['total']/$row['sample_duration'] > $SentPeak)
+               $SentPeak = $row['total']/$row['sample_duration'];
+       $TotalSent += $row['total'];
+       $total[$xint] += $row['total']/$row['sample_duration'];
+       $icmp[$xint] += $row['icmp']/$row['sample_duration'];
+       $udp[$xint] += $row['udp']/$row['sample_duration'];
+       $tcp[$xint] += $row['tcp']/$row['sample_duration'];
+       $ftp[$xint] += $row['ftp']/$row['sample_duration'];
+       $http[$xint] += $row['http']/$row['sample_duration'];
+       $p2p[$xint] += $row['p2p']/$row['sample_duration'];                                                                                                                             
+       }
+
+// One more time for the last IP
+AverageAndAccumulate();
+
+// Pull the data out of Accumulator
+$total = $a_total;
+$icmp = $a_icmp;
+$udp = $a_udp;
+$tcp = $a_tcp;
+$ftp = $a_ftp;
+$http = $a_http;
+$p2p = $a_p2p;
+
+$YMax += $YMax*0.05;    // Add an extra 5%
+
+// if a y scale was specified override YMax
+if (isset($yscale))
+    $YMax = $yscale/8;
+
+// Plot the data
+
+header("Content-type: image/png");
+
+$im = imagecreate($width, $height);
+$white = imagecolorallocate($im, 255, 255, 255);
+$yellow = ImageColorAllocate($im, 255, 255, 0);
+$purple = ImageColorAllocate($im, 255, 0, 255);
+$green  = ImageColorAllocate($im, 0, 255, 0);
+$blue   = ImageColorAllocate($im, 0, 0, 255);
+$lblue  = ImageColorAllocate($im, 128, 128, 255);
+$brown  = ImageColorAllocate($im, 128, 0, 0);
+$red    = ImageColorAllocate($im, 255, 0, 0);
+$black  = ImageColorAllocate($im, 0, 0, 0);
+
+for($Counter=XOFFSET+1; $Counter < $width; $Counter++)
+       {
+       if (isset($total[$Counter]))
+               {
+               // Convert the bytes/sec to y coords
+        $total[$Counter] = ($total[$Counter]*($height-YOFFSET))/$YMax;
+               $tcp[$Counter] = ($tcp[$Counter]*($height-YOFFSET))/$YMax;
+        $ftp[$Counter] = ($ftp[$Counter]*($height-YOFFSET))/$YMax;
+               $http[$Counter] = ($http[$Counter]*($height-YOFFSET))/$YMax;
+               $p2p[$Counter] = ($p2p[$Counter]*($height-YOFFSET))/$YMax;
+        $udp[$Counter] = ($udp[$Counter]*($height-YOFFSET))/$YMax;
+               $icmp[$Counter] = ($icmp[$Counter]*($height-YOFFSET))/$YMax;
+
+               // Stack 'em up!
+               // Total is stacked from the bottom
+               // Icmp is on the bottom too
+               // Udp is stacked on top of icmp
+               $udp[$Counter] += $icmp[$Counter];
+               // TCP and p2p are stacked on top of Udp
+               $tcp[$Counter] += $udp[$Counter];
+               $p2p[$Counter] += $udp[$Counter];
+               // Http is stacked on top of p2p
+               $http[$Counter] += $p2p[$Counter];
+               // Ftp is stacked on top of http
+        $ftp[$Counter] += $http[$Counter];
+
+               // Plot them!
+               //echo "$Counter:".$Counter." (h-y)-t:".($height-YOFFSET) - $total[$Counter]." h-YO-1:".$height-YOFFSET-1;
+        ImageLine($im, $Counter, ($height-YOFFSET) - $total[$Counter], $Counter, $height-YOFFSET-1, $yellow);
+        ImageLine($im, $Counter, ($height-YOFFSET) - $icmp[$Counter], $Counter, $height-YOFFSET-1, $red);
+        ImageLine($im, $Counter, ($height-YOFFSET) - $udp[$Counter], $Counter, ($height-YOFFSET) - $icmp[$Counter] - 1, $brown);
+        ImageLine($im, $Counter, ($height-YOFFSET) - $tcp[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $green);
+        ImageLine($im, $Counter, ($height-YOFFSET) - $p2p[$Counter], $Counter, ($height-YOFFSET) - $udp[$Counter] - 1, $purple);
+        ImageLine($im, $Counter, ($height-YOFFSET) - $http[$Counter], $Counter, ($height-YOFFSET) - $p2p[$Counter] - 1, $blue);
+        ImageLine($im, $Counter, ($height-YOFFSET) - $ftp[$Counter], $Counter, ($height-YOFFSET) - $http[$Counter] - 1, $lblue);
+               }
+//     else
+//             echo $Counter." not set<br>";
+       }                                                                                                                             
+
+// Margin Text
+if ($SentPeak < 1024/8)
+       $txtPeakSendRate = sprintf("Peak Send Rate: %.1f KBits/sec", $SentPeak*8);
+else if ($SentPeak < (1024*1024)/8)
+    $txtPeakSendRate = sprintf("Peak Send Rate: %.1f MBits/sec", ($SentPeak*8.0)/1024.0);
+else 
+       $txtPeakSendRate = sprintf("Peak Send Rate: %.1f GBits/sec", ($SentPeak*8.0)/(1024.0*1024.0));
+                                                                                                                             
+if ($TotalSent < 1024)
+       $txtTotalSent = sprintf("Sent %.1f KBytes", $TotalSent);
+else if ($TotalSent < 1024*1024)
+       $txtTotalSent = sprintf("Sent %.1f MBytes", $TotalSent/1024.0);
+else 
+       $txtTotalSent = sprintf("Sent %.1f GBytes", $TotalSent/(1024.0*1024.0));
+                                                                                                                             
+ImageString($im, 2, XOFFSET+5,  $height-20, $txtTotalSent, $black);
+ImageString($im, 2, $width/2+XOFFSET/2,  $height-20, $txtPeakSendRate, $black);
+
+// Draw X Axis
+
+ImageLine($im, 0, $height-YOFFSET, $width, $height-YOFFSET, $black);
+
+// Day/Month Seperator bars
+
+if ((24*60*60*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
+       {
+       $ts = getdate($timestamp);
+       $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
+       
+    $x = ts2x($MarkTime);
+    while ($x < XOFFSET)
+       {
+        $MarkTime += (24*60*60);
+           $x = ts2x($MarkTime);
+        }
+                                                                                                                             
+    while ($x < ($width-10))
+       {
+        // Day Lines
+        ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
+        ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
+                                                                                                                             
+        $txtDate = strftime("%a, %b %d", $MarkTime);
+        ImageString($im, 2, $x-30,  $height-YOFFSET+10, $txtDate, $black);
+                                                                                                                             
+        // Calculate Next x
+        $MarkTime += (24*60*60);
+           $x = ts2x($MarkTime);
+        }
+       }
+else if ((24*60*60*30*($width-XOFFSET))/$interval > ($width-XOFFSET)/10)
+       {
+       // Monthly Bars
+       $ts = getdate($timestamp);
+       $month = $ts['mon'];
+       $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
+       
+    $x = ts2x($MarkTime);
+    while ($x < XOFFSET)
+       {
+               $month++;
+        $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
+           $x = ts2x($MarkTime);
+        }
+                                                                                                                             
+    while ($x < ($width-10))
+       {
+        // Day Lines
+        ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
+        ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
+                                                                                                                             
+        $txtDate = strftime("%b, %Y", $MarkTime);
+        ImageString($im, 2, $x-25,  $height-YOFFSET+10, $txtDate, $black);
+                                                                                                                             
+        // Calculate Next x
+               $month++;
+        $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
+           $x = ts2x($MarkTime);
+        }
+       }
+else
+       {
+       // Year Bars
+    $ts = getdate($timestamp);
+    $year = $ts['year'];
+    $MarkTime = mktime(0, 0, 0, 1, 1, $year);
+                                                                                                                             
+    $x = ts2x($MarkTime);
+    while ($x < XOFFSET)
+        {
+        $year++;
+        $MarkTime = mktime(0, 0, 0, 1, 1, $year);
+        $x = ts2x($MarkTime);
+        }
+                                                                                                                             
+    while ($x < ($width-10))
+        {
+        // Day Lines
+        ImageLine($im, $x, 0, $x, $height-YOFFSET, $black);
+        ImageLine($im, $x+1, 0, $x+1, $height-YOFFSET, $black);
+                                                                                                                             
+        $txtDate = strftime("%b, %Y", $MarkTime);
+        ImageString($im, 2, $x-25,  $height-YOFFSET+10, $txtDate, $black);
+                                                                                                                             
+        // Calculate Next x
+        $year++;
+        $MarkTime = mktime(0, 0, 0, 1, 1, $year);
+        $x = ts2x($MarkTime);
+        }      
+       }
+
+// Draw Major Tick Marks
+if ((6*60*60*($width-XOFFSET))/$interval > 10) // pixels per 6 hours is more than 2
+       $MarkTimeStep = 6*60*60; // Major ticks are 6 hours
+else if ((24*60*60*($width-XOFFSET))/$interval > 10)
+       $MarkTimeStep = 24*60*60; // Major ticks are 24 hours;
+else if ((24*60*60*30*($width-XOFFSET))/$interval > 10)
+       {
+       // Major tick marks are months
+       $MarkTimeStep = 0; // Skip the standard way of drawing major tick marks below
+
+    $ts = getdate($timestamp);
+    $month = $ts['mon'];
+    $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
+                                                                                                                             
+    $x = ts2x($MarkTime);
+    while ($x < XOFFSET)
+        {
+        $month++;
+        $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
+        $x = ts2x($MarkTime);
+        }
+                                                                                                                             
+    while ($x < ($width-10))
+        {
+        // Day Lines
+               $date = getdate($MarkTime);
+               if ($date['mon'] != 1)
+                       {
+               ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);                                                                                                                      
+           $txtDate = strftime("%b", $MarkTime);
+               ImageString($im, 2, $x-5,  $height-YOFFSET+10, $txtDate, $black);
+               }
+                                                                                                                   
+        // Calculate Next x
+        $month++;
+        $MarkTime = mktime(0, 0, 0, $month, 1, $ts['year']);
+        $x = ts2x($MarkTime);
+        }
+       }
+else
+       $MarkTimeStep = 0; // Skip Major Tick Marks
+
+if ($MarkTimeStep)
+       {
+       $ts = getdate($timestamp);
+       $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
+       $x = ts2x($MarkTime);
+
+       while ($x < ($width-10))
+               {
+       if ($x > XOFFSET) 
+                       {
+           ImageLine($im, $x, $height-YOFFSET-5, $x, $height-YOFFSET+5, $black);
+               }
+               $MarkTime += $MarkTimeStep;
+           $x = ts2x($MarkTime);
+               }
+       }
+
+// Draw Minor Tick marks
+if ((60*60*($width-XOFFSET))/$interval > 4) // pixels per hour is more than 2
+       $MarkTimeStep = 60*60;  // Minor ticks are 1 hour
+else if ((6*60*60*($width-XOFFSET))/$interval > 4)
+       $MarkTimeStep = 6*60*60; // Minor ticks are 6 hours
+else if ((24*60*60*($width-XOFFSET))/$interval > 4)
+       $MarkTimeStep = 24*60*60;
+else
+       $MarkTimeStep = 0; // Skip minor tick marks
+
+if ($MarkTimeStep)
+       {
+       $ts = getdate($timestamp);
+       $MarkTime = mktime(0, 0, 0, $ts['mon'], $ts['mday'], $ts['year']);
+       $x = ts2x($MarkTime);
+
+       while ($x < ($width-10))
+               {
+       if ($x > XOFFSET) 
+                       {
+           ImageLine($im, $x, $height-YOFFSET, $x, $height-YOFFSET+5, $black);
+               }
+               $MarkTime += $MarkTimeStep;
+           $x = ts2x($MarkTime);
+               }
+       }
+
+// Draw Y Axis
+ImageLine($im, XOFFSET, 0, XOFFSET, $height, $black);
+
+$YLegend = 'k';
+$Divisor = 1;
+if ($YMax*8 > 1024*2)
+       {
+    $Divisor = 1024;    // Display in m
+    $YLegend = 'm';
+    }
+
+if ($YMax*8 > 1024*1024*2)
+       {
+    $Divisor = 1024*1024; // Display in g
+    $YLegend = 'g';
+       }
+
+if ($YMax*8 > 1024*1024*1024*2)
+       {
+    $Divisor = 1024*1024*1024; // Display in t
+    $YLegend = 't';
+    }
+                                                                                                                             
+$YStep = $YMax/10;
+if ($YStep < 1)
+       $YStep=1;
+$YTic=$YStep;
+                                                                                                                             
+while ($YTic <= ($YMax - $YMax/10))
+       {
+    $y = ($height-YOFFSET)-(($YTic*($height-YOFFSET))/$YMax);
+       ImageLine($im, XOFFSET, $y, $width, $y, $black);
+    $txtYLegend = sprintf("%4.1f %sbits/s", (8.0*$YTic)/$Divisor, $YLegend);
+    ImageString($im, 2, 3, $y-7, $txtYLegend, $black);
+       $YTic += $YStep;
+       }
+
+imagepng($im); 
+imagedestroy($im);
diff --git a/utils/bandwidthd-php/files/include.php b/utils/bandwidthd-php/files/include.php
new file mode 100644 (file)
index 0000000..6b435d1
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+define("INT_DAILY", 60*60*24*2);
+define("INT_WEEKLY", 60*60*24*8);
+define("INT_MONTHLY", 60*60*24*35);
+define("INT_YEARLY", 60*60*24*400);
+
+define("XOFFSET", 90);
+define("YOFFSET", 45);
+
+require("config.conf");
+
+function ConnectDb()
+    {
+       global $db_connect_string;
+
+    $db = pg_pconnect($db_connect_string);
+    if (!$db)
+        {
+        printf("DB Error, could not connect to database");
+        exit(1);
+        }
+    return($db);
+    }
+                                                                                                                             
+function fmtb($kbytes)
+       {
+       $Max = 1024;
+       $Output = $kbytes;
+       $Suffix = 'K';
+
+       if ($Output > $Max)
+               {
+               $Output /= 1024;
+               $Suffix = 'M';
+               }
+
+       if ($Output > $Max)
+               {
+               $Output /= 1024;
+               $Suffix = 'G';
+               }
+
+       if ($Output > $Max)
+               {
+               $Output /= 1024;
+               $Suffix = 'T';
+               }
+
+       return(sprintf("<td align=right><tt>%.1f%s</td>", $Output, $Suffix));
+       }
+
+$starttime = time();
+set_time_limit(300);
+?>
diff --git a/utils/bandwidthd-php/files/index.php b/utils/bandwidthd-php/files/index.php
new file mode 100644 (file)
index 0000000..5159f85
--- /dev/null
@@ -0,0 +1,197 @@
+<?php
+include("include.php");
+?>
+<html>
+<center>
+<img src=logo.gif>
+<?php 
+// Get variables from url
+
+if (isset($_GET['sensor_name']) && $_GET['sensor_name'] != "none")
+    $sensor_name = $_GET['sensor_name'];
+
+if (isset($_GET['interval']) && $_GET['interval'] != "none")
+    $interval = $_GET['interval'];
+
+if (isset($_GET['timestamp']) && $_GET['timestamp'] != "none")
+    $timestamp = $_GET['timestamp'];
+
+if (isset($_GET['subnet']) && $_GET['subnet'] != "none")
+    $subnet = $_GET['subnet'];
+
+if (isset($_GET['limit']) && $_GET['limit'] != "none")
+       $limit = $_GET['limit'];
+
+
+$db = ConnectDb();
+?>
+<FORM name="navigation" method=get action=<?php echo $PHP_SELF?>>
+<table width=100% cellspacing=0 cellpadding=5 border=1>
+<tr>
+<td><SELECT name="sensor_name">
+
+<OPTION value="none">--Select A Sensor--
+<?php 
+$sql = "SELECT sensor_name from sensors order by sensor_name;";
+$result = pg_query($sql);
+while ($r = pg_fetch_array($result))
+    echo "<option value=\"".$r['sensor_name']."\" ".($sensor_name==$r['sensor_name']?"SELECTED":"").">".$r['sensor_name']."\n";
+?>
+</SELECT>
+<td><SELECT name="interval">
+<OPTION value="none">--Select An Interval--
+<OPTION value=<?php echo INT_DAILY?> <?php echo $interval==INT_DAILY?"SELECTED":""?>>Daily
+<OPTION value=<?php echo INT_WEEKLY?> <?php echo $interval==INT_WEEKLY?"SELECTED":""?>>Weekly
+<OPTION value=<?php echo INT_MONTHLY?> <?php echo $interval==INT_MONTHLY?"SELECTED":""?>>Monthly
+<OPTION value=<?php echo INT_YEARLY?> <?php echo $interval==INT_YEARLY?"SELECTED":""?>>Yearly
+<OPTION value=<?php echo 24*60*60?> <?php echo $interval==24*60*60?"SELECTED":""?>>24hrs
+<OPTION value=<?php echo 30*24*60*60?> <?php echo $interval==30*24*60*60?"SELECTED":""?>>30days
+</select>
+
+<td><SELECT name="limit">
+<OPTION value="none">--How Many Results--
+<OPTION value=20 <?php echo $limit==20?"SELECTED":""?>>20
+<OPTION value=50 <?php echo $limit==50?"SELECTED":""?>>50
+<OPTION value=100 <?php echo $limit==100?"SELECTED":""?>>100
+<OPTION value=all <?php echo $limit=="all"?"SELECTED":""?>>All
+</select>
+
+<td>Subnet Filter:<input name=subnet value="<?php echo isset($subnet)?$subnet:"0.0.0.0/0"?>"> 
+<input type=submit value="Go">
+</table>
+</FORM>
+<?php 
+// Set defaults
+if (!isset($interval))
+       $interval = DFLT_INTERVAL;
+
+if (!isset($timestamp))
+       $timestamp = time() - $interval + (0.05*$interval);
+
+if (!isset($limit))
+       $limit = 20;
+
+// Validation
+if (!isset($sensor_name))
+       exit(0);
+
+// Print Title
+
+if (isset($limit))
+       echo "<h2>Top $limit - $sensor_name</h2>";
+else
+       echo "<h2>All Records - $sensor_name</h2>";
+
+// Sqlize the incomming variables
+if (isset($subnet))
+       $sql_subnet = "and ip <<= '$subnet'";
+
+// Sql Statement
+$sql = "select tx.ip, rx.scale as rxscale, tx.scale as txscale, tx.total+rx.total as total, tx.total as sent, 
+rx.total as received, tx.tcp+rx.tcp as tcp, tx.udp+rx.udp as udp,
+tx.icmp+rx.icmp as icmp, tx.http+rx.http as http,
+tx.p2p+rx.p2p as p2p, tx.ftp+rx.ftp as ftp
+from
+
+(SELECT ip, max(total/sample_duration)*8 as scale, sum(total) as total, sum(tcp) as tcp, sum(udp) as udp, sum(icmp) as icmp,
+sum(http) as http, sum(p2p) as p2p, sum(ftp) as ftp
+from sensors, bd_tx_log
+where sensor_name = '$sensor_name'
+and sensors.sensor_id = bd_tx_log.sensor_id
+$sql_subnet
+and timestamp > $timestamp::abstime and timestamp < ".($timestamp+$interval)."::abstime
+group by ip) as tx,
+
+(SELECT ip, max(total/sample_duration)*8 as scale, sum(total) as total, sum(tcp) as tcp, sum(udp) as udp, sum(icmp) as icmp,
+sum(http) as http, sum(p2p) as p2p, sum(ftp) as ftp
+from sensors, bd_rx_log
+where sensor_name = '$sensor_name'
+and sensors.sensor_id = bd_rx_log.sensor_id
+$sql_subnet
+and timestamp > $timestamp::abstime and timestamp < ".($timestamp+$interval)."::abstime
+group by ip) as rx
+
+where tx.ip = rx.ip
+order by total desc;";
+
+//echo "</center><pre>$sql</pre><center>"; exit(0);
+pg_query("SET sort_mem TO 30000;");
+$result = pg_query($sql);
+pg_query("set sort_mem to default;");
+
+if ($limit == "all")
+       $limit = pg_num_rows($result);
+
+echo "<table width=100% border=1 cellspacing=0><tr><td>Ip<td>Name<td>Total<td>Sent<td>Received<td>tcp<td>udp<td>icmp<td>http<td>p2p<td>ftp";
+
+if (!isset($subnet)) // Set this now for total graphs
+       $subnet = "0.0.0.0/0";
+
+// Output Total Line
+echo "<TR><TD><a href=Total>Total</a><TD>$subnet";
+foreach (array("total", "sent", "received", "tcp", "udp", "icmp", "http", "p2p", "ftp") as $key)
+       {
+       for($Counter=0, $Total = 0; $Counter < pg_num_rows($result); $Counter++)
+               {
+               $r = pg_fetch_array($result, $Counter);
+               $Total += $r[$key];
+               }
+       echo fmtb($Total);
+       }
+echo "\n";
+
+// Output Other Lines
+for($Counter=0; $Counter < pg_num_rows($result) && $Counter < $limit; $Counter++)
+       {
+       $r = pg_fetch_array($result, $Counter);
+       echo "<tr><td><a href=#".$r['ip'].">";
+       echo $r['ip']."<td>".gethostbyaddr($r['ip']);
+       echo "</a>";
+       echo fmtb($r['total']).fmtb($r['sent']).fmtb($r['received']).
+               fmtb($r['tcp']).fmtb($r['udp']).fmtb($r['icmp']).fmtb($r['http']).
+               fmtb($r['p2p']).fmtb($r['ftp'])."\n";
+       }
+echo "</table></center>";
+
+// Output Total Graph
+for($Counter=0, $Total = 0; $Counter < pg_num_rows($result); $Counter++)
+       {
+       $r = pg_fetch_array($result, $Counter);
+       $scale = max($r['txscale'], $scale);
+       $scale = max($r['rxscale'], $scale);
+       }
+
+if ($subnet == "0.0.0.0/0")
+       $total_table = "bd_tx_total_log";
+else
+       $total_table = "bd_tx_log";
+echo "<a name=Total><h3><a href=details.php?sensor_name=$sensor_name&ip=$subnet>";
+echo "Total - Total of $subnet</h3>";
+echo "</a>";
+echo "Send:<br><img src=graph.php?ip=$subnet&interval=$interval&sensor_name=".$sensor_name."&table=$total_table><br>";
+echo "<img src=legend.gif><br>\n";
+if ($subnet == "0.0.0.0/0")
+       $total_table = "bd_rx_total_log";
+else
+       $total_table = "bd_rx_log";
+echo "Receive:<br><img src=graph.php?ip=$subnet&interval=$interval&sensor_name=".$sensor_name."&table=$total_table><br>";
+echo "<img src=legend.gif><br>\n";
+
+
+// Output Other Graphs
+for($Counter=0; $Counter < pg_num_rows($result) && $Counter < $limit; $Counter++) 
+       {
+       $r = pg_fetch_array($result, $Counter);
+       echo "<a name=".$r['ip']."><h3><a href=details.php?sensor_name=$sensor_name&ip=".$r['ip'].">";
+       if ($r['ip'] == "0.0.0.0")
+               echo "Total - Total of all subnets</h3>";
+       else
+               echo $r['ip']." - ".gethostbyaddr($r['ip'])."</h3>";
+       echo "</a>";
+       echo "Send:<br><img src=graph.php?ip=".$r['ip']."&interval=$interval&sensor_name=".$sensor_name."&table=bd_tx_log&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+       echo "<img src=legend.gif><br>\n";
+       echo "Receive:<br><img src=graph.php?ip=".$r['ip']."&interval=$interval&sensor_name=".$sensor_name."&table=bd_rx_log&yscale=".(max($r['txscale'], $r['rxscale']))."><br>";
+       echo "<img src=legend.gif><br>\n";
+       }
+
+include('footer.php');
diff --git a/utils/bandwidthd-php/files/legend.gif b/utils/bandwidthd-php/files/legend.gif
new file mode 100644 (file)
index 0000000..3df9090
Binary files /dev/null and b/utils/bandwidthd-php/files/legend.gif differ
diff --git a/utils/bandwidthd-php/files/logo.gif b/utils/bandwidthd-php/files/logo.gif
new file mode 100644 (file)
index 0000000..2ffec56
Binary files /dev/null and b/utils/bandwidthd-php/files/logo.gif differ
git clone https://git.99rst.org/PROJECT