Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active March 8, 2023 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/3d7ab1c2456ddcc3ade57ea1ab3a23f0 to your computer and use it in GitHub Desktop.
Save gilangvperdana/3d7ab1c2456ddcc3ade57ea1ab3a23f0 to your computer and use it in GitHub Desktop.
Openstack Alerting when any Instance Spwaned or Destroyed

General

Script for alerting agent when a new Instance created or Destroyed on Openstack

Script.sh

#!/bin/bash

# Telegram
BOT_TOKEN="$BOTTOKEN"
CHAT_ID="$YOURCHATID"

# Export OS
export OS_PROJECT_DOMAIN_NAME=Default
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_NAME=admin
export OS_TENANT_NAME=admin
export OS_USERNAME=admin
export OS_PASSWORD=$PASSWORD_OS
export OS_AUTH_URL=$HTTPS_AUTH_API
export OS_INTERFACE=internal
export OS_ENDPOINT_TYPE=internalURL
export OS_IDENTITY_API_VERSION=3
export OS_REGION_NAME=RegionOne
export OS_AUTH_PLUGIN=password
export OS_CACERT=/etc/kolla/certificates/ca/root.crt

# Simpan output dari command openstack server list --all -f value -c Name dan ID ke dalam suatu file
openstack server list --all -f value -c Name -c ID > /root/dll/server_list.txt

# Hitung Instance Keseluruhan
instance_count=$(openstack server list --all-projects -f value -c ID | wc -l)

# Simpan hasil output command dari file terakhir ke variabel last_output
last_output=$(cat /root/dll/last_server_list.txt)

# Periksa apakah output dari command saat ini berbeda dengan output yang disimpan sebelumnya
if ! diff -q /root/dll/server_list.txt /root/dll/last_server_list.txt >/dev/null ; then

  # Periksa setiap baris dari output yang baru dan cari instance yang baru ditambahkan
  while IFS= read -r line; do
    # Ambil ID dari instance baru
    instance_id=$(echo $line | awk '{print $1}')
    if ! grep -q "$line" /root/dll/last_server_list.txt; then
      # Ambil project_id dari instance baru
      project_id=$(openstack server show $instance_id -f value -c project_id)
      # Ambil nama proyek dari project_id
      project_name=$(openstack project show $project_id -f value -c name)
      # Simpan informasi instance baru dalam file log atau database
      echo "$(date '+%Y-%m-%d %H:%M:%S') - $instance_id - $project_name" >> /root/dll/instance_log.txt
      # Alert ke Telegram dengan pesan bahwa ada instance baru yang ditambahkan beserta nama proyeknya
      message="Terdapat Instance <b>baru</b> bernama <b>$(echo $line | awk '{print $2}')</b> pada Project <b>$project_name.</b> Saat ini jumlah Instance sebanyak <b>$instance_count.</b>"
      curl -s -X POST https://api.telegram.org/bot$BOT_TOKEN/sendMessage -d chat_id=$CHAT_ID -d text="$message" -d parse_mode="HTML"
    fi
  done < /root/dll/server_list.txt

  # Periksa setiap baris dari output yang lama dan cari instance yang dihapus
  while IFS= read -r line; do
    # Ambil ID dari instance yang dihapus
    instance_id=$(echo $line | awk '{print $1}')
    if ! grep -q "$line" /root/dll/server_list.txt; then
      # Ambil informasi tentang instance yang dihapus
      instance_info=$(openstack server show $instance_id)
      # Ambil project_name dari file log
      project_name=$(grep "$instance_id" /root/dll/instance_log.txt | awk '{print $NF}')
      # Jika project_name tidak ditemukan di file log, cari project_name dari instance_info
      if [[ -z $project_name ]]; then
        project_name=$(echo "$instance_info" | awk -F "|" '/project_id/ {system("openstack project show "$3" -f value -c name")}')
      fi
      # Alert ke Telegram dengan pesan bahwa ada instance yang dihapus beserta nama proyeknya
      message="Terdapat Instance yang <b>dihapus</b> bernama <b>$(echo $line | awk '{print $2}')</b> pada Project <b>$project_name.</b> Saat ini jumlah Instance sebanyak <b>$instance_count.</b>"
      curl -s -X POST https://api.telegram.org/bot$BOT_TOKEN/sendMessage -d chat_id=$CHAT_ID -d text="$message" -d parse_mode="HTML"
    fi
  done < /root/dll/last_server_list.txt

  # Simpan output dari command saat ini ke dalam file last_server_list.txt
  cp /root/dll/server_list.txt /root/dll/last_server_list.txt
fi

Make it automaticly watch

crontab -e
* * * * * /root/dll/script.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment