Discord & TG Alert System

This Validator Monitoring Bot integrates with Prometheus to monitor the key metrics of your Berachain validator. This bot can automatically alert you via Discord or Telegram when there are issues.

Validator Monitoring Bot for Berachain

Introduction

This guide walks you through creating a Validator Monitoring Bot to track key metrics of your Berachain validator node using Prometheus and sending alerts via Discord. The bot monitors metrics such as missed blocks and CPU usage, ensuring you are notified when performance issues arise.

Features:

  • Real-time monitoring of validator performance.

  • Alerts for missed blocks and high CPU usage.

  • Configurable thresholds for warnings.

  • Notifications sent via Discord (can extend to Telegram).


Prerequisites

Before starting, ensure the following are installed and configured:

  1. Python 3.6+ installed.

  2. Prometheus running and scraping your validator node metrics.

  3. Discord bot created and token available.

  4. Optional: Node Exporter for monitoring system metrics like CPU usage.


Step 1: Install Required Python Libraries

First, install the necessary Python libraries to interact with Prometheus and Discord.

bashCopy codepip install requests prometheus_client discord.py

Step 2: Create the Validator Monitoring Script

Next, create a Python script that will query Prometheus for key metrics and send alerts to a Discord channel when certain thresholds are breached.

Create a file named validator_monitor.py and add the following code:

pythonCopy codeimport requests
import discord
from discord.ext import commands, tasks

# Discord bot setup
TOKEN = 'YOUR_DISCORD_BOT_TOKEN'
CHANNEL_ID = YOUR_DISCORD_CHANNEL_ID  # Replace with your Discord channel ID

client = commands.Bot(command_prefix='!')

# Prometheus API URL
PROMETHEUS_URL = 'http://localhost:9090/api/v1/query'

# Monitoring thresholds
MISSED_BLOCKS_THRESHOLD = 5
CPU_USAGE_THRESHOLD = 80

# Function to get Prometheus metrics
def get_prometheus_metric(query):
    try:
        response = requests.get(f'{PROMETHEUS_URL}?query={query}')
        results = response.json()['data']['result']
        return float(results[0]['value'][1]) if results else None
    except Exception as e:
        print(f"Error fetching Prometheus metrics: {e}")
        return None

# Task to monitor missed blocks and CPU usage
@tasks.loop(seconds=60)  # Run every 60 seconds
async def monitor_validator():
    missed_blocks = get_prometheus_metric('berachain_missed_blocks')
    cpu_usage = get_prometheus_metric('rate(node_cpu_seconds_total{mode="system"}[5m]) * 100')
    
    channel = client.get_channel(CHANNEL_ID)

    if missed_blocks and missed_blocks > MISSED_BLOCKS_THRESHOLD:
        await channel.send(f"🚨 Alert! Missed blocks detected: {missed_blocks} blocks missed!")
    
    if cpu_usage and cpu_usage > CPU_USAGE_THRESHOLD:
        await channel.send(f"⚠️ Warning! High CPU usage detected: {cpu_usage}%")

# Discord bot events
@client.event
async def on_ready():
    print(f'Logged in as {client.user}')
    monitor_validator.start()

# Run the bot
client.run(TOKEN)

Explanation:

  • TOKEN: Replace with your Discord bot token.

  • CHANNEL_ID: Replace with your Discord channel ID where alerts will be sent.

  • Prometheus Queries: The script queries Prometheus for missed blocks and CPU usage metrics.

  • Thresholds: Alerts are triggered when missed blocks exceed MISSED_BLOCKS_THRESHOLD or CPU usage exceeds CPU_USAGE_THRESHOLD.


Step 3: Configure Prometheus

Ensure that Prometheus is scraping the required metrics from your validator node. Here’s an example of what your prometheus.yml configuration should look like:

yamlCopy codescrape_configs:
  - job_name: 'berachain_validator'
    static_configs:
      - targets: ['localhost:26660']  # Replace with your Berachain node metrics endpoint
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']   # Node Exporter endpoint for system metrics

Step 4: Running the Monitoring Bot

Once everything is set up, run the Python script:

bashCopy codepython3 validator_monitor.py

You should see messages printed in your terminal indicating that the bot is active. Any violations of the thresholds (missed blocks or high CPU) will trigger an alert in your specified Discord channel.


Step 5: Extend to Telegram (Optional)

If you prefer to use Telegram for alerts, you can modify the script to use the Telegram Bot API. Install the python-telegram-bot library:

bashCopy codepip install python-telegram-bot

Then replace the Discord-related code in the script with Telegram bot logic.


Conclusion

With this Validator Monitoring Bot, you can keep an eye on the health of your Berachain validator and get notified when any issues arise. This bot provides a lightweight, customizable solution that can be expanded to include more metrics or different notification channels.

Next Steps:

  • Add more metrics for monitoring (e.g., memory usage, disk space).

  • Implement auto-recovery scripts or alerts for multiple communication platforms.

  • Integrate with Grafana for advanced visual monitoring.


By following this guide, you'll have a basic monitoring solution for your Berachain validator node, ensuring you're always aware of any critical issues in real-time.

Last updated

Was this helpful?