[Pkg-utopia-maintainers] Bug#627343: Improved Workaround for DHCP lease NTP servers with NetworkManager
ste-fan
s.t.e-f.a.n at web.de
Mon Oct 6 22:07:29 UTC 2014
Hi there,
since this issue really bugged me as well (why generate tons of public NTP
traffic if you have a server in-house?! [1]) I proudly present my
/etc/default/ntpdate:
https://gist.github.com/ste-fan/7b9b5bcf6656f59c280f [2]
I tried to clean Jan's "one"-liner up a bit. Basically it still does the
same
wonderful thing, but here's a quick changelog:
* updated DHCP lease folder to /var/lib/NetworkManager
+ of course there we must consider *.lease files only
* rewrote the whole method as a function (hopefully easier to read)
* reduced the use of redirection and temporary variables using slightly more
complex sed scripts and utilizing more of find's capabilites
* added debug output that responds to ntpdate's -d option and will also be
logged to syslog if the -s option is given (which is provided by
/etc/network/if-up.d/ntpdate)
* nevertheless there is no namespace pollution, thanks to
+ exclusively local function variables
+ unsetting function definitions after execution
* the upper (default) config part can be left as is!
+ if NTP servers are found in the latest lease, NTPSERVERS will get
updated
+ otherwise it won't be touched (keeping the above specified servers)
+ NTPDATE_USE_NTP_CONF can be left "yes" if you don't have any
ntp(d).conf*
files that could be read by ntpdate-debian
(namely: /var/lib/ntp/ntp.conf.dhcp /etc/ntp.conf
/etc/openntpd/ntpd.conf)
Well, now I hope someone finds this useful!
Cheers
~ste-fan
[1] OK, you could configure the in-house NTP server statically on each
client.
But ntpdate was made for PCs that do not run 24/7 and even Laptops that
move around, right? So we need a dynamic approach with fallback NTP
servers
that are used if the current DHCP server does not supply NTP server
information.
[2]
# The settings in this file are used by the program ntpdate-debian, but not
# by the upstream program ntpdate.
# Set to "yes" to take the server list from /etc/ntp.conf, from package ntp,
# so you only have to keep it in one place.
NTPDATE_USE_NTP_CONF=yes
# List of NTP servers to use (Separate multiple servers with spaces.)
# Not used if NTPDATE_USE_NTP_CONF is yes.
NTPSERVERS="ntp.ubuntu.com"
# Additional options to pass to ntpdate
NTPOPTIONS=""
# extract NTP servers from latest DHCP lease (if option was given by server)
get_debug_opts() {
local opt
debug_opt=false
debug_syslog=false
for opt in "$@" $( xargs -0 < /proc/$$/cmdline ); do
case "$opt" in
-d) debug_opt=true ;;
-s) debug_syslog=true ;;
esac
done
return 0
}
dhcp_ntp_debug_msg() {
if $debug_opt; then
echo "$(LC_ALL=C date +'%_d %b %H:%M:%S')" "ntpdate[$$]:" \
'(DCHP)' "$@"
if $debug_syslog; then
logger --tag "ntpdate[$$]" '(DCHP)' "$@"
fi
fi
return 0
}
get_dhcp_ntp_servers() {
local debug_opt debug_syslog leasefiledir recentleasefile ntpservers
leasefiledir=/var/lib/NetworkManager
# check whether we are in debug mode and logging to syslog
get_debug_opts $NTPOPTIONS
# get most recent lease file that is not older than 1 minute
recentleasefile=$( find $leasefiledir ! -type d -iname '*.lease' \
! -mmin +1 -printf '%T@ %p\n' 2>/dev/null \
| sort -k 1nr | sed 's/^\S\+ //; 1q' )
if [ -n "$recentleasefile" ]; then
dhcp_ntp_debug_msg 'Found a recent lease file:' \
$( basename $recentleasefile )
# extract NTP servers, if any (check only latest lease in file)
ntpservers=$( sed '1,/lease\s\+{/d
/option ntp-servers/!d
s/^.*ntp-servers\s\+\(.\+\);$/\1/; s/,/ /g' \
$recentleasefile )
if [ -n "$ntpservers" ]; then
dhcp_ntp_debug_msg 'Found NTP server(s) in latest lease:' \
$ntpservers
NTPSERVERS="$ntpservers"
else
dhcp_ntp_debug_msg 'Latest lease does not contain any NTP
servers.'
fi
else
dhcp_ntp_debug_msg 'There is no recent lease file.'
fi
return 0
}
get_dhcp_ntp_servers
unset get_debug_opts dhcp_ntp_debug_msg get_dhcp_ntp_servers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.alioth.debian.org/pipermail/pkg-utopia-maintainers/attachments/20141007/feb7fe90/attachment.html>
More information about the Pkg-utopia-maintainers
mailing list