Update with support for mac

This commit is contained in:
2025-10-30 16:16:50 +01:00
parent 30871760b9
commit 5bdff0d436
9 changed files with 689 additions and 171 deletions
+215 -52
View File
@@ -2,6 +2,7 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/../share/logging.sh"
. "${SCRIPT_DIR}/../share/os-detection.sh"
# Configuration
SCRIPTS=(
@@ -10,6 +11,9 @@ SCRIPTS=(
"../custom-metrics/temperature.sh"
)
OS_TYPE=$(DetectOS)
LAUNCHD_DIR="$HOME/Library/LaunchAgents"
CheckScriptExists() {
local script="$1"
local script_path="${SCRIPT_DIR}/${script}"
@@ -29,17 +33,29 @@ InstallCrontabEntries() {
local installed_count=0
local failed_count=0
BackupCrontab
if [[ "$OS_TYPE" == "macos" ]]; then
BackupLaunchAgents
else
BackupCrontab
fi
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++))
if [[ "$OS_TYPE" == "macos" ]]; then
if ManageLaunchAgent "$script"; then
((installed_count++))
else
((failed_count++))
fi
else
((failed_count++))
if ManageCrontabEntry "$script"; then
((installed_count++))
else
((failed_count++))
fi
fi
else
LogWarning "Script $script not found or not accessible, skipping..."
@@ -54,6 +70,62 @@ InstallCrontabEntries() {
fi
}
ManageLaunchAgent() {
local script="$1"
local script_path="${SCRIPT_DIR}/${script}"
local script_name=$(basename "$script" .sh)
local plist_name="com.servermetrics.${script_name}.plist"
local plist_path="${LAUNCHD_DIR}/${plist_name}"
# Ensure LaunchAgents directory exists
if [[ ! -d "$LAUNCHD_DIR" ]]; then
mkdir -p "$LAUNCHD_DIR"
fi
# Create plist file
cat > "$plist_path" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${plist_name%.plist}</string>
<key>ProgramArguments</key>
<array>
<string>${script_path}</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/${script_name}.err</string>
<key>StandardOutPath</key>
<string>/tmp/${script_name}.out</string>
</dict>
</plist>
EOF
if [[ $? -eq 0 ]]; then
LogInformation "Created LaunchAgent plist: $plist_path"
# Unload if already loaded (ignore errors)
launchctl unload "$plist_path" 2>/dev/null || true
# Load the plist
if launchctl load "$plist_path" 2>/dev/null; then
LogInformation "Loaded LaunchAgent for $script"
return 0
else
LogError "Failed to load LaunchAgent for $script"
return 1
fi
else
LogError "Failed to create plist for $script"
return 1
fi
}
ManageCrontabEntry() {
local script="$1"
local script_path="${SCRIPT_DIR}/${script}"
@@ -95,7 +167,13 @@ ManageCrontabEntry() {
}
ShowStatus() {
LogInformation "Crontab entries status:"
LogInformation "Scheduled task status:"
LogInformation "OS: $OS_TYPE"
if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation "Method: LaunchAgents"
else
LogInformation "Method: Crontab"
fi
LogInformation ""
local found_entries=0
@@ -103,64 +181,117 @@ ShowStatus() {
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)"
if [[ "$OS_TYPE" == "macos" ]]; then
local plist_name="com.servermetrics.$(basename "$script" .sh).plist"
local plist_path="${LAUNCHD_DIR}/${plist_name}"
if [[ -f "$plist_path" ]]; then
if launchctl list | grep -q "$(basename "$plist_name" .plist)"; then
LogInformation "$script_name - LOADED"
else
LogInformation "$script_name - INSTALLED but NOT LOADED"
fi
((found_entries++))
elif [[ -f "$script_path" ]]; then
LogInformation "$script_name - NOT INSTALLED (script available)"
else
LogInformation "$script_name - SCRIPT MISSING"
fi
else
LogInformation "$script_name - SCRIPT MISSING"
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
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"
if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation " List agents: launchctl list | grep servermetrics"
LogInformation " View logs: tail -f /tmp/*.out"
LogInformation " Unload agent: launchctl unload ~/Library/LaunchAgents/com.servermetrics.*.plist"
else
LogInformation " View crontab: crontab -l"
LogInformation " Edit crontab: crontab -e"
LogInformation " Check logs: sudo tail -f /var/log/syslog | grep CRON"
fi
else
LogWarning "No crontab entries are currently installed"
LogWarning "No scheduled tasks 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 [[ "$OS_TYPE" == "macos" ]]; then
# Remove LaunchAgents
for script in "${SCRIPTS[@]}"; do
local script_name=$(basename "$script" .sh)
local plist_name="com.servermetrics.${script_name}.plist"
local plist_path="${LAUNCHD_DIR}/${plist_name}"
if [[ -f "$plist_path" ]]; then
LogInformation "Removing LaunchAgent for $script_name..."
# Unload the agent
launchctl unload "$plist_path" 2>/dev/null || true
# Remove the plist file
rm -f "$plist_path"
((removed_count++))
else
LogDebug "No LaunchAgent found for $script_name"
fi
done
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++))
if [[ $removed_count -gt 0 ]]; then
LogInformation "Removed $removed_count LaunchAgents"
else
LogDebug "No crontab entry found for $script_name"
LogWarning "No LaunchAgents were found to remove"
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"
# Remove crontab entries
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
fi
}
@@ -174,16 +305,48 @@ BackupCrontab() {
fi
}
BackupLaunchAgents() {
local backup_dir="${SCRIPT_DIR}/launchagents_backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$backup_dir"
local backup_count=0
for script in "${SCRIPTS[@]}"; do
local script_name=$(basename "$script" .sh)
local plist_name="com.servermetrics.${script_name}.plist"
local plist_path="${LAUNCHD_DIR}/${plist_name}"
if [[ -f "$plist_path" ]]; then
cp "$plist_path" "$backup_dir/"
((backup_count++))
fi
done
if [[ $backup_count -gt 0 ]]; then
LogInformation "LaunchAgents backed up to: $backup_dir ($backup_count files)"
else
LogWarning "No existing LaunchAgents to backup"
rmdir "$backup_dir" 2>/dev/null
fi
}
ShowUsage() {
local task_method="crontab"
if [[ "$OS_TYPE" == "macos" ]]; then
task_method="LaunchAgents"
fi
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 " install - Install all scheduled tasks (default)"
echo " remove - Remove all scheduled tasks"
echo " status - Show status of scheduled tasks"
echo " help - Show this help message"
echo ""
echo "This script manages crontab entries for monitoring scripts:"
echo "OS: $OS_TYPE"
echo "Method: $task_method"
echo ""
echo "This script manages scheduled tasks for monitoring scripts:"
echo "$(printf ' %s\n' "${SCRIPTS[@]}" | sed 's|../custom-metrics/||')"
echo ""
echo "Examples:"
@@ -196,12 +359,12 @@ Main() {
case "$action" in
install)
LogInformation "Installing crontab entries for server monitoring scripts"
LogInformation "Installing scheduled tasks for server monitoring scripts (OS: $OS_TYPE)"
InstallCrontabEntries
LogInformation "Crontab setup completed successfully!"
LogInformation "Scheduled task setup completed successfully!"
;;
remove)
LogInformation "Removing crontab entries for server monitoring scripts"
LogInformation "Removing scheduled tasks for server monitoring scripts (OS: $OS_TYPE)"
RemoveCrontabEntries
;;
status)
+14 -3
View File
@@ -2,12 +2,14 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/../share/logging.sh"
. "${SCRIPT_DIR}/../share/os-detection.sh"
# Configuration
FILEBEAT_CONFIG_DIR="${SCRIPT_DIR}/../filebeat"
FILEBEAT_INSTALL_DIR="/etc/filebeat"
FILEBEAT_INSTALL_DIR=$(GetFilebeatConfigDir)
FILEBEAT_MODULES_D_DIR="${FILEBEAT_INSTALL_DIR}/modules.d"
CONFIG_FILE="filebeat.yml"
OS_TYPE=$(DetectOS)
CheckRoot() {
if [[ $EUID -ne 0 ]]; then
@@ -129,6 +131,8 @@ ValidateFilebeatConfig() {
ShowStatus() {
LogInformation "Filebeat configuration status:"
LogInformation "OS: $OS_TYPE"
LogInformation "Install directory: $FILEBEAT_INSTALL_DIR"
LogInformation ""
# Check main config file
@@ -157,8 +161,15 @@ ShowStatus() {
LogInformation "Commands:"
LogInformation " Test config: sudo filebeat test config -c ${FILEBEAT_INSTALL_DIR}/${CONFIG_FILE}"
LogInformation " Test output: sudo filebeat test output -c ${FILEBEAT_INSTALL_DIR}/${CONFIG_FILE}"
LogInformation " Start Filebeat: sudo systemctl start filebeat"
LogInformation " Check status: sudo systemctl status filebeat"
local service_cmd=$(GetServiceCommand)
if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation " Start Filebeat: sudo $service_cmd start filebeat"
LogInformation " Check status: $service_cmd list | grep filebeat"
else
LogInformation " Start Filebeat: sudo $service_cmd start filebeat"
LogInformation " Check status: sudo $service_cmd status filebeat"
fi
}
RemoveFilebeatConfigs() {
+56 -17
View File
@@ -2,9 +2,11 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/../share/logging.sh"
. "${SCRIPT_DIR}/../share/os-detection.sh"
LOGROTATE_DIR="${SCRIPT_DIR}/../logrotate"
LOGROTATE_INSTALL_DIR="/etc/logrotate.d"
LOGROTATE_INSTALL_DIR=$(GetLogRotateConfigDir)
OS_TYPE=$(DetectOS)
# List of logrotate config files to manage (without .conf extension)
LOGROTATE_CONFIGS=(
@@ -19,13 +21,27 @@ CheckRoot() {
}
CheckLogrotateInstalled() {
if ! command -v logrotate &> /dev/null; then
LogError "logrotate is not installed. Please install it first:"
LogError " Ubuntu/Debian: sudo apt-get install logrotate"
LogError " CentOS/RHEL: sudo yum install logrotate"
local log_cmd=$(GetLogRotateCommand)
if ! IsLogRotateAvailable; then
LogError "$log_cmd is not installed. Please install it first:"
if [[ "$OS_TYPE" == "macos" ]]; then
LogError " macOS uses newsyslog (built-in) but may need configuration at $LOGROTATE_INSTALL_DIR"
LogError " If directory doesn't exist: sudo mkdir -p $LOGROTATE_INSTALL_DIR"
else
LogError " Ubuntu/Debian: sudo apt-get install logrotate"
LogError " CentOS/RHEL: sudo yum install logrotate"
fi
exit 1
fi
LogInformation "logrotate is installed"
LogInformation "$log_cmd is available"
# Ensure the config directory exists (especially for macOS newsyslog.d)
if [[ ! -d "$LOGROTATE_INSTALL_DIR" ]]; then
LogInformation "Creating config directory: $LOGROTATE_INSTALL_DIR"
mkdir -p "$LOGROTATE_INSTALL_DIR"
chmod 755 "$LOGROTATE_INSTALL_DIR"
fi
}
InstallLogrotateConfigs() {
@@ -60,10 +76,11 @@ InstallLogrotateConfigs() {
}
TestLogrotateConfigs() {
LogInformation "Testing logrotate configurations..."
LogInformation "Testing log rotation configurations..."
local test_passed=0
local test_failed=0
local log_cmd=$(GetLogRotateCommand)
for config_name in "${LOGROTATE_CONFIGS[@]}"; do
local config_file="${LOGROTATE_INSTALL_DIR}/${config_name}"
@@ -74,13 +91,25 @@ TestLogrotateConfigs() {
fi
LogDebug "Testing configuration: $config_name"
if logrotate -d "$config_file" &> /dev/null; then
LogDebug "Test passed: $config_name"
((test_passed++))
if [[ "$OS_TYPE" == "macos" ]]; then
# newsyslog uses different syntax, just check file readability
if [[ -r "$config_file" ]]; then
LogDebug "Test passed: $config_name (file readable)"
((test_passed++))
else
LogError "Test failed: $config_name (file not readable)"
((test_failed++))
fi
else
LogError "Test failed: $config_name"
LogError "Run for details: logrotate -d $config_file"
((test_failed++))
if logrotate -d "$config_file" &> /dev/null; then
LogDebug "Test passed: $config_name"
((test_passed++))
else
LogError "Test failed: $config_name"
LogError "Run for details: logrotate -d $config_file"
((test_failed++))
fi
fi
done
@@ -93,7 +122,11 @@ TestLogrotateConfigs() {
}
ShowStatus() {
LogInformation "Logrotate configurations:"
local log_cmd=$(GetLogRotateCommand)
LogInformation "Log rotation configurations:"
LogInformation "OS: $OS_TYPE"
LogInformation "Tool: $log_cmd"
LogInformation "Install directory: $LOGROTATE_INSTALL_DIR"
LogInformation ""
local found_configs=0
@@ -114,9 +147,15 @@ ShowStatus() {
LogInformation ""
if [[ $found_configs -gt 0 ]]; then
LogInformation "Commands:"
LogInformation " Test rotation: sudo logrotate -f /etc/logrotate.d/<config-name>"
LogInformation " Check status: sudo cat /var/lib/logrotate/status"
LogInformation " Manual run: sudo logrotate /etc/logrotate.conf"
if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation " Manual run: sudo newsyslog -v"
LogInformation " Check config: cat /etc/newsyslog.conf"
LogInformation " Force rotation: sudo newsyslog -F"
else
LogInformation " Test rotation: sudo logrotate -f /etc/logrotate.d/<config-name>"
LogInformation " Check status: sudo cat /var/lib/logrotate/status"
LogInformation " Manual run: sudo logrotate /etc/logrotate.conf"
fi
else
LogWarning "No configurations are currently installed"
fi
+15 -4
View File
@@ -2,12 +2,14 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/../share/logging.sh"
. "${SCRIPT_DIR}/../share/os-detection.sh"
# Configuration
LOGSTASH_CONFIG_DIR="${SCRIPT_DIR}/../logstash"
LOGSTASH_INSTALL_DIR="/etc/logstash"
LOGSTASH_INSTALL_DIR=$(GetLogstashConfigDir)
LOGSTASH_CONF_D_DIR="${LOGSTASH_INSTALL_DIR}/conf.d"
OUTPUT_CONFIG_FILE="conf.d/90-output.conf"
OS_TYPE=$(DetectOS)
CheckRoot() {
if [[ $EUID -ne 0 ]]; then
@@ -143,6 +145,8 @@ ValidateLogstashConfig() {
ShowStatus() {
LogInformation "Logstash configuration status:"
LogInformation "OS: $OS_TYPE"
LogInformation "Install directory: $LOGSTASH_INSTALL_DIR"
LogInformation ""
# Check main config files
@@ -175,9 +179,16 @@ ShowStatus() {
LogInformation ""
LogInformation "Commands:"
LogInformation " Test config: sudo logstash -t --path.settings=/etc/logstash"
LogInformation " Start Logstash: sudo systemctl start logstash"
LogInformation " Check status: sudo systemctl status logstash"
local service_cmd=$(GetServiceCommand)
if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation " Test config: logstash -t --path.settings=$LOGSTASH_INSTALL_DIR"
LogInformation " Start Logstash: $service_cmd start logstash"
LogInformation " Check status: $service_cmd list | grep logstash"
else
LogInformation " Test config: sudo logstash -t --path.settings=/etc/logstash"
LogInformation " Start Logstash: sudo $service_cmd start logstash"
LogInformation " Check status: sudo $service_cmd status logstash"
fi
}
RemoveLogstashConfigs() {
+14 -3
View File
@@ -2,12 +2,14 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/../share/logging.sh"
. "${SCRIPT_DIR}/../share/os-detection.sh"
# Configuration
METRICBEAT_CONFIG_DIR="${SCRIPT_DIR}/../metricbeat"
METRICBEAT_INSTALL_DIR="/etc/metricbeat"
METRICBEAT_INSTALL_DIR=$(GetMetricbeatConfigDir)
METRICBEAT_MODULES_D_DIR="${METRICBEAT_INSTALL_DIR}/modules.d"
CONFIG_FILE="metricbeat.yml"
OS_TYPE=$(DetectOS)
CheckRoot() {
if [[ $EUID -ne 0 ]]; then
@@ -129,6 +131,8 @@ ValidateMetricbeatConfig() {
ShowStatus() {
LogInformation "Metricbeat configuration status:"
LogInformation "OS: $OS_TYPE"
LogInformation "Install directory: $METRICBEAT_INSTALL_DIR"
LogInformation ""
# Check main config file
@@ -157,8 +161,15 @@ ShowStatus() {
LogInformation "Commands:"
LogInformation " Test config: sudo metricbeat test config -c ${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
LogInformation " Test output: sudo metricbeat test output -c ${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
LogInformation " Start Metricbeat: sudo systemctl start metricbeat"
LogInformation " Check status: sudo systemctl status metricbeat"
local service_cmd=$(GetServiceCommand)
if [[ "$OS_TYPE" == "macos" ]]; then
LogInformation " Start Metricbeat: sudo $service_cmd start metricbeat"
LogInformation " Check status: $service_cmd list | grep metricbeat"
else
LogInformation " Start Metricbeat: sudo $service_cmd start metricbeat"
LogInformation " Check status: sudo $service_cmd status metricbeat"
fi
}
RemoveMetricbeatConfigs() {