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采

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.触发器设置

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

#检查配置文件是否有问题
docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml

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

 看到告警规则即成功。

  • 18
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你!要在Linux上搭建PrometheusGrafana,你可以按照以下步骤进行操作: 1. 首先,确保你已经在Linux服务器上安装了Docker和Docker Compose。如果没有安装,可以参考官方文档进行安装。 2. 创建一个新的目录来存放PrometheusGrafana的配置文件。进入该目录并创建一个名为docker-compose.yml的文件。 3. 打开docker-compose.yml文件,并添加以下内容: ```yaml version: '3' services: prometheus: image: prom/prometheus volumes: - ./prometheus:/etc/prometheus command: - --config.file=/etc/prometheus/prometheus.yml ports: - 9090:9090 grafana: image: grafana/grafana ports: - 3000:3000 ``` 上述配置将创建两个服务:PrometheusGrafanaPrometheus服务将监听9090端口,Grafana服务将监听3000端口。 4. 在同一目录下创建一个名为prometheus目录,用于存放Prometheus的配置文件。进入prometheus目录,并创建一个名为prometheus.yml的文件。 5. 打开prometheus.yml文件,并添加以下内容: ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] ``` 上述配置将告诉Prometheus每隔15秒抓取一次本地主机的metrics信息。 6. 返回到docker-compose.yml文件所在目录,并在终端中运行以下命令启动PrometheusGrafana容器: ``` docker-compose up -d ``` 这将在后台启动容器。 7. 等待一段时间,然后在浏览器中访问http://服务器IP:3000,会看到Grafana的登录页面。 8. 使用默认用户名(admin)和密码(admin)登录Grafana。 9. 在Grafana中,点击左侧面板上的"Configuration"选项,然后选择"Data Sources"。 10. 点击"Add data source"按钮,选择"Prometheus"作为数据源类型。 11. 在"HTTP"选项卡中,输入Prometheus服务的URL,即http://localhost:9090。 12. 点击"Save & Test"按钮,确保连接正常。 现在,你已经成功搭建了PrometheusGrafana。你可以开始创建仪表盘和监控指标了。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值