Tampilkan postingan dengan label alert. Tampilkan semua postingan
Tampilkan postingan dengan label alert. Tampilkan semua postingan

Sabtu, 21 September 2024

Bash Script Monitoring Certificate Kubernetes Cluster (Telegram Alert)

 

#!/bin/bash

 

HOST=$(hostname)

 

# ID Telegram

TOKEN=telegram_token

CHAT_ID=telegram_chatId

 

# Cek Sertifikat SSL Kubernetes

cert_info=$(kubeadm certs check-expiration)

expires_date=$(echo "$cert_info" | awk 'NR>6 {print $2, $3, $4, $5; exit}')

days_remaining=$(echo "$cert_info" | awk 'NR>6 {print $7; exit}')

 

# Hapus "d" dari variable days_remaining menjadi full integer

days_remaining="${days_remaining%d}"

 

# Mengecek apakah sisa hari kurang dari 90

if [ "$days_remaining" -lt 90 ]; then

  # Tampilkan Sertifikat SSL Kubernetes

  echo "Cluster    : $HOST" > /opt/scripts/ssl_kuber.log

  echo "Expire on : $expires_date" >> /opt/scripts/ssl_kuber.log

  echo "Counting  : $days_remaining days" >> /opt/scripts/ssl_kuber.log

 

  # Kirim Notifikasi

  MESSAGE="$(cat /opt/scriptss/ssl_kuber.log)"

  URL="https://api.telegram.org/bot$TOKEN/sendMessage"

  curl -s -X POST $URL -d chat_id=$CHAT_ID -d text="$(echo -e '\U0001f525 \U0001f514 \nNP2.1 Kubernetes SSLn\n')$(echo -e '\n-----------------------------------\n')$(echo -e '\n'"$MESSAGE")

"

else

  echo "check-expiration: OK: $days_remaining days"

fi

 

sleep 3

rm -f /opt/scripts/ssl_kuber.log

Bash Script Monitoring CSR Kubernetes (Telegram Alert)

 

#!/bin/bash

HOST=$(hostname)

TOKEN=telegram_token

CHAT_ID=telegram_chat_id

 

pending_csrs=$(kubectl get csr | awk '$4=="Pending"')

  if [ -z "$pending_csrs" ]; then

    echo "No pending CSRs."

  else

    # Tampilkan CSR Kubernetes

    echo "Cluster     : $HOST" > /opt/scripts/csr_kuber.log

    echo "Status CSR : You have pending CSRs" >> /opt/scripts/csr_kuber.log

    sumpending=$(kubectl get csr | awk '$4=="Pending"'| wc -l)

    echo "Total      : $sumpending" >> /opt/scripts/csr_kuber.log

 

    # Kirim telegram

    MESSAGE="$(cat /opt/scripts/csr_kuber.log)"

    curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" -d "chat_id=$CHAT_ID" -d "text="$(echo -e '\U0001f525 \U0001f514 \nNP2 Kubernetes SSLn\n')$(echo -e '\n-----------------------------------\n')$(echo -e '\n'"$MESSAGE")

  fi

 

sleep 3

rm -f /opt/scripts/csr_kuber.log

Bash Script Monitoring Service Kubernetes (Telegram Alert)

#!/bin/bash

 

TOKEN=telegram_token

CHAT_ID=telegram_id

log=/var/log/svc.log

tgl=$(date +"%A %d-%m-%Y %T")

 

export PATH=/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

export KUBECONFIG=/etc/kubernetes/admin.conf

 

function getsvc {

        echo "" > $log

        /usr/bin/kubectl get pods --all-namespaces --no-headers | grep 'ImagePullBackOff' | awk '{print "\nProject = " $1 "\nPods = " $2 "\nStatus =  " $4 "\n_____________________________"}' >> $log

        /usr/bin/kubectl get pods --all-namespaces --no-headers | grep 'CrashLoopBackOff' | awk '{print "\nProject = " $1 "\nPods = " $2 "\nStatus =  " $4 "\n_____________________________"}' >> $log

        /usr/bin/kubectl get pods --all-namespaces --no-headers | grep 'Pending' | awk '{print "\nProject = " $1 "\nPods = " $2 "\nStatus =  " $4 "\n_____________________________"}' >> $log

        /usr/bin/kubectl get pods --all-namespaces --no-headers | grep 'Outofmemory' | awk '{print "\nProject = " $1 "\nPods = " $2 "\nStatus =  " $4 "\n_____________________________"}' >> $log

        /usr/bin/kubectl get pods --all-namespaces --no-headers | grep 'Outofcpu' | awk '{print "\nProject = " $1 "\nPods = " $2 "\nStatus =  " $4 "\n_____________________________"}' >> $log

#       /usr/bin/kubectl get pods --all-namespaces --no-headers | grep 'Running' | awk '{print "\nProject = " $1 "\nPods = " $2 "\nStatus =  " $4 "\n_____________________________"}' >> $log

}

 

getsvc

 

message=$(<$log)

send() {

        curl  \

        -X POST \

        https://api.telegram.org/bot$TOKEN/sendMessage \

        -d chat_id=$CHAT_ID \

        -d text="`echo -e '\U0001f525 \u274C \U0001f514 \nNP2 Service Down\n.'`$tgl`echo -e '\n_____________________________\n'` $message"

}

 

if [[ ! -z "$message" ]]; then

        send

else

        echo "Error message"

fi