#!/bin/bash SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" . "${SCRIPT_DIR}/../share/logging.sh" # Configuration SCRIPTS=( "../metrics/disk-usage.sh" "../metrics/power-usage.sh" "../metrics/temperature.sh" ) CheckScriptExists() { local script="$1" local script_path="${SCRIPT_DIR}/${script}" if [[ -f "$script_path" && -x "$script_path" ]]; then return 0 elif [[ -f "$script_path" ]]; then LogWarning "Script $script exists but is not executable. Making it executable..." chmod +x "$script_path" return 0 else return 1 fi } InstallCrontabEntries() { local installed_count=0 local failed_count=0 BackupCrontab for script in "${SCRIPTS[@]}"; do LogInformation "Processing $script..." if CheckScriptExists "$script"; then LogInformation "Script $script found and is executable" if ManageCrontabEntry "$script"; then ((installed_count++)) else ((failed_count++)) fi else LogWarning "Script $script not found or not accessible, skipping..." ((failed_count++)) fi done LogInformation "Installation summary: $installed_count installed, $failed_count failed" if [[ $failed_count -gt 0 ]]; then exit 1 fi } ManageCrontabEntry() { local script="$1" local script_path="${SCRIPT_DIR}/${script}" local cron_pattern="* * * * * ${script_path}" local current_crontab current_crontab=$(crontab -l 2>/dev/null || echo "") if echo "$current_crontab" | grep -q "$script_path"; then LogInformation "Crontab entry for $script already exists" if echo "$current_crontab" | grep -q "^\* \* \* \* \* ${script_path}"; then LogInformation "Entry for $script is already set to run every minute" return 0 else LogInformation "Updating $script to run every minute" local new_crontab new_crontab=$(echo "$current_crontab" | grep -v "$script_path") new_crontab="${new_crontab}${new_crontab:+$'\n'}${cron_pattern}" if echo "$new_crontab" | crontab -; then LogInformation "Updated crontab entry for $script" return 0 else LogError "Failed to update crontab entry for $script" return 1 fi fi else LogInformation "Adding new crontab entry for $script" local new_crontab="${current_crontab}${current_crontab:+$'\n'}${cron_pattern}" if echo "$new_crontab" | crontab -; then LogInformation "Added crontab entry for $script" return 0 else LogError "Failed to add crontab entry for $script" return 1 fi fi } ShowStatus() { LogInformation "Crontab entries status:" LogInformation "" local found_entries=0 for script in "${SCRIPTS[@]}"; do local script_path="${SCRIPT_DIR}/${script}" local script_name=$(basename "$script") if crontab -l 2>/dev/null | grep -q "$script_path"; then LogInformation "✓ $script_name - INSTALLED" ((found_entries++)) elif [[ -f "$script_path" ]]; then LogInformation "✗ $script_name - NOT INSTALLED (script available)" else LogInformation "✗ $script_name - SCRIPT MISSING" fi done LogInformation "" if [[ $found_entries -gt 0 ]]; then LogInformation "Commands:" LogInformation " View crontab: crontab -l" LogInformation " Edit crontab: crontab -e" LogInformation " Check logs: sudo tail -f /var/log/syslog | grep CRON" else LogWarning "No crontab entries are currently installed" fi } RemoveCrontabEntries() { local removed_count=0 local current_crontab current_crontab=$(crontab -l 2>/dev/null || echo "") if [[ -z "$current_crontab" ]]; then LogWarning "No crontab entries found to remove" return fi local new_crontab="$current_crontab" for script in "${SCRIPTS[@]}"; do local script_path="${SCRIPT_DIR}/${script}" local script_name=$(basename "$script") if echo "$new_crontab" | grep -q "$script_path"; then LogInformation "Removing crontab entry for $script_name..." new_crontab=$(echo "$new_crontab" | grep -v "$script_path") ((removed_count++)) else LogDebug "No crontab entry found for $script_name" fi done if [[ $removed_count -gt 0 ]]; then if [[ -z "$new_crontab" ]]; then crontab -r 2>/dev/null || true LogInformation "Removed all entries, crontab cleared" else echo "$new_crontab" | crontab - LogInformation "Updated crontab with remaining entries" fi LogInformation "Removed $removed_count crontab entries" else LogWarning "No matching crontab entries were found to remove" fi } BackupCrontab() { local backup_file="${SCRIPT_DIR}/crontab_backup_$(date +%Y%m%d_%H%M%S)" crontab -l > "$backup_file" 2>/dev/null if [[ $? -eq 0 ]]; then LogInformation "Crontab backed up to: $backup_file" else LogWarning "No existing crontab to backup or backup failed" fi } ShowUsage() { echo "Usage: $0 [install|remove|status|help]" echo "" echo "Commands:" echo " install - Install all crontab entries (default)" echo " remove - Remove all crontab entries" echo " status - Show status of crontab entries" echo " help - Show this help message" echo "" echo "This script manages crontab entries for monitoring scripts:" echo "$(printf ' %s\n' "${SCRIPTS[@]}" | sed 's|../metrics/||')" echo "" echo "Examples:" echo " $0 install" echo " $0 status" } Main() { local action="${1:-install}" case "$action" in install) LogInformation "Installing crontab entries for server monitoring scripts" InstallCrontabEntries LogInformation "Crontab setup completed successfully!" ;; remove) LogInformation "Removing crontab entries for server monitoring scripts" RemoveCrontabEntries ;; status) ShowStatus ;; help|--help|-h) ShowUsage ;; *) LogError "Unknown action: $action" ShowUsage exit 1 ;; esac } Main "$@"