记-Windows环境下Prometheus+alertmanager+windows_exporter+mtail监控部署
1、概述
最近因项目需要统计服务的负载情况及机器的负载情况,但是项目里面却没有相关统计而服务所在的机器也没有相关的监控,因为工期原因就选择了相对轻量级的prometheus方案。其中windows_exporter用来采集机器的负载情况,如CPU、内存、磁盘io、网络等基础信息,使用mtail来采集应用日志统计服务情况(需要业务系统有基础的日志,如请求发起,是否正常结束等,或者通过nginx等中间件的日志来统计也行),如QPS、TPS、请求数、成功率、异常率等,使用prometheus来统计分析相应的指标及设置报警规则等,如通过请求总数来计算QPS等。
2、windows_exporter配置启动
建议下载最新版本,最新版本下载地址
,我下载的版本为windows_exporter-0.18.1-386.exe,将其解压到任意目录下,进入将其在后台启动:

其中链接Metrics为采集到的指标(metrics)如下:

3.mtail配置启动
mtail是谷歌基于golang开发的一款日志收集工具,用于从应用程序日志中提取白盒监视数据以收集在时间序列数据库中。
建议下载最新版本,最新版下载地址请戳此处,我下载的版本为mtail_3.0.0-rc48_Linux_arm64.tar.gz,此版本是可执行二进制文件,移动至/usr/local/sbin下即可使用。
查看mtail版本(验证matail命令是否可用):
mtail -version
输出如下:
mtail version 3.0.0-rc48 git revision e19766e45ca2dedf34794fea51444c7963a7c3d6 go version go1.17.5 go arch 386 go os linux
mtail启动命令如下:
nohup mtail -port 8080 -logtostderr -progs /etc/mtail/access.mtail -logs /xxx/xxx.log & #8080端口可以自定义,也可同时启动多个mtail监控多个日志文件。
-progs参数需要以.mtail结尾,脚本内容为需要从日志中提取的指标,具体格式脚本写法及例子请参考此链接。
例子中access.mtail的内容如下:
counter apache_http_requests_total by request_method, http_version, status_code
counter apache_http_bytes_total by request_method, http_version, status_code
gauge apache_http_response_time by remote_host, request_method, request_uri, status_code
gauge apache_http_response_size by remote_host, request_method, request_uri, status_code histogram apache_http_request_time_millseconds_bucket buckets 0, 1, 2, 4, 8 by status_code /^/ +
/(?P<remote_host>[0-9A-Za-z\.:-]+) / + # %h
/(?P<remote_logname>[0-9A-Za-z-]+) / + # %l
/(?P<remote_username>[0-9A-Za-z-]+) / + # %u
/\[(?P<timestamp>\d{2}\/\w{3}\/\d{4}:\d{2}:\d{2}:\d{2} (\+|-)\d{4})\] / + # %u
/"(?P<request_method>[A-Z]+) (?P<request_uri>\S+) (?P<http_version>HTTP\/[0-9\.]+)" / + # \"%r\"
/(?P<status_code>\d{3}) / + # %>s
/((?P<response_size>\d+)|-) / + # %b
/(?P<response_time>\d+) / + # %D
/"(?P<referer>\S+)" / + # \"%{Referer}i\"
/"(?P<user_agent>[[:print:]]+)"/ + # \"%{User-agent}i\"
/$/ {
strptime($timestamp, "02/Jan/2006:15:04:05 -0700") apache_http_requests_total[$request_method][$http_version][$status_code]++
$response_size > 0 {
apache_http_bytes_total[$request_method][$http_version][$status_code] += $response_size
apache_http_response_size[$remote_host][$request_method][$request_uri][$status_code] += $response_size
}
apache_http_response_time[$remote_host][$request_method][$request_uri][$status_code] = $response_time
apache_http_request_time_millseconds_bucket[$status_code] = $response_time
} getfilename() !~ /access_log.?log/ {
stop
}
counter media_request_total
/receive http request path is/ {
media_request_total++
}
#第一段代码是监控apache日志,第一阶段测试可用此段配置。
查看mtail信息,访问http://ip:3903:

这里需要注意的是:若监控apache日志,需配置好第一段代码运行后触发apache(重启)产生新日志才能被监控到。
4.prometheus配置启动
建议下载最新版本,最新版下载地址请戳此处,我下载的版本为prometheus-2.34.0.windows-amd64.zip,将其解压到任意目录下,进入目录修改配置文件prometheus.yml添加windows_exporter和matail 的采集路径,prometheus.yml文件配置如下:
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s). # Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "rules/*_rules.yml"
#告警规则yml文件
# - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus" # metrics_path defaults to '/metrics'
# scheme defaults to 'http'. static_configs:
- targets: ["localhost:9090"] - job_name: "node_exporter"
scrape_interval: 5s
#每五秒检查一次应用进程
metrics_path: "/metrics"
#指标路径
static_configs:
- targets: ["localhost:9182"] - job_name: "alertmanager"
scrape_interval: 5s static_configs:
- targets: ["localhost:9093"] - job_name: "mtail"
scrape_interval: 5s static_configs:
- targets: ["xx.xx.xx.xx:8080"]
prometheus默认端口为9090,通过网页访问http://ip:9090/:

如需配置告警,需在目录下新建rules文件夹,告警配置如下:
groups:
- name: node-up
# 分组名称
rules: # 规则设置
- alert: node-up
#告警名称
expr: up{job="windows_exporter"} == 0
# 表达式,查询式语句查询up的值是否等于0,如果等于则告警
for: 15s
# 告警持续时间
labels:
severity: 1
team: node
annotations: # 注解
summary: "{{ $labels.instance }} 已停止运行超过 15s!"

5、alertmanager配置
建议下载最新版本,最新版下载地址请戳此处,Alertmanager的配置有两个地方,一个是在Prometheus server端进行配置告警节点,指定匹配告警规则文件路径,以及监控alertmanager本身。另一个直接配置alertmanager自身的配置,在alertmanager.yml进行配置。
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.qq.com:465'
smtp_from: 'xxxxxxxxx@qq.com'
smtp_auth_username: 'xxxxxxxxx@qq.com'
smtp_auth_password: 'xxxxxxxxx' # 16位qq邮箱授权码作为密码
smtp_require_tls: false route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'email' # 选用邮箱告警发送 receivers:
- name: 'email'
email_configs:
- to: 'xxxxxxxxx@qq.com' inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']

6.总结
此套监控的解决方案优点在于完全对业务系统解耦,并且对业务系统透明,唯一需要的就是业务系统的日志所在路径。
而此套系统的关键点在于mtail脚本编写和mtail采集到的数据最终使用promQL分析,mtail脚本需要知道业务系统的日志是否满足监控系统的需求,如果不满足还是需要改动业务代码添加相应的日志。而promQL则需要掌握相关的语法规则及内置函数,只要掌握了这两个关键点,整套系统就搞定了。
转载请声明出处哦~,本篇文章发布于Biuget-Golang的博客:
https://www.cnblogs.com/Biuget-Golang/
记-Windows环境下Prometheus+alertmanager+windows_exporter+mtail监控部署的更多相关文章
- 《高可用MySQL》1 – Windows环境下压缩版MySQL安装
近日在读O’REILIY系列的<高可用MySQL>, 自然少不了主从(Master-Slave)配置和横向扩展相关的内容.Master-Slave这东西吧,在许多公司都是标配,开发中基本天 ...
- 浅谈Windows环境下DOS及MS-DOS以及常见一些命令的介绍
浅谈Windows环境下DOS及MS-DOS以及常见一些命令的介绍 前记 自己是搞编程的,首先我是一个菜鸟,接触计算机这么久了,感觉很多计算机方面的技术和知识朦朦胧胧.模模糊糊,貌似有些贻笑大方了:所 ...
- Windows环境下IOS APP打包上传AppStore详细流程
我们知道在上架苹果应用过程中,申请发布证书需要用到钥匙串,上传ipa需要用到xcode或Application loader提交构建版本,这都需要Mac苹果机. 本文介绍如何在Windows环境下申请 ...
- 用Hugo在gitee上构建博客(Windows环境下)
目录 用Hugo在gitee上构建博客(Windows环境下) 1.为什么要用gitee? 2.安装git 3.安装Hugo 4.创建远程仓库 5.搭建博客 (以下所有命令都在git bash中输入) ...
- 关于docker在windows环境下运行的第一次体验
关于docker在windows环境下执行的原理 1.1. 首先是Docker Quickstart启动,如果在虚拟机Oracle VM VirtualBox不存在default虚 ...
- Redis在windows环境下ThinkPHP的安装和使用
1.Redis概述: 2.Redis在windows环境下的安装: 下载地址:https://github.com/dmajkic/redis/downloads,选取其中一个zip压缩包:
- 【经验之谈】Git使用之Windows环境下配置
前言 安装 配置 关于git使用的几个问题 后记 关于代码托管,以前用过vss和svn,看博客或论坛的时候,经常有人提到github,有很多著名的开源软件都托管在github,想来肯定不错(莫笑),当 ...
- 【经验之谈】Windows环境下配置WordPress
前言 wordpress全球著名的开放博客平台,拥有成千上万个各式插件和不计其数的主题模板样式,使用php和mysql搭建,下面说下载windows环境下配置wordpress,经验之谈. 安装 关于 ...
- 在 windows 环境下安装 redislive
这是一篇在 windows 环境下安装 redislive 的教程! 项目地址:https://github.com/nkrode/RedisLive 配置文档:http://www.nkrode.c ...
随机推荐
- 深入MySQL(二):MySQL的数据类型
前言 对于MySQL中的数据类型的选择,不同的数据类型看起来可能是相同的效果,但是其实很多时候天差地别. 本章从MySQL中的常用类型出发,结合类型选择的常见错误,贯彻MySQL的常用类型选择. 常用 ...
- 为什么使用Mybatis对JDBC进行包装探究
一.原生JDBC在实际生产中使用存在的影响性能的问题 首先分析使用JDBC的代码: Connection connection = null; PreparedStatement preparedSt ...
- 6.Flink实时项目之业务数据分流
在上一篇文章中,我们已经获取到了业务数据的输出流,分别是dim层维度数据的输出流,及dwd层事实数据的输出流,接下来我们要做的就是把这些输出流分别再流向对应的数据介质中,dim层流向hbase中,dw ...
- rinetd基于内网TCP端口转发
在Linux系统中大多数情况选择用iptables来实现端口转发,iptables虽然强大,但配置不便,而且新手容易出错.在此分享另一个TCP/UDP端口转发工具rinetd,rinetd体积小巧,配 ...
- python开启httpserver
- PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
torch.mul() 函数功能:逐个对 input 和 other 中对应的元素相乘. 本操作支持广播,因此 input 和 other 均可以是张量或者数字. 举例如下: >>> ...
- 【C# 线程 】内存模型 与Volatile
CPU硬件有它自己的内存模型,不同的编程语言也有它自己的内存模型. 在 C# 的语言规范中 ECMA-334,对于Volatile关键字的描述: 15.5.4 Volatile fieldsWhen ...
- Windows Server 2012 R2通过命令行重置网络环境
转至:https://jingyan.baidu.com/article/48b37f8d5d89385a646488b5.html 我们使用Windows Server 2012 R2时会遇到通过命 ...
- Java课程设计---修改学生基本信息
1.修改窗体 2.在StudentDao中增加修改学生信息的方法 /** * 修改的方法 * * @param student * @return * @throws SQLException */ ...
- python 2.x 版本 pip 的使用
看文档要使用 python2.7 的 pip 安装 TensorFlow, 因为有python3和pip3的缘故(pip2 和 /usr/bin/pip2看起来像是基于python2.x的,打个-V便 ...