在现代IT运维中,对服务器和基础设施进行有效监控是保障系统稳定运行的关键。对于使用Ubuntu作为操作系统的用户来说,掌握一些基础的Ubuntu监控工具不仅能帮助你及时发现潜在问题,还能提升整体运维效率。本文将从零开始,手把手教你如何搭建一套简单但实用的Linux服务器监控体系,即使是完全的新手也能轻松上手。
所谓基础设施监控,就是对服务器的CPU、内存、磁盘、网络等关键资源进行持续观测,以便在异常发生前预警或快速定位故障。例如:
Ubuntu系统本身就内置了许多实用的监控命令,无需额外安装即可使用:
$ top# 如果未安装htop(更友好的界面),可执行:$ sudo apt update$ sudo apt install htop$ htop
$ df -h # 查看所有磁盘分区使用情况$ du -sh /var/log # 查看指定目录大小
$ sudo apt install iftop$ sudo iftop$ sudo apt install nethogs$ sudo nethogs
对于生产环境,建议部署一套完整的监控栈。这里推荐开源组合:Prometheus(数据采集)+ Node Exporter(指标暴露)+ Grafana(可视化)。这套方案是当前最流行的Linux服务器监控架构之一。
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz$ tar xvfz node_exporter-1.7.0.linux-amd64.tar.gz$ sudo mv node_exporter-1.7.0.linux-amd64/node_exporter /usr/local/bin/$ rm -rf node_exporter-1.7.0.linux-amd64*# 创建systemd服务$ sudo tee /etc/systemd/system/node_exporter.service <[Unit]Description=Node ExporterAfter=network.target[Service]User=nobodyExecStart=/usr/local/bin/node_exporter[Install]WantedBy=multi-user.targetEOF$ sudo systemctl daemon-reload$ sudo systemctl start node_exporter$ sudo systemctl enable node_exporter
启动后,访问 http://你的服务器IP:9100/metrics 即可看到原始指标数据。
$ sudo useradd --no-create-home --shell /bin/false prometheus$ cd /tmp$ wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz$ tar xvf prometheus-2.45.0.linux-amd64.tar.gz$ sudo mv prometheus-2.45.0.linux-amd64 /opt/prometheus# 编辑配置文件$ sudo nano /opt/prometheus/prometheus.yml
在配置文件中添加以下内容(假设Node Exporter运行在同一台机器):
scrape_configs: - job_name: 'node' static_configs: - targets: ['localhost:9100']
然后启动Prometheus:
$ sudo /opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml & $ sudo apt install -y apt-transport-https software-properties-common wget$ wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -$ echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list$ sudo apt update$ sudo apt install grafana$ sudo systemctl start grafana-server$ sudo systemctl enable grafana-server
完成后,访问 http://你的服务器IP:3000,默认账号密码为 admin/admin。添加Prometheus作为数据源(URL填 http://localhost:9090),然后导入官方提供的Node Exporter Dashboard(ID: 1860),即可看到漂亮的监控图表!
通过本文,你已经掌握了从基础命令到完整监控栈的多种Ubuntu监控工具使用方法。无论是临时排查问题,还是构建长期稳定的基础设施监控体系,这些技能都将为你提供强大支持。记住,监控不是目的,而是保障业务连续性和提升系统可靠性的手段。
关键词回顾:Ubuntu监控工具、系统性能监控、Linux服务器监控、基础设施监控
本文由主机测评网于2025-12-21发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/20251211032.html