vnStatでネットワークトラフィックを集計する

以下のエントリでipコマンドでネットワーク通信量が確認できました.

端末を起動してからの通信量は確認できますが,再起動したりするとデータ量はクリアされてしまいます.MRTGやZabbixなどを使うのは大げさな感じがします.
もう少しお手軽なものがないかなと少し探してvnStatというものを見つけたので今回試してみました.

Debianでは vnstatvnstati というパッケージが提供されています.vnstat が本体で, vnstati はイメージ出力のコマンドです.

まずは本体の vnstat を導入します.

$ sudo apt install vnstat

導入するとvnStatのデーモンが起動します.

$ sudo service vnstat status
● vnstat.service - vnStat network traffic monitor
     Loaded: loaded (/lib/systemd/system/vnstat.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-04-30 00:46:09 JST; 5min ago
       Docs: man:vnstatd(8)
             man:vnstat(1)
             man:vnstat.conf(5)
   Main PID: 2045411 (vnstatd)
      Tasks: 1 (limit: 18935)
     Memory: 808.0K
        CPU: 184ms
     CGroup: /system.slice/vnstat.service
             └─2045411 /usr/sbin/vnstatd -n

Apr 30 00:46:10 t430s vnstatd[2045411]: Interface "virbr0" added with 1000 Mbit bandwidth limit.
Apr 30 00:46:10 t430s vnstatd[2045411]: Interface "nm-br1" added with 1000 Mbit bandwidth limit.
Apr 30 00:46:10 t430s vnstatd[2045411]: Interface "anbox0" added with 1000 Mbit bandwidth limit.
Apr 30 00:46:10 t430s vnstatd[2045411]: Interface "lxcbr0" added with 1000 Mbit bandwidth limit.
Apr 30 00:46:10 t430s vnstatd[2045411]: Interface "docker0" added with 1000 Mbit bandwidth limit.
Apr 30 00:46:10 t430s vnstatd[2045411]: -> 7 new interfaces found.
Apr 30 00:46:10 t430s vnstatd[2045411]: Limits can be modified using the configuration file. See "man vnstat.conf".
Apr 30 00:46:10 t430s vnstatd[2045411]: Unwanted interfaces can be removed from monitoring with "vnstat --remove".
Apr 30 00:46:10 t430s vnstatd[2045411]: Info: vnStat daemon 2.6 started. (pid:2045411 uid:149 gid:165 64-bit)
Apr 30 00:46:10 t430s vnstatd[2045411]: Info: Monitoring (7): wlp3s0 (1000 Mbit) virbr0 (1000 Mbit) nm-br1 (1000 Mbit) lxcbr0 (1000 Mbit) enp0s2>

はじめはデータがないのでこんな感じの寂しい表示です.

$ vnstat                                                                                                            [169/1082]

                      rx      /      tx      /     total    /   estimated
 anbox0: Not enough data available yet.
 docker0: Not enough data available yet.
 enp0s25: Not enough data available yet.
 lxcbr0: Not enough data available yet.
 nm-br1: Not enough data available yet.
 virbr0: Not enough data available yet.
 wlp3s0: Not enough data available yet.

数分経ってから実行するとデータが記録されてログが出てきます.

$ vnstat

                      rx      /      tx      /     total    /   estimated
 anbox0: Not enough data available yet.
 docker0: Not enough data available yet.
 enp0s25: Not enough data available yet.
 lxcbr0: Not enough data available yet.
 nm-br1: Not enough data available yet.
 virbr0: Not enough data available yet.
 wlp3s0:
       2021-04     21.10 MiB  /    3.70 MiB  /   24.80 MiB  /   22.25 MiB
         today     21.10 MiB  /    3.70 MiB  /   24.80 MiB  /  673.77 MiB

ネットワークインターフェイスが複数あって,特定のインターフェイスの情報だけを確認したい場合は -i オプションでインターフェイスが指定できます.

$ vnstat -i wlp3s0
Database updated: 2021-04-30 00:53:40

   wlp3s0 since 2021-04-30

          rx:  21.10 MiB      tx:  3.70 MiB      total:  24.80 MiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2021-04     21.10 MiB |    3.70 MiB |   24.80 MiB |       82 bit/s
     ------------------------+-------------+-------------+---------------
     estimated     19.78 MiB |    2.47 MiB |   22.25 MiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
         today     21.10 MiB |    3.70 MiB |   24.80 MiB |   64.60 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated    573.38 MiB |  100.39 MiB |  673.77 MiB |

複数のインターフェイスの合計を指定する場合は, + でインターフェイスをつなげて指定します.

$ vnstat -i usb0+wlp3s0

vnStatインストール時に存在しなかったインターフェイスは監視されません.

$ vnstat -i usb0
Error: Interface "usb0" not found in database.

新しいネットワークインターフェイスを追加します.ここでは usb0 を登録しました.

$ sudo vnstat --add -i usb0 (1)
Adding interface "usb0" for monitoring to database...

Restart the vnStat daemon if it is currently running in order to start monitoring "usb0".
$ sudo service vnstat restart   (2)
$ vnstat -i usb0 -d (3)

 usb0  /  daily

          day        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2021-04-30     2.03 MiB |    8.18 MiB |   10.21 MiB |   12.90 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated     26.57 MiB |  107.12 MiB |  133.70 MiB |
  1. usb0 インターフェイスを登録
  2. 反映のためにvNstatを再起動する.
  3. 暫く待ってからカウントされているのを確認.

インターフェイスが切断されているときは disabled の表示になりますが,再接続されるとまたカウントされます.

$ vnstat | grep :
 usb0 [disabled]:
 wlp4s0:

情報の確認はいくつかの表示ができます.サマリー,5分毎,1時間毎,日毎,月毎,年毎,トップなど.
既定値がサマリーのようです.

-5 で5分毎(ここではwlp3s0を5分x6表示)
$ vnstat -i wlp3s0 -5 6

 wlp3s0  /  5 minute

         time        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2021-05-24
         14:35      1.43 MiB |    1.27 MiB |    2.70 MiB |   75.52 kbit/s
         14:40      7.11 MiB |    1.44 MiB |    8.55 MiB |  238.96 kbit/s
         14:45      1.60 MiB |    1.28 MiB |    2.87 MiB |   80.34 kbit/s
         14:50      1.12 MiB |    1.17 MiB |    2.29 MiB |   63.94 kbit/s
         14:55      1.49 MiB |    1.50 MiB |    2.99 MiB |   83.47 kbit/s
         15:00    748.04 KiB |    1.18 MiB |    1.91 MiB |   53.41 kbit/s
     ------------------------+-------------+-------------+---------------
-h で1時間毎(ここでは1時間x3表示)
$ vnstat -h 3

 wlp4s0  /  hourly

         hour        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2021-05-24
         05:00      3.28 MiB |  623.99 KiB |    3.88 MiB |    9.05 kbit/s
         17:00    510.09 MiB |    7.29 MiB |  517.38 MiB |    1.21 Mbit/s
         18:00    286.84 MiB |    8.32 MiB |  295.16 MiB |    1.65 Mbit/s
     ------------------------+-------------+-------------+---------------
-d で日毎(ここでは1日x3表示)
$ vnstat -d 3

 wlp3s0+enp0s25+usb0  /  daily

          day        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2021-05-23   936.47 MiB |  820.25 MiB |    1.72 GiB |  170.56 kbit/s
     2021-05-24     1.56 GiB |  300.01 MiB |    1.85 GiB |  183.94 kbit/s
     2021-05-28   483.40 MiB |  372.19 MiB |  855.60 MiB |   83.07 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated    642.75 MiB |  494.88 MiB |    1.11 GiB |

estimated も表示されます.これはこの調子で使い続けるとこのくらいという推測のようで利用時間や利用量が少なかったり,利用状況が変わる場合信頼できない値になります.

-m で月毎
$ vnstat -m

 wlp3s0+enp0s25+usb0  /  monthly

        month        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2021-04      1.54 GiB |  571.46 MiB |    2.09 GiB |    6.94 kbit/s
       2021-05     19.13 GiB |    4.53 GiB |   23.66 GiB |  303.40 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated     76.48 GiB |   18.12 GiB |   94.60 GiB |
-y で年毎
$ vnstat -y

 wlp4s0  /  yearly

         year        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
          2021     47.48 GiB |    3.16 GiB |   50.64 GiB |   34.07 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated    117.29 GiB |    7.81 GiB |  125.09 GiB |
-t でtop(ここではTop3)
$ vnstat -t 3

 wlp4s0  /  top 3

    #      day          rx      |     tx      |    total    |   avg. rate
   -----------------------------+-------------+-------------+---------------
    1   2021-05-19     7.76 GiB |    1.52 GiB |    9.29 GiB |  923.28 kbit/s
    2   2021-05-27     8.01 GiB |  373.46 MiB |    8.37 GiB |  832.45 kbit/s
    3   2021-05-18     5.66 GiB |  165.91 MiB |    5.83 GiB |  579.24 kbit/s
   -----------------------------+-------------+-------------+---------------
開始( -b ),終了( -e )期間指定(ここでは2021-05-18〜2021-05-25を指定)
$ vnstat -i usb0 -b 2021-05-18 -e 2021-05-25
Database updated: 2021-05-24 05:09:40

   usb0 [disabled] since 2021-05-17

          rx:  5.84 GiB      tx:  1.03 GiB      total:  6.87 GiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2021-05      5.84 GiB |    1.03 GiB |    6.87 GiB |   29.41 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated      7.80 GiB |    1.37 GiB |    9.17 GiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2021-05-23     3.24 GiB |  756.16 MiB |    3.98 GiB |  395.74 kbit/s
     2021-05-24    45.06 MiB |   10.71 MiB |   55.78 MiB |   25.18 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated    210.01 MiB |   49.92 MiB |  259.93 MiB |
-hg, --hoursgraph で1時間ごとのグラフ
$ vnstat -hg
 eth0                                                                     22:15
  ^                                                                     t
  |                                                                     t
  |                                                                     t
  |                                                                     t
  |                                                                     t
  |                                                                     t
  |                                                                     t
  |                                                                     t
  |                                                                    rt  t
  |                                                              rt r  rt rt
 -+--------------------------------------------------------------------------->
  |  23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22

 h  rx (MiB)   tx (MiB)  ][  h  rx (MiB)   tx (MiB)  ][  h  rx (MiB)   tx (MiB)
23        0.0        0.0 ][ 07        0.0        0.0 ][ 15        0.0        0.0
00        0.0        0.0 ][ 08        0.0        0.0 ][ 16        0.0        0.0
01        0.0        0.0 ][ 09        0.0        0.0 ][ 17        0.0        0.0
02        0.0        0.0 ][ 10        0.0        0.0 ][ 18        3.3        9.1
03        0.0        0.0 ][ 11        0.0        0.0 ][ 19       27.0       19.4
04        0.0        0.0 ][ 12        0.0        0.0 ][ 20       25.0       14.6
05        0.0        0.0 ][ 13        0.0        0.0 ][ 21       31.0      150.8
06        0.0        0.0 ][ 14        0.0        0.0 ][ 22       20.3       37.2
-l でリアルタイム(^cで終了,統計が表示される)
$ vnstat -l
Monitoring wlp4s0...    (press CTRL-C to stop)

   rx:     7.39 Mbit/s   632 p/s          tx:   234.20 kbit/s   306 p/s^C


 wlp4s0  /  traffic statistics

                           rx         |       tx
--------------------------------------+------------------
  bytes                   241.31 MiB  |       10.07 MiB
--------------------------------------+------------------
          max            8.33 Mbit/s  |   322.19 kbit/s
      average          322.69 kbit/s  |    13.47 kbit/s
          min                0 bit/s  |         0 bit/s
--------------------------------------+------------------
  packets                     187533  |           92295
--------------------------------------+------------------
          max                716 p/s  |         382 p/s
      average                 29 p/s  |          14 p/s
          min                  0 p/s  |           0 p/s
--------------------------------------+------------------
  time                104.55 minutes

機関の指定もできるようですが,使い方が悪いのか機関によってはうまく使えません.ここではサマリ表示で指定していない期間の2021-05-24も表示されていて, -h 表示でデータが出てこない.

$ vnstat -i usb0 --begin "2021-05-23 13:00" --end "2021-05-23 20:00"
Database updated: 2021-05-24 05:09:40

   usb0 [disabled] since 2021-05-17

          rx:  5.84 GiB      tx:  1.03 GiB      total:  6.87 GiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2021-05      5.84 GiB |    1.03 GiB |    6.87 GiB |   29.41 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated      7.80 GiB |    1.37 GiB |    9.17 GiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2021-05-23     3.24 GiB |  756.16 MiB |    3.98 GiB |  395.74 kbit/s
     2021-05-24    45.06 MiB |   10.71 MiB |   55.78 MiB |   25.18 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated    210.01 MiB |   49.92 MiB |  259.93 MiB |
$ vnstat -i usb0 --begin "2021-05-23 13:00" --end "2021-05-23 20:00" --hours

 usb0 [disabled]  /  hourly

         hour        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
                            no data available
     ------------------------+-------------+-------------+---------------

vnstati も試してみます.
これは vNstat の画像出力コマンドです.Debianではvnstatとは別パッケージの vnstati に分かれているのでこれを導入します.

$ sudo apt install vnstati

範囲などのオプションは vnstat と共通です.

$ vnstati -i usb0 -h 7 -o /tmp/today.png

vnstati

環境1
$ dpkg-query -W vnstat vnstati
vnstat  2.6-3
vnstati 2.6-3
$ lsb_release -dr
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
$ arch
x86_64
環境2
$ dpkg-query -W vnstat vnstati
vnstat  2.6-3
vnstati 2.6-3
$ lsb_release -dr
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
$ arch
x86_64
環境3
$ dpkg-query -W vnstat
vnstat  2.6-1
$ lsb_release -dr
Description:    Ubuntu 20.04.2 LTS
Release:        20.04
$ arch
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)