Script kiểm tra trạng thái của một URL và restart lại dịch vụ

Thứ bảy - 25/11/2023 03:29 118 0
Script dùng để kiểm tra trạng thái của 1 URL, nếu mã http code trả về khác 200 thì thực thi hành động
bash shell
bash shell
Script được đặt lên 1 máy chủ độc lập dùng để monitor
3 máy chủ web chạy cluster chứa website đang hoạt động
script khi chạy sẽ kiểm tra trạng thái URL của website, nếu HTTP code trả về khác với 200 sẽ thực thi lệnh restart service php7.4-fpm thông qua ssh đến 3 máy chủ

Để có thể Remote SSH, cần cài đặt và cấu SSH KEY ở link dưới
https://nxcuong.ued.vn/vi/news/linux/huong-dan-su-dung-ssh-key-de-ket-noi-den-linux-server-50.html

Tạo checkweb.sh
#!/bin/bash
# curl -k -sL -w "%{http_code}\n" "https://dttt.ued.udn.vn" -o /dev/null        Trả về code 200
# -s = Silent cURL's output
# -L = Follow redirects
# -w = Custom output format
# -o = Redirects the HTML output to /dev/null

DIR=/opt/check_web
PATH_LOG=$DIR/logs
TIMESTAMP=$(date +"%Y-%m-%d_%Hh%M")

url=https://dttt.ued.udn.vn

echo "path log = $PATH_LOG"

fn_remote_restart_php (){
  ssh root@10.1.16.17 "systemctl restart php7.4-fpm.service"
  ssh root@10.1.16.18 "systemctl restart php7.4-fpm.service"
  ssh root@10.1.16.19 "systemctl restart php7.4-fpm.service"
}

status_code=$(curl -k -sL -w "%{http_code}\n" "$url" -o /dev/null)        # nạp biến status_code là output của lệnh curl -k -sL -w "%{http_code}\n" "https://jshe.ued.udn.vn" -o /dev/null
if [[ "$status_code" -ne 200 ]] ; then            # nếu status_code không phải là giá trị 200 thì chạy 3 lệnh ssh dưới
  #echo "Site status changed to $status_code" | mail -s "SITE STATUS CHECKER" "my_email@email.com" -r "STATUS_CHECKER"
  error_code=$(curl -k -sL -w "%{http_code}\n" "$url" -o /dev/null)
  echo "Site status FAILED    at $TIMESTAMP ---> $error_code" >> $PATH_LOG/check_web.log
  curl 'https://api.telegram.org/bot1853514768:AAHIIEqiGlAqD1wZv6UFbk-sIR57vffFVBU/sendMessage?chat_id=-525800009&text=<DAO TAO TRUC TUYEN DOWN, HTTP Code:  $error_code>'
  fn_remote_restart_php
else
  echo "Site status OK    at $TIMESTAMP ---> $status_code" >> $PATH_LOG/check_web.log
  exit 0
fi
#!/bin/bash
# curl -k -sL -w "%{http_code}\n" "https://dttt.ued.udn.vn" -o /dev/null        Trả về code 200
# -s = Silent cURL's output
# -L = Follow redirects
# -w = Custom output format
# -o = Redirects the HTML output to /dev/null
DIR=/opt/check_web
PATH_LOG=$DIR/logs
TIMESTAMP=$(date +"%Y-%m-%d_%Hh%M")
url=https://dttt.ued.udn.vn

echo "path log = $PATH_LOG"

fn_remote_restart_php (){
  ssh root@10.1.16.17 "systemctl restart php7.4-fpm.service"
  ssh root@10.1.16.18 "systemctl restart php7.4-fpm.service"
  ssh root@10.1.16.19 "systemctl restart php7.4-fpm.service"
}

status_code=$(curl -k -sL -w "%{http_code}\n" "$url" -o /dev/null)        # nạp biến status_code là output của lệnh curl -k -sL -w "%{http_code}\n" "https://jshe.ued.udn.vn" -o /dev/null
if [[ "$status_code" -ne 200 ]] ; then            # nếu status_code không phải là giá trị 200 thì chạy 3 lệnh ssh dưới
  #echo "Site status changed to $status_code" | mail -s "SITE STATUS CHECKER" "my_email@email.com" -r "STATUS_CHECKER"
  error_code=$(curl -k -sL -w "%{http_code}\n" "$url" -o /dev/null)
  echo "Site status FAILED    at $TIMESTAMP ---> $error_code" >> $PATH_LOG/check_web.log
  curl 'https://api.telegram.org/bot1853514768:AAHIIEqiGlAqD1wZv6UFbk-sIR57vffFVBU/sendMessage?chat_id=-525800009&text=<DAO TAO TRUC TUYEN DOWN, HTTP Code:  $error_code>'
  fn_remote_restart_php
else
  echo "Site status OK    at $TIMESTAMP ---> $status_code" >> $PATH_LOG/check_web.log
  exit 0
fi

 

Tổng số điểm của bài viết là: 0 trong 0 đánh giá

Click để đánh giá bài viết
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây