mirror of
https://github.com/9technologygroup/patchmon.net.git
synced 2025-11-09 08:26:16 +00:00
Fixed agent outputs and improved crontab chancing logics, added timedatectl functionality
This commit is contained in:
@@ -25,7 +25,10 @@ NC='\033[0m' # No Color
|
|||||||
|
|
||||||
# Logging function
|
# Logging function
|
||||||
log() {
|
log() {
|
||||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
# Try to write to log file, but don't fail if we can't
|
||||||
|
if [[ -w "$(dirname "$LOG_FILE")" ]] 2>/dev/null; then
|
||||||
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE" 2>/dev/null
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Error handling
|
# Error handling
|
||||||
@@ -35,21 +38,21 @@ error() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Info logging
|
# Info logging (cleaner output - only stdout, no duplicate logging)
|
||||||
info() {
|
info() {
|
||||||
echo -e "${BLUE}INFO: $1${NC}"
|
echo -e "${BLUE}ℹ️ $1${NC}"
|
||||||
log "INFO: $1"
|
log "INFO: $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Success logging
|
# Success logging (cleaner output - only stdout, no duplicate logging)
|
||||||
success() {
|
success() {
|
||||||
echo -e "${GREEN}SUCCESS: $1${NC}"
|
echo -e "${GREEN}✅ $1${NC}"
|
||||||
log "SUCCESS: $1"
|
log "SUCCESS: $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Warning logging
|
# Warning logging (cleaner output - only stdout, no duplicate logging)
|
||||||
warning() {
|
warning() {
|
||||||
echo -e "${YELLOW}WARNING: $1${NC}"
|
echo -e "${YELLOW}⚠️ $1${NC}"
|
||||||
log "WARNING: $1"
|
log "WARNING: $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,6 +63,31 @@ check_root() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Verify system datetime and timezone
|
||||||
|
verify_datetime() {
|
||||||
|
info "Verifying system datetime and timezone..."
|
||||||
|
|
||||||
|
# Get current system time
|
||||||
|
local system_time=$(date)
|
||||||
|
local timezone="Unknown"
|
||||||
|
|
||||||
|
# Try to get timezone with timeout protection
|
||||||
|
if command -v timedatectl >/dev/null 2>&1; then
|
||||||
|
timezone=$(timedatectl show --property=Timezone --value 2>/dev/null || echo "Unknown")
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Log datetime info (non-blocking)
|
||||||
|
log "System datetime check - time: $system_time, timezone: $timezone" 2>/dev/null || true
|
||||||
|
|
||||||
|
# Simple check - just log the info, don't block execution
|
||||||
|
if [[ "$timezone" == "Unknown" ]] || [[ -z "$timezone" ]]; then
|
||||||
|
warning "System timezone not configured: $timezone"
|
||||||
|
log "WARNING: System timezone not configured - timezone: $timezone" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
# Create necessary directories
|
# Create necessary directories
|
||||||
setup_directories() {
|
setup_directories() {
|
||||||
mkdir -p /etc/patchmon
|
mkdir -p /etc/patchmon
|
||||||
@@ -786,19 +814,16 @@ get_system_info() {
|
|||||||
send_update() {
|
send_update() {
|
||||||
load_credentials
|
load_credentials
|
||||||
|
|
||||||
info "Collecting package information..."
|
# Verify datetime before proceeding
|
||||||
local packages_json=$(get_package_info)
|
if ! verify_datetime; then
|
||||||
|
warning "Datetime verification failed, but continuing with update..."
|
||||||
info "Collecting repository information..."
|
fi
|
||||||
local repositories_json=$(get_repository_info)
|
|
||||||
|
|
||||||
info "Collecting hardware information..."
|
|
||||||
local hardware_json=$(get_hardware_info)
|
|
||||||
|
|
||||||
info "Collecting network information..."
|
|
||||||
local network_json=$(get_network_info)
|
|
||||||
|
|
||||||
info "Collecting system information..."
|
info "Collecting system information..."
|
||||||
|
local packages_json=$(get_package_info)
|
||||||
|
local repositories_json=$(get_repository_info)
|
||||||
|
local hardware_json=$(get_hardware_info)
|
||||||
|
local network_json=$(get_network_info)
|
||||||
local system_json=$(get_system_info)
|
local system_json=$(get_system_info)
|
||||||
|
|
||||||
info "Sending update to PatchMon server..."
|
info "Sending update to PatchMon server..."
|
||||||
@@ -833,46 +858,36 @@ EOF
|
|||||||
|
|
||||||
if [[ $? -eq 0 ]]; then
|
if [[ $? -eq 0 ]]; then
|
||||||
if echo "$response" | grep -q "success"; then
|
if echo "$response" | grep -q "success"; then
|
||||||
success "Update sent successfully"
|
local packages_count=$(echo "$response" | grep -o '"packagesProcessed":[0-9]*' | cut -d':' -f2)
|
||||||
echo "$response" | grep -o '"packagesProcessed":[0-9]*' | cut -d':' -f2 | xargs -I {} info "Processed {} packages"
|
success "Update sent successfully (${packages_count} packages processed)"
|
||||||
|
|
||||||
# Check if auto-update is enabled and check for agent updates locally
|
# Check if auto-update is enabled and check for agent updates locally
|
||||||
if check_auto_update_enabled; then
|
if check_auto_update_enabled; then
|
||||||
info "Auto-update is enabled, checking for agent updates..."
|
info "Checking for agent updates..."
|
||||||
if check_agent_update_needed; then
|
if check_agent_update_needed; then
|
||||||
info "Agent update available, automatically updating..."
|
info "Agent update available, updating..."
|
||||||
if "$0" update-agent; then
|
if "$0" update-agent; then
|
||||||
success "PatchMon agent update completed successfully"
|
success "Agent updated successfully"
|
||||||
else
|
else
|
||||||
warning "PatchMon agent update failed, but data was sent successfully"
|
warning "Agent update failed, but data was sent successfully"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
info "Agent is up to date"
|
info "Agent is up to date"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check for crontab update instructions (look specifically in crontabUpdate section)
|
# Automatically check if crontab needs updating based on server settings
|
||||||
if echo "$response" | grep -q '"crontabUpdate":{'; then
|
info "Checking crontab configuration..."
|
||||||
local crontab_update_section=$(echo "$response" | grep -o '"crontabUpdate":{[^}]*}')
|
"$0" update-crontab
|
||||||
local should_update_crontab=$(echo "$crontab_update_section" | grep -o '"shouldUpdate":true' | cut -d':' -f2)
|
local crontab_exit_code=$?
|
||||||
if [[ "$should_update_crontab" == "true" ]]; then
|
if [[ $crontab_exit_code -eq 0 ]]; then
|
||||||
local crontab_message=$(echo "$crontab_update_section" | grep -o '"message":"[^"]*' | cut -d'"' -f4)
|
|
||||||
local crontab_command=$(echo "$crontab_update_section" | grep -o '"command":"[^"]*' | cut -d'"' -f4)
|
|
||||||
|
|
||||||
if [[ -n "$crontab_message" ]]; then
|
|
||||||
info "Crontab update detected: $crontab_message"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$crontab_command" == "update-crontab" ]]; then
|
|
||||||
info "Automatically updating crontab with new interval..."
|
|
||||||
if "$0" update-crontab; then
|
|
||||||
success "Crontab updated successfully"
|
success "Crontab updated successfully"
|
||||||
|
elif [[ $crontab_exit_code -eq 2 ]]; then
|
||||||
|
# Already up to date - no additional message needed
|
||||||
|
true
|
||||||
else
|
else
|
||||||
warning "Crontab update failed, but data was sent successfully"
|
warning "Crontab update failed, but data was sent successfully"
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
error "Update failed: $response"
|
error "Update failed: $response"
|
||||||
fi
|
fi
|
||||||
@@ -898,7 +913,7 @@ ping_server() {
|
|||||||
info "Connected as host: $hostname"
|
info "Connected as host: $hostname"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check for crontab update trigger
|
# Check for crontab update instructions
|
||||||
local should_update_crontab=$(echo "$response" | grep -o '"shouldUpdate":true' | cut -d':' -f2)
|
local should_update_crontab=$(echo "$response" | grep -o '"shouldUpdate":true' | cut -d':' -f2)
|
||||||
if [[ "$should_update_crontab" == "true" ]]; then
|
if [[ "$should_update_crontab" == "true" ]]; then
|
||||||
local message=$(echo "$response" | grep -o '"message":"[^"]*' | cut -d'"' -f4)
|
local message=$(echo "$response" | grep -o '"message":"[^"]*' | cut -d'"' -f4)
|
||||||
@@ -909,11 +924,16 @@ ping_server() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$command" == "update-crontab" ]]; then
|
if [[ "$command" == "update-crontab" ]]; then
|
||||||
info "Automatically updating crontab with new interval..."
|
info "Updating crontab with new interval..."
|
||||||
if "$0" update-crontab; then
|
"$0" update-crontab
|
||||||
|
local crontab_exit_code=$?
|
||||||
|
if [[ $crontab_exit_code -eq 0 ]]; then
|
||||||
success "Crontab updated successfully"
|
success "Crontab updated successfully"
|
||||||
|
elif [[ $crontab_exit_code -eq 2 ]]; then
|
||||||
|
# Already up to date - no additional message needed
|
||||||
|
true
|
||||||
else
|
else
|
||||||
warning "Crontab update failed, but ping was successful"
|
warning "Crontab update failed, but data was sent successfully"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -1144,7 +1164,7 @@ update_crontab() {
|
|||||||
# Check if crontab needs updating
|
# Check if crontab needs updating
|
||||||
if [[ "$current_patchmon_entry" == "$expected_crontab" ]]; then
|
if [[ "$current_patchmon_entry" == "$expected_crontab" ]]; then
|
||||||
info "Crontab is already up to date (interval: $update_interval minutes)"
|
info "Crontab is already up to date (interval: $update_interval minutes)"
|
||||||
return 0
|
return 2 # Special return code for "already up to date"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
info "Setting update interval to $update_interval minutes"
|
info "Setting update interval to $update_interval minutes"
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ set -e
|
|||||||
# This placeholder will be dynamically replaced by the server when serving this
|
# This placeholder will be dynamically replaced by the server when serving this
|
||||||
# script based on the "ignore SSL self-signed" setting. If set to -k, curl will
|
# script based on the "ignore SSL self-signed" setting. If set to -k, curl will
|
||||||
# ignore certificate validation. Otherwise, it will be empty for secure default.
|
# ignore certificate validation. Otherwise, it will be empty for secure default.
|
||||||
CURL_FLAGS=""
|
# CURL_FLAGS is now set via environment variables by the backend
|
||||||
|
|
||||||
# Colors for output
|
# Colors for output
|
||||||
RED='\033[0;31m'
|
RED='\033[0;31m'
|
||||||
@@ -40,6 +40,60 @@ if [[ $EUID -ne 0 ]]; then
|
|||||||
error "This script must be run as root (use sudo)"
|
error "This script must be run as root (use sudo)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Verify system datetime and timezone
|
||||||
|
verify_datetime() {
|
||||||
|
info "🕐 Verifying system datetime and timezone..."
|
||||||
|
|
||||||
|
# Get current system time
|
||||||
|
local system_time=$(date)
|
||||||
|
local timezone=$(timedatectl show --property=Timezone --value 2>/dev/null || echo "Unknown")
|
||||||
|
|
||||||
|
# Display current datetime info
|
||||||
|
echo ""
|
||||||
|
echo -e "${BLUE}📅 Current System Date/Time:${NC}"
|
||||||
|
echo " • Date/Time: $system_time"
|
||||||
|
echo " • Timezone: $timezone"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Check if we can read from stdin (interactive terminal)
|
||||||
|
if [[ -t 0 ]]; then
|
||||||
|
# Interactive terminal - ask user
|
||||||
|
read -p "Does this date/time look correct to you? (y/N): " -r response
|
||||||
|
if [[ "$response" =~ ^[Yy]$ ]]; then
|
||||||
|
success "✅ Date/time verification passed"
|
||||||
|
echo ""
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
echo ""
|
||||||
|
echo -e "${RED}❌ Date/time verification failed${NC}"
|
||||||
|
echo ""
|
||||||
|
echo -e "${YELLOW}💡 Please fix the date/time and re-run the installation script:${NC}"
|
||||||
|
echo " sudo timedatectl set-time 'YYYY-MM-DD HH:MM:SS'"
|
||||||
|
echo " sudo timedatectl set-timezone 'America/New_York' # or your timezone"
|
||||||
|
echo " sudo timedatectl list-timezones # to see available timezones"
|
||||||
|
echo ""
|
||||||
|
echo -e "${BLUE}ℹ️ After fixing the date/time, re-run this installation script.${NC}"
|
||||||
|
error "Installation cancelled - please fix date/time and re-run"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Non-interactive (piped from curl) - show warning and continue
|
||||||
|
echo -e "${YELLOW}⚠️ Non-interactive installation detected${NC}"
|
||||||
|
echo ""
|
||||||
|
echo "Please verify the date/time shown above is correct."
|
||||||
|
echo "If the date/time is incorrect, it may cause issues with:"
|
||||||
|
echo " • Logging timestamps"
|
||||||
|
echo " • Scheduled updates"
|
||||||
|
echo " • Data synchronization"
|
||||||
|
echo ""
|
||||||
|
echo -e "${GREEN}✅ Continuing with installation...${NC}"
|
||||||
|
success "✅ Date/time verification completed (assumed correct)"
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Run datetime verification
|
||||||
|
verify_datetime
|
||||||
|
|
||||||
# Clean up old files (keep only last 3 of each type)
|
# Clean up old files (keep only last 3 of each type)
|
||||||
cleanup_old_files() {
|
cleanup_old_files() {
|
||||||
# Clean up old credential backups
|
# Clean up old credential backups
|
||||||
@@ -64,6 +118,15 @@ info "🚀 Starting PatchMon Agent Installation..."
|
|||||||
info "📋 Server: $PATCHMON_URL"
|
info "📋 Server: $PATCHMON_URL"
|
||||||
info "🔑 API ID: ${API_ID:0:16}..."
|
info "🔑 API ID: ${API_ID:0:16}..."
|
||||||
|
|
||||||
|
# Display diagnostic information
|
||||||
|
echo ""
|
||||||
|
echo -e "${BLUE}🔧 Installation Diagnostics:${NC}"
|
||||||
|
echo " • URL: $PATCHMON_URL"
|
||||||
|
echo " • CURL FLAGS: $CURL_FLAGS"
|
||||||
|
echo " • API ID: ${API_ID:0:16}..."
|
||||||
|
echo " • API Key: ${API_KEY:0:16}..."
|
||||||
|
echo ""
|
||||||
|
|
||||||
# Install required dependencies
|
# Install required dependencies
|
||||||
info "📦 Installing required dependencies..."
|
info "📦 Installing required dependencies..."
|
||||||
|
|
||||||
@@ -175,7 +238,7 @@ fi
|
|||||||
# Step 4: Test the configuration
|
# Step 4: Test the configuration
|
||||||
info "🧪 Testing API credentials and connectivity..."
|
info "🧪 Testing API credentials and connectivity..."
|
||||||
if /usr/local/bin/patchmon-agent.sh test; then
|
if /usr/local/bin/patchmon-agent.sh test; then
|
||||||
success "✅ API credentials are valid and server is reachable"
|
success "✅ TEST: API credentials are valid and server is reachable"
|
||||||
else
|
else
|
||||||
error "❌ Failed to validate API credentials or reach server"
|
error "❌ Failed to validate API credentials or reach server"
|
||||||
fi
|
fi
|
||||||
@@ -183,7 +246,7 @@ fi
|
|||||||
# Step 5: Send initial data
|
# Step 5: Send initial data
|
||||||
info "📊 Sending initial package data to server..."
|
info "📊 Sending initial package data to server..."
|
||||||
if /usr/local/bin/patchmon-agent.sh update; then
|
if /usr/local/bin/patchmon-agent.sh update; then
|
||||||
success "✅ Initial package data sent successfully"
|
success "✅ UPDATE: Initial package data sent successfully"
|
||||||
else
|
else
|
||||||
warning "⚠️ Failed to send initial data. You can retry later with: /usr/local/bin/patchmon-agent.sh update"
|
warning "⚠️ Failed to send initial data. You can retry later with: /usr/local/bin/patchmon-agent.sh update"
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -1011,7 +1011,7 @@ const CredentialsModal = ({ host, isOpen, onClose }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const getCurlFlags = () => {
|
const getCurlFlags = () => {
|
||||||
return settings?.ignore_ssl_self_signed ? "-ks" : "-s";
|
return settings?.ignore_ssl_self_signed ? "-sk" : "-s";
|
||||||
};
|
};
|
||||||
|
|
||||||
const copyToClipboard = async (text) => {
|
const copyToClipboard = async (text) => {
|
||||||
|
|||||||
1
test_agent_functions.sh
Normal file
1
test_agent_functions.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_auto_crontab.sh
Normal file
1
test_auto_crontab.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_clean_output.sh
Normal file
1
test_clean_output.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_crontab_fix.sh
Normal file
1
test_crontab_fix.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_curl_fix.sh
Normal file
1
test_curl_fix.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_curl_flags.sh
Normal file
1
test_curl_flags.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_curl_flags_fix.sh
Normal file
1
test_curl_flags_fix.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_datetime_verification.sh
Normal file
1
test_datetime_verification.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_fixed_read.sh
Normal file
1
test_fixed_read.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_improved_datetime.sh
Normal file
1
test_improved_datetime.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_install_flow.sh
Normal file
1
test_install_flow.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_log_fix.sh
Normal file
1
test_log_fix.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_non_interactive_datetime.sh
Normal file
1
test_non_interactive_datetime.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_rhel_compatibility.sh
Normal file
1
test_rhel_compatibility.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_simple_datetime.sh
Normal file
1
test_simple_datetime.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
test_single_line_datetime.sh
Normal file
1
test_single_line_datetime.sh
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
Reference in New Issue
Block a user