心梦后花园

Back

之前使用Netdata监控服务器各项数据,但是完整版需要收费,在免费试用到期后换成 Grafana + Prometheus 做数据采集和可视化,下文介绍在Archlinux下配置教程

文件配置#

我配置了除系统信息配置外还有 Postgresql Systemd Docker 的图表,需要安装不同的 exporter 实现。先安装主要的程序:`

pacman -S prometheus grafana prometheus-node-exporter alertmanager
bash

上面的软件包含了数据源、图表展示,但是支持的数据只有基本的如CPU、内存、磁盘能的数据,我自己还安装了额外的 exporter ,可以使用 yay Prometheus 命令查看支持的,我自己安装的有 prometheus-postgres-exporterprometheus-systemd-exporter

各个软件需要修改的配置文件:

  • Prometheus(/etc/prometheus/prometheus.yml):

修改完可以启动 prometheus.service,启动后访问 http://127.0.0.1:9090 会有一个简陋的图表界面。

  • alertmanager(/etc/alertmanager/alertmanager.yml):

启动 alertmanager.service服务。

DATA_SOURCE_URI=":5432?host=/var/run/postgresql&sslmode=disable"
DATA_SOURCE_USER="postgres"
POSTGRES_EXPORTER_ARGS="--collector.postmaster"
yaml

同时因为我需要监控 docker容器,用到了 cAdvisor,但是 aur的包过期了,所以使用了 docker-compose.yml部署:

使用 docker-compose -f ./docker-compose.yml up -d 启动容器。

图表展示#

上节是数据采集方面的配置,现在需要启动图表展示 grafana服务,默认使用3000端口,如果端口被占用可以修改配置文件(/etc/grafana.ini)里的 http_port = 3300,修改为你需要的端口,然后启动服务。

打开网址使用账号密码都是admin/admin登录,第一次登录会让你修改密码。设置完密码需要添加数据源 添加prometheus数据源1

添加prometheus数据源2

设置完保存即可,然后就可以设置展示图表了。

图表

可以去(https://grafana.com/grafana/dashboards/)搜索面板,使用URL导入,或者使用面板id导入,推荐两个id:1860和193,效果图如下:

1860

193

关于 alertmanager 通知需要设置通知规则,我暂时只配置了一个,用来通知 systemd service失败退出的邮件,配置需要跟 prometheus配置同个目录,/etc/prometheus/alert.rules.yml:

groups:
- name: systemd_unit
  interval: 15s
  rules:
    - alert: systemd_unit_failed
      expr: |
        systemd_unit_state{state="failed"} > 0
      for: 3m
      labels:
        severity: critical
      annotations:
        description: 'Instance : Service failed'
        summary: 'Systemd unit failed'
yaml

同时需要修改 /etc/prometheus/prometheus.yml:

rule_files:
  - "alert.rules.yml"
  # - "first_rules.yml"
  # - "second_rules.yml"
yaml

如果不想使用包管理器安装软件,可以全部使用 docker,网上找了一个一整套性能,容器监控、日志收集和聚合、可视化展示和消息通知外加容器的自动更新,没有测试,需要的可以按需修改:

参考:

https://linux.do/t/topic/254125

https://wiki.eryajf.net/pages/2475.html

https://wiki.archlinux.org/title/Prometheus

https://www.cnblogs.com/ygbh/p/17306539.html

在Archlinux配置Prometheus和Grafana Dashboard
https://www.liyp.cc/blog/configure-prometheus-grafana-dashboard-arch-linux
Author 万古一梦
Published at 2024年11月9日
Comment seems to stuck. Try to refresh?✨