温馨提示×

ubuntu minimal系统监控与报警

小樊
50
2025-11-08 21:07:46
栏目: 智能运维

System Monitoring Tools for Ubuntu Minimal
Ubuntu Minimal systems require lightweight, efficient monitoring tools that minimize resource usage while providing critical insights. Below are top choices categorized by type:

Command-Line Tools (Lightweight, No GUI Required)

  • top/htop: Real-time process monitoring with CPU/memory usage, process IDs, and runtime. htop (install via sudo apt install htop) offers a more intuitive interface with color-coded metrics and mouse support, while top is pre-installed and suitable for quick checks.
  • vmstat/iostat: System-wide resource statistics. vmstat (reports virtual memory, CPU, disk I/O) and iostat (focuses on disk I/O) are part of the sysstat package (install via sudo apt install sysstat). Use vmstat 1 for 1-second interval updates or iostat -x 1 for detailed disk metrics.
  • lm-sensors: Hardware sensor monitoring (temperature, voltage, fan speed). Install with sudo apt install lm-sensors, then run sensors-detect to configure sensors. Use sensors to view real-time readings (e.g., CPU temperature).
  • netdata: Real-time web-based monitoring with minimal overhead. Install via bash <(curl -Ss https://my-netdata.io/kickstart.sh), then access http://localhost:19999 in a browser. Provides dashboards for CPU, memory, disk, network, and processes.

Graphical Tools (Requires Desktop Environment)

  • Conky: Highly customizable desktop widget for system stats (CPU, memory, disk, network). Install with sudo apt install conky, then configure ~/.conkyrc to display metrics in the desktop corner. Lightweight and supports themes.
  • Indicator-SysMonitor: Panel applet for Ubuntu’s GNOME desktop. Shows CPU/memory usage in the top panel and allows custom commands (e.g., disk usage). Add via sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor, install with sudo apt install indicator-sysmonitor, and run from the application menu.

Script-Based Monitoring (Customizable, Automated)

For minimal systems, bash scripts with cron are ideal for scheduled checks. Below is a sample script to monitor CPU, memory, and disk usage, with alerts sent via email when thresholds are exceeded:

#!/bin/bash
# System Monitoring Script with Alert
CPU_THRESHOLD=80
MEMORY_THRESHOLD=85
DISK_THRESHOLD=90
EMAIL="your_email@example.com"

# Get current usage
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
MEMORY_USAGE=$(free | awk '/Mem/ {printf("%.1f", ($3/$2) * 100)}')
DISK_USAGE=$(df -h / | awk '/\// {print $(NF-1)}')
DISK_USAGE=${DISK_USAGE%\%} # Remove % sign

# Alert function
send_alert() {
    SUBJECT="System Alert: $1 Usage Exceeded Threshold"
    BODY="Current $1 usage: ${2}% (Threshold: ${3}%)\n\n$(date)"
    echo -e "$BODY" | mail -s "$SUBJECT" "$EMAIL"
}

# Check thresholds
if (( $(echo "$CPU_USAGE > $CPU_THRESHOLD" | bc -l) )); then
    send_alert "CPU" "$CPU_USAGE" "$CPU_THRESHOLD"
fi
if (( $(echo "$MEMORY_USAGE > $MEMORY_THRESHOLD" | bc -l) )); then
    send_alert "Memory" "$MEMORY_USAGE" "$MEMORY_THRESHOLD"
fi
if (( $DISK_USAGE > $DISK_THRESHOLD )); then
    send_alert "Disk" "$DISK_USAGE" "$DISK_THRESHOLD"
fi

Setup Instructions:

  1. Save the script as ~/monitor_system.sh.
  2. Make it executable: chmod +x ~/monitor_system.sh.
  3. Install mailutils for email alerts: sudo apt install mailutils.
  4. Add to cron for hourly checks: crontab -e and append 0 * * * * ~/monitor_system.sh.

This script checks CPU (threshold: 80%), memory (85%), and disk (90%) usage every hour. If any metric exceeds the threshold, it sends an email alert with the current usage and timestamp.

Advanced Tools (Scalable, Full-Featured)

For larger setups or advanced needs, consider these tools (may require more resources but offer comprehensive features):

  • Prometheus + Grafana: Prometheus collects metrics, Grafana visualizes them in dashboards. Install Prometheus with wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz, extract, and configure prometheus.yml to scrape system metrics. Grafana (install via sudo apt install grafana) connects to Prometheus for real-time dashboards.
  • Monit: Monitors services/processes and restarts them if they fail. Install with sudo apt install monit, configure rules in /etc/monit/conf.d/ (e.g., check process nginx with pidfile /var/run/nginx.pid), and start with sudo systemctl start monit.

Each tool caters to different needs—choose based on your system’s complexity and monitoring goals. For minimal systems, command-line tools and scripts are recommended to avoid unnecessary overhead.

0