Ricerca nel sito web

Come monitorare il traffico di rete con vnStat su Ubuntu 22.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. Per iniziare
  3. Installa vnStat
  4. Usa Systemd per gestire VnStat
  5. Come monitorare il traffico di rete con vnStat
  6. 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. Questo tutorial ti mostrerà come installare e utilizzare vnStat per monitorare il traffico di rete.

Caratteristiche

  • Monitora più interfacce contemporaneamente.
  • Fornisce diverse opzioni di output, come giornaliero, settimanale, ecc.
  • Ordina i dati per ora, giorno, mese o settimana o ottieni i primi 10 giorni.
  • Consente di generare l'output in formato immagine.
  • Monitora più interfacce di rete contemporaneamente.
  • Leggero e basso utilizzo della CPU.

Questo tutorial ti mostrerà come installare e utilizzare lo strumento VnStat per monitorare il traffico di rete su Ubuntu 22.04.

Prerequisiti

  • Un server che esegue Ubuntu 20.04.
  • Sul server è configurata una password di root.

Iniziare

Prima di iniziare, si consiglia di aggiornare e aggiornare tutti i pacchetti di sistema all'ultima versione. Puoi aggiornarli tutti usando il seguente comando:

apt update -y
apt upgrade -y

Una volta che tutti i pacchetti sono stati aggiornati, puoi installare VnStat dall'origine nel passaggio successivo.

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.10.tar.gz

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

tar -xvzf vnstat-2.10.tar.gz

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

cd vnstat-2.10
./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.10 by Teemu Toivola 

Una volta terminato, puoi procedere al passaggio successivo.

Usa Systemd per gestire VnStat

Per gestire VnStat con systemd, dovrai copiare il file del servizio vnStat systemd dall'origine vnStat nella directory /etc/systemd/system/:

cp -v vnstat-2.10/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 controllare lo stato del servizio vnStat usando il seguente comando:

systemctl status vnstat

Dovresti ottenere il seguente output:

? vnstat.service - vnStat network traffic monitor
   Loaded: loaded (/lib/systemd/system/vnstat.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2022-11-29 08:34:08 IST; 2h 49min ago
     Docs: man:vnstatd(1)
           man:vnstat(1)
           man:vnstat.conf(5)
 Main PID: 1730 (vnstatd)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/vnstat.service
           ??1730 /usr/sbin/vnstatd -n

Nov 29 08:34:08 vyompc systemd[1]: Started vnStat network traffic monitor.
Nov 29 08:34:08 vyompc vnstatd[1730]: Info: vnStat daemon 1.18 started. (pid:1730 uid:122 gid:128)
Nov 29 08:34:08 vyompc vnstatd[1730]: Info: Monitoring: enp20s0 (1000 Mbit) wlp22s0 (1000 Mbit)

Una volta terminato, puoi procedere al passaggio successivo.

Come monitorare il traffico di rete con vnStat

vnStat fornisce diverse opzioni che consentono di monitorare il traffico di rete. Puoi elencare tutte le opzioni disponibili con il seguente comando:

vnstat --help

Dovresti ottenere il seguente output:

vnStat 2.10 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
 wlp22s0:
       Oct '22     17.80 GiB  /    4.35 GiB  /   22.15 GiB
       Nov '22     16.61 GiB  /    3.62 GiB  /   20.23 GiB  /   21.48 GiB
     yesterday    332.22 MiB  /  114.17 MiB  /  446.39 MiB
         today    710.25 MiB  /   44.85 MiB  /  755.11 MiB  /    1.55 GiB

Per utilizzare l'interfaccia di rete specifica, utilizzare 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 Saturday 29 January 2022

          rx:  185.89 GiB      tx:  38.82 GiB      total:  224.70 GiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       Oct '22     17.80 GiB |    4.35 GiB |   22.15 GiB |   71.04 kbit/s
       Nov '22     16.61 GiB |    3.62 GiB |   20.23 GiB |   71.20 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated     17.64 GiB |    3.85 GiB |   21.48 GiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     yesterday    332.22 MiB |  114.17 MiB |  446.39 MiB |   43.34 kbit/s
         today    710.25 MiB |   44.85 MiB |  755.11 MiB |  154.31 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated      1.46 GiB |      92 MiB |    1.55 GiB |

Per visualizzare le statistiche orarie, eseguire il seguente comando:

vnstat -h

Dovresti ottenere il seguente output:

 ens3  /  hourly

 h  rx (MiB)   tx (MiB)  ][  h  rx (MiB)   tx (MiB)  ][  h  rx (MiB)   tx (MiB) 
12        5.4        0.2 ][ 20       30.4       14.8 ][ 04        0.0        0.0
13       72.7       15.0 ][ 21       24.8        6.4 ][ 05        0.0        0.0
14        1.9        0.4 ][ 22       20.1        9.8 ][ 06        0.0        0.0
15        0.0        0.0 ][ 23        0.0        0.0 ][ 07        0.0        0.0
16        0.0        0.0 ][ 00        0.0        0.0 ][ 08      147.0       10.2
17       36.4        9.6 ][ 01        0.0        0.0 ][ 09       64.1        9.4
18       54.0       23.4 ][ 02        0.0        0.0 ][ 10      496.9       23.9
19       86.5       34.5 ][ 03        0.0        0.0 ][ 11        2.3        1.3

Per visualizzare le statistiche giornaliere, esegui il seguente comando:

vnstat -d

Dovresti ottenere il seguente output:

 ens3  /  daily
        day         rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     Sunday 30 October 2022  266.75 MiB |   47.81 MiB |  314.56 MiB |   30.54 kbit/s
     Monday 31 October 2022  701.49 MiB |  225.89 MiB |  927.38 MiB |   90.04 kbit/s
     Tuesday 01 November 2022  506.20 MiB |  184.50 MiB |  690.70 MiB |   67.06 kbit/s
     Wednesday 02 November 2022  631.15 MiB |  182.43 MiB |  813.59 MiB |   78.99 kbit/s
     Thursday 03 November 2022  189.21 MiB |   41.18 MiB |  230.39 MiB |   22.37 kbit/s
     Friday 04 November 2022  703.67 MiB |  175.49 MiB |  879.16 MiB |   85.36 kbit/s
     Saturday 05 November 2022  518.96 MiB |  130.47 MiB |  649.43 MiB |   63.05 kbit/s
     Sunday 06 November 2022  301.74 MiB |   81.95 MiB |  383.69 MiB |   37.25 kbit/s

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   Thursday 03 February 2022    2.58 GiB |  214.64 MiB |    2.79 GiB |  277.76 kbit/s
    2   Saturday 05 February 2022    2.11 GiB |  229.32 MiB |    2.33 GiB |  231.67 kbit/s
    3   Tuesday 01 February 2022    1.81 GiB |  190.56 MiB |    2.00 GiB |  198.71 kbit/s
    4   Wednesday 02 February 2022    1.80 GiB |  179.75 MiB |    1.98 GiB |  196.72 kbit/s
    5   Sunday 10 July 2022    1.69 GiB |  248.34 MiB |    1.93 GiB |  192.21 kbit/s
    6   Saturday 19 February 2022    1.56 GiB |  172.67 MiB |    1.73 GiB |  171.56 kbit/s
    7   Friday 16 September 2022    1.44 GiB |  201.21 MiB |    1.64 GiB |  163.16 kbit/s
    8   Friday 17 June 2022    1.34 GiB |  278.89 MiB |    1.61 GiB |  160.33 kbit/s
    9   Friday 25 March 2022    1.06 GiB |  504.19 MiB |    1.55 GiB |  154.51 kbit/s
   10   Monday 07 February 2022    1.30 GiB |  160.68 MiB |    1.46 GiB |  145.30 kbit/s
   -----------------------------+-------------+-------------+---------------

Per visualizzare le statistiche del traffico di rete in tempo reale, esegui il seguente comando:

vnstat -l

Dovresti ottenere il seguente output:

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

   rx:       11 kbit/s     7 p/s          tx:        9 kbit/s     6 p/s^C


 ens3  /  traffic statistics

                           rx         |       tx
--------------------------------------+------------------
  bytes                       17 KiB  |          18 KiB
--------------------------------------+------------------
          max              24 kbit/s  |       33 kbit/s
      average            6.78 kbit/s  |     7.39 kbit/s
          min               0 kbit/s  |        0 kbit/s
--------------------------------------+------------------
  packets                         78  |              82
--------------------------------------+------------------
          max                 10 p/s  |          11 p/s
      average                  3 p/s  |           4 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

In questo post, abbiamo spiegato come installare vnStat su Ubuntu 22.04. Ti mostriamo anche come usarlo per monitorare il traffico di rete con diversi esempi. Ora puoi utilizzare questo strumento per analizzare e risolvere i problemi relativi alla rete. Non esitate a chiedermi se avete domande.