GNU/Linux >> LINUX-Kenntnisse >  >> Ubuntu

Überwachen Sie Nginx-Protokolldateien mit ngxtop unter Ubuntu 20.04

ngxtop ist ein kostenloses, flexibles Open-Source-Echtzeit-Überwachungstool für Nginx-Webserver. Es kann das Nginx-Zugriffsprotokoll analysieren und die Informationen über die Anzahl der Anfragen, den angeforderten URI, die Anzahl der Anfragen nach Statuscode und vieles mehr drucken. Es ist ein einfaches und benutzerfreundliches Tool zur Überwachung der an einen Nginx-Webserver eingehenden Anfragen.

In diesem Artikel zeige ich Ihnen, wie Sie das ngxtop-Überwachungstool unter Ubuntu 20.04 installieren und verwenden.

Voraussetzungen

  • Ein Server mit Ubuntu 20.04.
  • Auf Ihrem Server ist ein Root-Passwort konfiguriert.

ngxtop installieren

ngxtop ist ein Python-basiertes Tool, daher müssen Sie Python- und PIP-Pakete auf Ihrem System installieren. Sie können sie mit dem folgenden Befehl mit Nginx installieren:

apt-get install nginx python3 python3-pip -y

Nach der Installation können Sie das ngxtop-Paket mit dem PIP wie unten gezeigt installieren:

pip3 install ngxtop

Sobald ngxtop installiert ist, können Sie die Version von ngxtop mit dem folgenden Befehl überprüfen:

ngxtop --version

Sie sollten die folgende Ausgabe erhalten:

xstat 0.1

So verwenden Sie ngxtop

In diesem Abschnitt zeigen wir Ihnen, wie Sie mit ngxtop den Nginx-Webserver überwachen.

Wenn Sie den Befehl ngxtop ohne Argument ausführen, wird die Zusammenfassung der Anzahl der Anfragen, der angeforderte URI und die Anzahl der Anfragen nach Statuscode angezeigt.

ngxtop

Sie sollten den folgenden Bildschirm sehen:

Sie können die Option -l verwenden, um das Zugriffsprotokoll anzugeben, das Sie analysieren möchten.

ngxtop -l /var/log/nginx/access.log

Sie sollten den folgenden Bildschirm sehen:

Führen Sie den folgenden Befehl aus, um die wichtigsten IPs aufzulisten, die auf Ihren Nginx-Server zugreifen:

ngxtop --group-by remote_addr -l /var/log/nginx/access.log

Sie sollten den folgenden Bildschirm sehen:

Mit dem folgenden Befehl können Sie 10 Anfragen mit den höchsten insgesamt gesendeten Bytes drucken:

ngxtop --order-by 'avg(bytes_sent) * count' -l /var/log/nginx/access.log

Sie sollten den folgenden Bildschirm sehen:

Mit ngxtop können Sie auch die Apache-Protokolldatei von einem Remote-Server aus analysieren. Sie können dies mit dem folgenden Befehl tun:

ssh [email protected] tail -f /var/log/apache2/access.log | ngxtop -f common

Führen Sie den folgenden Befehl aus, um eine Liste aller mit ngxtop verfügbaren Optionen zu erhalten:

ngxtop --help

Sie sollten die folgende Ausgabe sehen:

ngxtop - ad-hoc query for nginx access log.

Usage:
    ngxtop [options]
    ngxtop [options] (print|top|avg|sum)  ...
    ngxtop info
    ngxtop [options] query  ...

Options:
    -l , --access-log   access log file to parse.
    -f , --log-format   log format as specify in log_format directive. [default: combined]
    --no-follow  ngxtop default behavior is to ignore current lines in log
                     and only watch for new lines as they are written to the access log.
                     Use this flag to tell ngxtop to process the current content of the access log instead.
    -t , --interval   report interval when running in follow mode [default: 2.0]

    -g , --group-by   group by variable [default: request_path]
    -w , --having   having clause [default: 1]
    -o , --order-by   order of output for default query [default: count]
    -n , --limit   limit the number of records included in report for top command [default: 10]
    -a  ..., --a  ...  add exp (must be aggregation exp: sum, avg, min, max, etc.) into output

    -v, --verbose  more verbose output
    -d, --debug  print every line and parsed record
    -h, --help  print this help message.
    --version  print version information.

    Advanced / experimental options:
    -c , --config   allow ngxtop to parse nginx config file for log format and location.
    -i , --filter   filter in, records satisfied given expression are processed.
    -p , --pre-filter  in-filter expression to check in pre-parsing phase.

Examples:
    All examples read nginx config file for access log location and format.
    If you want to specify the access log file and / or log format, use the -f and -a options.

    "top" like view of nginx requests
    $ ngxtop

    Top 10 requested path with status 404:
    $ ngxtop top request_path --filter 'status == 404'

    Top 10 requests with highest total bytes sent
    $ ngxtop --order-by 'avg(bytes_sent) * count'

    Top 10 remote address, e.g., who's hitting you the most
    $ ngxtop --group-by remote_addr

    Print requests with 4xx or 5xx status, together with status and http referer
    $ ngxtop -i 'status >= 400' print request status http_referer

    Average body bytes sent of 200 responses of requested path begin with 'foo':
    $ ngxtop avg bytes_sent --filter 'status == 200 and request_path.startswith("foo")'

    Analyze apache access log from remote machine using 'common' log format
    $ ssh remote tail -f /var/log/apache2/access.log | ngxtop -f common

Schlussfolgerung

In der obigen Anleitung haben Sie gelernt, wie Sie ngxtop unter Ubuntu 20.04 installieren und verwenden. Ich hoffe, Sie können Ihr Nginx-Protokoll jetzt einfach über die Befehlszeilenschnittstelle überwachen.


Ubuntu
  1. So installieren Sie WordPress mit Nginx unter Ubuntu 18.04

  2. So überwachen Sie Nginx mit Netdata unter Ubuntu 16.04

  3. So überwachen und erkennen Sie modifizierte Dateien mit Tripwire IDS unter Ubuntu 16.04

  4. Installieren Sie LibreNMS auf Ubuntu 20.04 LTS mit Nginx – Schritt-für-Schritt-Anleitung?

  5. Protokolldateien mit Cron-Job entfernen

So installieren Sie Logrotate unter Ubuntu 16.04 LTS

So überwachen Sie die Apache-Leistung mit mod_status in Ubuntu

So überwachen Sie die Ubuntu-Leistung mit Netdata

So installieren Sie WordPress mit Nginx in Ubuntu Linux

Dateien in Ubuntu 22.04 finden

Installieren Sie Odoo 15 mit Docker, Nginx auf Ubuntu 22.04