Ricerca nel sito web

Monitora il traffico di rete con vnStat su Ubuntu 20.04


Questo tutorial esiste per queste versioni del sistema operativo

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)

Su questa pagina

  1. Prerequisiti
  2. Installa vnStat
  3. Gestisci servizio vnStat
  4. Lavorare con vnStat
  5. Conclusione

vnStat è uno strumento di monitoraggio del traffico di rete gratuito, open source e basato su console per il sistema operativo Linux. Con vnStat, puoi monitorare le statistiche di rete su vari periodi di tempo. È semplice, leggero e consuma una piccola parte delle risorse di sistema. vnStat ti consente di generare i dati sul traffico di rete in un'ora, giorno, mese, settimana e giorno.

In questo tutorial, ti mostreremo come installare e utilizzare vnStat per monitorare il traffico di rete.

Prerequisiti

  • Un server che esegue Ubuntu 20.04.
  • Il server è configurato con una password di root.

Installa vnStat

Per impostazione predefinita, l'ultima versione di vnStat non è disponibile nel repository predefinito di Ubuntu. Quindi dovrai compilarlo dalla fonte.

Innanzitutto, installa tutte le dipendenze necessarie per compilare vnStat con il seguente comando:

apt-get install build-essential gcc make libsqlite3-dev -y

Una volta installate tutte le dipendenze, scarica l'ultima versione del sorgente vnStat con il seguente comando:

wget https://humdi.net/vnstat/vnstat-2.6.tar.gz

Una volta completato il download, estrai il file scaricato con il seguente comando:

tar -xvzf vnstat-2.6.tar.gz

Quindi, cambia la directory nella directory estratta e configurala con il seguente comando:

cd vnstat-2.6
./configure --prefix=/usr --sysconfdir=/etc

Successivamente, installa vnStat eseguendo il seguente comando:

make
make install

Una volta installato vnStat, puoi verificare la versione installata di vnStat con il seguente comando:

vnstat -v

Dovresti ottenere il seguente output:

vnStat 2.6 by Teemu Toivola 

Una volta terminato, puoi procedere al passaggio successivo.

Gestisci il servizio vnStat

Successivamente, dovrai copiare il file del servizio systemd vnStat dall'origine vnStat nella directory /etc/systemd/system/:

cp -v vnstat-2.6/examples/systemd/vnstat.service /etc/systemd/system/

Successivamente, abilita il servizio vnStat e avvialo con il seguente comando:

systemctl enable vnstat
systemctl start vnstat

Puoi anche verificare lo stato del servizio vnStat utilizzando il seguente comando:

systemctl status vnstat

Dovresti ottenere il seguente output:

? vnstat.service - vnStat network traffic monitor
   Loaded: loaded (/etc/systemd/system/vnstat.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-11-12 06:24:01 UTC; 11s ago
     Docs: man:vnstatd(8)
           man:vnstat(1)
           man:vnstat.conf(5)
 Main PID: 20989 (vnstatd)
    Tasks: 1 (limit: 4701)
   Memory: 1.5M
   CGroup: /system.slice/vnstat.service
           ??20989 /usr/sbin/vnstatd -n

Nov 12 06:24:01 Ubuntu systemd[1]: Started vnStat network traffic monitor.
Nov 12 06:24:01 Ubuntu vnstatd[20989]: No interfaces found in database, adding available interfaces...
Nov 12 06:24:01 Ubuntu vnstatd[20989]: Interface "ens3" added with 1000 Mbit bandwidth limit.
Nov 12 06:24:01 Ubuntu vnstatd[20989]: Interface "ens4" added with 1000 Mbit bandwidth limit.
Nov 12 06:24:01 Ubuntu vnstatd[20989]: -> 2 new interfaces found.
Nov 12 06:24:01 Ubuntu vnstatd[20989]: Limits can be modified using the configuration file. See "man vnstat.conf".
Nov 12 06:24:01 Ubuntu vnstatd[20989]: Unwanted interfaces can be removed from monitoring with "vnstat --remove".
Nov 12 06:24:01 Ubuntu vnstatd[20989]: Info: vnStat daemon 2.6 started. (pid:20989 uid:0 gid:0 64-bit)
Nov 12 06:24:01 Ubuntu vnstatd[20989]: Info: Monitoring (2): ens4 (1000 Mbit) ens3 (1000 Mbit)

Una volta terminato, puoi procedere al passaggio successivo.

Lavorare con vnStat

vnStat viene fornito con diverse opzioni che ti aiutano a monitorare il traffico di rete. Puoi elencare tutte le opzioni disponibili con il seguente comando:

vnstat --help

Dovresti ottenere il seguente output:

vnStat 2.6 by Teemu Toivola 

      -5,  --fiveminutes [limit]   show 5 minutes
      -h,  --hours [limit]         show hours
      -hg, --hoursgraph            show hours graph
      -d,  --days [limit]          show days
      -m,  --months [limit]        show months
      -y,  --years [limit]         show years
      -t,  --top [limit]           show top days

      -b, --begin            set list begin date
      -e, --end              set list end date

      --oneline [mode]             show simple parsable format
      --json [mode] [limit]        show database in json format
      --xml [mode] [limit]         show database in xml format

      -tr, --traffic [time]        calculate traffic
      -l,  --live [mode]           show transfer rate in real time
      -i,  --iface      select interface

Use "--longhelp" or "man vnstat" for complete list of options.

Prima di utilizzare vnStat, dovrai attendere un po' di tempo per aggiornare il database vnStat. Dopo aver aggiornato il database, puoi eseguire vnStat senza alcuna opzione come mostrato di seguito:

vnstat

Dovresti vedere il seguente output:

                      rx      /      tx      /     total    /   estimated
 ens3:
       2020-11    164.83 KiB  /  495.17 KiB  /  660.01 KiB  /     --     
         today    164.83 KiB  /  495.17 KiB  /  660.01 KiB  /    2.38 MiB

È inoltre possibile specificare l'interfaccia di rete specifica utilizzando l'opzione -i come mostrato di seguito:

vnstat -i ens3

Dovresti ottenere le statistiche giornaliere e mensili nel seguente output:

Database updated: 2020-11-12 06:30:00

   ens3 since 2020-11-12

          rx:  164.83 KiB      tx:  495.17 KiB      total:  660.01 KiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2020-11    164.83 KiB |  495.17 KiB |  660.01 KiB |        5 bit/s
     ------------------------+-------------+-------------+---------------
     estimated       --      |     --      |     --      |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
         today    164.83 KiB |  495.17 KiB |  660.01 KiB |      231 bit/s
     ------------------------+-------------+-------------+---------------
     estimated       608 KiB |    1.79 MiB |    2.38 MiB |

Per visualizzare le statistiche orarie, eseguire il seguente comando:

vnstat -h

Dovresti ottenere il seguente output:

 ens3  /  hourly

         hour        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2020-11-12
         06:00    164.83 KiB |  495.17 KiB |  660.01 KiB |    3.00 kbit/s
     ------------------------+-------------+-------------+---------------

Per visualizzare le statistiche giornaliere, esegui il seguente comando:

vnstat -d

Dovresti ottenere il seguente output:

 ens3  /  daily

          day        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2020-11-12   164.83 KiB |  495.17 KiB |  660.01 KiB |      231 bit/s
     ------------------------+-------------+-------------+---------------
     estimated       608 KiB |    1.79 MiB |    2.38 MiB |

Puoi utilizzare l'opzione -t per visualizzare i giorni di maggior traffico:

vnstat -t

Dovresti ottenere il seguente output:

 ens3  /  top 1

    #      day          rx      |     tx      |    total    |   avg. rate
   -----------------------------+-------------+-------------+---------------
 >  1   2020-11-12   164.83 KiB |  495.17 KiB |  660.01 KiB |       62 bit/s

Per visualizzare le statistiche sul traffico di rete in tempo reale, eseguire il seguente comando:

vnstat -l

Dovresti ottenere il seguente output:

Monitoring ens3...    (press CTRL-C to stop)

   rx:     1.31 kbit/s     2 p/s          tx:     1.49 kbit/s     1 p/s^C


 ens3  /  traffic statistics

                           rx         |       tx
--------------------------------------+------------------
  bytes                     3.75 KiB  |        5.42 KiB
--------------------------------------+------------------
          max            6.55 kbit/s  |    11.17 kbit/s
      average            1.53 kbit/s  |     2.22 kbit/s
          min              264 bit/s  |       264 bit/s
--------------------------------------+------------------
  packets                         41  |              37
--------------------------------------+------------------
          max                  7 p/s  |           8 p/s
      average                  2 p/s  |           1 p/s
          min                  0 p/s  |           0 p/s
--------------------------------------+------------------
  time                    20 seconds

Puoi anche cancellare le voci del database per l'interfaccia ens3 e interrompere il monitoraggio utilizzando il seguente comando:

vnstat -i ens3 --remove --force

Dovresti ottenere il seguente output:

Interface "ens3" removed from database.
The interface will no longer be monitored. Use --add
if monitoring the interface is again needed.

Puoi anche aggiungere nuovamente l'interfaccia rimossa ens3 eseguendo il seguente comando:

vnstat -i ens3 --add

Dovresti ottenere il seguente output:

Adding interface "ens3" for monitoring to database...

Restart the vnStat daemon if it is currently running in order to start monitoring "ens3".

Successivamente, riavvia il servizio vnStat per applicare le modifiche:

systemctl restart vnstat

Conclusione

Nella guida sopra, hai imparato come installare vnStat e usarlo per monitorare il traffico di rete. Spero che questo strumento ti aiuti nell'analisi e nella risoluzione dei problemi relativi alla rete. Non esitate a chiedermi se avete domande.