adding initial files for watchcat script

This commit is contained in:
Russel Yasol
2024-05-31 19:50:55 +08:00
parent 96566daeea
commit a1598a7206
8 changed files with 607 additions and 181 deletions

View File

@@ -0,0 +1,60 @@
#!/bin/sh
# Function to create and run the Watchcat script
create_and_run_watchcat_script() {
local ip=$1
local timeout=$2
local failure_count=$3
local script_path="/usrdata/simpleadmin/script/watchcat.sh"
# Create the script with the watchcat logic
sudo cat << EOF > $script_path
#!/bin/sh
failures=0
while :; do
if ping -c 1 $ip > /dev/null 2>&1; then
failures=0
else
failures=\$((failures + 1))
if [ "\$failures" -ge "$failure_count" ]; then
echo "Rebooting system due to \$failures consecutive ping failures."
/sbin/reboot
exit 0
fi
fi
sleep $timeout
done
EOF
# Make the watchcat script executable
chmod +x $script_path
# Create a JSON to be fetched later
echo "{\"enabled\": true, \"track_ip\": \"$ip\", \"ping_timeout\": $timeout, \"ping_failure_count\": $failure_count}" > /usrdata/simpleadmin/script/watchcat.json
# Check if the script was created successfully
if [ -f "$script_path" ]; then
# Make the script executable
chmod +x "$script_path"
# Run the script in the background
# nohup /bin/sh "$script_path" &
/bin/sh "$script_path" &
echo "Watchcat script created and running."
else
echo "Failed to create the Watchcat script."
echo "Please check the script path: $script_path"
fi
}
# Check if the script is called with the required parameters
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <IP> <timeout> <failure_count>"
exit 1
fi
# Call the function with the provided arguments
create_and_run_watchcat_script "$1" "$2" "$3"

View File

@@ -0,0 +1,32 @@
#!/bin/sh
# Function to remove the Watchcat script and JSON file
remove_watchcat_script() {
local script_path="/usrdata/simpleadmin/script/watchcat.sh"
local json_path="/usrdata/simpleadmin/script/watchcat.json"
# Mount as read-write
mount -o remount,rw /
# Remove the watchcat script if it exists
if [ -f "$script_path" ]; then
rm "$script_path"
echo "Removed $script_path"
else
echo "$script_path does not exist"
fi
# Remove the JSON file if it exists
if [ -f "$json_path" ]; then
rm "$json_path"
echo "Removed $json_path"
else
echo "$json_path does not exist"
fi
# Mount as read-only
mount -o remount,ro /
}
# Call the function to remove the scripts
remove_watchcat_script