Liunx性能监控

场景:

  这几天项目做的差不多了,但是服务器经常崩掉,然后之前用 c# 写的性能监控模块也不准,于是放弃了。还是用别人的模块吧。这里我采用 node export + prometheus + grafana 的方案进行性能监控。

  prometheus 是系统监控和报警系统,它可以通过监控系统的性能数据,获取并存储数据(默认是两个小时的数据)。同类型的软件有 zabbix,但是我还没接触到。同时也有报警功能,可以配置告警规则,通过发送邮件,微信,钉钉等通知运维人员。它的 Exporter 就是用来收集数据的组件。node export 就是 prometheus 在 Liunx 下的组件。prometheus 自带可视化组件,但是不是很好用(本人没用过),于是大家都是用 Grafana 进行可视化的。

  grafana 是一个用于数据展示的可视化工具,它可以接入很多的数据源,并把数据源的内容进行展示。注意,它不能进行数据的采集(指从原数据采集),只能用于显示。它可以适配很多数据源,比如 Prometheus,Mysql,InfluxDB 等, 一般配合时序数据库 (正好项目中是 pg + timescaledb)。下图是部分可以做数据源的数据。

 

   然后它可以自己配置面板,配置里面的显示信息。也有官方网站上大家做好的模板,一般用大家做好的就够用了。下面讲一下怎么使用,针对的都是 Liunx(centos) 下的。

安装 node export:

  下载,安装

wget https://github.com/prometheus/node_exporter/releases/download/v1.1.1/node_exporter-1.1.1.linux-amd64.tar.gz

// 解压
tar -zxvf node_exporter-1.1.1.linux-amd64.tar.gz
mv node_exporter
-1.1.1.linux-amd64 /usr/local/node_exporter

  运行一下,去 http://localhost:9100 看看,这里:

./node_exporter

  

 

   点击 Metrics 就可以看它采集的数据了

  添加服务,设置启动:

// 创建
vim /usr/lib/systemd/system/node_exporter.service

// 内容如下
[Unit]
Description
=node_export
Documentation
=https://github.com/prometheus/node_exporter
After=network.target

[Service]
Type=simple
User
=prometheus
ExecStart
=/usr/local/bin/node_exporter
Restart
=on-failure
[Install]
WantedBy
=multi-user.target

// 启动
systemctl daemon-reload
systemctl start node_exporter.service

 

安装 prometheus:

  下载

wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz

  解压,并测试启动。这个.yml 是配置文件,可以配置多个数据源。要是需要配置 prometheus 的端口,加上:--web.listen-address=:1888

tar -xzvf prometheus-2.27.2.linux-amd64.tar.gz
./prometheus --config.file=prometheus.yml

  

 

   如果是这样,则说明运行成功。

  打开 localhost:9090 看一下:可以看到这里的 UP 状态就是已经成功获取到 node_export 的数据了。如果是 DOWN 的话,需要看看有什么问题。

 

 

  然后做一个服务,使它可以后台运行

// 创建 sh 脚本
 vi prometheus.sh

// 添加以下内容
#!/bin/bash
路径
/prometheus --web.enable-lifecycle --config.file= 路径 /prometheus.yml &>> 路径 /prometheus.log

// 修改权限
chmod 755 prometheus.sh

// 创建服务
vi /usr/lib/systemd/system/prometheus.service

// 添加内容:
[Unit]
Description
=Prometheus
Documentation
=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After
=network-online.target

[Service]
User=root
Group
=root
Type
=simple

启动脚本

ExecStart= 路径 /prometheus.sh

[Install]
WantedBy=multi-user.target

  记得 reload 一下 Unit,就可以使用了

systemctl daemon-reload

// 开机启动
systemctl enable prometheus.service

// 启动
systemctl start prometheus.service

// 关闭
systemctl stop prometheus.service

 

配置 Grafana:

  安装

// 下载
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.0.0-1.x86_64.rpm
sudo yum install grafana-enterprise-8.0.0-1.x86_64.rpm

  使用

// 启动
systemctl start  grafana-server

// 查看状态
systemctl status grafana-server

  默认会在 http://localhost:3000 显示界面,可以在 /etc/grafana/grafana.ini 文件中,配置端口信息等。默认用户,密码为 admin,进来之后要求改密码。

 

   添加数据源:

 

   这里 URL 填:http://localhost:9090,HTTP Method 选 Get,然后 Save and Test,如果有错的话,慢慢排查。看看能访问到吗,会不会是防火墙的问题

  然后在 create->import 中,导入相应的模板。可以去 https://grafana.com/grafana/dashboards/8919-1-node-exporter-for-prometheus-dashboard-cn-0413-consulmanager/,选一些模板,复制模板的 id,保存模板的 json 文件。

  填如 id,并且上传 json 文件,

 

   然后修改 Name, 选择数据源,最后 import 就可以了。

 

   大概的界面:

 

 

  本人水平有限,这里我就讲一下大概的使用,每个软件都很很多可以操作的空间,有不足之处请多指教。

参考文献:

  官方:

  https://grafana.com/

  https://github.com/prometheus/prometheus

  yml 配置信息:

  https://blog.csdn.net/qq_32486597/article/details/110383388

  node_server 数据意义:

  https://blog.csdn.net/m0_59207224/article/details/122342860

  介绍,安装

  https://blog.csdn.net/qq_34556414/article/details/113107269

  https://blog.csdn.net/A13581861752/article/details/124148803

  https://blog.csdn.net/cp3_zyh/article/details/124019043

  https://blog.csdn.net/qq_32415063/article/details/105607008

  https://blog.csdn.net/weixin_43258559/article/details/109767442