#!/bin/bash # Setup script directory cd ~ mkdir scripts cd ~/scripts # Install check disk script echo "Install check disk script" touch check-disk.sh chmod +x check-disk.sh cat <<'EOF' > check-disk.sh #!/bin/bash IP_ADDRESS=$(ip addr show | awk '/inet / && /scope global/{split($2, a, "/"); print a[1]; exit}') usage_threshold=80 main_partition=$(df -h | awk '$NF=="/"{print $(NF-1)}' | tr -d '%') if [ -n "$main_partition" ] && [ "$main_partition" -gt "$usage_threshold" ]; then echo "Disk warning on: $IP_ADDRESS" | mail -s "`hostname` Disk usage over 80% ($main_partition)" support@starltd.net else echo "Disk OK: $main_partition% / 100%" fi EOF # Add to crontab cat <<< '# Check if disk if full every 1 h 0 * * * * root /root/scripts/check-disk.sh' >> /etc/crontab # Install check RAM script echo "Install check RAM script" touch check-ram.sh chmod +x check-ram.sh cat <<'EOF' > check-ram.sh #!/bin/bash IP_ADDRESS=$(ip addr show | awk '/inet / && /scope global/{split($2, a, "/"); print a[1]; exit}') # Get CPU and RAM usage with top ram_usage=$(top -b -n 1 | grep "MiB Mem" | awk '{print $8/$4 * 100}') # Check if RAM usage is higher than 80% if (( $(echo "$ram_usage > 80" | bc -l) )); then # Restart php/mysqld systemctl restart php* #systemctl restart mysqld # Send email echo "`hostname` $IP_ADDRESS RAM usage: $ram_usage%" | mail -s "`hostname` php/mysql restart High Memory usage" support@starltd.net else echo "RAM Ok" fi EOF # Add to crontab cat <<< '# Check if RAM is over 80% / restart php-fpm+mysqld * * * * * root /root/scripts/check-ram.sh' >> /etc/crontab if systemctl status mysqld | grep "active" > /dev/null; then echo "Install MYSQL script" touch check-mysqld.sh chmod +x check-mysqld.sh cat <<'EOF' > check-mysqld.sh #!/bin/bash IP_ADDRESS=$(ip addr show | awk '/inet / && /scope global/{split($2, a, "/"); print a[1]; exit}') if systemctl status mysqld.service | grep "active (running)" > /dev/null; then echo "MySQL OK" else systemctl restart mysqld.service echo "MySQL restart on: $IP_ADDRESS" | mail -s "`hostname` MySQL restart" support@starltd.net fi EOF # Add to crontab cat <<< '# Check mysqld service / restart * * * * * root /root/scripts/check-mysqld.sh' >> /etc/crontab else echo "MYSQL does not exists, skipping.." fi if systemctl status httpd | grep "active" > /dev/null; then echo "Install HTTPD script" touch check-httpd.sh chmod +x check-httpd.sh cat <<'EOF' > check-httpd.sh #!/bin/bash IP_ADDRESS=$(ip addr show | awk '/inet / && /scope global/{split($2, a, "/"); print a[1]; exit}') if systemctl status httpd.service | grep "active (running)" > /dev/null; then echo "Apache OK" else systemctl restart httpd.service echo "Apache restart on: $IP_ADDRESS" | mail -s "`hostname` Apache restart" support@starltd.net fi EOF # Add to crontab cat <<< '# Check httpd service / restart * * * * * root /root/scripts/check-httpd.sh' >> /etc/crontab else echo "HTTPD does not exists, skipping.." fi if systemctl status apache2 | grep "active" > /dev/null; then echo "Install Apache script" touch check-apache2.sh chmod +x check-apache2.sh cat <<'EOF' > check-apache2.sh #!/bin/bash IP_ADDRESS=$(ip addr show | awk '/inet / && /scope global/{split($2, a, "/"); print a[1]; exit}') if systemctl status apache2.service | grep "active (running)" > /dev/null; then echo "Apache OK" else systemctl restart apache2.service echo "Apache restart on: $IP_ADDRESS" | mail -s "`hostname` Apache restart" support@starltd.net fi EOF # Add to crontab cat <<< '# Check apche2 service / restart * * * * * root /root/scripts/check-apache2.sh' >> /etc/crontab else echo "Apache does not exists, skipping.." fi if systemctl status nginx | grep "active" > /dev/null; then echo "Install NGINX script" touch check-nginx.sh chmod +x check-nginx.sh cat <<'EOF' > check-nginx.sh #!/bin/bash IP_ADDRESS=$(ip addr show | awk '/inet / && /scope global/{split($2, a, "/"); print a[1]; exit}') if systemctl status nginx.service | grep "active (running)" > /dev/null; then echo "Nginx OK" else systemctl restart nginx.service echo "Nginx restart on: $IP_ADDRESS" | mail -s "`hostname` Nginx restart" support@starltd.net fi EOF # Add to crontab cat <<< '# Check nginx service / restart * * * * * root /root/scripts/check-nginx.sh' >> /etc/crontab else echo "NGINX does not exists, skipping.." fi if systemctl status redis-server | grep "active" > /dev/null; then echo "Install Redis script" touch check-redis.sh chmod +x check-redis.sh cat <<'EOF' > check-redis.sh #!/bin/bash IP_ADDRESS=$(ip addr show | awk '/inet / && /scope global/{split($2, a, "/"); print a[1]; exit}') if systemctl status redis-server.service | grep "active (running)" > /dev/null; then echo "Redis OK" else systemctl restart redis-server.service echo "Redis restart on: $IP_ADDRESS" | mail -s "`hostname` Redis restart" support@starltd.net fi EOF # Add to crontab cat <<< '# Check redis service / restart * * * * * root /root/scripts/check-redis.sh' >> /etc/crontab else echo "Redis does not exists, skipping.." fi # Check if a parameter was provided if [ $# -eq 0 ]; then echo "URL was not provided, skipping CURL check" else echo "Installing curl script for: $1" touch check-curl.sh chmod +x check-curl.sh #cat <<'EOF' > check-curl.sh cat >> check-curl.sh < /dev/null; then echo "FPM is UP" else systemctl restart php*.service echo "\`hostname\` \$IP_ADDRESS php-fpm restart" | mail -s "\`hostname\` php-fpm restart" support@starltd.net fi EOF # Add to crontab cat <<< '# Check if 200/OK * * * * * root /root/scripts/check-curl.sh' >> /etc/crontab fi echo "Done installing!"