<commit_before>#!/usr/bin/python
#Copyright (C) 2009 <NAME>, <EMAIL>
#
#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 Class is an example of an Scheduler module
#Here for the configuration phase AND running one


#This text is print at the import
print "Detected module : Memcache retention file for Scheduler"


import time
import memcache


properties = {
    'type' : 'memcache_retention',
    'external' : False,
    'phases' : ['retention'],
    }


#called by the plugin manager to get a broker
def get_instance(plugin):
    print "Get a memcache retention scheduler module for plugin %s" % plugin.get_name()
    server = plugin.server
    port = plugin.port
    instance = Memcache_retention_scheduler(plugin.get_name(), server, port)
    return instance



#Just print some stuff
class Memcache_retention_scheduler:
    def __init__(self, name, server, port):
        self.name = name
        self.server = server
        self.port = port

    #Called by Scheduler to say 'let's prepare yourself guy'
    def init(self):
        print "Initilisation of the memcache module"
        #self.return_queue = self.properties['from_queue']
        self.mc = memcache.Client(['%s:%s' % (self.server, self.port)], debug=0)


#    mc.set("some_key", "Some value")
#    value = mc.get("some_key")

    def get_name(self):
        return self.name


    #Ok, main function that is called in the retention creation pass
    def update_retention_objects(self, sched, log_mgr):
        print "[MemcacheRetention] asking me to update the retention objects"
        #Now the flat file method
        for h in sched.hosts:
            key = "HOST-%s" % h.host_name
            self.mc.set(key, h)
        for s in sched.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')
            #print "Using key", key
            self.mc.set(key, s)
        log_mgr.log("Retention information updated in Memcache")



    #Should return if it succeed in the retention load or not
    def load_retention_objects(self, sched, log_mgr):
        return False
        print "[PickleRetention] asking me to load the retention objects"
        
        #Now the old flat file way :(
        log_mgr.log("[PickleRetention]Reading from retention_file %s" % self.path)
        try:
            f = open(self.path, 'rb')
            all_data = cPickle.load(f)
            f.close()
        except EOFError , exp:
            print exp
            return False
        except ValueError , exp:
            print exp
            return False
        except IOError , exp:
            print exp
            return False
        except IndexError , exp:
            s = "WARNING: Sorry, the ressource file is not compatible"
            log_mgr.log(s)
            return False
        except TypeError , exp:
            s = "WARNING: Sorry, the ressource file is not compatible"
            log_mgr.log(s)
            return False

            
        #Now load interesting properties in hosts/services
        #Taging retention=False prop that not be directly load
        #Items will be with theirs status, but not in checking, so
        #a new check will be launch like with a normal begining (random distributed
        #scheduling)

        ret_hosts = all_data['hosts']
        for ret_h in ret_hosts:
            h = sched.hosts.find_by_name(ret_h.host_name)
            if h != None:
                running_properties = h.__class__.running_properties
                for prop in running_properties:
                    entry = running_properties[prop]
                    if 'retention' in entry and entry['retention']:
                        setattr(h, prop, getattr(ret_h, prop))
                        for a in h.notifications_in_progress.values():
                            a.ref = h
                            sched.add(a)
                        h.update_in_checking()

        ret_services = all_data['services']
        for ret_s in ret_services:
            s = sched.services.find_srv_by_name_and_hostname(ret_s.host_name, ret_s.service_description)
            if s != None:
                running_properties = s.__class__.running_properties
                for prop in running_properties:
                    entry = running_properties[prop]
                    if 'retention' in entry and entry['retention']:
                        setattr(s, prop, getattr(ret_s, prop))
                        for a in s.notifications_in_progress.values():
                            a.ref = s
                            sched.add(a)
                        s.update_in_checking()

        log_mgr.log("[PickleRetention] OK we've load data from retention file")

        return True
<commit_msg>Add: memcache retention load now work. Need a timestamp things I think to do not go in all modules.<commit_after>#!/usr/bin/python
#Copyright (C) 2009 <NAME>, <EMAIL>
#
#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 Class is an example of an Scheduler module
#Here for the configuration phase AND running one


#This text is print at the import
print "Detected module : Memcache retention file for Scheduler"


import time
import memcache


properties = {
    'type' : 'memcache_retention',
    'external' : False,
    'phases' : ['retention'],
    }


#called by the plugin manager to get a broker
def get_instance(plugin):
    print "Get a memcache retention scheduler module for plugin %s" % plugin.get_name()
    server = plugin.server
    port = plugin.port
    instance = Memcache_retention_scheduler(plugin.get_name(), server, port)
    return instance



#Just print some stuff
class Memcache_retention_scheduler:
    def __init__(self, name, server, port):
        self.name = name
        self.server = server
        self.port = port

    #Called by Scheduler to say 'let's prepare yourself guy'
    def init(self):
        print "Initilisation of the memcache module"
        #self.return_queue = self.properties['from_queue']
        self.mc = memcache.Client(['%s:%s' % (self.server, self.port)], debug=0)


#    mc.set("some_key", "Some value")
#    value = mc.get("some_key")

    def get_name(self):
        return self.name


    #Ok, main function that is called in the retention creation pass
    def update_retention_objects(self, sched, log_mgr):
        print "[MemcacheRetention] asking me to update the retention objects"
        #Now the flat file method
        for h in sched.hosts:
            key = "HOST-%s" % h.host_name
            self.mc.set(key, h)

        for s in sched.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')
            #print "Using key", key
            self.mc.set(key, s)
        log_mgr.log("Retention information updated in Memcache")



    #Should return if it succeed in the retention load or not
    def load_retention_objects(self, sched, log_mgr):
        print "[MemcacheRetention] asking me to load the retention objects"
        
        #Now the old flat file way :(
        log_mgr.log("[MemcacheRetention] asking me to load the retention objects")
        
        #We got list of loaded data from retention server
        ret_hosts = []
        ret_services = []

        #Now the flat file method
        for h in sched.hosts:
            key = "HOST-%s" % h.host_name
            val = self.mc.get(key)
            if val != None:
                ret_hosts.append(val)

        for s in sched.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')
            #print "Using key", key
            val = self.mc.get(key)
            if val != None:
                ret_services.append(val)

            
        #Now load interesting properties in hosts/services
        #Taging retention=False prop that not be directly load
        #Items will be with theirs status, but not in checking, so
        #a new check will be launch like with a normal begining (random distributed
        #scheduling)

#        ret_hosts = all_data['hosts']
        for ret_h in ret_hosts:
            h = sched.hosts.find_by_name(ret_h.host_name)
            if h != None:
                running_properties = h.__class__.running_properties
                for prop in running_properties:
                    entry = running_properties[prop]
                    if 'retention' in entry and entry['retention']:
                        setattr(h, prop, getattr(ret_h, prop))
                        for a in h.notifications_in_progress.values():
                            a.ref = h
                            sched.add(a)
                        h.update_in_checking()

#        ret_services = all_data['services']
        for ret_s in ret_services:
            s = sched.services.find_srv_by_name_and_hostname(ret_s.host_name, ret_s.service_description)
            if s != None:
                running_properties = s.__class__.running_properties
                for prop in running_properties:
                    entry = running_properties[prop]
                    if 'retention' in entry and entry['retention']:
                        setattr(s, prop, getattr(ret_s, prop))
                        for a in s.notifications_in_progress.values():
                            a.ref = s
                            sched.add(a)
                        s.update_in_checking()

        log_mgr.log("[MemcacheRetention] OK we've load data from memcache server")

        return True

{'fraction_non_alphanumeric': 0.08022455632017385, 'fraction_numerical': 0.0010865628395508873, 'mean_word_length': 2.132444696539989, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '246420', 'n_tokens_mistral': 3094, 'n_tokens_neox': 2971, 'n_words': 1080}