Hướng dẫn cài đặt Telegraf để lấy traffic từ server Nginx

Để cấu hình Telegraf, Quý khách cần thực hiện các bước sau:

I. Cài đặt và cấu hình trạng thái trên Nginx
  1. Truy cập vào server NGINX.
  2. Tạo một file cấu hình nginx /etc/nginx/conf.d/stub_status_nginx.conf với nội dung như sau:
    server {
      listen 81 default_server;
      root /var/www/html;
      index index.html index.htm index.nginx-debian.html;
      server_name _;
      location / {
        try_files $uri $uri/ =404;
      }
      location /nginx_status {
        stub_status;
        allow 127.0.0.1;
        deny all;
      }
    }
  3. Kiểm tra lại cấu hình Nginx bằng lệnh:
    # nginx -t
  4. Load lại nginx để áp dụng cấu hình
    # systemctl reload nginx
  5. Kiểm tra lại hoạt động của url metric bằng lệnh
    # curl http://127.0.0.1:81/nginx_status
II. Cài đặt Telegraf:

Có thể tham khảo tại https://portal.influxdata.com/downloads/

  1. Thêm source list với lệnh:
    # echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main' | sudo tee /etc/apt/sources.list.d/influxdata.list
  2. Thực hiện import key:
    # wget -q https://repos.influxdata.com/influxdata-archive_compat.key
    # echo '393e8779c89ac8d958f81f942f9ad7fb82a25e133faddaf92e15b16e6ac9ce4c influxdata-archive_compat.key' | sha256sum -c && cat influxdata-archive_compat.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null
  3. Thực hiên cài đặt với lệnh sau:
    sudo apt-get update && sudo apt-get install telegraf
  4. Cấu hình lại telegraf ở /etc/telegraf/telegraf.conf với nội dung như sau
    #nginx-metrics and logs
    [[inputs.nginx]]
        urls = ["http://localhost:81/nginx_status"]
        response_timeout = "5s"
    
    [[inputs.tail]]
        name_override = "nginxlog"
        files = ["/var/log/nginx/access.log"]
        from_beginning = true
        pipe = false
        data_format = "grok"
        grok_patterns = ["%{COMBINED_LOG_FORMAT}"]
    
    [[outputs.prometheus_client]]
        listen = "0.0.0.0:9125"

    Trong đó, /var/log/nginx/access.log là file log của website cần theo dõi traffic.

  5. Để đảm bảo telegraf có thể đọc được file log, file log cần được phân quyền với lệnh sau:
    # chmod 755 /var/log/nginx/error.log
  6. Enable và start telegraf với lệnh:
    # systemctl restart telegraf.service
    # systemctl enable telegraf.service
  7. Cấu hình firewall chỉ cho phép IP của server monitor được phép truy cập vào port 9125.
    Đến bước này, Quý khách có thể cấu hình lấy dữ liệu telegraf từ URL https://[your-ip]:9125

Như vậy, Long Vân đã hoàn thành hướng dẫn Quý khách cách cài đặt telegraf để lấy traffic để có thể monitor server Nginx. Chúc Quý khách thành công!