]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
examples: improve NetworkManager dispatcher script
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 3 Aug 2017 14:13:05 +0000 (16:13 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 9 Aug 2017 07:57:13 +0000 (09:57 +0200)
When no default route is configured, check each source if it has a
route. If the system has multiple network interfaces, this prevents
setting local NTP servers to offline when they can still be reached over
one of the interfaces.

examples/chrony.nm-dispatcher

index 084aed64e0d729c99fa1746b0d0e632b772f7144..51d7fa2588b68aef70626890126d2aaad3332c19 100644 (file)
@@ -1,17 +1,35 @@
 #!/bin/sh
 # This is a NetworkManager dispatcher script for chronyd to set its NTP sources
-# online/offline when a default route is configured/removed on the system.
+# online or offline when a network interface is configured or removed
 
 export LC_ALL=C
 
-if [ "$2" = "up" ]; then
-       /sbin/ip route list dev "$1" | grep -q '^default' &&
-               /usr/bin/chronyc online > /dev/null 2>&1
-fi
+# Check if there is a default route
 
-if [ "$2" = "down" ]; then
-       /sbin/ip route list | grep -q '^default' ||
-               /usr/bin/chronyc offline > /dev/null 2>&1
+if /sbin/ip route list 2> /dev/null | grep -q '^default'; then
+  chronyc online > /dev/null 2>&1
+  exit 0
 fi
 
+sources=$(chronyc -c -n sources 2> /dev/null)
+
+[ $? -ne 0 ] && exit 0
+
+# Check each configured source if it has a route
+
+echo "$sources" | while IFS=, read mode state address rest; do
+  [ "$mode" != '^' ] && [ "$mode" != '=' ] && continue
+
+  /sbin/ip route get "$address" > /dev/null 2>&1 && command="online" || command="offline"
+
+  # Set priority of sources so that the selected source is set as
+  # last if offline to avoid unnecessary reselection
+  [ "$state" != '*' ] && priority=1 || priority=2
+
+  echo "$priority $command $address"
+
+done | sort | while read priority command address; do
+  echo "$command $address"
+done | chronyc > /dev/null 2>&1
+
 exit 0