Nodejs|PM2增加Prometheus监控


前面介绍了PM2的基本使用,也提到通过pm2 list命令可以很方便查看应用的CPU和内存的使用情况,但这是个瞬间值,怎么保存下来呢?这就要借助普罗米休斯了


$ pm2 list
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 1  │ time               │ fork     │ 1    │ online    │ 0%       │ 6.6mb    │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

我们知道Prometheus的监控,需要exporter,今天要用到的叫pm2-prometheus-exporter
地址: https://github.com/saikatharryc/pm2-prometheus-exporter

安装很简单

pm2 install pm2-metrics
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 1  │ time               │ fork     │ 1    │ online    │ 0%       │ 6.6mb    │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
Module
┌────┬────────────────────┬──────────┬──────────┬──────────┐
│ id │ name               │ status   │ cpu      │ mem      │
├────┼────────────────────┼──────────┼──────────┼──────────┤
│ 2  │ pm2-metrics        │ online   │ 0%       │ 43.4mb   │
└────┴────────────────────┴──────────┴──────────┴──────────┘

然后访问测试

curl 0.0.0.0:9209/metrics
# HELP pm2_up Is the process running
# TYPE pm2_up gauge
pm2_up{id="1",name="time",instance="0",interpreter="python",node_version="undefined"} 1
pm2_up{id="2",name="pm2-metrics",instance="0",interpreter="node",node_version="13.12.0"} 1

# HELP pm2_cpu Process cpu usage
# TYPE pm2_cpu gauge
pm2_cpu{id="1",name="time",instance="0",interpreter="python",node_version="undefined"} 0
pm2_cpu{id="2",name="pm2-metrics",instance="0",interpreter="node",node_version="13.12.0"} 0.2

# HELP pm2_memory Process memory usage
# TYPE pm2_memory gauge
pm2_memory{id="1",name="time",instance="0",interpreter="python",node_version="undefined"} 6930432
pm2_memory{id="2",name="pm2-metrics",instance="0",interpreter="node",node_version="13.12.0"} 47222784

# HELP pm2_uptime Process uptime
# TYPE pm2_uptime gauge
pm2_uptime{id="1",name="time",instance="0",interpreter="python",node_version="undefined"} 8741
pm2_uptime{id="2",name="pm2-metrics",instance="0",interpreter="node",node_version="13.12.0"} 85

# HELP pm2_instances Process instances
# TYPE pm2_instances gauge
pm2_instances{id="1",name="time",instance="0",interpreter="python",node_version="undefined"} 1
pm2_instances{id="2",name="pm2-metrics",instance="0",interpreter="node",node_version="13.12.0"} 1

# HELP pm2_restarts Process restarts
# TYPE pm2_restarts gauge
pm2_restarts{id="1",name="time",instance="0",interpreter="python",node_version="undefined"} 1
pm2_restarts{id="2",name="pm2-metrics",instance="0",interpreter="node",node_version="13.12.0"} 0
...

配置Prometheus

vi prometheus.yaml
- job_name: pm2-metrics
  scrape_interval: 10s 
  scrape_timeout: 10s 
  metrics_path: /metrics
  scheme: http
  static_configs:
  - targets:
    - 192.168.1.24:9209
    labels:
      group: pm1_pro
      instance: pm1

配置Granfa

Granfa界面是自己配的,获取在公众号直接回复: pm2-granfa
获取到json文件,直接导入到granfa即可

pm2-prometheus


相关博文

About rainbird

IOS攻城狮
This entry was posted in NodeJS and tagged , , , , , , , . Bookmark the permalink.

发表评论