Kapacitor & InfluxDB: Professional Automation of Alerts and Time-series Data Processing

Monitoring tutorial - IT technology blog
Monitoring tutorial - IT technology blog

The Problem: Don’t Turn Your Database Into a “Dead Warehouse”

3 AM, the phone rings incessantly. A production server suddenly goes down due to a full disk. Checking the logs the next morning, I realized InfluxDB had recorded the disk usage spike starting 4 hours prior. However, because I was only focused on storage without any proactive monitoring mechanism, I was completely unaware of the brewing crisis.

Before discovering Kapacitor, I used to run Python scripts via cronjobs every 5 minutes to query InfluxDB and check thresholds. This method was extremely resource-intensive and always had high latency. Kapacitor was created to end this struggle. It helps you process data as soon as it enters the database and triggers immediate response actions.

What is Kapacitor?

In the TICK Stack (Telegraf, InfluxDB, Chronograf, Kapacitor), Kapacitor acts as the “central processing station.” If InfluxDB is the storage, Kapacitor is the analytical brain. This tool excels in three areas:

  • Stream Processing: Monitors real-time data streams and alerts immediately.
  • Batch Processing: Runs complex computational queries on large volumes of historical data.
  • Diverse Outputs: Sends alerts via Telegram, Slack, Email, or calls Webhooks to automatically scale-up servers.

Compared to Prometheus’s Alertmanager, Kapacitor is significantly more flexible thanks to TICKscript, which allows you to write complex alerting logic like a true developer.

Step 1: Installing InfluxDB and Kapacitor on Ubuntu

In this guide, I am using Ubuntu 22.04 and InfluxDB version 1.8 to ensure the highest stability with traditional TICKscript.

# Add the official repository from InfluxData
wget -q https://repos.influxdata.com/influxdata-archive_compat.key
echo '393e8779c8945d31955614b03973f3510af1022fe4440455b2395634179739a5 influxdata-archive_compat.key' | sha256sum -c && cat influxdata-archive_compat.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null
echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main' | sudo tee /etc/apt/sources.list.d/influxdata.list

# Quickly install the InfluxDB & Kapacitor combo
sudo apt update && sudo apt install influxdb kapacitor -y

# Enable system services
sudo systemctl enable --now influxdb
sudo systemctl enable --now kapacitor

Type the command kapacitor version to check. If the terminal returns a version (e.g., 1.6.x), you have successfully installed it.

Step 2: Configuring the Connection

By default, Kapacitor looks for InfluxDB at localhost:8086. If you have moved the database to a separate server, edit the following configuration file:

sudo nano /etc/kapacitor/kapacitor.conf

Find the [[influxdb]] block and update the correct URL:

[[influxdb]]
  enabled = true
  urls = ["http://127.0.0.1:8086"]
  timeout = "0s"

After saving, restart the service: sudo systemctl restart kapacitor.

Step 3: Writing a TICKscript for CPU Alerts

Let’s create a script to monitor CPU metrics. We will configure the system to log an entry if the average CPU usage exceeds 80% within a 1-minute window.

Create a file named cpu_alert.tick:

stream
    |from()
        .database('telegraf')
        .retentionPolicy('autogen')
        .measurement('cpu')
        .where(lambda: "cpu" == 'cpu-total')
    |window()
        .period(1m)
        .every(1m)
    |mean('usage_user')
    |alert()
        .crit(lambda: "mean" > 80)
        .log('/tmp/cpu_alerts.log')

The mechanism of this script is quite simple. The window() command helps group data over 60 seconds to calculate the average. This is crucial because it helps eliminate momentary CPU “spikes” that could cause false alarms.

Step 4: Deploying the Task

To get the script running, you need to load it into Kapacitor through two steps: define and enable.

# Define the new task
kapacitor define cpu_high_alert -tick cpu_alert.tick -dbrp telegraf.autogen

# Enable the task
kapacitor enable cpu_high_alert

Check the status using the command kapacitor show cpu_high_alert. You will see a text-based graph showing the number of data points being processed through each node.

Step 5: Connecting Telegram for Instant Notifications

Logging to a file isn’t enough. To receive alerts directly on your phone, open /etc/kapacitor/kapacitor.conf and find the [telegram] section:

[telegram]
  enabled = true
  url = "https://api.telegram.org/bot"
  token = "123456789:ABCDefGhIJK..." # Token from BotFather
  chat-id = "987654321"

Update your TICKscript file at the alert node:

    |alert()
        .crit(lambda: "mean" > 80)
        .telegram()

Then, rerun the command kapacitor define cpu_high_alert -tick cpu_alert.tick so Kapacitor updates with the new configuration.

Field Experience from a 500+ Node System

After years of operating large-scale IoT and monitoring systems, I’ve gathered 3 important takeaways:

  1. Prioritize Batch for Big Data: If you have thousands of sensors sending data every second, using stream will consume all your RAM. Switch to batch to query data periodically every 5-10 minutes.
  2. Deadman’s Switch: This is a “lifesaver” feature. If a device suddenly goes silent (network loss, power failure), Kapacitor will detect the lack of data and alert you immediately.
  3. Check Retention Policy: Tasks will never run if you declare the wrong database or retention policy in TICKscript. Always double-check these parameters with InfluxDB.

Conclusion

Kapacitor is not just an alerting tool; it is a powerful data processing engine. Combining InfluxDB and Kapacitor helps you shift from a reactive to a fully proactive stance. Your system will no longer just store data but will be able to think and react to incidents on its own.

Share: