Sabtu, 21 September 2024

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

 

Tidak ada komentar:

Posting Komentar