Here at balena, we have developed a complete set of tools to deploy and manage your fleet of IoT devices, but what if you also want a monitor detailed metrics of a single device or of your entire fleet? That’s when Datadog comes into play as a great complement to your balenaCloud setup.
Datadog is software as a service (SaaS) that enables you to gather a wide range of metrics about your system and display it all in one place. You can create multiple dashboards to help your monitor your devices and create alarms for different events, such as when a device goes offline, or if a value (temperature, bandwidth, CPU usage, etc.) passes a defined threshold.
In this blog post, we will walk you through the steps of setting up and configuring an application with balenaCloud and Datadog to deploy a fleet of two Raspberry Pi 3 devices, although this project can be scaled to any number of devices.
To get started, you will need accounts for both balenaCloud and Datadog. If you are new to the balena ecosystem, we highly recommend checking the getting started guide.
Create a new balenaCloud project and flash the SD Card
After registering for a balenaCloud account, go ahead and create a new application. For this tutorial, we will create one called datadogMonitor
.
This will take you to the dashboard for your newly created application, where you can move on to the next step and add your device. As we are going to use a fleet of Raspberry Pi 3’s for this project, let’s go ahead and download the image for that device.
With the image downloaded to your computer, we need to flash it to an SD Card that will be inserted into the Raspberry Pi. For that, we will use balenaEtcher.
Once you are done flashing the device, insert it in the Raspberry Pi and power on your device.
When the device boots for the first time, it connects automatically to balenaCloud, and you’ll be able to see it listed as online on the dashboard and move onto the next step.
Troubleshooting: It should only take a few minutes for the new device to appear in your dashboard, If your device still hasn’t shown up on your dashboard after a few minutes, something has gone wrong. There’s an extensive troubleshooting guide in the documentation, with lots of information on why this could be, but if you still can’t get your device online, come on over to the forums where we’ll be able to help out.
Create a datadog account
The next step is to create an account on Datadog and install the datadog-agent
. Note that you can skip the 2nd step as we are not defining a specific software stack for our project, and move directly to step 3, the agent setup.
On 3. Agent Setup, select From Source
, and select and save the DATADOG_API_KEY
. You will need it for the next step. Keep this page open and move to the next step.
Configure environment variables and push the code to the Raspberry Pi
While Datadog is waiting for the agent to report back, let’s jump back to the balenaCloud dashboard to finish the configuration process.
In order to visualize all devices inside our datadogMonitor
application in one single Datadog instance, we need to add the API key variable as an Application Environment Variable, which makes it available to all devices in that application. From your Application, click on Environment Variables
and add a variable called DATADOG_API_KEY
with the value from the API key that we saved from the Datadog setup.
Now that balenaCloud is configured and your devices are showing up in the dashboard, it’s time to push the project code, which can be downloaded from GitHub at https://github.com/balena-io-playground/balena-datadog. Go to the page and download or clone the repository to your computer.
There are two ways to push the code from your computer to the Raspberry Pi, via balena-cli or git push. If you already have (or can setup) npm on your machine, this is most likely the easiest way to get the CLI tools up and running quickly. However, there are also standalone binaries for Windows, macOS, and Linux available.
The documentation for the CLI tools is the best place to start and covers the installation and setup of both the npm package and the standalone binaries. This is also the method we will use in this tutorial, but if you would rather use git to push the code, you can follow the getting started guide to setup your environment.
When you have the CLI installed and working, the first step is to log in to balenaCloud by issuing the balena login
command:
Once you’ve reached this point, and have a working CLI which has been logged in to your account, you’re ready to start pushing code to your Raspberry Pi.
From within the project directory, execute balena push <appName>
, where appName is the application name you set back at the beginning of the guide. For example: balena push datadogMonitor
. With this single command, you can push the code to balenaCloud, which will build the Docker image and handle the process of setting it up and running it on your device.
After a few seconds you should see the 🦄:
Now head back to the balenaCloud dashboard and you will be able to follow the progress of downloading and installing the containers inside your device, as displayed in the image below:
This sample project is a multi-container application, including the simple-server-python project to create an HTTP server with the Datadog monitoring agent.
After the containers have finished downloading, they will automatically start, running the Flask server and the Datadog agent.
If you have done everything correctly you can now go back to the Datadog dashboard, and you will see that the devices are now showing up! 🎆🎆🎆
Now visit Infrastructure List and you will be able to view all devices connected to this datadog instance. Right now we are connecting two Raspberry Pi 3 devices, so you will be able to see two devices connected.
Note that the hostname
displayed on datadog matches the UUID
on the balenaCloud dashboard, which makes it straightforward to connect devices between the services.
💥 Boom, everything is ready to go and now you can take advantage of datadog to help you monitor your device fleet. On the infrastructure list
page, you can click on each device and it will open a dashboard showing live charts so that you can track anything happening on the system.
Monitoring multiple devices and triggers
An important feature that many users request is a way to monitor that the devices are online, and if in case of downtime, be notified, for example, by email. Let’s go ahead and create a new monitor and trigger by going to https://app.datadoghq.com/monitors#/create.
Step One, select All Monitored Hosts
so that we can monitor the entire fleet of devices with that API key.
Step Two you can leave as is with Check Alert
checked.
Step Three, you can enter a customized message in this field, which can be written in markdown.
Step Four, add your account name or email address in this field and you will receive an email every time this monitor changes state (device goes offline or online).
Click Save
and you can now monitor all the devices in a timeline bar, get notifications if any devices go offline and also check the overall system uptime.
In this post, we have setup a sample project connecting balenaCloud with Datadog to monitor a fleet of devices and track metrics such as device uptime. We’re only scratching the surface of what Datadog can do and how it can help you manage your IoT project in conjunction with balenaCloud, so let us know what else you come up with!
If you have any questions or feedback, or have built something based on this project, we’d love to see you share your work over in our forums! Happy developing!