[Pkg-nagios-changes] [SCM] UNNAMED PROJECT branch, debian/master, updated. 810edbdd3feedbfe37f4a65bee50b57b2f60fa2a
Naparuba
naparuba at gmail.com
Tue Feb 28 22:14:54 UTC 2012
The following commit has been merged in the debian/master branch:
commit ab4e79781e9785679dc1545197433403e00a6f14
Author: Naparuba <naparuba at gmail.com>
Date: Fri Jan 20 16:27:21 2012 +0100
Add : mongodb retention module.
diff --git a/etc/shinken-specific.cfg b/etc/shinken-specific.cfg
index fb676af..734412c 100755
--- a/etc/shinken-specific.cfg
+++ b/etc/shinken-specific.cfg
@@ -442,6 +442,15 @@ define module{
}
+# A Mongodb retention module for the scheduler
+define module{
+ module_name MongodbRetention
+ module_type mongodb_retention
+ server localhost
+ database shinken
+}
+
+
#Now the memcache one
#Now the good flat file for retention module
define module{
diff --git a/shinken/modules/mongodb_retention.py b/shinken/modules/mongodb_retention.py
new file mode 100644
index 0000000..a643f6c
--- /dev/null
+++ b/shinken/modules/mongodb_retention.py
@@ -0,0 +1,154 @@
+#!/usr/bin/python
+#Copyright (C) 2009 Gabes Jean, naparuba at gmail.com
+#
+#This file is part of Shinken.
+#
+#Shinken is free software: you can redistribute it and/or modify
+#it under the terms of the GNU Affero General Public License as published by
+#the Free Software Foundation, either version 3 of the License, or
+#(at your option) any later version.
+#
+#Shinken 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 Affero General Public License for more details.
+#
+#You should have received a copy of the GNU Affero General Public License
+#along with Shinken. If not, see <http://www.gnu.org/licenses/>.
+
+"""
+This is a scheduler module to save host/sevice retention data into a mongodb databse
+"""
+
+import cPickle
+
+from pymongo.connection import Connection
+import gridfs
+from gridfs import GridFS
+
+from shinken.basemodule import BaseModule
+from shinken.log import logger
+
+properties = {
+ 'daemons' : ['scheduler'],
+ 'type' : 'mongodb_retention',
+ 'external' : False,
+ }
+
+
+# Called by the plugin manager to get a broker
+def get_instance(plugin):
+ print "Get a Mongodb retention scheduler module for plugin %s" % plugin.get_name()
+ server = plugin.server
+ database = plugin.database
+ instance = Mongodb_retention_scheduler(plugin, server, database)
+ return instance
+
+
+
+# Just print some stuff
+class Mongodb_retention_scheduler(BaseModule):
+ def __init__(self, modconf, server, database):
+ BaseModule.__init__(self, modconf)
+ self.server = server
+ self.database = database
+
+
+ # Called by Scheduler to say 'let's prepare yourself guy'
+ def init(self):
+ print "Initilisation of the mongodb module"
+ self.con = Connection(self.server)
+ # Open a gridfs connection
+ self.db = getattr(self.con, self.database)
+ self.hosts_fs = GridFS(self.db, collection='retention_hosts')
+ self.services_fs = GridFS(self.db, collection='retention_services')
+
+
+ # Ok, main function that is called in the retention creation pass
+ def hook_save_retention(self, daemon):
+ log_mgr = logger
+ print "[MongodbRetention] asking me to update the retention objects"
+
+ all_data = daemon.get_retention_data()
+
+ hosts = all_data['hosts']
+ services = all_data['services']
+
+ #Now the flat file method
+ for h_name in hosts:
+ h = hosts[h_name]
+ key = "HOST-%s" % h_name
+ val = cPickle.dumps(h, protocol=cPickle.HIGHEST_PROTOCOL)
+ # First delete if a previous one is here, because gridfs is a versionned
+ # fs, so we only want the last version...
+ self.hosts_fs.delete(key)
+ # We save it in the Gridfs for hosts
+ fd = self.hosts_fs.put(val, _id=key, filename=key)
+
+ for (h_name, s_desc) in services:
+ s = services[(h_name, s_desc)]
+ key = "SERVICE-%s,%s" % (h_name, s_desc)
+ # space are not allowed in a key.. so change it by SPACE token
+ key = key.replace(' ', 'SPACE')
+ val = cPickle.dumps(s, protocol=cPickle.HIGHEST_PROTOCOL)
+
+ # We save the binary dumps in a gridfs system
+ # First delete if a previous one is here, because gridfs is a versionned
+ # fs, so we only want the last version...
+ self.services_fs.delete(key)
+ fd = self.services_fs.put(val, _id=key, filename=key)
+
+
+ log_mgr.log("Retention information updated in Mongodb")
+
+
+
+ # Should return if it succeed in the retention load or not
+ def hook_load_retention(self, daemon):
+ log_mgr = logger
+
+ # Now the new redis way :)
+ log_mgr.log("MongodbRetention] asking me to load the retention objects")
+
+ #We got list of loaded data from retention server
+ ret_hosts = {}
+ ret_services = {}
+
+ # We must load the data and format as the scheduler want :)
+ for h in daemon.hosts:
+ key = "HOST-%s" % h.host_name
+ try:
+ fd = self.hosts_fs.get_last_version(key)
+ except gridfs.errors.NoFile, exp:
+ # Go in the next host object
+ continue
+ val = fd.read()
+
+ if val is not None:
+ val = cPickle.loads(val)
+ ret_hosts[h.host_name] = val
+
+ for s in daemon.services:
+ key = "SERVICE-%s,%s" % (s.host.host_name, s.service_description)
+ #space are not allowed in memcache key.. so change it by SPACE token
+ key = key.replace(' ', 'SPACE')
+ try:
+ fd = self.services_fs.get_last_version(key)
+ except gridfs.errors.NoFile, exp:
+ # Go in the next host object
+ continue
+ val = fd.read()
+
+ if val is not None:
+ val = cPickle.loads(val)
+ ret_services[(s.host.host_name, s.service_description)] = val
+
+
+ all_data = {'hosts' : ret_hosts, 'services' : ret_services}
+
+ # Ok, now comme load them scheduler :)
+ daemon.restore_retention_data(all_data)
+
+ log_mgr.log("[MongodbRetention] OK we've load data from redis server")
+
+ return True
diff --git a/test/test_module_redis_retention.py b/test/test_module_mongodb_retention.py
similarity index 82%
copy from test/test_module_redis_retention.py
copy to test/test_module_mongodb_retention.py
index 82c8fe0..02775b4 100755
--- a/test/test_module_redis_retention.py
+++ b/test/test_module_mongodb_retention.py
@@ -29,26 +29,28 @@ from shinken_test import unittest, ShinkenTest
from shinken.log import logger
from shinken.objects.module import Module
-from shinken.modules import redis_retention_scheduler
-from shinken.modules.redis_retention_scheduler import get_instance
+from shinken.modules import mongodb_retention
+from shinken.modules.mongodb_retention import get_instance
modconf = Module()
-modconf.module_name = "RedisRetention"
-modconf.module_type = redis_retention_scheduler.properties['type']
-modconf.properties = redis_retention_scheduler.properties.copy()
+modconf.module_name = "MongodbRetention"
+modconf.server = 'localhost'
+modconf.database = 'test'
+modconf.module_type = mongodb_retention.properties['type']
+modconf.properties = mongodb_retention.properties.copy()
-class TestConfig(ShinkenTest):
+class TestMongodb(ShinkenTest):
#setUp is in shinken_test
#Change ME :)
- def test_redis_retention(self):
+ def test_mongodb_retention(self):
print self.conf.modules
#get our modules
- mod = redis_retention_scheduler.Redis_retention_scheduler(modconf, 'localhost')
+ sl = mongodb_retention.Mongodb_retention_scheduler(modconf, 'localhost', 'test')
- sl = get_instance(mod)
+ #sl = get_instance(mod)
print "Instance", sl
#Hack here :(
sl.properties = {}
--
UNNAMED PROJECT
More information about the Pkg-nagios-changes
mailing list