一、摘要

promethues监控nginx可选两个exporter,通过nginx_exporter主要是获取nginx-status中的内建的指标,nginx自身提供status信息,较为简单,promethues中对应的metrics也较少,想要监控更多的指标可以通过nginx-vts-exporter采集信息,依赖在编译nginx的时候添加nginx-module-vts模块来实现。

nginx virtual host traffic status模块是nginx第三方模块之一,vts提供了访问虚拟主机状态的信息,包含server,upstream以及cache的当前状态,类似于NGINX Plus 提供的在线活动监控功能。

二、Nginx_exporter安装

下载链接:

https://github.com/discordianfish/nginx_exporter

nginx_exporter.go,需要使用go语言环境将源码编译成二进制可执行文件。

虽然我尝试安装了golang,使用

go build 代码.go

build编译未能成功,果断放弃了。

这里,介绍一个很简单的办法,先下载nginx_exporter的docker镜像。

docker pull fish/nginx-exporter

先run一下,执行之后,会hold住,先不要关闭窗口。

docker run -it fish/nginx-exporter

新开一个窗口,查看docker进程

root@ubuntu:~# docker ps|grep nginx-exporter
5baa06db43d9 fish/nginx-exporter "/usr/local/bin/ngin…" minutes ago Up minutes /tcp lucid_germain

复制编译后的文件到/opt

docker cp 5baa06db43d9:/usr/local/bin/nginx_exporter /opt/

执行帮助命令:

root@ubuntu:~# /opt/nginx_exporter --help
Usage of /opt/nginx_exporter:
-insecure
Ignore server certificate if using https (default true)
-nginx.scrape_uri string
URI to nginx stub status page (default "http://localhost/nginx_status")
-telemetry.address string
Address on which to expose metrics. (default ":9113")
-telemetry.endpoint string
Path under which to expose metrics. (default "/metrics")

如果能执行成功,说明脚本可以运行了。

然后使用Ctrl+c 关闭docker容器。

三、Nginx开启状态

在线安装nginx

安装nginx

apt-get install -y nginx

编译安装nginx

wget -c http://mirror.nienbo.com/nginx/nginx-1.12.1.tar.gz
tar -zxvf nginx-1.12..tar.gz
cd nginx-1.12.
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
make
make install

编辑配置文件

vi /etc/nginx/sites-enabled/status.conf

注意:这是在线安装nginx的路径。请根据实际情况修改。

内容如下:

server {
listen ;
server_name localhost;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
}
}

重新加载配置文件

nginx -s reload

访问nginx状态页面

root@ubuntu:/etc/nginx/sites-enabled# curl 127.0.0.1:8011/nginx_status
Active connections:
server accepts handled requests Reading: Writing: Waiting:

四、nginx_exporter封装service服务

创建nginx_exporter目录,移动文件

mkdir -p /etc/nginx_exporter/bin/
mv /opt/nginx_exporter /etc/nginx_exporter/bin/

封装service

vim /lib/systemd/system/nginx_exporter.service

内容如下:

[Unit]
Description=nginx monitor
After=network.target [Service]
ExecStart=/etc/nginx_exporter/bin/nginx_exporter -nginx.scrape_uri="http://127.0.0.1:8011/nginx_status"
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5
TimeoutStopSec=5
KillMode=mixed [Install]
WantedBy=multi-user.target

重新加载service配置文件,设置开机自启动,并启动服务

systemctl daemon-reload
systemctl enable nginx_exporter.service
systemctl start nginx_exporter.service

查看端口

root@ubuntu:/etc/nginx/sites-enabled# netstat -anpt|grep nginx_exporte
tcp 127.0.0.1: 127.0.0.1: ESTABLISHED /nginx_exporte
tcp6 ::: :::* LISTEN /nginx_exporte
tcp6 192.168.1.24: 172.17.0.2: ESTABLISHED /nginx_exporte

使用网页访问metrics

http://192.168.1.24:9113/metrics

效果如下:

如果数据输出正常,则表示成功。

五、promethus增加job

vim /opt/prometheus/prometheus.yml

增加以下内容:

- job_name: nginx_exporter
static_configs:
- targets:
- 192.168.1.24:
labels:
instance: nginx-

重启prometheus,访问页面:

http://192.168.1.24:9090/targets

确保nginx_exporter状态为UP

六、Grafana导入Dashboards

下载json文件

https://files.cnblogs.com/files/xiao987334176/Nginx%E7%9B%91%E6%8E%A7-1563001010074.zip

解压之后,里面就是一个json文件

点击import

上传json文件,设置Name,选择prometheus,点击import

等待一段时间,数据多了之后,效果如下:

注意:TOP10 是没有数据的,据说需要使用脚本实现,具体怎么实现,我也不太清理。

可以直接将没有数据的图标删除掉!

本文参考链接:

https://it.baiked.com/nginx/2697.html

promethus监控nginx的更多相关文章

  1. Zabbix 监控 Nginx(四)

    简介: 如何使用 Zabbix 监控 Nginx 状态 ? 1.获取 Nginx 状态( HTTP Stub Status ) [root@localhost ~]# /apps/product/ng ...

  2. Python 监控nginx服务是否正常

    Python 监控nginx服务是否正常 #!/usr/bin/env python import os, sys, time from time import strftime while True ...

  3. 让keepalived监控NginX的状态

    经过前面的配置,如果主服务器的keepalived停止服务,从服务器会自动接管VIP对外服务:一旦主服务器的keepalived恢复,会重新接管VIP. 但这并不是我们需要的,我们需要的是当NginX ...

  4. ngxtop:在命令行实时监控 Nginx 的神器

    Nginx网站服务器在生产环境中运行的时候需要进行实时监控.实际上,诸如Nagios, Zabbix, Munin 的网络监控软件是支持 Nginx 监控的. 如果你不需要以上软件提供的综合性报告或者 ...

  5. PHP实现简单的监控nginx日志文件功能

    此功能是为了实现,定时监控nginx生成的日志数据,并将新增的数据提交到一个接口(比如大数据的接口,让大数据来进行分析) define("MAX_SHOW", 8192*5); / ...

  6. Zabbix 监控 Nginx 状态

    简介: 如何使用 Zabbix 监控 Nginx 状态 ? 1.获取 Nginx 状态( HTTP Stub Status ) shell > /usr/local/nginx/sbin/ngi ...

  7. zabbix监控nginx

     nginx status详解 active connections – 活跃的连接数量server accepts handled requests — 总共处理了11989个连接 , 成功创建11 ...

  8. 如何监控 Nginx?

    什么是 Nginx? Nginx("engine-x")是一个 HTTP 和反向代理服务器,同时也是一个邮件代理服务器和通用的 TCP 代理服务器.作为一个免费开源的服务器,Ngi ...

  9. 使用zabbix监控nginx

    在zabbix agentd客户端上,查看nginx是否加载了--with-http_stub_status_module.因为zabbix监控nginx是根据 nginx的Stub Status模块 ...

随机推荐

  1. SpringBoot中Example的动态条件查询

    一.无匹配器的情况: Person person = new Person(); person.setName("test"); Role role = new Role(); r ...

  2. fillter根据value来匹配字段

    字段对应 let cashBackState = { 'WAIT_FIVE': '满5单可返现', 'FINISHED': '已返现' } filters: { cashBackStateFilter ...

  3. Vue2.0 render: h => h(App)的解释

    render: h => h(App)是ES6的写法,其实就是如下内容的简写: render: function (createElement) { return createElement(A ...

  4. 【大数据应用技术】作业十一|分布式并行计算MapReduce

    本次作业在要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3319  1.用自己的话阐明Hadoop平台上HDFS和MapRe ...

  5. 解决'python -m pip install --upgrade pip' 报错问题

    再安装包的时候提示 You are using pip version 9.0.3, however version 10.0.1 is available.You should consider u ...

  6. 【mybatis源码学习】mybatis的结果映射

    一.mybatis结果映射的流程 二.mybatis结果映射重要的类 1.org.apache.ibatis.executor.resultset.ResultSetWrapper(对sql执行返回的 ...

  7. 详解intent和intentfilter

    1.Intent对象简介 Intent中文意思指"意图",按照Android的设计理念,Android使用Intent来封装程序的"调用意图",不管启动Acti ...

  8. js页面 :函数名 is not defined

    最初的写法如下 function GetDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate() + AddDayCoun ...

  9. Win10 LTSC 2019 长期支持版

    win 10 LTSB 2016 文件名:cn_windows_10_enterprise_2016_ltsb_x86_dvd_9057089.iso (2.62GB) 语言: Chinese – S ...

  10. 爬虫中BeautifulSoup4解析器

    CSS 选择器:BeautifulSoup4 和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. lxml 只会 ...