最新版本:Prometheus 监控带用户名密码的 API (NGINX)采集配置
优采云 发布时间: 2022-10-20 05:59最新版本:Prometheus 监控带用户名密码的 API (NGINX)采集配置
去魏世东技术专栏了解爬虫架构/反向爬虫/存储引擎/消息队列/Python/Golang
这个文章的主要目的是告诉你如何配置Prometheus,使它可以从指定的Web Api接口采集访问metrics数据。文章 中使用的案例是 NGINX 的 采集 配置,采集 数据来自 NGINX 数据指标页面,并设置了用户名和密码,因此这个 文章 的副标题可能是 nginx 的 prometheus 采集 配置或者 prometheus 采集 basic auth nginx。
上图为配置完成后在 Grafana 中配置模板的效果。
用过 Prometheus 的朋友一定知道如何配置 address:port 服务。例如,在采集某个 Redis 的信息时,可以这样写配置:
- job_name: 'redis'
static_configs:
- targets: ['11.22.33.58:6087']
注意:以上示例假设 Redis Exporter 的地址和端口为 11.22.33.58:6087。
这是最简单也是最广为人知的方法。但是如果要监控指定的Web API,就不能这样写。如果你没有看到这个文章,你可以在搜索引擎中这样搜索:
但是很遗憾,我找不到任何有效的信息(现在是2021年3月),基本上我能找到的都是坑。
条件假设
假设我们现在需要从 address 的接口采集相关的 Prometheus 监控指标,并且该接口使用 basic auth(假设用户名为 weishidong,密码为 0099887kk)进行基本权限验证。
配置实践
如果填写之前看到的Prometheus配置,很有可能会这样写配置:
- job_name: 'web'
static_configs:
- targets: ['http://www.weishidong.com/status/format/prometheus']
basic_auth:
username: weishidong
password: 0099887kk
保存配置文件,重启服务后,你会发现这是没有数据采集,太可怕了。
官方配置指南
刚才的*敏*感*词*实在是太糟糕了。当我们遇到不理解的问题时,当然是去官方文档 -> Prometheus 配置。建议从上到下阅读,但如果赶时间,可以直接进入采集配置部分。官方示例如下(内容太多,这里只保留与本文相关的部分,建议大家阅读原文):
# The job name assigned to scraped metrics by default.
job_name:
# How frequently to scrape targets from this job.
[ scrape_interval: | default = ]
# Per-scrape timeout when scraping this job.
[ scrape_timeout: | default = ]
# The HTTP resource path on which to fetch metrics from targets.
[ metrics_path: | default = /metrics ]
<p>
# honor_labels controls how Prometheus handles conflicts between labels that are
# already present in scraped data and labels that Prometheus would attach
# server-side ("job" and "instance" labels, manually configured target
# labels, and labels generated by service discovery implementations).
#
# If honor_labels is set to "true", label conflicts are resolved by keeping label
# values from the scraped data and ignoring the conflicting server-side labels.
#
# If honor_labels is set to "false", label conflicts are resolved by renaming
# conflicting labels in the scraped data to "exported_" (for
# example "exported_instance", "exported_job") and then attaching server-side
# labels.
#
# Setting honor_labels to "true" is useful for use cases such as federation and
# scraping the Pushgateway, where all labels specified in the target should be
# preserved.
#
# Note that any globally configured "external_labels" are unaffected by this
# setting. In communication with external systems, they are always applied only
# when a time series does not have a given label yet and are ignored otherwise.
[ honor_labels: | default = false ]
# honor_timestamps controls whether Prometheus respects the timestamps present
# in scraped data.
#
# If honor_timestamps is set to "true", the timestamps of the metrics exposed
# by the target will be used.
#
# If honor_timestamps is set to "false", the timestamps of the metrics exposed
# by the target will be ignored.
[ honor_timestamps: | default = true ]
# Configures the protocol scheme used for requests.
[ scheme: | default = http ]
# Optional HTTP URL parameters.
params:
[ : [, ...] ]
# Sets the `Authorization` header on every scrape request with the
# configured username and password.
# password and password_file are mutually exclusive.
basic_auth:
[ username: ]
[ password: ]
[ password_file: ]
# Sets the `Authorization` header on every scrape request with
# the configured bearer token. It is mutually exclusive with `bearer_token_file`.
[ bearer_token: ]
# Sets the `Authorization` header on every scrape request with the bearer token
# read from the configured file. It is mutually exclusive with `bearer_token`.
[ bearer_token_file: ]</p>
如果你仔细看,你应该注意到一些关键信息:metrics_path 和 basic_auth。其中,metrics_path用于指定HTTP类型的metrics信息采集的路由地址,默认值为/metrics;字段basic_auth用于授权验证,密码可以在这里指定密码文件,而不是直接填写明文(一般情况下,指定密码文件比明文稍微安全一些)。
有效配置
根据官方文档的指导,我们可以快速推导出正确的配置写法:
- job_name: 'web'
metrics_path: /status/format/prometheus
static_configs:
- targets: ['www.weishidong.com']
basic_auth:
username: weishidong
password: 0099887kk
需要注意的是,这里的文字不需要填写,因为Prometheus的默认Scheme是http。如果地址的Scheme是https,根据文档我们需要添加scheme字段,对应的配置是:
- job_name: 'web'
metrics_path: /status/format/prometheus
static_configs:
- targets: ['www.weishidong.com']
scheme: https
basic_auth:
username: weishidong
password: 0099887kk
配置完成后,Prometheus 应该可以顺利采集到数据了,配合 Grafana 可以看到开头给出的监控效果图。
最新版:DedeCMS v5.7自带采集体验
Dedecms基于PHP+MySQL的技术开发,支持多种服务器平台。自 2009 年发布第一个版本以来,已经发布了五个主要版本。Dedecms 简单、健壮、灵活且开源。国内大部分cms市场目前有10000多个站点使用Dedecms或基于Dedecms的自收录采集类似织梦cms 内置了普通的文章图片采集软件等内容模型,我们来测试一下常用的文章和图片模型,以便更好的和我们的cms其他cms对比采集目标网站同上一期cms采集体验系列织梦cms的PHPcms文章 采集新建一个文章采集节点背景——采集——采集节点管理PrivateConstSMART_I(null,invalidaddr..)——新增节点选择内容模型普通文章设置节点的基本信息。目标源码同上一篇文章文章。查看目标网站的源码后,设置区域匹配模式。仅当字符串无法确定区域时才使用默认字符串。正则表达式只是偶尔使用,但一般不使用。最后,它是织梦cms采集的特点之一。如果目标网站有防盗链功能,就会开启该功能可以成功采集但是会降低速度采集测试目标网站那里是没有防盗功能,所以没有启用列表网站获取规则同上一篇文章Capabi列表URL解析是有规律的,可以批量生成。这里要说一下dedecms的强大之处不仅在于获取列表url的方式可以灵活组合,而且如果目标网站对整个站点使用同一个模板可以启用“多列通配符(#)”功能。通过设置采集规则,可以采集整个站点,发布不同的栏目,不需要一栏对应一条规则(这个功能小了,小编会单独写教程)文章URL匹配规则查看目标列表页源码设置为采集文章URL区首尾的html后跟dedecms的特点之一>如果采集URL页面链接有图片,可以直接设置为缩略图采集再次过滤区域URL非常方便。“收录”和“不能收录”的优先级 本文体验目标站点的不干扰URL,所以留空