prometheus监控nginx和php-fpm

主机信息

主机名   部署服务     类型     系统
server  prometheus  监控端    centos7.9
client  nginxphp    被监控端  centos7.9

大致步骤如下:
1、被监控端开启nginx和php的收集参数插件
2、被监控的安装export为监控端提供数据采集端口
3、监控端添加被监控端的信息并验证是否有数据

具体步骤

需要在监控端部署Prometheus和grafana,过程比较简单,此处省略
需要在被监控端部署nginx和php,均为二进制安装。
nginx在编译安装时需要添加模块status_module

#检查nginx是否安装所需模块,#返回with-http_stub_status_module表示已经安装过了
nginx -V 2>&1 | grep -o with-http_stub_status_module

#nginx编译参考,注意,如果按照本文档的编译参数编译,那么nginx配置文件为/usr/local/nginx/nginx.conf
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --user=nginx --group=nginx --with-http_stub_status_module

#php编译参数
./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php8/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-mysqlnd --with-mysqli --with-pdo-mysql --enable-opcache --with-pcre-jit --enable-gd --with-jpeg --with-freetype --with-gettext --with-curl --with-openssl --enable-sockets --enable-mbstring --enable-xml --with-zip --with-zlib --with-snmp --with-mhash --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-pcntl --with-gmp 

一、在确认nginx有status_module模块后,编辑nginx配置文件(编辑前请备份原配置文件)

#在server段中添加
             ..........
        location /stub_status {
          stub_status on; #开启stub_status模块
          access_log off; #关闭access日志
          #添加访问限制
          allow 192.168.180.0/24; #
          allow 127.0.0.1;
          deny all;
        }
           .............
           
#添加后检查nginx配置文件是否有误,并重启
[root@client ~]# nginx -t
#返回ok和successful表示没问题
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
#重启nginx
[root@client ~]# /usr/local/nginx/sbin/nginx -s reload

二、验证是否有nginx状态数据

#访问ip/stub_status
[root@client ~]# curl 127.0.0.1/stub_status
Active connections: 2 
server accepts handled requests
 175 175 10659 
Reading: 0 Writing: 1 Waiting: 1 
#有返回值就说明正确
#参数说明 
Active connections -- 活动连接数 
accepts -- 接收请求数 
handled -- 成功处理请求数 
requests -- 总请求数 
reding -- 正在进⾏读操作的请求数 
writing -- 正在进⾏写操作的请求数 
waiting -- 正在等待的请求数 

三、编辑php配置文件(编辑前请备份)

#开启status状态,在php配置文件最后添加两行
[root@client ~]# vim /usr/local/php7/etc/php-fpm.conf
......
pm.status_path = /status
ping.path = /ping

四、在nginx配置文件中添加php访问

 #编辑nginx配置文件
 [root@client ~]# vim /usr/local/nginx/nginx.conf
              ..........
          location ~ ^/status$ {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
          }
              ..........
#重启php和nginx
[root@client ~]# service php-fpm restart && /usr/local/nginx/sbin/nginx -s reload
Gracefully shutting down php-fpm . done
Starting php-fpm  done
#验证是否能够收集到php数据信息
[root@client ~]# curl 127.0.0.1/status
pool:                 www
process manager:      dynamic
start time:           05/Jul/2023:22:32:41 -0400
start since:          135
accepted conn:        1
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       1
active processes:     1
total processes:      2
max active processes: 1
max children reached: 0
slow requests:        0             

五、安装nginx_export和php_export

nginx_export下载链接:
https://pan.baidu.com/s/1ZNhivKIyPP4vKaVCrKX4MQ?pwd=0928
提取码:0928
php_export下载链接:
https://pan.baidu.com/s/1quKa3ncwo5mxTqxfVQa1Zw?pwd=0928
提取码:0928

解压nginx_export,并运行

[root@client ~]# tar -xf nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gz -C /usr/local/nginx_export
[root@client ~]# cd /usr/local/nginx_export
[root@client nginx_export]# vim nginx_expory_start.sh
#启动脚本
nohup ./nginx-prometheus-exporter -nginx.scrape-uri=http://192.168.180.111/stub_status  &
[root@client nginx_export]# chmod +x nginx_expory_start.sh
[root@client nginx_export]# ./nginx_expory_start.sh
[root@client nginx_export]# ss -anltup | grep 9113 #默认监听9113端口
tcp    LISTEN     0      128    [::]:9113               [::]:*                   users:(("nginx-prometheu",pid=1227,fd=7))
[root@client nginx_export]# 

解压php_export,并运行

#解压php_export,并运行
[root@client ~]# tar -xf php-fpm-exporter.tar.gz -C /usr/local/
[root@client ~]# cd /usr/local/php-fpm-exporter/
[root@client php-fpm-exporter]# vim php-export-start.sh
nohup ./php-fpm-exporter --addr 0.0.0.0:9190 --endpoint http://127.0.0.1/status &
[root@client php-fpm-exporter]# ./hp-export-start.sh
[root@client php-fpm-exporter]# ss -anltup | grep 9190
tcp    LISTEN     0      128    [::]:9190               [::]:*                   users:(("php-fpm-exporte",pid=10271,fd=3))

#通过systemctl管理
#nginx
[root@client ~]# cd /usr/lib/systemd/system/
[root@client system]# vim nginx.service
[Unit]
Description=nginx server daemon
After=network.target sshd-keygen.service

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
Restart=on-failure

[Install]
WantedBy=multi-user.target

#php
[root@client system]# vim php.service
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.service

[Service]
Type=forking
ExecStart=service php-fpm start
ExecStop=service php-fpm stop
ExecReload=service php-fpm restart
Restart=on-failure

[Install]
WantedBy=multi-user.target

#nginx-exporter
[root@client system]# vim nginx-exporter.service
[Unit]
Description=nginx-exporter server daemon
After=network.target sshd-keygen.service

[Service]
Type=simple
Restart=always
ExecStart=/usr/local/nginx-export/nginx-prometheus-exporter -nginx.scrape-uri=http://192.168.180.111/stub_status

[Install]
WantedBy=multi-user.target

#php-exporter
[root@client system]# vim php-fpm-exporter.service
[Unit]
Description=OpenSSH server daemon
After=network.target sshd-keygen.service

[Service]
Type=simple
Restart=always
ExecStart=/usr/local/php-fpm-exporter/php-fpm-exporter --addr 0.0.0.0:9190 --endpoint http://127.0.0.1/status

[Install]
WantedBy=multi-user.target

#设置开机自启
[root@client ~]# systemctl enable --now nginx
[root@client ~]# systemctl enable --now php-fpm
[root@client ~]# systemctl enable --now nginx-exporter
[root@client ~]# systemctl enable --now php-fpm-exporter

六、在监控端的Prometheus中添加被监控主机信息

#编辑Prometheus配置文件
[root@server ~]# vim /usr/local/prometheus/prometheus.yml
#在scrape_configs段下添加
    ..........
  - job_name: "nginx"
    static_configs:
      - targets: ["192.168.***.***:9113"]

  - job_name: "php-fpm"
    static_configs:
      - targets: ["192.168.***.***:9190"]
#添加后重启Prometheus
[root@server ~]# systemctl restart prometheus.service 

七、访问Prometheus的web界面验证是否能监控到主机
在这里插入图片描述
点击metrics,能监控采集到数据说明没问题
在这里插入图片描述
八、添加至grafana
在这里插入图片描述
添加数据
在这里插入图片描述
选3901模板
在这里插入图片描述
设置图标名字,选择监控的数据源,点击“import”导入
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
一、prometheus简介 Prometheus是一个开源的系统监控和告警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF维护管理的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控prometheus支持多种exporter采集数据,还支持通过pushgateway进行数据上报,Prometheus再性能上可支撑上万台规模的集群。 二、prometheus架构图 三、prometheus组件介绍 1.Prometheus Server: 用于收集和存储时间序列数据。 2.Client Library: 客户端库,检测应用程序代码,当Prometheus抓取实例的HTTP端点时,客户端库会将所有跟踪的metrics指标的当前状态发送到prometheus server端。 3.Exporters: prometheus支持多种exporter,通过exporter可以采集metrics数据,然后发送到prometheus server端 4.Alertmanager: 从 Prometheus server 端接收到 alerts 后,会进行去重,分组,并路由到相应的接收方,发出报警,常见的接收方式有:电子邮件,微信,钉钉, slack等。 5.Grafana:监控仪表盘 6.pushgateway: 各个目标主机可上报数据到pushgatewy,然后prometheus server统一从pushgateway拉取数据。 四、课程亮点 五、效果图展示 六、讲师简介 先超(lucky):高级运维工程师、资深DevOps工程师,在互联网上市公司拥有多年一线运维经验,主导过亿级pv项目的架构设计和运维工作 主要研究方向: 1.云计算方向:容器 (kubernetes、docker),虚拟化(kvm、Vmware vSphere),微服务(istio),PaaS(openshift),IaaS(openstack)等2.系统/运维方向:linux系统下的常用组件(nginx,tomcat,elasticsearch,zookeeper,kafka等),DevOps(Jenkins+gitlab+sonarqube+nexus+k8s),CI/CD,监控(zabbix、prometheus、falcon)等 七、课程大纲
Prometheus可以监控nginx的流量。通过使用Prometheus的插件或者导出器可以收集nginx监控数据。其中,可以使用nginx-vts-exporter来收集Nginx监控数据,并将其通过HTTP暴露给Prometheus进行消费。nginx-vts-exporter是一个简单的服务器,它会抓取Nginx的vts(虚拟主机统计)统计数据,并通过HTTP接口提供给Prometheus。默认情况下,nginx-vts-exporter监听的端口号是9913。 Prometheus具有多维数据模型,可以很好地对nginx的指标进行收集和存储,例如流量。你可以自定义Prometheus的数据标签,以便针对不同的机房和项目对nginx进行监控。这样,你可以根据需要对各个维度的流量进行可视化展示,也可以使用Grafana等工具来生成图像展示和监控报告。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [用PrometheusNginx监控](https://blog.csdn.net/weixin_41709748/article/details/88063548)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [使用prometheus监控nginx](https://blog.csdn.net/Mob_com/article/details/89312413)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值