SHINESUNNY 2015-04-13
vnStat 是一款轻量级的网络流量监控工具,目前各大 Linux/BSD 系统都已内置支持。
vnStat 本身只支持命令行的交互方式,而 Web 前端则由第三方工具支持。这里汇总并对比下 vnStat 的几种 Web 前端并为大家推荐最佳的一款。
vnStat 首页推荐的四个前端有:
vnStat SVG frontend: 基于 CGI / SVG / AJAX 的轻量级 web 前端
仅仅需要一个支持 CGI 的 HTTP 服务器,可以产生非常漂亮的基于 SVG 的图形报告。支持按月/日/时/秒等查看流量信息,还支持 top10 展示。
因为不需要安装额外的 PHP 解析器,所以轻松支持 Apache, Nginx 以及 Busybox 内置的 httpd 服务器。
更重要的是,vnStatSVG 不仅支持普通的 Linux 主机,还可以轻松支持服务器,集群抑或是一个小型的嵌入式系统。
jsvnstat – interactive network traffic analysis
jsvnstat 是另外一款 Web 前端,基于 Javascript 可以实现简单的交互,不过它依赖 PHP 支持,而且不支持集群和嵌入式系统。
vnStat PHP frontend: 一款基于 PHP 的 Web 前端
严重依赖 PHP 和 GD image libraries。同样不支持集群和嵌入式系统。
VnstatSystrayIcon (Windows): 基于 Windows 平台
综合上述比较,不难发现 vnStatSVG 是一款最佳的 vnStat 前端。
关于它的更多特性,可以从其官方主页找到:
总之,vnStatSVG 不仅支持普通的 Linux 主机,服务器,集群,而且支持基于 Busybox 这样的小型嵌入式系统。
vnStatSVG 首页详细介绍了其用法,不过用的是英文,咱们用中文简单介绍一下如何在 Ubuntu 主机上快速安装和使用它。
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install vnstat apache2</span>
<span class="pln">git clonehttps</span><span class="pun">:</span><span class="com">//gitlab.com/tinylab/vnstatsvg.gi</span>
t
假设 Apache 的根目录放在 /var/www/
,可以在 /var/www/
创建一个 vnstatsvg
目录,然后把 Web 前端安装到下面。
<span class="pln">sudo</span><span class="pun">-</span><span class="pln">scd vnstatsvg</span><span class="pun">.</span><span class="pln">gitmkdir</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">vnstatsvg</span><span class="pun">./</span><span class="pln">configure</span><span class="pun">-</span><span class="pln">dvnstatsvgmake</span><span class="pun">&&</span><span class="pln">make install</span>
如果根目录不在 /var/www/
,请用 ./configure w
指定。
默认就可以通过浏览器打开 http://localhost/vnstatsvg/
查看流量信息了。
可以通过编辑 /var/www/vnstatsvg/sidebar.xml
修改各个设备节点的信息,也可以直接复制一个模板过去:
<span class="pln">sudo cp src</span><span class="pun">/</span><span class="pln">admin</span><span class="pun">/</span><span class="pln">sidebar</span><span class="pun">.</span><span class="pln">xml</span><span class="pun">-</span><span class="kwd">template</span><span class="pun">-</span><span class="lit">4</span><span class="pun">-</span><span class="pln">singlehost</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">vnstatsvg</span><span class="pun">/</span><span class="pln">sidebar</span><span class="pun">.</span><span class="pln">xml</span>
配置大体如下:
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">'1.0'</span><span class="pln">encoding</span><span class="pun">=</span><span class="str">'UTF-8'</span><span class="pln">standalone</span><span class="pun">=</span><span class="str">'no'</span><span class="pun">?></span><span class="tag"><sidebar</span><span class="atn">id</span><span class="pun">=</span><span class="atv">"sidebar"</span><span class="tag">></span><br><span class="com"><!--thisconfiguration isforsingle host,the hosts anddump_tool field should be the same--></span><br><span class="tag"><iface></span><br><span class="tag"><name></span><span class="pln">eth0</span><span class="tag"></name></span><br><span class="tag"><host></span><span class="pln">localhost</span><span class="tag"></host></span><br><span class="tag"><description></span><span class="pln">Local Host</span><span class="tag"></description></span><br><span class="tag"></iface></span><br><span class="tag"></sidebar></span>
更多模板请查看 src/admin/sidebar.xml-template*
。
之后,编辑 sidebar.xml
配置各类网络设备节点的信息:
/cgi-bin/vnstat.sh
如果要同时监控多个主机,最简单的方式莫过于在其他机器上用同样方式安装一份 vnstat
和 vnstatsvg
,这样就只需要配置 name
,host
和 description
,其他保持默认。
例如,如果要监控 localhost
和 泰晓科技(域名为 tinylab.org) 的数据,可以添加一份如下配置:
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">'1.0'</span><span class="pln">encoding</span><span class="pun">=</span><span class="str">'UTF-8'</span><span class="pln">standalone</span><span class="pun">=</span><span class="str">'no'</span><span class="pun">?></span><br><span class="tag"><sidebar</span><span class="atn">id</span><span class="pun">=</span><span class="atv">"sidebar"</span><span class="tag">></span><br><span class="com"><!--thisconfiguration isforsingle host,the hosts anddump_tool field should be the same--></span><br><span class="tag"><iface></span><br><span class="tag"><name></span><span class="pln">eth0</span><span class="tag"></name></span><br><span class="tag"><host></span><span class="pln">localhost</span><span class="tag"></host></span><br><span class="tag"><description></span><span class="pln">Local Host</span><span class="tag"></description></iface><iface></span><br><span class="tag"><name></span><span class="pln">eth1</span><span class="tag"></name></span><br><span class="tag"><host></span><span class="pln">tinylab.org</span><span class="tag"></host></span><br><span class="tag"><description></span><span class="pln">TinyLab.org</span><span class="tag"></description></span><br><span class="tag"></iface></span><br><span class="tag"></sidebar></span>
如果不想在其他机器上安装一份额外的 vnstatsvg
,那么可以只安装 vnstat
,但是需要有一种方式从其他主机上把数据拷贝到本地,例如,拷贝到本地的 vnstat
数据目录下 /var/lib/vnstat
。
例如,可以用 ssh 协议(可以通过配置公钥免密登录)。
<span class="com"># collect-data.sh</span><br><span class="pln">hosts</span><span class="pun">=</span><span class="str">"tinylab.org"</span><br><span class="pln">ifaces</span><span class="pun">=</span><span class="str">"eth0 eth1"</span><br><span class="kwd">while</span><span class="pun">:;</span><span class="kwd">do</span><br><span class="kwd">for</span><span class="pln"> h </span><span class="kwd">in</span><span class="pln"> hosts</span><br><span class="kwd">do</span><br><span class="kwd">for</span><span class="pln"> i </span><span class="kwd">in</span><span class="pln"> $ifaces</span><br><span class="kwd">do</span><br><span class="pln">scp $</span><span class="pun">{</span><span class="pln">h</span><span class="pun">}:</span><span class="str">/var/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">vnstat</span><span class="pun">/</span><span class="pln">$</span><span class="pun">{</span><span class="pln">i</span><span class="pun">}</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">vnstat</span><span class="pun">/</span><span class="pln">$</span><span class="pun">{</span><span class="pln">h</span><span class="pun">}-</span><span class="pln">$</span><span class="pun">{</span><span class="pln">i</span><span class="pun">}</span><br><span class="pln">scp $</span><span class="pun">{</span><span class="pln">h</span><span class="pun">}:</span><span class="str">/proc/</span><span class="pln">net</span><span class="pun">/</span><span class="pln">dev </span><span class="pun">></span><span class="str">/var/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">vnstat</span><span class="pun">/</span><span class="pln">$</span><span class="pun">{</span><span class="pln">h</span><span class="pun">}-</span><span class="pln">$</span><span class="pun">{</span><span class="pln">i</span><span class="pun">}-</span><span class="pln">second</span><br><span class="kwd">done</span><br><span class="kwd">done</span><br><span class="pln">sleep </span><span class="lit">5</span><br><span class="kwd">done</span>
可以在后台一直执行该脚本或者启动另外一个 cron
任务来执行该脚本。这样就可以用 file
虚拟协议,如下的 sidebar.xml
就可以实现同样的效果了。
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">'1.0'</span><span class="pln">encoding</span><span class="pun">=</span><span class="str">'UTF-8'</span><span class="pln">standalone</span><span class="pun">=</span><span class="str">'no'</span><span class="pun">?></span>
<span class="tag"><sidebar</span><span class="atn">id</span><span class="pun">=</span><span class="atv">"sidebar"</span><span class="tag">></span>
<span class="com"><!--thisconfiguration isforsingle host,the hosts anddump_tool field should be the same--></span>
<span class="tag"><iface></span>
<span class="tag"><name></span><span class="pln">eth0</span><span class="tag"></name></span>
<span class="tag"><host></span><span class="pln">localhost</span><span class="tag"></host></span>
<span class="tag"><description></span><span class="pln">Local Host</span><span class="tag"></description></span>
<span class="tag"></iface></span>
<span class="tag"><iface></span>
<span class="tag"><name></span><span class="pln">tinylab.org-eth0</span><span class="tag"></name></span>
<span class="tag"><host></span><span class="pln">localhost</span><span class="tag"></host></span>
<span class="tag"><description></span><span class="pln">TinyLab.org:eth0</span><span class="tag"></description></span>
<span class="tag"></iface></span>
<span class="tag"><iface></span>
<span class="tag"><name></span><span class="pln">tinylab.org-eth1</span><span class="tag"></name></span>
<span class="tag"><host></span><span class="pln">localhost</span><span class="tag"></host></span>
<span class="tag"><description></span><span class="pln">TinyLab.org:eth1</span><span class="tag"></description></span>
<span class="tag"></iface></span>
<span class="tag"></sidebar></span>
vnStatSVG 的确是一款非常小巧但是功能强大的 vnStat web 前端,非常推荐!
关于更多用法,比如说嵌入式系统支持,请参考其项目首页。
另外,如果要给 Nginx 添加 CGI 支持,可以参考 Add CGI support for Nginx。