How to Monitor VPS Resource Usage?

Hey there, fellow developer! Tired of battling slow VPS performance and mysterious errors? Spent hours searching for bottlenecks? Then you’ve come to the right place! In this article, we’ll explore how to effectively monitor your virtual server’s resources, avoiding headaches and frustration. Trust me, I’ve been there… many times. Here you’ll find practical tips, real commands, and even a little humor (because it’s essential!). Get ready, it’s going to be hot!

CPU Monitoring
How to Monitor VPS Resource Usage? - CPU Load Graph

The CPU is the heart of your VPS. If it’s malfunctioning, everything else struggles. Here are a few ways to check CPU load. The easiest way is to use the top command. Run it in the terminal, and you’ll see the real-time picture. Cool, right?

top

Pay attention to the «%CPU» column. If the value consistently stays at 90% or higher, it’s time to sound the alarm! You’ve clearly overloaded the server. To understand which process is consuming resources, use htop – it’s a more user-friendly interactive version of top. You can even kill processes directly from the interface there!

sudo apt update && sudo apt install htop
htop

Another useful tool is mpstat. It provides more detailed statistics for each CPU core. This is especially important if you have a multi-core VPS.

VPS Hosting

Virtual servers with guaranteed resources

Choose VPS

sudo apt install sysstat
sudo mpstat -P ALL 1 5

(This command will output statistics for 5 seconds with a 1-second interval for all CPU cores). If you see that a core is constantly loaded at 100%, it means there’s a resource-intensive process that needs optimization or, as a last resort, termination.

Pro tip: Don’t forget about iostat for monitoring the disk subsystem; it can also significantly impact CPU performance!

RAM Monitoring
How to Monitor VPS Resource Usage? - RAM Usage Graph

RAM is random access memory, and its shortage is like an endless browser loading screen. Endlessly long and agonizingly irritating. To monitor RAM, use the free -h command. It will show you the total amount of memory, how much is used, how much is free, and how much is used for cache.

free -h

Pay attention to used and cache. If used is close to total, then there is not enough memory. If cache is very large, then the system is efficiently using the cache, and this is normal. But if used + cache are close to total, then there is little free memory.

For a more detailed analysis, you can use top or htop – they also show memory usage by processes. Sometimes you have to look for memory leaks in applications – this can be very painful, I know from personal experience…

Another useful command is vmstat. It shows statistics on virtual memory, swapping, and other parameters, helping to identify memory problems related to swapping (when the system starts using the hard drive as RAM — very slow!).

vmstat 1 5

(Outputs statistics 5 times with a 1-second interval). Pay attention to the si (swap in) and so (swap out) columns. High values indicate swapping problems.

Disk Space Monitoring

A full disk is a disaster. The server may suddenly stop working, and you’ll be sitting there kicking yourself. Therefore, regular monitoring is a must-have. The simplest command is df -h. It will show you disk space usage on all partitions.

df -h

If you see that a partition is almost full, immediately find the culprit! This could be logs, temporary files, or something else. The du -sh * command in the required directory will help determine what takes up the most space.

du -sh *

And here’s my favorite trick: ncdu – an interactive tool for visualizing disk space usage. It shows a hierarchical directory structure and helps quickly find «large» files and folders. Install it – you won’t regret it!

sudo apt install ncdu
ncdu

Don’t forget about regular log cleanup! logrotate is your friend. Configure it correctly, and you’ll have fewer problems with disk space filling up.

Network Traffic Monitoring

Network traffic is the lifeblood of your server. If it’s clogged, everything works slowly. For monitoring, use iftop. This is a cool interactive program that shows in real-time who is sending and receiving how much traffic.

sudo apt install iftop
sudo iftop

This command shows the activity of network interfaces. If you notice suspicious activity or a large amount of traffic from unknown sources, this is a reason to be wary. There may be a DDoS attack or malware.

For more in-depth analysis, you can use tcpdump, but you need to be careful with it; otherwise, you can fill the disk with log files and need to understand packets. It allows you to capture and analyze network traffic at a low level.

sudo tcpdump -i eth0 -nn -s 0 -w traffic.pcap

(Records traffic on the eth0 interface to the traffic.pcap file. Replace eth0 with the name of your interface.)

ss is another useful command. It shows the status of network connections. You can see which ports are used and who is connecting to whom. This is useful for diagnosing network problems and detecting suspicious connections.

ss -tulnp

Using Specialized Tools

Manually monitoring everything is tedious and inefficient. There are a lot of cool tools that do it for you! For example, Zabbix, Nagios, Prometheus, Grafana. They allow you to build beautiful graphs, configure alerts, and generally simplify life.

For example, Grafana is a dashboard for visualizing data. It works with various data sources, including Prometheus. This stack is *simply amazing*! You can create custom dashboards that track all the important metrics of your VPS.

Installing and configuring such tools may require certain knowledge and time, but believe me, it’s worth it. Once you spend the time on configuration, you will save it tenfold in the future.

Here’s a link to the Prometheus documentation: https://prometheus.io/docs/introduction/overview/ (Be sure to check it out!)

And here’s a link to the Grafana documentation: https://grafana.com/docs/ (Equally important!)

Setting Up Alerts

Reactive monitoring is not the right approach. Proactive – that’s what you need! Set up alerts – and you’ll know about problems immediately, not when it’s too late. This could be email, SMS, or Telegram.

Most monitoring tools (Zabbix, Nagios, etc.) allow you to configure alert conditions. For example, you can set up a notification if the CPU load exceeds 80%, or if the disk space drops below 10%.

Here is an example of configuring email notifications (this depends on the specific tool). In this example, we will assume that you are using monit:

set mailserver localhost
set mailfrom monit@example.com
set auth-method simple
set smtp-port 25
set smtp-username my_user
set smtp-password my_password

Remember to replace localhost, monit@example.com, my_user, and my_password with your actual mail server settings. Yeah, this part always trips people up, trust me on this one…

Check your settings regularly! Nothing works without testing. Run a test alert to make sure everything works as intended. Boom! That’s it!

In conclusion, VPS monitoring is not a one-time procedure, but a continuous process. Regularly check resources, configure alerts, and your server will work stably and reliably. No cap!

ToolDescriptionProsCons
topBasic monitoring toolEasy to useNot very visual
htopInteractive topMore convenient interfaceRequires installation
ZabbixMonitoring systemMultifunctionalComplex configuration
PrometheusMonitoring systemScalable, flexibleRequires knowledge