Esempio n. 1
0
class Daemon(object):

    properties = {
        # workdir is relative to $(dirname "$0"/..) 
        # where "$0" is the path of the file being executed, 
        # in python normally known as:
        #
        #  os.path.join( os.getcwd(), sys.argv[0] )
        # 
        # as returned once the daemon is started.
        'workdir':       PathProp(default='var'),
        'host':          StringProp(default='0.0.0.0'),
        'user':          StringProp(default=get_cur_user()),
        'group':         StringProp(default=get_cur_group()),
        'use_ssl':       BoolProp(default='0'),
        'certs_dir':     StringProp(default='etc/certs'),
        'ca_cert':       StringProp(default='etc/certs/ca.pem'),
        'server_cert':   StringProp(default='etc/certs/server.pem'),
        'use_local_log': BoolProp(default='1'),
        'hard_ssl_name_check':    BoolProp(default='0'),
        'idontcareaboutsecurity': BoolProp(default='0'),
        'spare':         BoolProp(default='0')
    }

    def __init__(self, name, config_file, is_daemon, do_replace, debug, debug_file):
        
        self.check_shm()
        
        self.name = name
        self.config_file = config_file
        self.is_daemon = is_daemon
        self.do_replace = do_replace
        self.debug = debug
        self.debug_file = debug_file
        self.interrupted = False

        # Track time
        now = time.time()
        self.program_start = now
        self.t_each_loop = now # used to track system time change
        self.sleep_time = 0.0 #used to track the time we wait

        self.pyro_daemon = None

        # Log init
        self.log = logger
        self.log.load_obj(self)
        
        self.new_conf = None # used by controller to push conf 
        self.cur_conf = None

        # Flag to know if we need to dump memory or not
        self.need_dump_memory = False

        #Keep a trace of the local_log file desc if need
        self.local_log_fd = None

        # Put in queue some debug output we will raise
        # when we will be in daemon
        self.debug_output = []

        self.modules_manager = ModulesManager(name, self.find_modules_path(), [])

        os.umask(UMASK)
        self.set_exit_handler()



    # At least, lose the local log file if need
    def do_stop(self):
        if self.modules_manager:
            # We save what we can but NOT for the scheduler
            # because the current sched object is a dummy one
            # and the old one aleady do it!
            if not hasattr(self, 'sched'):
                self.hook_point('save_retention')
            # And we quit
            logger.log('Stopping all modules')
            self.modules_manager.stop_all()
        if self.pyro_daemon:
            pyro.shutdown(self.pyro_daemon)#.shutdown(True)
        logger.quit()
        

    def request_stop(self):
        self.unlink()  ## unlink first
        self.do_stop()
        print("Exiting")
        sys.exit(0)

    def do_loop_turn(self):
        raise NotImplementedError()

    # Main loop for nearly all daemon
    # the scheduler is not managed by it :'(
    def do_mainloop(self):
        while True:
            self.do_loop_turn()
            # If ask us to dump memory, do it
            if self.need_dump_memory:
                self.dump_memory()
                self.need_dump_memory = False
            # Maybe we ask us to die, if so, do it :)
            if self.interrupted:
                break
        self.request_stop()
    
    def do_load_modules(self):
        self.modules_manager.load_and_init()
        self.log.log("I correctly loaded the modules : [%s]" % (','.join([inst.get_name() for inst in self.modules_manager.instances])))
 
 
    def add(self, elt):
        """ Dummy method for adding broker to this daemon """
        pass

    def dump_memory(self):
        logger.log("I dump my memory, it can ask some seconds to do")
        try:
            from guppy import hpy
            hp = hpy()
            logger.log(hp.heap())
        except ImportError:
            logger.log('I do not have the module guppy for memory dump, please install it')
            

 
    def load_config_file(self):
        self.parse_config_file()
        if self.config_file is not None:
            # Some paths can be relatives. We must have a full path by taking
            # the config file by reference
            self.relative_paths_to_full(os.path.dirname(self.config_file))
            pass
        


    def change_to_workdir(self):
        try:
            os.chdir(self.workdir)
        except Exception, e:
            raise InvalidWorkDir(e)
        print("Successfully changed to workdir: %s" % (self.workdir))
class TestModuleManager(ShinkenTest):
    def setUp(self):
        self.setup_with_file('etc/shinken_1r_1h_1s.cfg')
        time_hacker.set_real_time()

    # Try to see if the module manager can manage modules
    def test_modulemanager(self):
        mod = Module({
            'module_name': 'DummyExternal',
            'module_type': 'dummy_broker_external'
        })
        self.modulemanager = ModulesManager('broker',
                                            "var/lib/shinken/modules", [])
        self.modulemanager.set_modules([mod])
        self.modulemanager.load_and_init()
        # And start external ones, like our LiveStatus
        self.modulemanager.start_external_instances()
        print "I correctly loaded the modules: %s " % (
            [inst.get_name() for inst in self.modulemanager.instances])

        print "*** First kill ****"
        # Now I will try to kill the livestatus module
        ls = self.modulemanager.instances[0]
        ls._BaseModule__kill()

        time.sleep(1)
        print "Check alive?"
        print "Is alive?", ls.process.is_alive()
        # Should be dead
        self.assertFalse(ls.process.is_alive())
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()

        # In fact it's too early, so it won't do it

        # Here the inst should still be dead
        print "Is alive?", ls.process.is_alive()
        self.assertFalse(ls.process.is_alive())

        # So we lie
        ls.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()

        # In fact it's too early, so it won't do it

        # Here the inst should be alive again
        print "Is alive?", ls.process.is_alive()
        self.assertTrue(ls.process.is_alive())

        # should be nothing more in to_restart of
        # the module manager
        self.assertEqual([], self.modulemanager.to_restart)

        # Now we look for time restart so we kill it again
        ls._BaseModule__kill()
        time.sleep(1)
        self.assertFalse(ls.process.is_alive())

        # Should be too early
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()
        print "Is alive or not", ls.process.is_alive()
        self.assertFalse(ls.process.is_alive())
        # We lie for the test again
        ls.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()

        # Here the inst should be alive again
        print "Is alive?", ls.process.is_alive()
        self.assertTrue(ls.process.is_alive())

        # And we clear all now
        print "Ask to die"
        self.modulemanager.stop_all()
        print "Died"
Esempio n. 3
0
class TestModuleManager(ShinkenTest):
    def setUp(self):
        self.setup_with_file('etc/shinken_1r_1h_1s.cfg')
        time_hacker.set_real_time()

    # Try to see if the module manager can manage modules
    def test_modulemanager(self):
        mod = Module({'module_name': 'DummyExternal', 'module_type': 'dummy_broker_external'})
        self.modulemanager = ModulesManager('broker', modules_dir, [])
        self.modulemanager.set_modules([mod])
        self.modulemanager.load_and_init()
        # And start external ones, like our LiveStatus
        self.modulemanager.start_external_instances()
        print "I correctly loaded the modules: %s " % ([inst.get_name() for inst in self.modulemanager.instances])

        print "*** First kill ****"
        # Now I will try to kill the livestatus module
        ls = self.modulemanager.instances[0]
        ls._BaseModule__kill()

        time.sleep(1)
        print "Check alive?"
        print "Is alive?", ls.process.is_alive()
        # Should be dead
        self.assertFalse(ls.process.is_alive())
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()

        # In fact it's too early, so it won't do it

        # Here the inst should still be dead
        print "Is alive?", ls.process.is_alive()
        self.assertFalse(ls.process.is_alive())

        # So we lie
        ls.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()

        # In fact it's too early, so it won't do it

        # Here the inst should be alive again
        print "Is alive?", ls.process.is_alive()
        self.assertTrue(ls.process.is_alive())

        # should be nothing more in to_restart of
        # the module manager
        self.assertEqual([], self.modulemanager.to_restart)

        # Now we look for time restart so we kill it again
        ls._BaseModule__kill()
        time.sleep(1)
        self.assertFalse(ls.process.is_alive())

        # Should be too early
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()
        print "Is alive or not", ls.process.is_alive()
        self.assertFalse(ls.process.is_alive())
        # We lie for the test again
        ls.last_init_try = -5
        self.modulemanager.check_alive_instances()
        self.modulemanager.try_to_restart_deads()

        # Here the inst should be alive again
        print "Is alive?", ls.process.is_alive()
        self.assertTrue(ls.process.is_alive())

        # And we clear all now
        print "Ask to die"
        self.modulemanager.stop_all()
        print "Died"
Esempio n. 4
0
class Daemon(object):

    properties = {
        'workdir': PathProp(default='/usr/local/shinken/var'),
        'host': StringProp(default='0.0.0.0'),
        'user': StringProp(default='shinken'),
        'group': StringProp(default='shinken'),
        'use_ssl': BoolProp(default='0'),
        'certs_dir': StringProp(default='etc/certs'),
        'ca_cert': StringProp(default='etc/certs/ca.pem'),
        'server_cert': StringProp(default='etc/certs/server.pem'),
        'use_local_log': BoolProp(default='0'),
        'hard_ssl_name_check': BoolProp(default='0'),
        'idontcareaboutsecurity': BoolProp(default='0'),
        'spare': BoolProp(default='0')
    }

    def __init__(self, name, config_file, is_daemon, do_replace, debug,
                 debug_file):

        self.check_shm()

        self.name = name
        self.config_file = config_file
        self.is_daemon = is_daemon
        self.do_replace = do_replace
        self.debug = debug
        self.debug_file = debug_file
        self.interrupted = False

        # Track time
        now = time.time()
        self.program_start = now
        self.t_each_loop = now  # used to track system time change
        self.sleep_time = 0.0  #used to track the time we wait

        self.pyro_daemon = None

        # Log init
        self.log = logger
        self.log.load_obj(self)

        self.new_conf = None  # used by controller to push conf
        self.cur_conf = None

        # Flag to know if we need to dump memory or not
        self.need_dump_memory = False

        #Keep a trace of the local_log file desc if need
        self.local_log_fd = None

        self.modules_manager = ModulesManager(name, self.find_modules_path(),
                                              [])

        os.umask(UMASK)
        self.set_exit_handler()

    # At least, lose the local log file if need
    def do_stop(self):
        if self.modules_manager:
            # We save what we can but NOT for the scheduler
            # because the current sched object is a dummy one
            # and the old one aleady do it!
            if not hasattr(self, 'sched'):
                self.hook_point('save_retention')
            # And we quit
            logger.log('Stopping all modules')
            self.modules_manager.stop_all()
        if self.pyro_daemon:
            pyro.shutdown(self.pyro_daemon)  #.shutdown(True)
        logger.quit()

    def request_stop(self):
        self.unlink()  ## unlink first
        self.do_stop()
        print("Exiting")
        sys.exit(0)

    def do_loop_turn(self):
        raise NotImplementedError()

    # Main loop for nearly all daemon
    # the scheduler is not managed by it :'(
    def do_mainloop(self):
        while True:
            self.do_loop_turn()
            # If ask us to dump memory, do it
            if self.need_dump_memory:
                self.dump_memory()
                self.need_dump_memory = False
            # Maybe we ask us to die, if so, do it :)
            if self.interrupted:
                break
        self.request_stop()

    def do_load_modules(self):
        self.modules_manager.load_and_init()
        self.log.log("I correctly loaded the modules : [%s]" % (','.join(
            [inst.get_name() for inst in self.modules_manager.instances])))

    def add(self, elt):
        """ Dummy method for adding broker to this daemon """
        pass

    def dump_memory(self):
        logger.log("I dump my memory, it can ask some seconds to do")
        try:
            from guppy import hpy
            hp = hpy()
            logger.log(hp.heap())
        except ImportError:
            logger.log(
                'I do not have the module guppy for memory dump, please install it'
            )

    def load_config_file(self):
        self.parse_config_file()
        if self.config_file is not None:
            # Some paths can be relatives. We must have a full path by taking
            # the config file by reference
            self.relative_paths_to_full(os.path.dirname(self.config_file))
        # Then start to log all in the local file if asked so
        self.register_local_log()

    def change_to_workdir(self):
        try:
            os.chdir(self.workdir)
        except Exception, e:
            raise InvalidWorkDir(e)
        print("Successfully changed to workdir: %s" % (self.workdir))