Back to Resources
Level
Script
General
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.
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.
1#!/bin/bash
2
3# This resource is provided as a convenience for Level users. We cannot
4# guarantee it will work in all environments. Please test before deploying
5# to your production environment. We welcome contributions to our community
6# library
7
8# Level Library
9# https://level.io/library/script-linux-monitor-system-time-check
10
11# Set the target time zone
12TARGET_TIMEZONE="{{cf_timezone}}"
13
14# Variables to hold checks
15check_count=0
16checks_failed=""
17
18# Capture the output of timedatectl
19output=$(timedatectl)
20
21# Check and display the current time zone
22timezone=$(echo "$output" | grep 'Time zone:' | awk '{print $3}')
23echo "Current Time Zone: $timezone"
24
25# Check if the time zone matches the target
26if [[ $timezone != "$TARGET_TIMEZONE" ]]; then
27 echo "ALERT: Time zone is not $TARGET_TIMEZONE. Attempting to set to $TARGET_TIMEZONE..."
28 sudo timedatectl set-timezone "$TARGET_TIMEZONE"
29 if [[ $? -eq 0 ]]; then
30 echo "Time zone set to $TARGET_TIMEZONE successfully."
31 else
32 echo "Failed to set time zone to $TARGET_TIMEZONE."
33 check_count=$((check_count + 1))
34 checks_failed+=" Failed to set time zone to $TARGET_TIMEZONE."
35 fi
36fi
37
38# Check if the system clock is synchronized
39clock_sync=$(echo "$output" | grep 'System clock synchronized:' | awk '{print $4}')
40if [[ $clock_sync != "yes" ]]; then
41 echo "ALERT: System clock is not synchronized."
42 check_count=$((check_count + 1))
43 checks_failed+=" System clock is not synchronized."
44fi
45
46# Check if NTP service is active
47# Debian
48ntp_service_debian=$(echo "$output" | grep 'NTP service:' | awk '{print $3}')
49# Ubuntu
50ntp_service_ubuntu=$(echo "$output" | grep 'systemd-timesyncd.service active:' | awk '{print $3}')
51
52if [[ $ntp_service_debian != "active" ]] && [[ $ntp_service_ubuntu != "yes" ]]; then
53 echo "ALERT: NTP service is not active."
54 check_count=$((check_count + 1))
55 checks_failed+=" NTP service is not active."
56fi
57
58# Final check summary
59if [ $check_count -gt 0 ]; then
60 echo "ALERT: $check_count checks failed."
61 echo "Checks Failed: $checks_failed"
62 exit 1
63else
64 echo "SUCCESS: All checks passed."
65 exit 0
66fi
timedatectl
?timedatectl
. Some older distros may not include it by default, so verify your system compatibility.Linux Monitor - System Time Check
This script validates critical time-related configurations on a Linux system. It checks if the system's time zone is set to UTC, ensures the system clock is synchronized, and verifies that the NTP service is active. Any discrepancies trigger alerts, and the script attempts to correct the time zone if it's not UTC. A summary of failed checks is displayed at the end, providing detailed feedback for system administrators.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.