الملفات
edited/Final_MOnitor.sh
abdelrhman 31977b98c7
فشلت بعض الفحوصات
/ deploy (push) Has been skipped
/ build_and_push_image (push) Failing after 10s
second
2025-10-23 17:29:28 +03:00

121 أسطر
2.7 KiB
Bash
ملف تنفيذي

#!/bin/bash
function get_server_metrics() {
SERVER_IP=$(hostname -I | awk '{print $1}')
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}' | cut -d'.' -f1)%
RAM_AVAILABLE=$(free -m | awk 'NR==2{print $4 "MB"}')
DISK_SPACE=$(df -h / | awk 'NR==2 {print $4}')
cat <<EOF
"ip": "$SERVER_IP",
"cpu_usage": "$CPU_USAGE",
"ram_available": "$RAM_AVAILABLE",
"disk_space": "$DISK_SPACE"
EOF
}
#_____________________________________________________
LOG_FILE="/home/abdelrhman/PyCharmMiscProject/AP.log"
#STATUS_CODE=$(tail -n1 "$LOG_FILE" | cut -d " " -f9)
#REQUEST=$(tail -n1 $LOG_FILE | cut -d " " -f6)
#DATE_OF_REQUEST=$(tail -n1 $LOG_FILE | cut -d " " -f1)
#REQUEST_TIME=$(tail -n1 $LOG_FILE | cut -d " " -f2)
#IP_SERVER=$(tail -n1 $LOG_FILE | cut -d " " -f4 | cut -d ":" -f1)
#ALERT_API=______________________________________________________________
#SERVER_METRICS=$(get_server_metrics)
#JSON_PAYLOAD=$(cat <<EOF
#{
# "error": "$STATUS_CODE Error",
#"timestamp": "$DATE_OF_REQUEST:$REQUEST_TIME",
#"server_metrics": {
# $SERVER_METRICS
#}
#}
#EOF
#)
tail -n 0 -f "$LOG_FILE" | while read -r NEW_LINE; do
STATUS_CODE=$(tail -n1 "$LOG_FILE" | cut -d " " -f9)
REQUEST=$(tail -n1 $LOG_FILE | cut -d " " -f6)
DATE_OF_REQUEST=$(tail -n1 $LOG_FILE | cut -d " " -f1)
REQUEST_TIME=$(tail -n1 $LOG_FILE | cut -d " " -f2)
IP_SERVER=$(tail -n1 $LOG_FILE | cut -d " " -f4 | cut -d ":" -f1)
#ALERT_API=______________________________________________________________
#SERVER_METRICS=$(get_server_metrics)
JSON_PAYLOAD=$(cat <<EOF
{
"error": "$STATUS_CODE Error",
"timestamp": "$DATE_OF_REQUEST:$REQUEST_TIME",
"server_metrics": {
$SERVER_METRICS
}
}
EOF
)
if [[ "$STATUS_CODE" =~ ^[0-9] ]];
then
# echo $NEW_LINE
echo "Status code is $STATUS_CODE "
echo "IP is $IP_SERVER "
echo "Date of request $DATE_OF_REQUEST "
echo "Request time $REQUEST_TIME"
echo "The request is $REQUEST "
case $STATUS_CODE in
2??)
echo -e "The process\033[32m[DONE]\033[0m correctly"
echo
;;
3??)
echo -e "there is a \033[33m[REDIRECT]\033[0m Error"
echo
;;
4??)
echo -e "Not found \033[31m[ERROR]\033[0m"
echo
;;
5??)
echo -e "Server \033[31m[ERROR]\033[0m"
echo
#function get_server_metrics
#curl -s -X POST -H "Content-Type: application/json" -d "$JSON_PAYLOAD" $ALERT_API
;;
esac
fi
done
# if [ $STATUS_CODE -ge 200 ] && [ $STATUS_CODE -lt 300 ]
# then
# echo "its done bro"
#
# elif [ $STATUS_CODE -ge 300 ] && [ $STATUS_CODE -lt 400 ]
# then
# echo "Redirect Error"
# elif [ $STATUS_CODE -ge 400 ] && [ $STATUS_CODE -lt 500 ]
# then
# echo "NOT Found"
#
# else
# echo "Server Error"
# fi
#done