~ubuntu-branches/ubuntu/jaunty/landscape-client/jaunty-proposed

« back to all changes in this revision

Viewing changes to landscape/monitor/service.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-06-28 18:07:18 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100628180718-m3t2ttgg8efbg19d
Tags: 1.5.2.1-0ubuntu0.9.04.0
Filter duplicate network interfaces in get_active_interfaces (LP: #597000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Deployment code for the monitor."""
 
2
 
 
3
import os
 
4
 
 
5
from twisted.python.reflect import namedClass
 
6
 
 
7
from landscape.service import LandscapeService, run_landscape_service
 
8
from landscape.monitor.config import MonitorConfiguration
 
9
from landscape.monitor.monitor import Monitor
 
10
from landscape.broker.amp import (
 
11
    BrokerClientProtocolFactory, RemoteBrokerConnector)
 
12
 
 
13
 
 
14
class MonitorService(LandscapeService):
 
15
    """
 
16
    The core Twisted Service which creates and runs all necessary monitoring
 
17
    components when started.
 
18
    """
 
19
 
 
20
    service_name = Monitor.name
 
21
 
 
22
    def __init__(self, config):
 
23
        self.persist_filename = os.path.join(
 
24
            config.data_path, "%s.bpickle" % self.service_name)
 
25
        super(MonitorService, self).__init__(config)
 
26
        self.plugins = self.get_plugins()
 
27
        self.monitor = Monitor(self.reactor, self.config, self.persist,
 
28
                               persist_filename=self.persist_filename)
 
29
        self.factory = BrokerClientProtocolFactory(object=self.monitor)
 
30
 
 
31
    def get_plugins(self):
 
32
        return [namedClass("landscape.monitor.%s.%s"
 
33
                           % (plugin_name.lower(), plugin_name))()
 
34
                for plugin_name in self.config.plugin_factories]
 
35
 
 
36
    def startService(self):
 
37
        """Start the monitor."""
 
38
        super(MonitorService, self).startService()
 
39
 
 
40
        def start_plugins(broker):
 
41
            self.broker = broker
 
42
            self.monitor.broker = broker
 
43
            for plugin in self.plugins:
 
44
                self.monitor.add(plugin)
 
45
            return self.broker.register_client(self.service_name)
 
46
 
 
47
        self.connector = RemoteBrokerConnector(self.reactor, self.config)
 
48
        connected = self.connector.connect()
 
49
        return connected.addCallback(start_plugins)
 
50
 
 
51
    def stopService(self):
 
52
        """Stop the monitor.
 
53
 
 
54
        The monitor is flushed to ensure that things like persist databases
 
55
        get saved to disk.
 
56
        """
 
57
        self.monitor.flush()
 
58
        self.connector.disconnect()
 
59
        super(MonitorService, self).stopService()
 
60
 
 
61
 
 
62
def run(args):
 
63
    run_landscape_service(MonitorConfiguration, MonitorService, args)