Problem overview
Time drift can cause issues with logging, scheduling, and security across Linux environments, often leading to missed backups and failed authentication checks. This script helps IT Professionals and MSPs maintain consistent time settings, protecting against operational disruptions and compliance risks.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-linux-monitor-system-time-check
# Set the target time zone
TARGET_TIMEZONE="{{cf_timezone}}"
# Variables to hold checks
check_count=0
checks_failed=""
# Capture the output of timedatectl
output=$(timedatectl)
# Check and display the current time zone
timezone=$(echo "$output" | grep 'Time zone:' | awk '{print $3}')
echo "Current Time Zone: $timezone"
# Check if the time zone matches the target
if [[ $timezone != "$TARGET_TIMEZONE" ]]; then
echo "ALERT: Time zone is not $TARGET_TIMEZONE. Attempting to set to $TARGET_TIMEZONE..."
sudo timedatectl set-timezone "$TARGET_TIMEZONE"
if [[ $? -eq 0 ]]; then
echo "Time zone set to $TARGET_TIMEZONE successfully."
else
echo "Failed to set time zone to $TARGET_TIMEZONE."
check_count=$((check_count + 1))
checks_failed+=" Failed to set time zone to $TARGET_TIMEZONE."
fi
fi
# Check if the system clock is synchronized
clock_sync=$(echo "$output" | grep 'System clock synchronized:' | awk '{print $4}')
if [[ $clock_sync != "yes" ]]; then
echo "ALERT: System clock is not synchronized."
check_count=$((check_count + 1))
checks_failed+=" System clock is not synchronized."
fi
# Check if NTP service is active
# Debian
ntp_service_debian=$(echo "$output" | grep 'NTP service:' | awk '{print $3}')
# Ubuntu
ntp_service_ubuntu=$(echo "$output" | grep 'systemd-timesyncd.service active:' | awk '{print $3}')
if [[ $ntp_service_debian != "active" ]] && [[ $ntp_service_ubuntu != "yes" ]]; then
echo "ALERT: NTP service is not active."
check_count=$((check_count + 1))
checks_failed+=" NTP service is not active."
fi
# Final check summary
if [ $check_count -gt 0 ]; then
echo "ALERT: $check_count checks failed."
echo "Checks Failed: $checks_failed"
exit 1
else
echo "SUCCESS: All checks passed."
exit 0
fi
This script inspects the current time zone, system clock synchronization, and NTP service status on a Linux system. If the time zone is not UTC, it automatically attempts to correct it by setting the system to UTC. It then checks whether the system clock is synchronized and whether the NTP service is active, generating alerts or errors if any step fails. This script uses a Level Custom Field, Timezone.
You can configure this script as part of a script-based monitor in Level to instantly detect and remedy time-related issues. Alternatively, you can integrate it into a scheduled Automation in Level to regularly verify and correct the system time settings, helping maintain an accurate network environment around the clock.
Use cases
- Ensuring consistent time settings on production servers
- Detecting potential drift across distributed environments
- Automating clock synchronization in compliance-focused industries
- Quickly verifying NTP service activity across multiple systems
Recommendations
- Test this script in a non-production environment before deploying
- Use a script-based monitor in Level to trigger immediate alerts if time drift is detected
- Consider scheduling it via an Automation in Level for regular, hands-off verification
- Verify you have internet connectivity for NTP synchronization
- Check script output carefully for any failed checks