Prometheus+Grafana-2-Linux监控-四种指标类型

一、概念

1.时间序列

安装完成后prometheus会暴露一个/metrics的HTTP服务,默认会加上/metrics,Prometheus就会采集这里面的样本数据。

样本

​ 样本数据会以时间序列的方式保存在内存数据库中,并且定时保存到硬盘上,时间序列是以时间戳和值的序列顺序存放的,称之为向量vector,每条时间序列通过指标名称(metrics name)和一组标签集(label)命名。

每一个点称为样本(sample),样本由三部分组成

  • 指标metrics:指标名 和 描述当前样本特征的标签集合

  • 时间戳timestamp:一个精确到毫秒的时间戳

  • 样本值value:一个float64的浮点型数据表示当前样本的值

访问192.168.88.129:9090/metrics

点进去可以看到

process_open_fds 就是一个样本 随时会变

可以看到每一行是三个部分:标签 - 时间戳 - 值

#可以指定搜索
process_open_fds{job='prometheus'}

2.指标类型

底层没有对指标类型进行区分,都是以时间序列的方式保存,但是为了理解不同指标之间的差异,定义了四种Metrics类型。

Counter计数器,Gauge仪表盘,Histogram直方图,Summary摘要

Counter

Counter只增不减,一般定义时使用_total作为后缀。

Gauge

随时变动,如CPU使用率等等指标。

Histogram和Summary

为了解决长尾问题,区分了直方图和摘要。

3.Job任务和Instance实例

每个暴露样本数据的HTTP服务都称为一个实例 Instance,而具有相同采集目的实例集合 称为任务

4.Exporter

所有向Prometheus提供服务的程序都可以称为Exporter,一个Exporter实例称为target。

来源

社区提供 或者 用户自定义

类型

  • 直接采集型

    ​ 这类Exporter直接内置了相应的应用程序,用于向Prometheus直接提供Target数据支持。这样设计的好处是,可以更好地监控各自系统的内部运行状态,同时也适合更多自定义监控指标的项目实施。例如cAdvisor、Kubernetes等,它们均内置了用于向Prometheus提供监控数据的端点。

  • 简介采集型

    ​ 原始监控目标并不直接支持Prometheus,需要我们使用Prometheus提供的Client Library编写该监控目标的监控采集程序,用户可以将该程序独立运行,去获取指定的各类监控数据值。例如,由于Linux操作系统自身并不能直接支持Prometheus ,用户无法从操作系统层面上直接提供对Prometheus的支持,因此单独安装Node exporter,还有数据库或网站HTTP应用类等Exporter。

数据规范

在服务地址后缀加上/metrics访问就能看到。

二、监控Linux服务器

1.CPU采

bash 复制代码
node_cpu_seconds_total

node_load1 #1分钟内的cpu负载
node_load5 #5分钟内的cpu负载
node_load15 #15分钟内的cpu负载

2.内存采集

开头为:node_memory_

3.磁盘采集

指标开头为:node_disk_

4.文件系统采集

指标开头为:node_filesystem_

5.网络采集

开头为:node_network_

6.触发器设置

bash 复制代码
cd /data/docker-prometheus/

cat >> prometheus/alert.yml <<"EOF"
- name: node-exporter
  rules:
  - alert: HostOutOfMemory
    expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100 < 10
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "主机内存不足,实例:{{ $labels.instance }}"
      description: "内存可用率<10%,当前值:{{ $value }}"
  - alert: HostMemoryUnderMemoryPressure
    expr: rate(node_vmstat_pgmajfault[1m]) > 1000
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "内存压力不足,实例:{{ $labels.instance }}"
      description: "节点内存压力大。 重大页面错误率高,当前值为:{{ $value }}"
  - alert: HostUnusualNetworkThroughputIn
    expr: sum by (instance) (rate(node_network_receive_bytes_total[2m])) / 1024 / 1024 > 100
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "异常流入网络吞吐量,实例:{{ $labels.instance }}"
      description: "网络流入流量 > 100 MB/s,当前值:{{ $value }}"
  - alert: HostUnusualNetworkThroughputOut
    expr: sum by (instance) (rate(node_network_transmit_bytes_total[2m])) / 1024 / 1024 > 100
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "异常流出网络吞吐量,实例:{{ $labels.instance }}"
      description: "网络流出流量 > 100 MB/s,当前值为:{{ $value }}"
  - alert: HostUnusualDiskReadRate
    expr: sum by (instance) (rate(node_disk_read_bytes_total[2m])) / 1024 / 1024 > 50
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "异常磁盘读取,实例:{{ $labels.instance }}"
      description: "磁盘读取> 50 MB/s,当前值:{{ $value }}"
  - alert: HostUnusualDiskWriteRate
    expr: sum by (instance) (rate(node_disk_written_bytes_total[2m])) / 1024 / 1024 > 50
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "异常磁盘写入,实例:{{ $labels.instance }}"
      description: "磁盘写入> 50 MB/s,当前值:{{ $value }}"
  - alert: HostOutOfDiskSpace
    expr: (node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes < 10 and ON (instance, device, mountpoint) node_filesystem_readonly == 0
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "磁盘空间不足告警,实例:{{ $labels.instance }}"
      description: "剩余磁盘空间< 10% ,当前值:{{ $value }}"
  - alert: HostDiskWillFillIn24Hours
    expr: (node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes < 10 and ON (instance, device, mountpoint) predict_linear(node_filesystem_avail_bytes{fstype!~"tmpfs"}[1h], 24 * 3600) < 0 and ON (instance, device, mountpoint) node_filesystem_readonly == 0
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "磁盘空间将在24小时内耗尽,实例:{{ $labels.instance }}"
      description: "以当前写入速率预计磁盘空间将在 24 小时内耗尽,当前值:{{ $value }}"
  - alert: HostOutOfInodes
    expr: node_filesystem_files_free{mountpoint ="/"} / node_filesystem_files{mountpoint="/"} * 100 < 10 and ON (instance, device, mountpoint) node_filesystem_readonly{mountpoint="/"} == 0
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "磁盘Inodes不足,实例:{{ $labels.instance }}"
      description: "剩余磁盘 inodes < 10%,当前值: {{ $value }}"
  - alert: HostUnusualDiskReadLatency
    expr: rate(node_disk_read_time_seconds_total[1m]) / rate(node_disk_reads_completed_total[1m]) > 0.1 and rate(node_disk_reads_completed_total[1m]) > 0
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "异常磁盘读取延迟,实例:{{ $labels.instance }}"
      description: "磁盘读取延迟 > 100ms,当前值:{{ $value }}"
  - alert: HostUnusualDiskWriteLatency
    expr: rate(node_disk_write_time_seconds_total[1m]) / rate(node_disk_writes_completed_total[1m]) > 0.1 and rate(node_disk_writes_completed_total[1m]) > 0
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "异常磁盘写入延迟,实例:{{ $labels.instance }}"
      description: "磁盘写入延迟 > 100ms,当前值:{{ $value }}"
  - alert: high_load 
    expr: node_load1 > 4
    for: 2m
    labels:
      severity: page
    annotations:
      summary: "CPU1分钟负载过高,实例:{{ $labels.instance }}"
      description: "CPU1分钟负载>4,已经持续2分钟。当前值为:{{ $value }}"
  - alert: HostCpuIsUnderUtilized
    expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[2m])) * 100) > 80
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "cpu负载高,实例:{{ $labels.instance }}"
      description: "cpu负载> 80%,当前值:{{ $value }}"
  - alert: HostCpuStealNoisyNeighbor
    expr: avg by(instance) (rate(node_cpu_seconds_total{mode="steal"}[5m])) * 100 > 10
    for: 0m
    labels:
      severity: warning
    annotations:
      summary: "CPU窃取率异常,实例:{{ $labels.instance }}"
      description: "CPU 窃取率 > 10%。 嘈杂的邻居正在扼杀 VM 性能,或者 Spot 实例可能失去信用,当前值:{{ $value }}"
  - alert: HostSwapIsFillingUp
    expr: (1 - (node_memory_SwapFree_bytes / node_memory_SwapTotal_bytes)) * 100 > 80
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "磁盘swap空间使用率异常,实例:{{ $labels.instance }}"
      description: "磁盘swap空间使用率>80%"
  - alert: HostNetworkReceiveErrors
    expr: rate(node_network_receive_errs_total[2m]) / rate(node_network_receive_packets_total[2m]) > 0.01
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "异常网络接收错误,实例:{{ $labels.instance }}"
      description: "网卡{{ $labels.device }}在过去2分钟接收错误率大于0.01,当前值:{{ $value }}"
  - alert: HostNetworkTransmitErrors
    expr: rate(node_network_transmit_errs_total[2m]) / rate(node_network_transmit_packets_total[2m]) > 0.01
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "异常网络传输错误,实例:{{ $labels.instance }}"
      description: "网卡{{ $labels.device }}在过去2分钟传输错误率大于0.01,当前值:{{ $value }}"
  - alert: HostNetworkInterfaceSaturated
    expr: (rate(node_network_receive_bytes_total{device!~"^tap.*"}[1m]) + rate(node_network_transmit_bytes_total{device!~"^tap.*"}[1m])) / node_network_speed_bytes{device!~"^tap.*"} > 0.8 < 10000
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "异常网络接口饱和,实例:{{ $labels.instance }}"
      description: "网卡{{ $labels.device }}正在超载,当前值{{ $value }}"
  - alert: HostConntrackLimit
    expr: node_nf_conntrack_entries / node_nf_conntrack_entries_limit > 0.8
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "异常连接数,实例:{{ $labels.instance }}"
      description: "连接数过大,当前连接数:{{ $value }}"
  - alert: HostClockSkew
    expr: (node_timex_offset_seconds > 0.05 and deriv(node_timex_offset_seconds[5m]) >= 0) or (node_timex_offset_seconds < -0.05 and deriv(node_timex_offset_seconds[5m]) <= 0)
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "异常时钟偏差,实例:{{ $labels.instance }}"
      description: "检测到时钟偏差,时钟不同步。值为:{{ $value }}"
  - alert: HostClockNotSynchronising
    expr: min_over_time(node_timex_sync_status[1m]) == 0 and node_timex_maxerror_seconds >= 16
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "时钟不同步,实例:{{ $labels.instance }}"
      description: "时钟不同步"
  - alert: NodeFileDescriptorLimit
    expr: node_filefd_allocated / node_filefd_maximum * 100 > 80
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "预计内核将很快耗尽文件描述符限制"
      description: "{{ $labels.instance }}}已分配的文件描述符数超过了限制的80%,当前值为:{{ $value }}"
EOF
bash 复制代码
#检查配置文件是否有问题
docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml

# 重载配置
curl -X POST http://localhost:9090/-/reload

看到告警规则即成功。

相关推荐
鱼饼6号7 小时前
Prometheus 上手指南
linux·运维·centos·prometheus
Alone80461 天前
prometheus概念
prometheus
研究司马懿3 天前
【云原生监控】Prometheus监控系统
云原生·prometheus·监控系统·promesql·企业级监控·二进制部署
wgc891783 天前
监控系列之-Grafana面板展示及制作
grafana
豆瑞瑞3 天前
压测服务器并使用 Grafana 进行可视化
grafana
研究司马懿3 天前
【云原生监控】Prometheus之PushGateway
云原生·prometheus·云原生监控·企业级监控系统·promesql
Aray12344 天前
Using Prometheus+Grafana+VMware-exporter to monitor VMware EXSi Hosts and VMs
grafana·prometheus
行走的山峰4 天前
在grafana上配置显示全部node资源信息概览
grafana
一瓢一瓢的饮 alanchan4 天前
【运维监控】系列文章汇总索引
java·运维·kafka·grafana·prometheus·influxdb·运维监控
MarkHD5 天前
Prometheus+grafana+kafka_exporter监控kafka运行情况
kafka·grafana·prometheus