prometheus监控windows主机
本文介绍通过powershell脚本部署windows_exporter并把指标展示在grafana上
1.windows_exporter下载
提前需要在C盘创建exporter_dir目录和logs\node_exporter.log文件
链接:https://pan.baidu.com/s/1wxGpBD_MJFPa942A6mbONQ
提取码:gffo
将下载好的windows_exporter.exe放到C:\exporter_dir下
2.部署脚本
# 文件路径:C:\node_exporter_windows.ps1
#!/bin/bash
param(
[Parameter()]
[String]$operateType
)
$LogFile = "C:\logs\node_exporter.log"
$ExporterDir = "C:\exporter_dir"
function PrintLog($log)
{
$CurDate = Get-Date
$LogContent = "[$CurDate] [$operateType] [$log]"
Tee-Object -FilePath $LogFile -InputObject $LogContent -Append
}
function Prepare()
{
New-Item -Path "C:\logs" -ItemType "directory"
}
function StopNodeExporter()
{
PrintLog "Start stop node exporter..."
Get-Process windows_exporter* | Stop-Process
PrintLog "Finished stop node exporter..."
}
function StartNodeExporter()
{
PrintLog "Start deploy node exporter..."
$FilePath = $ExporterDir + "\windows_exporter.exe"
Start-Process -WindowStyle hidden -FilePath $FilePath --web.listen-address=:49150
PrintLog "Finished deploy node exporter..."
}
function Install()
{
PrintLog "Start install node exporter..."
Prepare
StopNodeExporter
StartNodeExporter
PrintLog "Finished install node exporter..."
}
function Uninstall()
{
PrintLog "Start uninstall node exporter..."
StopNodeExporter
Remove-Item -Path $ExporterDir -Recurse
PrintLog "Finished uninstall node exporter..."
}
function Main()
{
If ($operateType -eq "install")
{
Install
}
ELSEIF($operateType -eq "uninstall")
{
Uninstall
}
}
Main
3.启动windows_exporter
打开电脑powershell执行以下命令
# 启动
C:\node_exporter_windows.ps1 install
# 查看是否启动成功
PS C:\Users\xxx> Get-Process windows_exporter*
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
361 22 28784 30028 4836 2 windows_exporter
4.prometheus配置
# 采集项配置
- job_name: 'node-exporter'
honor_labels: true
consul_sd_configs:
- server: 'xx.xx.xx.xx:8500' # 配置consul服务发现
services: [ ]
relabel_configs:
- source_labels: [ __meta_consul_tags ]
regex: .*node-exporter.*
action: keep
- regex: __meta_consul_service_metadata_(.+)
action: labelmap![请添加图片描述](https://i-blog.csdnimg.cn/direct/bea5f7f5747a4acf9b36aab484f17a42.png)
# 告警规则配置
# cpu
- alert: vhost-default
annotations:
description: windows CPU空闲不足20%
summary: 'windows CPU空闲不足20%,value: {{ $value | printf "%.2f" }}%'
expr: avg by (instance) (rate(windows_cpu_time_total{mode="idle"}[1m])) * 100<20
for: 2m
labels:
prom_id: '59'
rule_type: cpu
rule_value: '20'
severity: warning
- alert: vhost-default
annotations:
description: windows CPU空闲不足10%
summary: 'windows CPU空闲不足10%,value: {{ $value | printf "%.2f" }}%'
expr: avg by (instance) (rate(windows_cpu_time_total{mode="idle"}[1m])) * 100<10
for: 2m
labels:
prom_id: '58'
rule_type: cpu
rule_value: '10'
severity: critical
- alert: vhost-default
annotations:
description: windows CPU空闲不足5%
summary: 'windows CPU空闲不足5%,value: {{ $value | printf "%.2f" }}%'
expr: avg by (instance) (rate(windows_cpu_time_total{mode="idle"}[1m])) * 100<5
for: 1m
labels:
prom_id: '112'
rule_type: cpu
rule_value: '5'
severity: emergency
# 内存
- alert: vhost-default
annotations:
description: windows主机内存不足20%
summary: windows主机内存不足20%
expr: (windows_os_physical_memory_free_bytes / windows_cs_physical_memory_bytes)
* 100<20
for: 3m
labels:
prom_id: '114'
rule_type: memory
rule_value: '20'
severity: warning
- alert: vhost-default
annotations:
description: windows内存可用空间不足10%
summary: windows内存可用空间不足10%
expr: (windows_os_physical_memory_free_bytes / windows_cs_physical_memory_bytes)
* 100<10
for: 30s
labels:
prom_id: '61'
rule_type: memory
rule_value: '10'
severity: critical
- alert: vhost-default
annotations:
description: windows内存可用空间不足5%
summary: windows内存可用空间不足5%
expr: (windows_os_physical_memory_free_bytes / windows_cs_physical_memory_bytes)
* 100<5
for: 30s
labels:
prom_id: '60'
rule_type: memory
rule_value: '5'
severity: emergency
# 磁盘
- alert: vhost-default
annotations:
description: windows磁盘可用空间不足20%
summary: windows磁盘可用空间不足20%
expr: 100 * ((windows_logical_disk_free_bytes / 1024 / 1024) / (windows_logical_disk_size_bytes
/ 1024 / 1024))<20
for: 2m
labels:
prom_id: '64'
rule_type: disk
rule_value: '20'
severity: warning
- alert: vhost-default
annotations:
description: windows磁盘可用空间不足10%
summary: windows磁盘可用空间不足10%
expr: 100 * ((windows_logical_disk_free_bytes / 1024 / 1024) / (windows_logical_disk_size_bytes
/ 1024 / 1024))<10
for: 30s
labels:
prom_id: '63'
rule_type: disk
rule_value: '10'
severity: critical
- alert: vhost-default
annotations:
description: windows磁盘可用空间不足10G
summary: windows磁盘可用空间不足10G
expr: windows_logical_disk_free_bytes{volume!~"HarddiskVolume1"} / 1024 / 1024
/ 1024<10
for: 30s
labels:
prom_id: '74'
rule_type: disk
rule_value: '10'
severity: emergency
5.grafana展示
模板:Windows Exporter Dashboard 20230531-StarsL.cn | Grafana Labs