Back to Resources

Level Verified

Linux Monitor - System Time Check Script

Created by

Level

Type

Script

Category

General

Platforms
WindowsApple iOSLinux

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.

Description

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.

Script

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

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

FAQ

  • How do I run this script in Level?
    Import it into Level and add it as a resource. Configure the script-based monitor or schedule an Automation for periodic checks.
  • Why does it set the time zone to UTC?
    UTC is a universal standard for timekeeping, minimizing discrepancies across distributed systems.
  • What permissions are required?
    The script runs with System or Root permissions under Level, ensuring it can modify time zone settings and check services.
  • What if my distribution doesn’t support timedatectl?
    This script relies on timedatectl. Some older distros may not include it by default, so verify your system compatibility.
  • Can I keep a different time zone?
    Yes. Update or remove the relevant lines in the script if you prefer to maintain a non-UTC time zone.

Included with this Script:

Below is a list of what you can expect to find when importing this Script.

Script details:

The following data and settings will be imported with your script.

Script Name

Linux Monitor - System Time Check

Description

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.

Language

Bash

Timeout (In Seconds)

100

Run As

Local system

Import into Level

Related resources

Explore more automations, scripts, and policies to further enhance your IT operations.

View all resources