There are upcoming maintenance events which may impact our services. Learn more

How to Monitor Server Performance Print

  • 197

Monitoring your server’s performance is essential to ensure it runs smoothly and efficiently. By keeping an eye on resource usage (CPU, RAM, disk, and network), you can identify and resolve issues before they impact your applications or users. This guide will walk you through the tools and steps to monitor your server’s performance effectively.

Why Monitor Server Performance?

  • Prevent Downtime: Identify resource bottlenecks before they cause your server to crash.
  • Optimize Resource Usage: Ensure your server is using CPU, RAM, and disk space efficiently.
  • Improve User Experience: Keep your applications running smoothly for your users.
  • Plan for Scaling: Understand when you need to upgrade your server or add more resources.

Step 1: Use Built-in Linux Tools

Linux provides several built-in tools for monitoring server performance. Here are the most commonly used ones:

1. Check CPU and Memory Usage with top

The top command provides a real-time overview of your server’s resource usage.

  1. Run the top command:

    top

  2. Key metrics to watch:
    • %CPU: Percentage of CPU usage.
    • %MEM: Percentage of memory usage.
    • LOAD AVERAGE: Average system load over 1, 5, and 15 minutes.
  3. Press q to exit.

2. Check Disk Usage with df

The df command shows disk space usage for all mounted filesystems.

  1. Run the df command:

    df -h

  2. Key metrics to watch:
    • Size: Total disk space.
    • Used: Disk space used.
    • Avail: Disk space available.

3. Check Network Usage with iftop

The iftop tool monitors network bandwidth usage in real-time.

  1. Install iftop (if not already installed):

    sudo apt install iftop (Debian/Ubuntu)

    sudo yum install iftop (CentOS)

  2. Run iftop:

    sudo iftop

  3. Key metrics to watch:
    • TX: Outgoing traffic.
    • RX: Incoming traffic.
  4. Press q to exit.

Step 2: Set Up Monitoring Tools

For more advanced monitoring, you can use dedicated tools that provide detailed insights and alerts.

1. Install and Configure htop

htop is an interactive process viewer that provides a more user-friendly interface than top.

  1. Install htop:

    sudo apt install htop (Debian/Ubuntu)

    sudo yum install htop (CentOS)

  2. Run htop:

    htop

  3. Use the arrow keys to navigate and F10 to exit.

2. Set Up Glances for Real-Time Monitoring

Glances is a cross-platform monitoring tool that provides a comprehensive overview of your server’s performance.

  1. Install Glances:

    sudo apt install glances (Debian/Ubuntu)

    sudo yum install glances (CentOS)

  2. Run Glances:

    glances

  3. Key metrics to watch:
    • CPU, Memory, Disk I/O, and Network usage.
  4. Press q to exit.

3. Use Prometheus and Grafana for Advanced Monitoring

For long-term monitoring and visualization, you can use Prometheus to collect metrics and Grafana to create dashboards.

  1. Install Prometheus and Grafana:

    Follow the official installation guides for Prometheus and Grafana.

  2. Configure Prometheus to scrape your server’s metrics.
  3. Set up Grafana dashboards to visualize the data.

Step 3: Set Up Alerts

To proactively address issues, set up alerts for high resource usage or downtime. Since email functionality may not be available on your server, we’ll use log-based alerts and webhooks for notifications.

1. Use cron for Log-Based Alerts

You can use cron to schedule scripts that check resource usage and log alerts to a file.

  1. Create a script to check CPU usage:

    nano check_cpu.sh

    Add the following content:

    #!/bin/bash
    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
    if (( $(echo "$CPU_USAGE > 90" | bc -l) )); then
        echo "$(date): High CPU usage: $CPU_USAGE%" >> /var/log/cpu_alert.log
    fi
    
  2. Make the script executable:

    chmod +x check_cpu.sh

  3. Add the script to cron to run every 5 minutes:

    crontab -e

    Add the following line:

    */5 * * * * /path/to/check_cpu.sh

2. Use Webhooks for Real-Time Alerts

If you use a messaging platform like Slack or Discord, you can send alerts to a webhook.

  1. Create a webhook URL for your Slack or Discord channel.
  2. Modify the script to send alerts to the webhook:

    nano check_cpu_webhook.sh

    Add the following content:

    #!/bin/bash
    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
    if (( $(echo "$CPU_USAGE > 90" | bc -l) )); then
        MESSAGE="High CPU usage detected: $CPU_USAGE%"
        curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$MESSAGE\"}" YOUR_WEBHOOK_URL
    fi
    
  3. Make the script executable:

    chmod +x check_cpu_webhook.sh

  4. Add the script to cron to run every 5 minutes:

    crontab -e

    Add the following line:

    */5 * * * * /path/to/check_cpu_webhook.sh

Conclusion

Monitoring your server’s performance is crucial for maintaining uptime, optimizing resources, and providing a great user experience. By using the tools and techniques outlined in this guide, you can stay on top of your server’s health and address issues before they become critical.


Was this answer helpful?

« Back