appsignal

Monitoring CPU and Memory on Your VPS with AppSignal

Muhammed Ali

Muhammed Ali on

Monitoring CPU and Memory on Your VPS with AppSignal

Most of us run multiple virtual private servers (VPS) at a time. That’s why it’s important to keep an eye on the CPU usage and memory. However, since this step often slips our minds, there is room for automated monitoring. Open-source tools tend to be a default choice, and for a good reason. The problem is that they don't provide everything you need for monitoring in a single place. As a result, you may find yourself writing custom shell scripts for automation.

In this article, we’ll start by exploring why monitoring system metrics is important. Then, we’ll introduce AppSignal as a solution for tracking CPU and memory usage on your VPS.

AppSignal is an all-in-one tool that keeps an eye on the performance of an application and its home (the host). With it, you can analyze application performance, track errors, and monitor the host. You can plug AppSignal into your VPS and let it track your CPU, memory, disk performance and usage, load, and other useful stats.

In this guide, we’ll focus on its host monitoring capabilities specifically. To follow along, you need to have at least one VPS running.

Why Do You Need to Monitor Your Infrastructure?

The reasons why you may want to monitor your VPS infrastructure include:

  • Reducing infrastructure cost: Resources are limited and expensive to purchase, and many teams don’t actually have room for excess, except when they are part of a well-funded company. Even then, you’d still need to manage resources efficiently. With the insights you get from monitoring, you can identify resources such as servers with far more RAM or CPU than the application requires and scale them down to cheaper configurations.
  • Preventing performance bottlenecks: Using your CPU and memory to their full capacity would lead to performance bottlenecks and perhaps even downtime. By checking your resources, you can make sure they're in optimal condition/state. Plus, you will avoid issues that may arise down the line.
  • Efficient resource usage: You might also encounter situations where services that should work perfectly on just 1GB of RAM aren't running at all. That’s why it’s important to make sure CPU and RAM usage are well-optimized and no resources are wasted. By doing this, you can tune your application or resource allocation to fit the service’s actual needs without unnecessary waste.
  • Locating the source of an issue: Thanks to AppSignal’s unified platform, you can view host usage alongside errors and application performance in one place, which will allow you to identify issues quickly without digging through logs.

Advantages of Using AppSignal to Monitor Your VPS

As engineers, we tend to default to open-source options to solve problems. In this case, since we want to monitor CPU and memory, your mind may go straight to htop or Netdata. The problem with these tools, though, is that they mostly give you real-time data. They don’t provide any historical trends or intelligent alerting for proactive monitoring.

If you are looking for a well-rounded solution, AppSignal offers some advantages for monitoring VPS CPU and memory. For starters, it stores all its data in Europe, so compliance requirements are one less thing to worry about. In addition, with its amazing Time Detective feature, you get a snapshot of the relevant monitoring data at a specific moment in time. For example, you can open Time Detective on your app dashboard, click on the Hosts option, and see notable events as they occurred, minute by minute.

Time detective on your app dashboard
Time detective on your app dashboard

Beyond RAM and CPU, you get access to other relevant resource metrics, like disk I/O, network traffic, etc. In practice, this means that the moment you install AppSignal alongside your app, you get real-time and historical CPU/RAM charts and dashboards without needing a separate agent or custom scripts.

Another major benefit is the correlation between infrastructure and application. Since APM and host metrics are unified here, you can see CPU and memory usage on the exact same timeline as your app’s performance metrics and errors. For example, a spike in memory use or CPU can be viewed alongside slow requests or errors. This kind of convenience helps you pinpoint whether a code change or a resource issue is the culprit.

Monitoring CPU and Memory on Your VPS

If you’re wondering how this all works, AppSignal’s use of OpenTelemetry might be worth mentioning.

Those of you who have been in DevOps for a while must have come across OpenTelemetry, which is a standardized way of collecting telemetry data like trace, metrics, and logs. By using the Node.js integration, AppSignal is able to support OpenTelemetry's tracing component.

In this section, you will learn how to monitor the CPU and memory on your VPS via AppSignal. We’ll run some examples using the Node.js package and Docker. If you are running your application inside Docker in the VPS, you can still access container host metrics. Check the documentation for the full list of the packages where this feature is available.

Using the Node.js Package

To get started, you need an Ubuntu VPS, as mentioned earlier. We will be working with a simple Express project.

On your VPS, create two files: server.js and appsignal.cjs. Then, paste the following code into the server.js file.

JavaScript
// server.js const express = require("express"); const { expressErrorHandler } = require("@appsignal/nodejs"); const app = express(); app.get("/", (req, res) => { res.send("Hello World"); }); // route that throws app.get("/error", (req, res) => { throw new Error("Something broke!"); }); // Send errors that occur in your Express application to AppSignal app.use(expressErrorHandler()); app.listen(3000, () => { console.log("Server running on port 3000"); });

This AppSignal configuration should be put in the appsignal.cjs file.

JavaScript
// appsignal.cjs const { Appsignal } = require("@appsignal/nodejs"); new Appsignal({ active: true, name: "test-node", });

Sign in to AppSignal and follow the steps below:

  • Create a new application using the Add app button.
  • Choose Node.js.
  • Fill in the App name.
  • Install Express.
  • Copy the export command and run on your VPS.
Appsignal API key
Appsignal API key

Now, on your VPS, run the following commands to install the AppSignal package and Express.

Shell
npm install @appsignal/nodejs express

After everything has been installed, you can run the application. Make sure to require appsignal.cjs before the app starts by running the following command:

Shell
node --require './appsignal.cjs' server.js

Next, you can simulate some traffic using this command:

Shell
npx autocannon -c 2 -d 300s <http://localhost:3000/>

Head over to your AppSignal dashboard and ensure you're in the Host metrics tab. You should see some basic metrics displayed:

Host metrics tab on AppSignal dashboard
Host metrics tab on AppSignal dashboard

Clicking on your host name (here, it's "vultr") will show you the metrics and usage in a clear graph that’s easy to understand.

Host metrics on AppSignal
Host metrics on AppSignal

Using Docker

For this, your application need to be running in Docker. While writing the configuration for your app, limits needs to be set for your monitored containers to ensure accurate metrics. In this case, since we want to monitor CPUs and memory, we have to configure the container to have a limited number of CPUs and a set amount of memory allocated to it.

Here is the Dockerfile we will be using:

Dockerfile
# Use a lightweight Node.js base image FROM node:20-alpine # Install build tools for native Node modules RUN apk add --no-cache python3 make g++ bash # Set working directory WORKDIR /app # Copy package files first (better caching) COPY package*.json ./ # Install dependencies RUN npm install # Copy application code COPY . . # Expose application port EXPOSE 3000 # Start the application # Start Node with AppSignal preloaded CMD ["node", "--require", "./appsignal.cjs", "server.js"]

Now, run the following command. Make sure to update it with your AppSignal push API key.

Shell
docker run -it --name express-app -p 3000:3000 --memory="512m" --cpus="1.0" -e APPSIGNAL_PUSH_API_KEY=<push-api-key> express-appsignal-app

Since the host metrics are collected by default, you don't need to do anything else. When you simulate traffic again, you will see you container host metrics.

Memory metrics for Docker
Memory metrics for Docker
CPU metrics for Docker
CPU metrics for Docker

Setup Alerts and Notifications

With AppSignal's Anomaly detection feature, you can set triggers that send notifications when a metric value goes over or dips below a stated value. For example, if you want to be notified when the memory or CPU consumption are above 90%, you can do that with this feature. This detection takes place on a minute-by-minute basis, so it’s very efficient.

Before we can set up alerts, we need to create the triggers that will initiate them first. For this example, we’ll create a trigger that sends an alert whenever CPU usage exceeds 80%. Go to your app navigation and click on the Anomaly Detection tab. Then, choose Triggers. You will see a button to add triggers, click on it.

Steps to add triggers for anomaly detection
Steps to add triggers for anomaly detection

Now, click CPU Usage under HOST METRICS. Then select the following options and save the trigger:

  • When: total_usage
  • Threshold is: more than, 80%
  • Tick "Default email notifier"

If you simulate enough traffic to get your CPU usage up to 80%, you will get this email:

Anomaly detection email
Anomaly detection email

As you can see in the image above, CPU usage went past our 80% threshold, and the alert was sent as expected.

Wrap-Up

In this tutorial, we started by explaining how AppSignal can help you monitor your host metrics. Then, we demonstrated how you can set it up on a barebone VPS and on a container running on a VPS. Finally, you learned how you can set triggers and alerts for certain thresholds.

Even though we just scratched the surface of what AppSignal is capable of in terms of monitoring, this tutorial is a good starting point. If you want to create a more rounded observability setup, you can build on the knowledge gained here by exploring AppSignal's other monitoring features.

Wondering what you can do next?

Finished this article? Here are a few more things you can do:

Muhammed Ali

Muhammed Ali

Muhammed is a Software Developer with a passion for technical writing and open source contribution. His areas of expertise are full-stack web development and DevOps.

All articles by Muhammed Ali

Become our next author!

Find out more

AppSignal monitors your apps

AppSignal provides insights for Ruby, Rails, Elixir, Phoenix, Node.js, Express and many other frameworks and libraries. We are located in beautiful Amsterdam. We love stroopwafels. If you do too, let us know. We might send you some!

Discover AppSignal
AppSignal monitors your apps