首页 > 系统相关 >Nginx loki监控日志的学习

Nginx loki监控日志的学习

时间:2023-11-30 13:02:57浏览次数:37  
标签:http upstream request Nginx loki time protocol 日志

Nginx loki监控日志的学习


背景

学习 
https://mp.weixin.qq.com/s/Qt1r7vzWvCcJpNDilWHuxQ
增加了一些自己的理解

第一部分nginx日志的完善

在logformat的后面增加一个:
log_format json_analytics escape=json '{'
                            '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
                            '"connection": "$connection", ' # connection serial number
                            '"connection_requests": "$connection_requests", ' # number of requests made in connection
                    '"pid": "$pid", ' # process pid
                    '"request_id": "$request_id", ' # the unique request id
                    '"request_length": "$request_length", ' # request length (including headers and body)
                    '"remote_addr": "$remote_addr", ' # client IP
                    '"remote_user": "$remote_user", ' # client HTTP username
                    '"remote_port": "$remote_port", ' # client port
                    '"time_local": "$time_local", '
                    '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
                    '"request": "$request", ' # full path no arguments if the request
                    '"request_uri": "$request_uri", ' # full path and arguments if the request
                    '"args": "$args", ' # args
                    '"status": "$status", ' # response status code
                    '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
                    '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
                    '"http_referer": "$http_referer", ' # HTTP referer
                    '"http_user_agent": "$http_user_agent", ' # user agent
                    '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
                    '"http_host": "$http_host", ' # the request Host: header
                    '"server_name": "$server_name", ' # the name of the vhost serving the request
                    '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
                    '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
                    '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
                    '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
                    '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
                    '"upstream_response_length": "$upstream_response_length", ' # upstream response length
                    '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
                    '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
                    '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
                    '"scheme": "$scheme", ' # http or https
                    '"request_method": "$request_method", ' # request method
                    '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
                    '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
                    '"gzip_ratio": "$gzip_ratio", '
                    '"http_cf_ray": "$http_cf_ray"'
                    '}';
然后日志修改为:
access_log /data/nginx/gscloudsu.log json_analytics ;
注意文件日志等属性. 
重启ningx:
lsof -i:5000 |awk '{print $2}' |xargs kill -9
cd /data/nginx
./nginx -c 5000.conf

下载loki等组件

https://github.com/grafana/loki/releases/tag/v2.9.2

最新版本是 2.9.2 我这边下载了四个文件备用
promtail-linux-arm64.zip
promtail-linux-amd64.zip
loki-linux-arm64.zip
loki-linux-amd64.zip

将 x86的截止放到 /usr/bin 目录下面
scp loki-linux-amd64 /usr/bin/loki
scp promtail-linux-amd64 /usr/bin/promtail

解压缩开启服务等

下载loki的配置文件:
地址为: 
https://raw.githubusercontent.com/grafana/loki/main/examples/getting-started/loki-config.yaml
详细内容为: 
mkdir /etc/loki 
cat >/etc/loki/loki.yaml <<EOF
---
server:
  http_listen_port: 3100
#memberlist:
#  join_members:
#    - loki:7946
schema_config:
  configs:
    - from: 2021-08-01
      store: tsdb
      object_store: s3
      schema: v12
      index:
        prefix: index_
        period: 24h
common:
  path_prefix: /loki
  replication_factor: 1
  storage:
    s3:
      endpoint: minio:9000
      insecure: true
      bucketnames: loki-data
      access_key_id: loki
      secret_access_key: supersecret
      s3forcepathstyle: true
  ring:
    kvstore:
      store: memberlist
ruler:
  storage:
    s3:
      bucketnames: loki-ruler
EOF

启动服务:
nohup /usr/bin/loki  -config.file=/etc/loki/loki.yaml -auth.enabled=false &

配置promtail

cat >/etc/loki/promtail.yaml <<EOF
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://127.0.0.1:3100/loki/api/v1/push

scrape_configs:
- job_name: nginx
  static_configs:
  - targets:
      - localhost
    labels:
      job: nginx_logs
      __path__: /data/nginx/gscloudsu.log
EOF

# 启动服务
nohup /usr/bin/promtail  -config.file=/etc/loki/promtail.yaml &

数据源以及grafana的dashboard

增加 loki的数据源
增加 12559 的 2022年版本的json导入文件
注意不能选择新版本的. 新版本换了数据源. 

todo:
1. loki 设置密码
2. nginx 的geoip的设置
3. 学习另外一个springboot的监控指标
https://grafana.com/grafana/dashboards/17175-spring-boot-observability/
https://github.com/blueswen/spring-boot-observability

标签:http,upstream,request,Nginx,loki,time,protocol,日志
From: https://blog.51cto.com/u_11529070/8628255

相关文章

  • clion nginx remote debug 简单说明
    社区有人共享了一个基于clion通过gdbserver进行nginx调整远程调试的实践,很不错值得学习下具体参考https://www.jianshu.com/p/3c2fdb06de9e 但是实际上我们可以直接自己配置基于clion启动remotenginx调整如下(核心是自己添加了一个debug的配置)构建makefile配置这部分,添加......
  • logback丢失日志
    问题描述、现象项目正常运行日志文件生成以及文件名都看不出来问题日志里记录的信息也可以记录完整的每次请求记录偶尔随机丢失一次或者多次完整的请求记录,与没有收到用户请求情况相同 问题排查排除AsyncAppender的丢失机制导致,可以将日志记录方式先改为同步方式或者......
  • Linux学习——日志系统
    目录Linux学习——日志系统10.1日志管理系统10.1.2常见的日志文件10.1.3rsylogd子系统10.2logrotate日志轮转10.3本章小结10.4参考资料Linux学习——日志系统10.1日志管理系统​ ​ ​ ​ ​ ​ ​ rsyslogd进程采集与记录绝大部分与系统相关的日志,包括安全、认......
  • Docker 容器日志查看和清理
    ......
  • mysql慢查询日志
    一、开启并查看慢查询日志1、查看慢查询配置showvariableslike'%query%' 可以看到slow_query_log的值是OFF,也就是mysql默认是不启用慢查询日志的。这里还有个long_query_time,默认是10秒,也就是超过了10秒即为慢查询。log_queries_not_using_indexes,如果设置为ON,则会将所......
  • 日志系统
    1.日志概念日志文件是重要的信息系统文件,其中记录了许多重要的系统事件,包括用户的登录信息、系统的启动信息、系统的安全信息、邮寄相关信息、各种服务相关信息。不仅如此,日志对于安全来说也很重要,它记录了系统每天发生的各种事情,可以通过日志来检查错误发生的原因或者受到攻击时......
  • nginx 报 accept4() failed (24: Too many open files)
    nginx报accept4()failed(24:Toomanyopenfiles)ulimit-aulimit-n204800vim/etc/security/limits.conf<domain><type><item><value>*softnofile204800*hardnofile204......
  • nginx依赖包:pcre、zlib、OpenSSL、gd-devel
    学习自:《nginx经典教程》13页1、问题来源在安装nginx时,有一步需要安装依赖包:sudoyuminstallgcc-c++pcrepcre-develzlibzlib-developensslopenssl-devel这一段是啥意思,这些包有什么用?2、依赖包gcc-c++configure脚本中使用的C编译器为cc。该依赖包为C编译器......
  • SIEM系列|一文读懂 Linux 日志安全分析之文件监控
    摘自:https://zhuanlan.zhihu.com/p/259808863背景介绍在Linux操作系统中,所有内容都是以文件的形式保存和管理的,包括普通文件、目录、网络通信资源等都是文件,即“一切皆文件”。基于这种机制,针对Linux系统层的攻击方式,本质上往往是通过各种方式,对某些敏感文件进行篡改,使入侵......
  • nginx1.24.0安装服务
    下载了一个nginx版本1.24.0安装为服务,然后启动不了。 下面是正确的方法:1、下载WinSW.Net4.exe https://github.com/winsw/winsw/releases下载的是画框的exe,64位2、将 WinSW.NET4.exe 复制到 nginx-1.24.0 目录中,修改文件名称为 nginxservice.exe 。 3、nginx-1.......