[Pkg-nagios-changes] [SCM] UNNAMED PROJECT branch, debian/master, updated. 810edbdd3feedbfe37f4a65bee50b57b2f60fa2a

Gerhard Lausser gerhard.lausser at consol.de
Tue Feb 28 22:16:58 UTC 2012


The following commit has been merged in the debian/master branch:
commit dfd2f06860b484fcfdb4f1b3d588caa325788a47
Author: Gerhard Lausser <gerhard.lausser at consol.de>
Date:   Sat Jan 28 23:08:57 2012 +0100

    Livestatus: can now connect to a replica set through the parameter replica_set
    Old entries are deleted at midnight

diff --git a/shinken/modules/logstore_mongodb.py b/shinken/modules/logstore_mongodb.py
index 6a7f029..4d0f760 100644
--- a/shinken/modules/logstore_mongodb.py
+++ b/shinken/modules/logstore_mongodb.py
@@ -15,7 +15,7 @@ from shinken.objects.service import Service
 from livestatus_broker.livestatus_stack import LiveStatusStack
 from livestatus_broker.mapping import LOGCLASS_ALERT, LOGCLASS_PROGRAM, LOGCLASS_NOTIFICATION, LOGCLASS_PASSIVECHECK, LOGCLASS_COMMAND, LOGCLASS_STATE, LOGCLASS_INVALID, LOGOBJECT_INFO, LOGOBJECT_HOST, LOGOBJECT_SERVICE, Logline
 
-from pymongo import Connection, ReplicaSetConnection
+from pymongo import Connection, ReplicaSetConnection, ReadPreference
 from pymongo.errors import AutoReconnect
 
 
@@ -96,16 +96,24 @@ class LiveStatusLogStoreMongoDB(BaseModule):
         pass
 
     def open(self):
-        print "open LiveStatusLogStoreMongoDB ok"
         try:
-            self.conn = pymongo.Connection(self.mongodb_uri, fsync=True)
+            if self.replica_set:
+                self.conn = pymongo.ReplicaSetConnection(self.mongodb_uri, replicaSet=self.replica_set, fsync=True)
+            else:
+                self.conn = pymongo.Connection(self.mongodb_uri, fsync=True)
             self.db = self.conn[self.database]
             self.db[self.collection].ensure_index([('time', pymongo.ASCENDING), ('lineno', pymongo.ASCENDING)], name='time_idx')
+            if self.replica_set:
+                pass
+                # This might be a future option prefer_secondary
+                #self.db.read_preference = ReadPreference.SECONDARY
             self.is_connected = CONNECTED
+            self.next_log_db_rotate = time.time()
         except AutoReconnect, exp:
             # now what, ha?
             print "LiveStatusLogStoreMongoDB.AutoReconnect", exp
-            raise
+            # The mongodb is hopefully available until this module is restarted
+            raise LiveStatusLogStoreError
             pass
 
     def close(self):
@@ -115,8 +123,23 @@ class LiveStatusLogStoreMongoDB(BaseModule):
         pass
 
     def commit_and_rotate_log_db(self):
-        """ Not necessary for a MongoDB."""
-        pass
+        """For a MongoDB there is no rotate, but we will delete old contents."""
+        now = time.time()
+        if self.next_log_db_rotate <= now:
+            today = datetime.date.today()
+            today0000 = datetime.datetime(today.year, today.month, today.day, 0, 0, 0)
+            today0005 = datetime.datetime(today.year, today.month, today.day, 0, 5, 0)
+            oldest = today0000 - datetime.timedelta(days=self.max_logs_age)
+            self.db[self.collection].remove({ u'time' : { '$lt' : time.mktime(oldest.timetuple()) }}, safe=True)
+
+            if now < time.mktime(today0005.timetuple()):
+                nextrotation = today0005
+            else:
+                nextrotation = today0005 + datetime.timedelta(days=1)
+
+            # See you tomorrow
+            self.next_log_db_rotate = time.mktime(nextrotation.timetuple())
+            print "next rotation at %s " % time.asctime(time.localtime(self.next_log_db_rotate))
 
     def do_i_need_this_manage_brok(self, brok):
         """ Look for a manager function for a brok, and call it """
@@ -160,6 +183,7 @@ class LiveStatusLogStoreMongoDB(BaseModule):
             # After 5 seconds we either have a successful write
             # or another exception which means, we are disconnected
         except Exception, exp:
+            self.is_connected = DISCONNECTED
             print "An error occurred:", exp
             print "DATABASE ERROR!!!!!!!!!!!!!!!!!"
         #FIXME need access to this#self.livestatus.count_event('log_message')
diff --git a/test/test_livestatus_mongodb.py b/test/test_livestatus_mongodb.py
index 5b1da9c..3975c2a 100755
--- a/test/test_livestatus_mongodb.py
+++ b/test/test_livestatus_mongodb.py
@@ -293,7 +293,10 @@ class TestConfigBig(TestConfig):
         dbmodconf = Module({'module_name' : 'LogStore',
             'module_type' : 'logstore_mongodb',
             'database' : 'bigbigbig',
-            'mongodb_uri' : "mongodb://127.0.0.1:27017",
+            #'mongodb_uri' : "mongodb://127.0.0.1:27017",
+            'mongodb_uri' : "mongodb://10.0.12.50:27017",
+            'replica_set' : 'livestatus',
+            'max_logs_age' : '14',
         })
         modconf.modules = [dbmodconf]
         self.livestatus_broker = LiveStatus_broker(modconf)
@@ -490,6 +493,44 @@ OutputFormat: json"""
         print "clientselected", len(clientselected)
         self.assert_(len(pyresponse) == len(clientselected))
 
+        # now delete too old entries from the database (> 14days)
+        # that's the job of commit_and_rotate_log_db()
+        #
+        numlogs = self.livestatus_broker.db.conn.bigbigbig.logs.find().count()
+        times = [x['time'] for x in self.livestatus_broker.db.conn.bigbigbig.logs.find()]
+        print "whole database", numlogs, min(times), max(times)
+        numlogs = self.livestatus_broker.db.conn.bigbigbig.logs.find({
+            '$and' : [
+                {'time' : { '$gt' : min(times)} },
+                {'time' : { '$lte' : max(times)} }
+            ]}).count()
+        now = max(times)
+        daycount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
+        for day in xrange(25):
+            one_day_earlier = now - 3600*24
+            numlogs = self.livestatus_broker.db.conn.bigbigbig.logs.find({
+                '$and' : [
+                    {'time' : { '$gt' : one_day_earlier} },
+                    {'time' : { '$lte' : now} }
+                ]}).count()
+            daycount[day] = numlogs
+            print "day -%02d %d..%d - %d" % (day, one_day_earlier, now, numlogs)
+            now = one_day_earlier
+        self.livestatus_broker.db.commit_and_rotate_log_db()
+        now = max(times)
+        for day in xrange(25):
+            one_day_earlier = now - 3600*24
+            numlogs = self.livestatus_broker.db.conn.bigbigbig.logs.find({
+                '$and' : [
+                    {'time' : { '$gt' : one_day_earlier} },
+                    {'time' : { '$lte' : now} }
+                ]}).count()
+            print "day -%02d %d..%d - %d" % (day, one_day_earlier, now, numlogs)
+            now = one_day_earlier
+        numlogs = self.livestatus_broker.db.conn.bigbigbig.logs.find().count()
+        print numlogs, sum(daycount[:14]), daycount[:14]
+        self.assert_(numlogs == sum(daycount[:14]))
+
 
 if __name__ == '__main__':
     #import cProfile

-- 
UNNAMED PROJECT



More information about the Pkg-nagios-changes mailing list