Discord & TG Alert System

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

Validator Monitoring Bot for Story Protocol

Introduction

This guide walks you through creating a Validator Monitoring Bot to track key metrics of your Story Protocol 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 (extendable to Telegram).

Prerequisites

Before starting, ensure the following are installed and configured:

  • Python 3.6+ installed.

  • Prometheus running and scraping your validator node metrics.

  • Discord bot created and token available.

  • 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 queries Prometheus for key metrics and sends 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('storyprotocol_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.

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


Step 3: Configure Prometheus

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

yamlCopy codescrape_configs:
  - job_name: 'storyprotocol_validator'
    static_configs:
      - targets: ['localhost:26660']  # Replace with your Story Protocol 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 in your terminal indicating that the bot is active. If any thresholds (missed blocks or high CPU) are breached, an alert will be sent to your specified Discord channel.


Step 5: Extend to Telegram (Optional)

If you prefer Telegram for alerts, modify the script to use the Telegram Bot API. First, 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 monitor the health of your Story Protocol validator and get real-time alerts about any critical issues. This lightweight, customizable solution can be expanded to include more metrics or additional 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 Story Protocol validator node, ensuring you’re always aware of critical issues in real-time.

Last updated

Was this helpful?