Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Created October 29, 2022 00:28
Embed
What would you like to do?
Alert when Network Floating on Openstack is Unreachable

Brief

This script just a simple logic, when vRouter on Openstack not reachable script will execute Network Agent Report to Telegram

Use

  • 172.20.1.230 is your vRouter
  • TELEGRAM_BOT_TOKEN is your
  • CHAT_ID is your Telegram CHAT ID

Create Network Agent Watchers

nano /root/os-sh/check-netagent.sh
#!/bin/bash

# Environment
DATE="$(date "+%d %b %Y %H:%M")"
TELEGRAM_BOT_TOKEN=""
CHAT_ID=""
ALERT=$(echo -e "\u26A0")
source /root/openstack/admin-openrc.sh

HOSTUP=$(openstack network agent list | grep UP | cut -d "|" -f 4 )
AGENTUP=$(openstack network agent list | grep UP | cut -d "|" -f 3)
AGENTDOWN=$(openstack network agent list | grep DOWN | cut -d "|" -f 3)
HOSTDOWN=$(openstack network agent list | grep DOWN | cut -d "|" -f 4 )
STATUS=$(openstack network agent list | grep UP | cut -d "|" -f 7)

if ! [ "$STATUS" == "UP" ]; then
        TEXT="$ALERT *Network Agent Hourly Report :* $ALERT

*This agent is already UP :*
$AGENTUP

*That agent UP runs on :*
$HOSTUP

*This agent is DOWN :*
$AGENTDOWN

*That agent DOWN runs on :*
$HOSTDOWN

*Reported on $DATE*"

        curl -s -d "chat_id=$CHAT_ID&text=${TEXT}&disable_web_page_preview=true&parse_mode=markdown" https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage? > /dev/null
fi

Create Triggering Report Script

nano /root/os-sh/ping.sh
#!/bin/bash
SERVERIP=172.20.1.230

ping -c 3 $SERVERIP > /dev/null 2>&1
if [ $? -ne 0 ]
then
   /bin/bash /root/os-sh/check-netagent.sh
fi

Chmod 777 our script

chmod 777 /root/os-sh/check-netagent.sh
chmod 777 /root/os-sh/ping.sh

Create crontab every minutes

crontab -e
* * * * * /bin/bash /root/os-sh/ping.sh

So, monitor your telegram!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment