Telegraf监控Nginx,Grafana展示数据

一、环境

  • 系统版本:CentOS
  • nginx版本:1.19.5
  • Prometheus版本:2.32.1
  • Grafana版本:v8.3.3 (30bb7a93ca)
  • Telegra版本:1.27.4

二、需求背景

我需要对通过nginx代理的Java web服务的所有接口进行监控。这些接口的监控信息包括各种状态码(例如5xx、200、4xx)的接口、接口访问的详细信息以及连接数。最后通过Grafana将这些监控数据展示出来。

三、Telegraf下载->安装->配置

官网:https://portal.influxdata.com/downloads/

image.png

你想将Telegra安装到哪,则在哪个目录下执行以下两句命令。

  1. 下载:wget https://dl.influxdata.com/telegraf/releases/telegraf-1.27.4_linux_amd64.tar.gz
  2. 解压:tar xf telegraf-1.27.4_linux_amd64.tar.gz,解压之后进入./telegraf-1.27.4目录找到bin目录,里面会有一个二进制文件,这个二进制文件就是telegraf程序
  3. 生成telegraf配置文件
    进入telegraf程序所在的目录:cd /opt/telegraf-1.27.4/usr/bin
    通过telegraf程序生成配置文件:sudo ./telegraf -sample-config -input-filter cpu:mem:disk -output-filter prometheus_client > /opt/telegraf-1.27.4/telegraf.conf
    注意这里要确保配置文件所在的文件目录存在。
  4. 修改telegraf.conf配置文件
# 表示周期性地向指定的`http://localhost:8088/nginx_status`路径发起HTTP请求,以获取Nginx服务器的统计信息(例如连接数、请求数、响应时间等)
#该接口 “:8088/nginx_status ” 的配置需要在nginx的server中做相对应的配置
[[inputs.nginx]]
   urls = ["http://localhost:8088/nginx_status"]
   response_timeout = "5s"
[[inputs.tail]]
  name_override = "nginxlog"
# 此处替换为你nginx的access.log日志文件路径
  files = ["access.log日志路径"]
  from_beginning = true
  pipe = false
  data_format = "grok"
  grok_patterns = ["%{COMBINED_LOG_FORMAT}"]
[[inputs.cpu]]
  percpu = true
[[inputs.disk]]
[[inputs.diskio]]
[[inputs.io]]
[[inputs.net]]
[[inputs.mem]]
[[inputs.system]]
#  配置Telegraf的输出插件为Prometheus的格式。使Telegraf输出的数据能够被Promtheus处理
[[outputs.prometheus_client]]
  listen = ":8098" # 通过此端口能获取Telegraf的输出数据
  • [[outputs.influxdb]]:telegraf默认会将监控数据写入至influxdb时序数据库中;如果想要使用Promtheus代替influxdb存储数据,则需要将这一段配置给注释掉。如果不注释telegraf将会一直报错。

5.修改nginx.conf配置文件,记得重启nginx。

  • location /nginx_status:注意:这个接口是一定要配置的,否则telegraf无法获取到nginx服务的信息
  • location /metrics:如果你telegraf的数据输出接口方便暴露出来的话,这个其实可以不用配,Promtheus直接配http://ip:8098/就好了
    server {
        listen       8088;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    # 表示通过/nginx_status 接口可以获取到nginx的服务器数据
    location /nginx_status {
            stub_status on;
            allow 127.0.0.1;  # 修改为允许访问的IP地址或者网段
            deny all;
    }
    # 请求代理:当Promtheus请求访问/metrics接口时,将会被转发至在http://localhost:8098
     location /metrics {
            proxy_pass  http://localhost:8098;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
  1. 指定配置文件启动telegraf :./telegraf -config /etc/telegraf/telegraf.conf
    访问配置好的地址(例如我的地址:http://ip:8088/metrics),如果输出以下监控数据则表示telegraf已经在正常监控了。
image.png

四、Promtheus配置

找到Promtheus的配置文件prometheus.yml 更改配置;之后重启Promtheus即可。

global:
  # 指定了 Prometheus 抓取指标数据的时间间隔为 1 分钟。
  scrape_interval: 1m
  # 指定了 Prometheus 抓取指标数据的时间间隔为 1 分钟。
  evaluation_interval: 1m
scrape_configs:
    # 定义了一个监控任务
  - job_name: nginx
    # 默认访问 /metrics接口,如果你出数据的服务接口不是/metrics,因更改此配置
    # metrics_path: metrics
    # 定义prometheus去哪抓取指标
    static_configs:
      - targets: ['192.168.1.119:8088']

五、Grafana数据展示

模板:https://grafana.com/grafana/dashboards/14900-nginx/

到这一步的话其实很简单了,导入模板后,选择好数据源,数据自然就展示出来了。如果你是刚使用Grafana的话可以找一下其他关于Grafana的使用教程,上手很快的。

1693137087357.jpg
image.png
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容