commit f0ef3afd32add54e4c9a2f9ba42a7f8a3421ed1c Author: Micha vor dem Berge m@mcvdb.de Date: Wed Jan 27 12:19:37 2010 +0000
Added a new module (and a testcase) for ntpd-messages
Signed-off-by: Konstantin Ryabitsev mricon@kernel.org
doc/testcase/ntp.testcase | 16 ++++++ modules/ntp_mod.py | 129 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 0 deletions(-) --- diff --git a/doc/testcase/ntp.testcase b/doc/testcase/ntp.testcase new file mode 100644 index 0000000..7507e89 --- /dev/null +++ b/doc/testcase/ntp.testcase @@ -0,0 +1,16 @@ +Jul 27 00:55:18 trixi ntpd[2852]: synchronized to 213.239.202.209, stratum 2 +Jul 27 02:20:42 trixi ntpd[2852]: synchronized to 78.47.89.89, stratum 2 +Jul 27 03:11:30 trixi ntpd[2852]: synchronized to 213.239.202.209, stratum 2 +Jul 27 00:55:18 foo ntpd[2852]: synchronized to 213.239.202.209, stratum 2 +Jul 27 02:20:42 foo ntpd[2852]: synchronized to 78.47.89.89, stratum 2 +Jul 27 03:11:30 foo ntpd[2852]: synchronized to 213.239.202.209, stratum 2 +Jul 27 02:20:42 trixi ntpd[2852]: synchronized to 78.47.89.89, stratum 1 +Jul 27 03:11:30 trixi ntpd[2852]: synchronized to 213.239.202.209, stratum 1 +Jul 27 00:55:18 foo ntpd[2852]: synchronized to 213.239.202.209, stratum 1 +Jul 27 02:20:42 foo ntpd[2852]: synchronized to 78.47.89.89, stratum 1 +Jan 24 02:25:25 trixi ntpd[3326]: kernel time sync status change 0001 +Jan 24 02:59:35 trixi ntpd[3326]: kernel time sync status change 4001 +Jan 24 03:33:42 trixi ntpd[3326]: kernel time sync status change 0001 +Jan 24 04:07:51 trixi ntpd[3326]: kernel time sync status change 4001 +Jan 24 03:33:42 bar ntpd[3326]: kernel time sync status change 0001 +Jan 24 04:07:51 bar ntpd[3326]: kernel time sync status change 4001 diff --git a/modules/ntp_mod.py b/modules/ntp_mod.py new file mode 100644 index 0000000..fdf94b0 --- /dev/null +++ b/modules/ntp_mod.py @@ -0,0 +1,129 @@ +#!/usr/bin/python -tt +""" +This is an epylog module to monitor output from ntpd. +It considers outputs which look like "status change to ..." +and "synchronized to stratum ..." +""" +## +# Copyright (C) 2009-2010 Micha vor dem Berge +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +# 02111-1307, USA. +# +# $Id: ntp_mod.py 383 2010-01-16 12:45:24Z mvdb $ +# +# @Author Micha vor dem Berge m@mcvdb.de +# @version $Date: 2010-01-16 13:45:24 +0100 (Sa, 16. Jan 2010) $ +# + +import sys +import re + +sys.path.insert(0, '../py/') +from epylog import InternalModule + +class ntp_mod(InternalModule): + def __init__(self, opts, logger): + InternalModule.__init__(self) + self.logger = logger + + rc = re.compile + + self.msg = 1 + self.error = 2 + + self.regex_map = { + rc('ntpd.*:\ssynchronized\sto.*'): self.ntp_synchronized, + rc('kernel time sync status change'): self.ntp_status_change + } + + self.ntp_synchronized_re = rc('ntpd[\d+]:\ssynchronized\sto\s*(\S*),\s*(.*)') + self.ntp_status_change_re = rc('ntpd[\d+]:\skernel\stime\ssync\sstatus\schange\s(\d+)') + + self.ntp_message_title = '<font color="blue">Message</font>' + self.report_wrap = '<table width="100%%" rules="cols" cellpadding="2">%s</table>' + self.subreport_wrap = '<tr><th align="left" colspan="3"><h3>%s</h3></th></tr>\n%s\n' + self.line_rep = '<tr%s><td valign="top" width="15%%">%s</td><td valign="top" width="15%%">%s</td><td width="70%%">%s</td></tr>\n' + self.flip = ' bgcolor="#dddddd"' + + + ## + # Line-matching routines + # + def ntp_synchronized (self, linemap): + action = self.msg + system, message, mult = self.get_smm(linemap) + mo = self.ntp_synchronized_re.search(message) + if not mo: + self.logger.put(3, 'Odd ntp synchronized string: %s' % message) + return None + rhost, stratum = mo.groups() + stratum = "synchronized to " + stratum + restuple = (action, system, stratum, rhost) + return {restuple: linemap['multiplier']} + + + def ntp_status_change (self, linemap): + action = self.msg + system, message, mult = self.get_smm(linemap) + ntp_status = self.ntp_status_change_re.search(message).group(1) + if not ntp_status: + self.logger.put(3, 'Odd ntp status change string: %s' % message) + return None + ntp_status = ntp_status + restuple = (action, system, "status change to", ntp_status) + return {restuple: linemap['multiplier']} + + + def finalize(self, resultset): + logger = self.logger + + report = '' + rep = {} + for action in [self.msg]: + rep[action] = '' + flipper = '' + for key in resultset.get_distinct((action,)): + if flipper: flipper = '' + else: flipper = self.flip + ntp_msg_rep = [] + for ntp_msg in resultset.get_distinct((action, key)): + mymap = resultset.get_submap((action, key, ntp_msg)) + key2s = [] + for key2 in mymap.keys(): + loghost = key2[0] + key2s.append('%s (%d)' % (loghost, mymap[key2])) + loghosts = ', '.join(key2s) + ntp_msg_rep.append([ntp_msg, loghosts]) + #if len(key2s) > self.systems_collapse: + # loghosts = self.collapsed_rep % (key2s[0],len(key2s)-1) + #else: + blank = 0 + for msgrep in ntp_msg_rep: + if blank: key = ' ' + else: blank = 1 + rep[action] += self.line_rep % (flipper, + key, msgrep[0], msgrep[1]) + if rep[self.msg]: + report += self.subreport_wrap % (self.ntp_message_title, + rep[self.msg]) + + report = self.report_wrap % report + return report + + +if __name__ == '__main__': + from epylog.helpers import ModuleTest + ModuleTest(ntp_mod, sys.argv)