新建监控项

监控mysql

首先确认本机是否安装mysql:

解压缩mysql_exporter安装包:

tar -xf mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /data
cd /data
mv mysqld_exporter-0.13.0.linux-amd64 mysqld_exporter
  • 1.
  • 2.
  • 3.

授权属主属组:

chown -R prometheus:prometheus /data/mysqld_exporter/
chmod +x /data/mysqld_exporter/mysqld_exporter
  • 1.
  • 2.

修改mysql_exporter配置文件:

vim mysqld_exporter/my.cnf 
[client]
user=xxxx		#监控数据库的用户
password=xxxx	#用户密码
host=127.0.0.1	#本机ip
port=3306		#数据库端口号
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

注册为服务:

vim /usr/lib/systemd/system/mysqld_exporter.service 

[Unit]
Description=mysqld_exporter
After=network.target

[Service]
Restart=on-failure
ExecStart=/data/mysqld_exporter/mysqld_exporter --config.my-cnf=/data/mysqld_exporter/my.cnf

[Install]
WantedBy=multi-user.target


#重新加载配置
systemctl daemon-reload
#启动服务
systemctl start mysqld_exporter
#查看服务状态
systemctl status mysqld_exporter
#设置为开机自启
systemctl enable mysqld_exporter
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

prometheus配置文件添加mysqld_exporter配置:

...................................
...................................
...................................
  - job_name: 'mysqld_exporter'		
	static_configs:
    - targets: ['127.0.0.1:9104']
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

检查prometheus配置文件语法:

[root@localhost prometheus]# ./promtool check config prometheus.yml 
Checking prometheus.yml
  SUCCESS: 1 rule files found
 SUCCESS: prometheus.yml is valid prometheus config file syntax
  • 1.
  • 2.
  • 3.
  • 4.

使用热加载重新加载prometheus:

[root@localhost prometheus]# curl -XPOST http://127.0.0.1:9090/-/reload
[root@localhost prometheus]#
  • 1.
  • 2.

监控ping

black_exporter下载地址:  https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz

解压缩

tar -xf blackbox_exporter-0.25.0.linux-amd64.tar.gz -C /data
cd /data
mv blackbox_exporter-0.25.0.linux-amd64/ blackbox_exporter
  • 1.
  • 2.
  • 3.

修改目录属主属组

chown -R prometheus:prometheus /data/blackbox_exporter/
  • 1.

修改blaxkbox_exporter配置文件

vim blackbox.yml

...................
...................
...................
...................
  icmp:
    prober: icmp
  icmp_ttl5:
    prober: icmp
    timeout: 5s
    icmp:
      ttl: 5
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

注册为服务

vim /usr/lib/systemd/system/blackbox_exporter.service 

[Unit]
Description=blackbox_exporter
After=network.target

[Service]
Type=simple
User=prometheus
Group=prometheus
AmbientCapabilities=CAP_NET_RAW
ExecStart=/data/blackbox_exporter/blackbox_exporter \
--config.file=/data/blackbox_exporter/blackbox.yml \
--web.listen-address=:9115 \
--no-config.check \
--history.limit=100 \
--log.format=logfmt \
--log.level=info
Restart=on-failure

[Install]
WantedBy=multi-user.target


#重新加载配置
systemctl daemon-reload
#启动服务
systemctl start blackbox_exporter
#查看服务状态
systemctl status blackbox_exporter
#设置为开机自启
systemctl enable blackbox_exporter
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

写入prometheus配置文件

vim /data/prometheus/prometheus.yml
..........................................
..........................................
..........................................
..........................................
  - job_name: 'ping_status'
    metrics_path: /probe
    params:
      module: [icmp]
    static_configs:
      - targets:
        - '127.0.0.1'
        labels:
          instance: '本机'
      - targets:
        - '192.168.223.138'
        labels:
          instance: '192.168.223.138'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115


#检查prometheus配置文件语法
[root@localhost prometheus]# ./promtool check config prometheus.yml 
Checking prometheus.yml
 SUCCESS: prometheus.yml is valid prometheus config file syntax

#通过热加载重新加载prometheus页面
curl -XPOST http://127.0.0.1:9090/-/reload
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
blackbox_exporter.yml详解

http_2xx 、http_post_2xx

http_2xx:
    prober: http	#  指定使用HTTP探测器
    http:
      preferred_ip_protocol: "ip4"	# 使用IPv4协议进行探测
      
  http_post_2xx:
    prober: http	# 使用HTTP探测器
    http:
      method: POST		# 设置HTTP请求方法为POST



http_2xx
prober: http
  指定该模块使用的探测器类型为http。这意味着该模块将执行HTTP探测。
http
  该部分包含HTTP探测器的具体配置。
preferred_ip_protocol
  指定了在进行网络请求时优先使用的IP协议版本。在这个例子中,设置为了"ip4",意味着探测器将优先使用IPv4地址进行探测。如果服务器同时支持IPv4和IPv6,那么设置这个参数可以确保使用特定的IP版本来进行测试。

http_post_2xx
prober: http
  指定该模块使用的探测器类型为http。这意味着该模块将执行HTTP探测。
http
  该部分包含HTTP探测器的具体配置。
method: POST
  设置HTTP请求的方法为POST。默认情况下,HTTP探测器可能使用GET方法,但在这个模块中,明确指定使用POST方法。可以用于测试那些需要POST请求才能正常工作的HTTP接口。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

tcp_connect、 pop3s_banner

tcp_connect:
    prober: tcp		# 使用TCP连接探测器,仅检查TCP连接是否可以建立
    
  pop3s_banner:
    prober: tcp		# 使用TCP探测器
    tcp:
      query_response:	# 定义查询和响应的期望序列
      - expect: "^+OK"	# 期望收到的响应以+OK开头
      tls: true	# 启用TLS加密
      tls_config:
        insecure_skip_verify: false	# 不跳过TLS证书的验证


tcp_connect
prober: tcp
  指定该模块使用的探测器类型为tcp。将尝试建立TCP连接到目标地址和端口,以检查连接是否可以成功建立。这个模块不发送或接收任何特定的数据,只是简单地测试TCP连接性。

pop3s_banner
tcp
  该部分包含TCP探测器的具体配置。
query_response
  定义一个查询和响应的期望序列,用于与目标服务器进行交互。
expect: “^+OK”
  指定探测器期望从服务器接收到的响应应该以+OK开头。通常用于POP3协议中,服务器在成功连接后会发送一个以+OK开头的欢迎消息。
tls: true
  探测器在建立TCP连接后启动TLS加密。
insecure_skip_verify: false
  是否在TLS握手过程中验证服务器的SSL证书。设置为false意味着探测器将验证服务器的证书是否有效和受信任。如果设置为true,则会跳过证书验证。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

grpc、grpc_plain

grpc:
    prober: grpc	# 使用gRPC探测器
    grpc:
      tls: true	# 启用TLS加密
      preferred_ip_protocol: "ip4"	# 使用IPv4协议
      
  grpc_plain:
    prober: grpc	# 使用gRPC探测器
    grpc:
      tls: false	# 不使用TLS加密
      service: "service1"	# 要探测的gRPC服务名称


grpc
prober: grpc
  指定该模块使用的探测器类型为grpc。意味该模块将尝试通过gRPC协议与目标服务器进行通信。
grpc
  包含gRPC探测器的具体配置。
tls: true
  指示探测器在建立gRPC连接时使用TLS加密。
preferred_ip_protocol: “ip4”
  指定探测器在尝试连接目标服务器时优先使用的IP协议版本。在这个例子中,它被设置为"ip4",表示探测器将优先使用IPv4地址进行连接。对于那些仅支持或偏好IPv4的网络环境特别有用。

grpc_plain
prober: grpc
  指定该模块使用的探测器类型为grpc。意味该模块将尝试通过gRPC协议与目标服务器进行通信。
grpc
  包含gRPC探测器的具体配置。
tls: false
  与grpc模块相反,这个参数指示探测器在建立gRPC连接时不使用TLS加密。
service: “service1”
  指定探测器要连接的gRPC服务的名称。在gRPC中,服务名称通常用于标识和路由到正确的服务处理程序。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

ssh_banner、irc_banner

ssh_banner:
    prober: tcp	# 使用TCP探测器
    tcp:
      query_response:
      - expect: "^SSH-2.0-"	# 期望收到的响应以SSH-2.0-开头
      - send: "SSH-2.0-blackbox-ssh-check"	# 发送特定的SSH客户端标识
      - 
  irc_banner:
    prober: tcp	# 使用TCP探测器
    tcp:
      query_response:	# 定义了与IRC服务器的交互序列,包括发送NICK、USER等命令,并期望特定的响应
      - send: "NICK prober"
      - send: "USER prober prober prober :prober"
      - expect: "PING :([^ ]+)"
        send: "PONG ${1}"
      - expect: "^:[^ ]+ 001"


ssh_banner
prober: tcp
  指定使用TCP探测器。
tcp
  TCP探测器的配置部分。
query_response
  定义了一个查询和响应的序列。
expect: “^SSH-2.0-”
  指定探测器期望从服务器接收到的响应应该以SSH-2.0-开头。这是SSH服务器在接收到客户端连接时通常会发送的版本标识。
send: “SSH-2.0-blackbox-ssh-check”
  指定探测器向服务器发送的SSH客户端版本标识。通常用于模拟SSH客户端与服务器进行初始的版本协商。

irc_banner
prober: tcp
  指定使用TCP探测器。
tcp
  TCP探测器的配置部分。
query_response
  定义了一个查询和响应的序列。
send: “NICK prober”
   向IRC服务器发送设置昵称(NICK)的命令,昵称为“prober”。
send: “USER prober prober prober :prober”
  向IRC服务器发送USER命令,用于设置用户信息。这里所有的参数都设置为“prober”。
expect: “PING : ([^ ]+)”
  定义一个正则表达式,探测器会等待服务器发送一个PING消息,并捕获PING后面的参数值。
send: “PONG ${1}”
  当收到PING消息后,探测器会使用之前捕获的参数值(${1})来回应一个PONG消息。这是IRC协议中保持连接活跃的一种机制。
expect: “^:[^ ]+ 001”
  探测器期望从服务器接收到一个以:开头,后面跟着若干非空格字符,再接着是 001的响应。在IRC协议中,001是一个欢迎消息,表示客户端已成功连接到服务器。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.

icmp、icmp_ttl5

icmp:
    prober: icmp	# 使用ICMP探测器,通常用于检测网络连通性(ping)
    
  icmp_ttl5:
    prober: icmp	# 使用ICMP探测器
    timeout: 5s	# 设置探测超时时间为5秒
    icmp:
      ttl: 5	# 设置ICMP包的TTL(生存时间)为5,这可以用于追踪网络路径或检测特定网络跳数的问题


icmp
prober: icmp
  指定该模块使用的探测器类型为icmp。这意味着该模块将发送ICMP Echo请求(ping请求)到目标地址,以检查网络的连通性。

icmp_ttl5
prober: icmp
  同样指定了使用的探测器类型为icmp。
timeout: 5s
  设置ICMP探测的超时时间为5秒。如果在5秒内没有收到ICMP Echo响应(pong响应),则探测器会认为目标不可达。
ttl: 5
  设置ICMP数据包的Time To Live (TTL)值为5。TTL是IP数据包在网络中传输时可以经过的最大路由器数量。每当数据包经过一个路由器时,TTL值就会减1。当TTL值减至0时,数据包将被丢弃,并且发送方会收到一个ICMP超时消息。通过设置较低的TTL值(如5),可以限制数据包的传输范围,这在某些网络诊断场景中可能很有用。例如,如果你想确认数据包是否能够到达离你较近的网络节点,但不想让它们传播得太远,就可以通过设置较低的TTL值来实现。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

配置ping报警规则

vim /data/prometheus/rules/ping_fail.yml

groups:
- name: ping失败
  rules:
  - alert: ping失败
    expr: (sum_over_time(probe_success{job="ping_status"}[10m]) / count_over_time(probe_success{job="ping_status"}[10m])) < 0.99
    for: 10m
    labels:
      severity: critical
    annotations:
      summary: "Ping{{ $labels.instance }}失败"
      description: "ping{{ $labels.instance }}十分钟内成功率低于99%."


vim /data/prometheus/rules/ping_timeout.yml

groups:
- name: Ping超时
  rules:
  - alert: Ping超时
    expr: probe_duration_seconds > 0.25
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: Ping {{ $labels.instance }} 超时
      description: "ping {{ $labels.instance }} 超时250毫秒."
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

监控nginx

nginx-vts-exporter下载地址: https://github.com/sysulq/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz

首先确认本机是否安装nginx

修改nginx配置文件:

vim /etc/nginx/nginx.conf
..........................
..........................
..........................
..........................

    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;


    server {
        listen       80;
        server_name  localhost;
        
        location /status {
           vhost_traffic_status_display;
           vhost_traffic_status_display_format html;
        }
 
    }


#检查nginx语法
[root@localhost data]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

#重新加载nginx配置
[root@localhost data]# nginx -s reload
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

解压nginx-vts-exporter包:

tar -xf nginx-vtx-exporter_0.10.8_linux_amd64.tar.gz

#移动到启动目录
mv /data/nginx_exporter/nginx-vts-exporter /usr/local/bin/
#赋权
chmod +x /usr/local/bin/nginx-vts-exporter
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

注册为服务:

vim /usr/lib/systemd/system/nginx_exporter.service
 
 [Unit]
Description=nginx_vts_exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/nginx-vts-exporter -nginx.scrape_uri http://127.0.0.1/status/format/json
Restart=on-failure

[Install]
WantedBy=multi-user.target


#重新加载配置
systemctl daemon-reload
#启动服务
systemctl start nginx_exporter
#查看服务状态
systemctl status nginx_exporter
#设置为开机自启
systemctl enable nginx_exporter
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

prometheus配置文件写入配置:

vim prometheus.yml

.......................................
.......................................
.......................................
.......................................
.......................................
  - job_name: 'nginx'
    static_configs:
    - targets: ['127.0.0.1:9913']
      labels:
        instance: nginx



#检查prometheus配置文件语法
[root@localhost prometheus]# ./promtool check config prometheus.yml 
Checking prometheus.yml
 SUCCESS: prometheus.yml is valid prometheus config file syntax

#通过热加载重新加载prometheus页面
curl -XPOST http://127.0.0.1:9090/-/reload
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

测试本机访问:

[root@localhost prometheus]# curl -i http://127.0.0.1/status/format/json
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Wed, 14 Aug 2024 09:05:15 GMT
Content-Type: application/json
Content-Length: 4442
Connection: keep-alive

{"hostName":"localhost.localdomain","moduleVersion":"v0.2.2","nginxVersion":"1.25.4","loadMsec":1723454542050,"nowMsec":1723626315715,"connections":{"active":2,"reading":0,"writing":1,"waiting":1,"accepted":36,"handled":36,"requests":6295},"sharedZones":{"name":"ngx_http_vhost_traffic_status","maxSize":1048575,"usedSize":7054,"usedNode":2},"serverZones":{"192.168.223.140":{"requestCounter":1257,"inBytes":530702,"outBytes":5712069,"responses":{"1xx":0,"2xx":1256,"3xx":0,"4xx":1,"5xx":0,"miss":0,"bypass":0,"expired":0,"stale":0,"updating":0,"revalidated":0,"hit":0,"scarce":0},"requestMsecCounter":0,"requestMsec":0,"requestMsecs":{"times":[1723469055742,1723469115735,1723469175727,1723469235731,1723469295731,1723469355735,1723469415731,1723469475730,1723469535740,1723469595727,1723469655737,1723469715728,1723469775727,1723469835739,1723469895741,1723469955738,1723470015729,1723470075725,1723470135740,1723470195729,1723470255728,1723470315729,1723470364677,1723470426827,1723470486838,1723470546828,1723470606835,1723470666826,1723470726828,1723470786835,1723470846824,1723470906831,1723470966836,1723471026833,1723471086836,1723471146835,1723471206825,1723471266825,1723471326831,1723471386835,1723471446832,1723471506838,1723471566836,1723471626838,1723471686825,1723471746827,1723471806825,1723471866829,1723471926838,1723471986831,1723472046834,1723472106835,1723472166834,1723512499561,1723512559547,1723512619549,1723512679554,1723512739548,1723512799553,1723512859553,1723512919550,1723512979564,1723513039553],"msecs":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},"requestBuckets":{"msecs":[],"counters":[]},"overCounts":{"maxIntegerSize":18446744073709551615,"requestCounter":0,"inBytes":0,"outBytes":0,"1xx":0,"2xx":0,"3xx":0,"4xx":0,"5xx":0,"miss":0,"bypass":0,"expired":0,"stale":0,"updating":0,"revalidated":0,"hit":0,"scarce":0,"requestMsecCounter":0}},"127.0.0.1":{"requestCounter":5037,"inBytes":543979,"outBytes":23125984,"responses":{"1xx":0,"2xx":5037,"3xx":0,"4xx":0,"5xx":0,"miss":0,"bypass":0,"expired":0,"stale":0,"updating":0,"revalidated":0,"hit":0,"scarce":0},"requestMsecCounter":0,"requestMsec":0,"requestMsecs":{"times":[1723625376762,1723625391761,1723625406762,1723625421764,1723625436761,1723625451764,1723625466763,1723625481767,1723625496762,1723625511762,1723625526762,1723625541761,1723625556763,1723625571762,1723625586763,1723625601762,1723625616761,1723625631763,1723625646762,1723625661761,1723625676762,1723625691762,1723625706762,1723625721762,1723625736762,1723625751761,1723625766762,1723625781761,1723625796762,1723625811762,1723625826761,1723625841762,1723625856761,1723625871761,1723625886762,1723625901761,1723625916764,1723625931763,1723625946764,1723625961761,1723625976761,1723625991761,1723626006761,1723626021762,1723626036761,1723626051761,1723626066762,1723626081762,1723626096762,1723626111763,1723626126762,1723626141762,1723626156761,1723626171762,1723626186762,1723626201761,1723626216762,1723626231764,1723626246761,1723626261761,1723626276762,1723626291761,1723626306761],"msecs":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},"requestBuckets":{"msecs":[],"counters":[]},"overCounts":{"maxIntegerSize":18446744073709551615,"requestCounter":0,"inBytes":0,"outBytes":0,"1xx":0,"2xx":0,"3xx":0,"4xx":0,"5xx":0,"miss":0,"bypass":0,"expired":0,"stale":0,"updating":0,"revalidated":0,"hit":0,"scarce":0,"requestMsecCounter":0}},"*":{"requestCounter":6294,"inBytes":1074681,"outBytes":28838053,"responses":{"1xx":0,"2xx":6293,"3xx":0,"4xx":1,"5xx":0,"miss":0,"bypass":0,"expired":0,"stale":0,"updating":0,"revalidated":0,"hit":0,"scarce":0},"requestMsecCounter":0,"requestMsec":0,"requestMsecs":{"times":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1723626261761,1723626276762,1723626291761,1723626306761],"msecs":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},"requestBuckets":{"msecs":[],"counters":[]},"overCounts":{"maxIntegerSize":18446744073709551615,"requestCounter":0,"inBytes":0,"outBytes":0,"1xx":0,"2xx":0,"3xx":0,"4xx":0,"5xx":0,"miss":0,"bypass":0,"expired":0,"stale":0,"updating":0,"revalidated":0,"hit":0,"scarce":0,"requestMsecCounter":0}}}}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

浏览器访问:


访问prometheus页面


监控php

监控php首先确认需监控服务器已安装php:

[root@localhost data]# php -v
PHP 8.0.30 (cli) (built: Jun  4 2024 15:19:49) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.30, Copyright (c), by Zend Technologies
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

下载php_exporter:

curl -LO https://github.com/bakins/php-exporter/releases/download/v0.6.0/php-exporter_v0.6.0_linux_amd64
  • 1.

重命名文件:

mv php-fpm-exporter.linux.amd64  php_exporter
  • 1.

赋予执行权限:

chmod +x php_exporter
  • 1.

配置php-fpm,暴露php-fpm状态信息,

在 php 的安装目录下的 www.conf 中打开 pm.status_path和ping.path配置项:

vim /etc/php-fpm.d/www.conf
#去掉分号
;pm.status_path = /status
pm.status_path = /status
;ping.path = /ping
ping.path = /ping

#重启php-fpm
systemctl restart php-fpm
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

注册为开机自启服务:

vim /usr/lib/systemd/system/php_exporter.service 

[Unit]
Description=OpenSSH server daemon
After=network.target sshd-keygen.service

[Service]
Type=simple
Restart=always
ExecStart=/data/php_exporter --addr 0.0.0.0:9190 --endpoint http://127.0.0.1:12010/status

[Install]
WantedBy=multi-user.target


#重新加载配置
systemctl daemon-reload
#启动服务
systemctl start php_exporter
#查看服务状态
systemctl status php_exporter
#设置为开机自启
systemctl enable php_exporter
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

nginx新建配置:

server {
        listen 12010;
        server_name 127.0.0.1;
        allow 127.0.0.1;
location ~ ^/status$ {
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;#调用php-fpm后台端口
        }
}


#检查nginx配置语法
[root@localhost data]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重新加载nginx配置
[root@localhost data]# nginx -s reload
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

本地测试网页:

[root@localhost data]# curl -i 127.0.0.1:12010/status
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Tue, 13 Aug 2024 16:42:39 GMT
Content-Type: text/plain;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/8.0.30
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate, max-age=0

pool:                 www
process manager:      dynamic
start time:           13/Aug/2024:23:15:24 +0800
start since:          5235
accepted conn:        250
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       5
active processes:     1
total processes:      6
max active processes: 2
max children reached: 0
slow requests:        0
[root@localhost data]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

浏览器访问测试:


prometheus.yml添加php-fpm配置:

vim /data/prometheus/prometheus.yml
.......................
.......................
.......................
.......................
  - job_name: 'PHP-FPM'
    static_configs:
      - targets: ['192.168.223.142:9190']


#检查prometheus配置文件语法
[root@localhost prometheus]# ./promtool check config prometheus.yml 
Checking prometheus.yml
 SUCCESS: prometheus.yml is valid prometheus config file syntax

#通过热加载重新加载prometheus页面
curl -XPOST http://127.0.0.1:9090/-/reload
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

访问prometheus页面: