在Linux上使用Grafana监控服务器性能,需要以下几个步骤:
安装Grafana和Prometheus首先,你需要在Linux服务器上安装Grafana和Prometheus。这里以Ubuntu为例:
# 添加Grafana官方仓库echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.listcurl https://packages.grafana.com/gpg.key | sudo apt-key add -# 添加Prometheus官方仓库echo "deb https://packages.cloud.google.com/apt prometheus-release main" | sudo tee -a /etc/apt/sources.list.d/prometheus.listcurl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -# 更新软件包列表sudo apt update# 安装Grafana和Prometheussudo apt install grafana prometheus配置Prometheus编辑/etc/prometheus/prometheus.yml文件,添加需要监控的服务器和指标。例如,监控本地服务器的CPU、内存和磁盘使用情况:
global: scrape_interval: 15sscrape_configs: - job_name: 'node_exporter' static_configs: - targets: ['localhost:9100']安装并配置Node ExporterNode Exporter是一个收集服务器性能指标的工具,可以与Prometheus一起使用。在Ubuntu上安装Node Exporter:
sudo apt install prometheus-node-exporter然后,编辑/etc/systemd/system/prometheus-node-exporter.service文件,将ExecStart行修改为:
ExecStart=/usr/bin/node_exporter --collector.systemd --collector.textfile.directory=/var/lib/prometheus/node-exporter保存文件后,重启Node Exporter服务:
sudo systemctl daemon-reloadsudo systemctl restart prometheus-node-exporter启动Grafana和Prometheus服务sudo systemctl start grafana-serversudo systemctl enable grafana-serversudo systemctl start prometheussudo systemctl enable prometheus配置Grafana打开浏览器,访问http://your_server_ip:3000,登录Grafana(默认用户名和密码都是admin)。
创建一个新的数据源,类型选择Prometheus,URL填写http://localhost:9090,然后保存。
接下来,你可以创建一个新的Dashboard,或者导入一个现有的Dashboard模板,以监控服务器性能。例如,你可以导入这个Dashboard模板来监控服务器的CPU、内存和磁盘使用情况。
完成以上步骤后,你就可以在Grafana中实时查看和监控Linux服务器的性能了。


