FIXME This page is not fully translated, yet. Please help completing the translation.
(remove this paragraph once the translation is finished)

Bandwidthd

http://bandwidthd.sourceforge.net/ (openWRT older than 17)

https://github.com/NethServer/bandwidthd (openWRT 17 or higher)

Note: bandwidthd in LEDE/OpenWRT 17.01 or higher tracks the SMTP data (ports 25, 465 and 587) while the version in the previous version of OpenWrt (Chaos Calmer and older) tracks the P2P data (Gnutella, eDonkey, etc.). All of the other data (FTP, UDP, TCP, etc.) is tracked the same way in all versions.

Bandwidthd涉及到4个软件包:

注意:bandwidthd, bandwidthd-pgsql, bandwidthd-sqliteinstall 只能安装三者中的一个!!

  1. bandwidthd: 如果安装这个包,不能保存数据到postgresql 或者 sqlite 数据库。路由器生成的bandwidth数据,可以通过这个网址访问 http://192.168.1.1/bandwidthd. 也就是如果您不需要保存数据到 postgresql 或者 sqlite 数据库,那么安装这个包即可。注意:路由器重启后历史数据会丢失。
  2. bandwidthd-pgsql: bandwidthd能干的活,这个包也能干,同时还支持保存数据到postgresql数据库,这个数据库可以位于路由器或者其他服务器。这些数据可以由PHP脚本分析,并在浏览器内展现。需要知晓的是,路由器、posgresql数据库、生成图形的PHP服务,可以位于3个不同设备上,各自分工一项任务(例如:路由器收集数据,然后发送至单独的数据库服务器,然后PHP服务器调取数据库服务器的数据生成图形展示)。如果postgresql数据不保存在路由器上,那么没有必要安装postgresql包。
  3. bandwidthd-php: 这个包提供了PHP文件位于路由器的/www/phphtdocs目录。只有在OpenWrt路由器作为网页服务器生成图形时才需要安装,访问 http://192.168.1.1/phphtdocs. 注意:如果安装bandwidthd-sqlite则不要安装bandwidthd-php,因为bandwidthd-sqlite包含了所需的PHP文件用于生成图形文件。
  4. bandwidthd-sqlite. 这软件包,除了bandwidthd能干的活以外,还可以保存数据到路由器的sqlite数据库,也能够通过PHP生成图形文件。在某种意义上来说,它是 bandwidthd-pgsql 和 bandwidthd-php 的组合, 但是是使用的sqlite数据库而不是postgresql。

不同的版本的OpenWrt/LEDE可用包情况不一致:

bandwidthd

  • In Backfire (10.03 or 10.03.1) as a package.
  • In Attitude Adjustment (12.09) as a package.
  • In Barrier Breaker (14.07) in “oldpackages” as a package.
  • In original Chaos Calmer (15.05) it is not available as a precompiled package: you have to compile it yourself and install it. See https://wiki.openwrt.org/doc/howto/build to have details on how to compile a package.
  • As a package in 15.05.1 or higher.

bandwidthd-pgsql

  • In the original Chaos Calmer (15.05) it is not available as a precompiled package: you have to compile it yourself and install it. See https://wiki.openwrt.org/doc/howto/build to have details on how to compile a package.
  • In the minor fix release of Chaos Calmer (15.05.1) it is available as a package.
  • As a package in 15.05.1 or higher.

bandwidthd-php

  • Available in LEDE/OpenWRT 17.01 or higher as a package and uses php7 or php8. The PHP files found in these versions work fine with Chaos Calmer 15.05 or 15.05.1 with php5 (tested).

bandwidthd-sqlite

  • Available in LEDE/OpenWRT 17.01 or higher.

安装bandwidthd包:

opkg install bandwidthd uhttpd
/etc/init.d/uhttpd enable
/etc/init.d/uhttpd start
/etc/init.d/bandwidthd enable
/etc/init.d/bandwidthd start

安装bandwidthd-pgsql包(注意:如不在路由器上生成图形,那么可以不安装uhttpd):

opkg install bandwidthd-pgsql uhttpd
/etc/init.d/uhttpd enable
/etc/init.d/uhttpd start
/etc/init.d/bandwidthd enable
/etc/init.d/bandwidthd start

所需其他依赖大致需要500kB空间,如果路由器IP地址为默认的192.168.1.1,那么即可通过 http://192.168.1.1/bandwidthd 访问服务。 如果修改了路由器IP,那么编辑配置文件(/etc/config/bandwidthd)内的IP地址为修改后的IP。

3个软件包 bandwidth, bandwidthd-pgsqlbandwidthd-sqlite 默认使用同一个配置文件: /etc/config/bandwidthd

无论哪一个软件包,安装后都会自动生成合适的配置文件,通常需要修改的配置项目不多(如果有的话)。

3个软件包的基础配置选项是一样的。bandwidthd-pgsql有额外2个配置项,bandwidthd-sqlite也有另外2个配置项。

具体配置选项:

option dev: 监听的设备,默认是 br-lan.

option subnets: 子网设置,不是制定子网的流量信息不进行统计,设置选项可以是IP子网掩码或CIDR。例如: “10.0.0.0 255.0.0.0”, “192.168.0.0/16” or “172.16.0.0/12”. 默认 “192.168.1.0/24”.

option skip_intervals: 重新生成图形文件的时间间隔,默认是 0.

option graph_cutoff: 制定一个IP生成图形数据的起始流量大小,单位是K,默认是 1024.

option promiscuous: 将接口设置为混杂模式,以对可能未通过主路由的流量进行记录,默认是 true.

option output_cdf: 将数据记录到cdf文件(log.cdf),所有软件包都可以记录到cdf文件,但仅在使用 bandwidthd 包时有用,因为其他包(bandwidthd-pgsql 和 bandwidthd-sqlite)可以将数据存储在数据库中。这些文件位于路由器的根目录 (/)。 bandwidthd启动时可以读取cdf文件(请参阅选项 recover_cdf),在您重启路由器可以恢复bandwidthd统计数据。 默认设置为 false.

option recover_cdf: 在启动时重新读取cdf文件。请参阅上面 output_cdf 的说明。默认值为 false

option filter: Libpcap 格式过滤器字符串用于控制 bandwidthd 能看到的内容。请始终在字符串中包含“ip”以避免出现奇怪的问题。默认值为 ip

option graph: 生成图形文件 - 默认为 true 以绘制bandwidthd正在记录的流量图。如果您只需要 cdf 输出或者您正在使用数据库输出选项(bandwidthd-pgsqlbandwidthd-sqlite),那颗可将此设置为 false。如果设置为 false,Bandwidthd 将使用较少的内存和cpu。默认值为 true

option meta_refresh: 设置元数据刷新时长,单位为秒(默认为150,设置0禁用),默认是 150.

option pgsql_connect_string: 仅用于 bandwidthd-pgsql。postgre数据库连接字符串。默认是 “user = postgres dbname = bandwidthd host = 192.168.1.1”.

option sensor_id: 适用于 bandwidthd-pgsqlbandwidthd-sqlite,设置sensor名称,对于 bandwidthd-pgsql,可设置为任何内容,但对于 bandwidthd-sqlite,必须是 “default”bandwidthd-pgsql 的默认值为 “openwrt”

option sqlite_filename: 仅适用 bandwidthd-sqlite,这个是 sqlite 数据库文件。默认是 “/www/bandwidthd/stats.db”.

bandwidthd-php软件包使用另外一个配置文件:/etc/config/bandwidthd-php。这个配置文件bandwidthd-sqlite也可使用(bandwidthd-sqlite 适用2个配置文件: /etc/config/bandwidthd/etc/config/bandwith-php)。

注意: bandwidthd-sqlite 软件包不提供 /etc/config/bandwidthd-php 文件(非必要),由于初始化文件(/etc/init.d/bandwidthd)会为bandwidthd应用提供默认的图形尺寸(900和256)和interval选项(INT_DAILY),以及默认的sqlite数据库: /www/bandwidthd/stats.db。如需修改默认,那么为bandwidthd-sqlite自行建立一个配置文件(/etc/config/bandwidthd-php)即可。

/etc/config/bandwidthd-php 配置如下:

option dflt_width: 宽度。默认 '900'.

option dflt_height: 高度。默认 '256'.

option dflt_interval: 图形展示的默认间隔时长,默认值是 'INT_DAILY'。可选项为: INT_DAILY, INT_WEEKLY, INT_MONTHLY and INT_YEARLY.

option host: 设置postgresql数据库服务器的IP,默认是 '127.0.0.1' 也就是路由器本身。

option user: 设置访问postgresql数据库的用户名,默认是 'postgres'

option dbname: 设置postgresql数据库的名称,默认是 'bandwidthd'

bandwidthd-sqlite建立的 /etc/config/bandwidthd-php 配置文件也是一样的数据结构,但是以下选项不需要 host, user and dbname。替换为: option sqlite_dbname “/www/bandwidthd/stats.db”

默认情况下bandwidthd在 /bandwidthd 目录存放分析数据。任意一个软件包 (bandwidthd, bandwidthd-pgsql and bandwidthd-sqlite) 默认设置均会生成图形文件,如果不需要生成,修改配置文件(/etc/config/bandwidthd)的 option graph 选项为 false 如果路由器IP为192.168.1.1,那么即可访问http://192.168.1.1/bandwidthd

bandwidthd-pgsql 可以将数据库保存到postgresql数据库,然后通过PHP生成图形文件,访问地址为:http://192.168.1.1/phphtdocs/index.php 当然,这里的IP需要修改为web服务器(包含PHP文件)的IP,可以是你的路由器或者其他任何服务器(只要能生成图形文件)。

默认情况下bandwidthd会在重启后丢失之前的数据,如果保存历史数据,那么需要更大的储存空间。通过编辑“/etc/config/bandwidthd”配置文件,设置保存历史数据。

修改3个选项: “option output_cdf true” 和 “option recover_cdf true” 和 “option sqlite_filename 'file.db' ”

config bandwidthd
	option dev		br-lan
	option subnets		"192.168.1.0/24"
	option skip_intervals	0
	option graph_cutoff	1024
	option promiscuous	true
	option output_cdf	true
	option recover_cdf	true
	option filter		ip
	option graph		true
	option meta_refresh	150
        option SQLite_filename  "/path/to/file.db" # 默认的配置可以自动生成数据库文件,通常能正常使用。 如果有外置储存空间,按需设置实际的储存空间目录。

修改完配置文件后,重启服务,然后在外置储存目录内会自动生成下列文件结构:

root@openwrt:~# ls -la /mnt/usb/bandwidthd/
drwxr-xr-x    3 root     root         1024 Aug 25 12:21 .
drwxr-xr-x    6 root     root         1024 Aug 25 00:39 ..
-rw-r--r--    1 root     root          158 Aug 25 00:40 bandwidthd.conf
drwxr-xr-x    2 root     root         2048 Aug 25 00:40 htdocs
-rw-r--r--    1 root     root        50034 Aug 25 12:21 log.1.0.cdf
-rw-r--r--    1 root     root        22204 Aug 25 12:21 log.2.0.cdf
-rw-r--r--    1 root     root         6698 Aug 25 12:10 log.3.0.cdf
-rw-r--r--    1 root     root          529 Aug 25 00:39 log.4.0.cdf

Bandwidthd支持保存数据到外部数据库(仅限bandwidthd-pgsql软件包), bandwidthd 包不支持。这样的一个系统由3部分组成,每个部分都可位于不同的设备。

  1. bandwidthd 就像一个传感器,收集流量信息,保存在路由器内的数据库,或者通过网络保存至外部数据库。这种模式下Bandwidthd占用非常小的内存和CPU。此外,多个路由器的bandwidthd可保存在同一个数据库内。
  2. 数据库系统 目前Bandwidthd仅支持Postgresql数据库系统。请注意OpenWrt适用的postgres-server软件包体积非常大(数据库程序,加上空的数据库文件,体积已经超过15M) ,除非路由器有足够多的空间,否则不建议在路由器部署数据库。
  3. 网页服务器和PHP应用 如需在OpenWrt路由器上部署,bandwidthd-php 软件包包含了所需的文件,“/www/phphtdocs”目录内就是PHP程序,可以把数据库内的数据生成图形文件,这样设计的目的是方便自定义。所有相关的都通过 url 网址传递,只需稍作修改,即可非常容易地生成自定义图表。

使用数据库有很多优点,高效灵活,通过SQL可以很方便的生成报告,而 php+sql 大大提高了报告的交互性。

强烈建议路由器仅仅收集数据,储存数据交由单独的postgres服务器进行。 路由器部署网页服务和PHP也可以生成图形文件,但是对于大部分路由器的性能而言生成图形文件会消耗大量的CPU资源,同时需要大量的储存空间(可能大部分路由器都不具备)。

说明

你需要有一台Postgresql数据库服务器,同时要一台支持php的网页服务器用于生成图形文件。关于在OpenWrt上安装postgresql,见:https://forum.openwrt.org/viewtopic.php?id=11812 ,至于其他系统 (Linux, NetBSD, Unix, etc.) 查看相应系统的帮助文档。

数据库部署 注意!数据库不一定要在路由器,可以是一台单独的设备。如果不在路由器部署数据库,那么所需的文件可访问:http://bandwidthd.sourceforge.net/

  1. 为bandwidthd创建数据库,同时创建可以远程访问数据库的用户(如果远程传输数据)。在OpenWrt内的操作:登录用户postgres (su - postgres),执行命令 “createdb bandwidthd”, 然后通过“exit”命令回到root用户。
  2. Bandwidthd的schema文件是“schema.postgresql”,位于 /usr/share/postgresql (由bandwidthd-pgsql软件包提供)。“psql mydb username < schema.postgresql”

需要通过这个命令加载它并创建2个表和4个索引(2 tables and 4 indexes)。

此外,需要定期执行 bd_pgsql_purge.sh 脚本,建议每周运行一次。此脚本执行聚合数据库中较旧数据点的sql语句,以减少生成年度、每月和每周图表所需的数据量。

以postgres用户运行的示例:

bd_pgsql_purge.sh | psql bandwidthd postgres

脚本将以用户postgres连接到本地数据库,整理数据。

Bandwidthd服务设置 以下是由bandwidthd-pgsql提供的配置文件,位于 /etc/config/bandwidthd

config bandwidthd
        option dev      br-lan
        option subnets          "192.168.1.0/24"
        option skip_intervals   0
        option graph_cutoff     1024
        option promiscuous      true
        option output_cdf       false
        option recover_cdf      false
        option filter           ip
        option graph            true
        option meta_refresh     150
        option pgsql_connect_string    "user = postgres dbname = bandwidthd host = 192.168.1.1"
        option sensor_id       "openwrt"

有的情况默认配置即可工作,但是有时候需要手动修改配置文件,修改以下选项:

option graph false
   #默认是true,这样无论如何都可以通过http://192.168.1.1/bandwidthd 即使pgsql设置有问题。如果你计划仅使用PHP获得图形,那么可以设置这个选项为false。
option pgsql_connect_string    "user = postgres dbname = bandwidthd host = 192.168.1.1"
   #user 修改用户名
   #dbname 修改数据库名称,默认是bandwidthd
   #host 修改数据库服务器IP地址或者域名
 option sensor_id "openwrt"
   #可按需设置sensor名称,这个选项没有严格要求,不重要。

启动bandwidthd服务 (/etc/init.d/bandwidthd start), 几分钟后数据将会出现在数据库内,如果没有,那么检查系统日志 (logread命令) 的错误信息。 (访问 https://wiki.openwrt.org/doc/howto/log.essentials 获取更多关于OpenWRT syslog的信息)

网页服务器设置 注意,网页服务器不一定非要在路由器上,可以是独立于路由器之外的远程服务器。在OpenWrt安装PHP的信息详见 http://wiki.openwrt.org/doc/howto/php 。其他系统 (Linux, NetBSD, Unix, etc.) 可参考对应系统的帮助文档。你也需要配置网页服务器,以支持PHP,如果在OpenWrt上运行此项服务,大约需要占用2M的空间。

  1. 复制PHP文件到网页服务器的制定目录,PHP文件下载地址(项目存档地址) http://bandwidthd.sourceforge.net/, https://github.com/NethServer/bandwidthd ,或者安装 bandwidthd-php 软件包,会将这些文件放置于 /www/phphtdocs 目录。
  2. 编辑配置文件 /etc/config/bandwidthd-php 设置数据库连接相关的信息 ($db_connect_string = “host=192.168.1.1 user=postgres dbname=bandwidthd”). 这些变量 host, user and dbname 用于连接到数据库,必须设置!!
  3. 在OpenWrt这些包会被自动安装 libpcre; libxml2; php7; php7-cgi; php7-mod-pgsql and php7-mod-gd. 至于其他系统,可能会有所区别. 系统支持php5(用于Chaos Calmer,由于php7不支持该版本的OpenWrt)
  4. 从 2021 年底开始,可能会安装 php8 而不是 php7
  5. 对于php5, 一个zoneinfo的软件包需要安装, (ex: zoneinfo-northamericazoneinfo-europe). 这个事php.ini设置时区(date.timezone)的需求。
  6. If you are not using the bandwidthd-php package from OpenWrt (i.e. you got the php files on the web at one of the links above), the file /etc/php.ini should be edited to have the following: short_open_tag = On: the reason is that the php files taken from http://bandwidthd.sourceforge.net/ or https://github.com/NethServer/bandwidthd have the php short tag (<? ?>) instead of the normal tag (<?php ?>). If you do not do it, then you will get a bunch of garbage on the screen. If you installed the bandwidthd-php package on your router, then this is not necessary to set the short_open_tag = at On as the tags in the php files have been corrected to have <?php ?>.
  7. In the /etc/php.ini file a date.timezone = whould be present (ex:date.timezone = “America/Montreal”). Without the date.timezone set to something valid, the graphs will not be drawn.
  8. In the /etc/php.ini file the display_errors should be set to off as many variables are not defined. If you have display_errors = On the web page will most probably not work.
  9. If you are using uhttpd, the following two lines should be added to /etc/config/uhttpd: list interpreter '.php=/usr/bin/php-cgi' and option index_page 'index.php'.

You should now be able to access the web application and see you graphs. All graphing is done by graph.php, all parameters are passed to it in it's url. You can create custom urls to pull custom graphs from your own index pages, or use the canned reporting system.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2022/11/21 11:33
  • by yodo