Last active
July 23, 2019 05:06
-
-
Save virbo/4ba8c43b748e5c24e4d96284764ec9b5 to your computer and use it in GitHub Desktop.
Script buat check service-service yang non aktif, kirim notif via email dan telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Description: File library berisikan variabel-variabel dan function-function | |
#Author: Yusuf Ayuba | |
#user yang bisa menjalankan script ini | |
USER_ALLOW="root" | |
#inisial variabel-variabel (Silahkan sesuaikan isian email pengirim dan email tujuan) | |
FROM="email pengirim" | |
EMAIL="email tujuan" | |
FILE_MAIL="mail.txt" | |
#list service yang akan dicek (tambahkan service-service lain yang ingin dicek) | |
SERVICES=('httpd' 'mysql' 'mongod' 'clamd') | |
#inisialisasi variabel bot telegram (silahkan diisi token telegram dan id telegram) | |
TOKEN="token telegram" | |
CHAT_ID="id telegram" | |
ENDPOINT="https://api.telegram.org/bot$TOKEN" | |
#method bot telegram (more method https://core.telegram.org/bots/api#available-methods) | |
SEND_MESSAGE="$ENDPOINT/sendMessage" | |
# ===== BATAS PENGEDITAN | |
#function kirim email | |
function send_mail() { | |
echo "From: $FROM" > $FILE_MAIL | |
echo "To: $EMAIL" >> $FILE_MAIL | |
echo "Subject: Service $2 tidak aktif" >> $FILE_MAIL | |
echo "" >> $FILE_MAIL | |
echo $1 >> $FILE_MAIL | |
cat $FILE_MAIL | sendmail -t | |
} | |
#function kirim pesan telegram | |
function send_message() { | |
curl -s -X GET $SEND_MESSAGE -d chat_id=$CHAT_ID -d text="$1" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Description: Script check service yang non aktif | |
#Author: Yusuf Ayuba | |
#set path (ditest dan berfungsi normal di ubuntu 16.04 dan centos 7) | |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/script | |
#include file lib.inc | |
source lib.inc | |
#check user allow | |
if [ "$(whoami)" != $USER_ALLOW ]; then | |
echo "***********************************************" | |
echo "Script harus dijalankan menggunakan user: ${USER_ALLOW}" | |
echo "***********************************************" | |
exit 1 | |
fi | |
#looping check | |
#status 0 = aktif, 1 = tidak aktif | |
for service in "${SERVICES[@]}" | |
do | |
#check service | |
`pgrep $service >/dev/null 2>&1` | |
STATUS=$(echo $?) | |
echo $service": "$([ $STATUS == 0 ] && echo "AKTIF" || echo "NON AKTIF") | |
if [ $STATUS == 1 ]; then | |
#restart service yang down (non aktif) | |
service $service start | |
#untuk memastikan, check kembali service yang telah direstart | |
`pgrep $service >/dev/null 2>&1` | |
RESTART=$(echo $?) | |
#kirim notifikasi ke email dan telegram | |
if [ $RESTART == 0 ]; then | |
MESSAGE="Service $service down, tapi berhasil diaktifkan kembali" | |
send_message "$MESSAGE" | |
send_mail "$MESSAGE" "$service" | |
else | |
MESSAGE="Service $service down, harus diaktifkan secara manual" | |
send_message "$MESSAGE" | |
send_mail "$MESSAGE" "$service" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment