thomas - r7252 - in flumotion/trunk: . flumotion/monitor/nagios
flumotion-commit at lists.fluendo.com
flumotion-commit at lists.fluendo.com
Wed Aug 20 15:49:09 CEST 2008
Author: thomas
Date: Wed Aug 20 15:49:09 2008
New Revision: 7252
Log:
* flumotion/monitor/nagios/Makefile.am:
* flumotion/monitor/nagios/main.py:
* flumotion/monitor/nagios/log.py (added):
Add implementation of flumotion-nagios log recent which can
check if a given string was logged recently.
Added:
flumotion/trunk/flumotion/monitor/nagios/log.py
Modified:
flumotion/trunk/ChangeLog
flumotion/trunk/flumotion/monitor/nagios/Makefile.am
flumotion/trunk/flumotion/monitor/nagios/main.py
Modified: flumotion/trunk/ChangeLog
==============================================================================
--- flumotion/trunk/ChangeLog (original)
+++ flumotion/trunk/ChangeLog Wed Aug 20 15:49:09 2008
@@ -1,3 +1,11 @@
+2008-08-20 Thomas Vander Stichele <thomas at apestaart dot org>
+
+ * flumotion/monitor/nagios/Makefile.am:
+ * flumotion/monitor/nagios/main.py:
+ * flumotion/monitor/nagios/log.py (added):
+ Add implementation of flumotion-nagios log recent which can
+ check if a given string was logged recently.
+
2008-08-20 Johan Dahlin <johan at gnome.org>
* flumotion/admin/assistant/Makefile.am (component_PYTHON):
Modified: flumotion/trunk/flumotion/monitor/nagios/Makefile.am
==============================================================================
--- flumotion/trunk/flumotion/monitor/nagios/Makefile.am (original)
+++ flumotion/trunk/flumotion/monitor/nagios/Makefile.am Wed Aug 20 15:49:09 2008
@@ -4,6 +4,7 @@
flumotion_PYTHON = \
__init__.py \
+ log.py \
util.py \
process.py \
main.py
Added: flumotion/trunk/flumotion/monitor/nagios/log.py
==============================================================================
--- (empty file)
+++ flumotion/trunk/flumotion/monitor/nagios/log.py Wed Aug 20 15:49:09 2008
@@ -0,0 +1,111 @@
+# -*- Mode: Python -*-
+# vi:si:et:sw=4:sts=4:ts=4
+
+import os
+import commands
+import time
+
+# F0.8
+# makes for an easier backport to platform-3
+try:
+ from flumotion.common.format import formatTime
+except ImportError:
+ from flumotion.common.common import formatTime
+
+from flumotion.monitor.nagios import util
+
+__version__ = "$Rev: 6687 $"
+
+
+class Recent(util.LogCommand):
+ summary = 'check for recent logging of a string'
+ description = """
+Check the log file to assure that the given string was logged recently.
+
+The given string will be passed to grep.
+
+Note that Flumotion log files do not log the year, so this check assumes
+that log lines are from the current year, or the previous year if the log
+line would end up being in the future.
+
+Note that Flumotion log files log in the server's local time, and this check
+compares against the local time too, so the server logging and the server
+checking should be in the same timezone.
+"""
+ usage = "recent [-s string] log-file-path"
+
+ def addOptions(self):
+ self.parser.add_option('-s', '--string',
+ action="store", dest="string",
+ help="string to grep for in the log",
+ default="")
+ default = 3600
+ self.parser.add_option('-w', '--warning',
+ action="store", dest="warning",
+ help="age to warn for, in seconds (defaults to %r)" %
+ default, default=default)
+ default = 7200
+ self.parser.add_option('-c', '--critical',
+ action="store", dest="critical",
+ help="age to critical for, in seconds (defaults to %r)" %
+ default, default=default)
+
+ def do(self, args):
+ if not args:
+ return util.unknown('Please specify a log file to check.')
+ if len(args) > 1:
+ return util.unknown('Please specify only one log file to check.')
+
+ command = "grep '%s' %s | tail -n 1" % (
+ self.options.string, " ".join(args))
+ self.debug('executing %s' % command)
+ output = commands.getoutput(command)
+ self.debug('output: %s' % output)
+
+ if not output:
+ return util.unknown('Could not find string %s in log file' %
+ self.options.string)
+
+ level = output[:5].strip()
+ if level not in ['ERROR', 'WARN', 'INFO', 'DEBUG', 'LOG']:
+ return util.unknown("Last line is not a log line: '%s'" % output)
+
+ # matches flumotion.extern.log.log
+ # level pid object cat time
+ # 5 + 1 + 7 + 1 + 32 + 1 + 17 + 1 + 15 == 80
+ position = 5 + 1 + 7 + 1 + 32 + 1 + 17 + 1
+
+ # log timestrings are currently in local time, which might be a mistake
+ timestring = output[position:position + 15]
+ timetuple = time.strptime(timestring, "%b %d %H:%M:%S")
+ now = time.time()
+ nowtuple = time.localtime(now)
+
+ # since the year does not get logged, assume the log line is from this
+ # year, or last year if the delta becomes negative
+ timelist = list(timetuple)
+ timelist[0] = nowtuple[0]
+ if time.mktime(tuple(timelist)) > time.mktime(nowtuple):
+ self.debug('timestamp is past now, so assume it is from last year')
+ timelist[0] = nowtuple[0] - 1
+
+ # mktime also works in local time, which hopefully matches the log's
+ # local time
+ timestamp = time.mktime(tuple(timelist))
+ delta = now - int(timestamp)
+
+ msg = 'Last log line%s is %s old.' % (
+ self.options.string and " with '%s'" % self.options.string or '',
+ formatTime(delta, fractional=2))
+ if delta > int(self.options.critical):
+ return util.critical(msg)
+ elif delta > int(self.options.warning):
+ return util.warning(msg)
+ else:
+ return util.ok(msg)
+
+
+class Log(util.LogCommand):
+ description = "Check log files."
+
+ subCommandClasses = [Recent]
Modified: flumotion/trunk/flumotion/monitor/nagios/main.py
==============================================================================
--- flumotion/trunk/flumotion/monitor/nagios/main.py (original)
+++ flumotion/trunk/flumotion/monitor/nagios/main.py Wed Aug 20 15:49:09 2008
@@ -29,7 +29,7 @@
from flumotion.admin.connections import parsePBConnectionInfoRecent
from flumotion.admin import admin
-from flumotion.monitor.nagios import util, process
+from flumotion.monitor.nagios import util, process, log as nlog
__version__ = "$Rev$"
@@ -235,7 +235,7 @@
usage = "%prog %command"
description = "Run Flumotion-related Nagios checks."
- subCommandClasses = [Manager, process.ProcessCommand]
+ subCommandClasses = [Manager, process.ProcessCommand, nlog.Log]
def addOptions(self):
self.parser.add_option('-v', '--version',
More information about the flumotion-commit
mailing list