00:00:00
加载中...
📊 Prometheus 监控命令速查表
本文最后更新于11 天前,其中的信息可能已经过时,如有错误请发送邮件到big_fw@foxmail.com

# Prometheus 命令速查表

> 来源: 尹正杰云原生视频 day14-18

## 一、Prometheus环境部署

### 1. 二进制部署

# 下载
wget https://github.com/prometheus/prometheus/releases/download/v2.53.4/prometheus-2.53.4.linux-amd64.tar.gz

# 解压
tar xf prometheus-2.53.4.linux-amd64.tar.gz -C /usr/local/

# 启动
cd /usr/local/prometheus-2.53.4.linux-amd64/
./prometheus

# 访问WebUI
http://IP:9090/

### 2. 脚本安装

# 下载脚本
wget http://192.168.14.253/Resources/Prometheus/Scripts/oldboyedu-install-prometheus-server-v2.53.4.tar.gz

# 安装
tar xf oldboyedu-install-prometheus-server-v2.53.4.tar.gz
./install-prometheus-server.sh i

# 卸载
./install-prometheus-server.sh r

## 二、Node Exporter部署

# 下载
wget https://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gz

# 解压运行
tar xf node_exporter-1.10.2.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/node_exporter-1.10.2.linux-amd64/
./node_exporter

# 访问指标
http://IP:9100/metrics

## 三、Prometheus配置文件

### prometheus.yml 示例

global:
  scrape_interval: 15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
    - static_configs:
        - targets:
          - 10.0.0.43:9093

rule_files:
  - "rules/*.yml"

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "node-exporter"
    metrics_path: "/metrics"
    static_configs:
      - targets: ["10.0.0.41:9100","10.0.0.42:9100","10.0.0.43:9100"]

### 热加载配置

curl -X POST http://IP:9090/-/reload

## 四、PromQL查询语句

### 1. 基础查询

# 精确匹配
node_cpu_seconds_total{instance="10.0.0.42:9100",cpu="1"}

# 正则匹配
node_cpu_seconds_total{instance="10.0.0.42:9100",mode=~"i.*"}

# 取反
node_cpu_seconds_total{instance="10.0.0.42:9100",cpu!="1"}

### 2. 常用监控查询

#### CPU使用率

(1 - sum(increase(node_cpu_seconds_total{mode="idle"}[1m])) by (instance) / sum(increase(node_cpu_seconds_total[1m])) by (instance)) * 100

#### 内存使用率

(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100

#### 磁盘使用率

(1 - node_filesystem_avail_bytes{fstype=~"ext4|xfs"} / node_filesystem_size_bytes{fstype=~"ext4|xfs"}) * 100

### 3. 常用函数

# rate: 计算增长率
rate(http_requests_total[5m])

# increase: 计算增量
increase(http_requests_total[1h])

# histogram_quantile: 分位值
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))

## 五、Grafana部署

# 安装依赖
apt-get install -y adduser libfontconfig1 musl

# 下载安装
wget https://dl.grafana.com/enterprise/release/grafana-enterprise_9.5.21_amd64.deb
dpkg -i grafana-enterprise_9.5.21_amd64.deb

# 启动
systemctl enable --now grafana-server

# 访问WebUI
http://IP:3000/
# 默认账号: admin/admin

## 六、Alertmanager部署

# 下载
wget https://github.com/prometheus/alertmanager/releases/download/v0.31.1/alertmanager-0.31.1.linux-amd64.tar.gz

# 解压
tar xf alertmanager-0.31.1.linux-amd64.tar.gz -C /usr/local/

# 启动
cd /usr/local/alertmanager-0.31.1.linux-amd64/
./alertmanager

# 访问WebUI
http://IP:9093/

## 七、常用Exporter

| Exporter | 端口 | 用途 |
|———-|——|——|
| node_exporter | 9100 | Linux系统监控 |
| mysqld_exporter | 9104 | MySQL监控 |
| redis_exporter | 9121 | Redis监控 |
| mongodb_exporter | 9216 | MongoDB监控 |
| nginx_exporter | 9113 | Nginx监控 |
| blackbox_exporter | 9115 | 黑盒监控 |
| cadvisor | 8080 | 容器监控 |
| pushgateway | 9091 | 自定义监控 |

## 八、Grafana Dashboard ID

| 模板ID | 说明 |
|——–|——|
| 8919 | Node Exporter监控 |
| 1860 | Node Exporter Full |
| 14057 | MySQL监控 |
| 16504 | MongoDB监控 |
| 11835 | Redis监控 |

## 九、Prometheus数据类型

| 类型 | 说明 |
|——|——|
| Gauge | 当前值,可增可减 |
| Counter | 累计值,只增不减 |
| Histogram | 直方图,分布统计 |
| Summary | 摘要,分位值统计 |


来源: 尹正杰云原生视频 day14-18

文末附加内容
上一篇
下一篇