~free.ekanayaka/landscape-client/lucid-1.5.4-0ubuntu0.10.04.0

« back to all changes in this revision

Viewing changes to landscape/monitor/deployment.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-vytyqgbtkiirv5sb
Tags: 1.5.2.1-0ubuntu0.10.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.deployment import (LandscapeService, Configuration,
8
 
                                  run_landscape_service)
9
 
from landscape.monitor.monitor import (MonitorPluginRegistry,
10
 
                                       MonitorDBusObject)
11
 
from landscape.broker.remote import (RemoteBroker,
12
 
                                     DBusSignalToReactorTransmitter)
13
 
 
14
 
 
15
 
ALL_PLUGINS = ["ActiveProcessInfo", "ComputerInfo", "HardwareInventory",
16
 
               "LoadAverage", "MemoryInfo", "MountInfo", "ProcessorInfo",
17
 
               "Temperature", "PackageMonitor", "UserMonitor",
18
 
               "RebootRequired", "AptPreferences"]
19
 
 
20
 
 
21
 
class MonitorConfiguration(Configuration):
22
 
    """Specialized configuration for the Landscape Monitor."""
23
 
 
24
 
    def make_parser(self):
25
 
        """
26
 
        Specialize L{Configuration.make_parser}, adding many
27
 
        monitor-specific options.
28
 
        """
29
 
        parser = super(MonitorConfiguration, self).make_parser()
30
 
 
31
 
        parser.add_option("--monitor-plugins", metavar="PLUGIN_LIST",
32
 
                          help="Comma-delimited list of monitor plugins to "
33
 
                               "use. ALL means use all plugins.",
34
 
                          default="ALL")
35
 
        parser.add_option("--flush-interval", default=5*60, type="int",
36
 
                          metavar="INTERVAL",
37
 
                          help="The number of seconds between flushes "
38
 
                               "(default is 300s).")
39
 
        return parser
40
 
 
41
 
    @property
42
 
    def plugin_factories(self):
43
 
        if self.monitor_plugins == "ALL":
44
 
            return ALL_PLUGINS
45
 
        return [x.strip() for x in self.monitor_plugins.split(",")]
46
 
 
47
 
 
48
 
class MonitorService(LandscapeService):
49
 
    """
50
 
    The core Twisted Service which creates and runs all necessary monitoring
51
 
    components when started.
52
 
    """
53
 
 
54
 
    service_name = "monitor"
55
 
 
56
 
    def __init__(self, config):
57
 
        self.persist_filename = os.path.join(config.data_path,
58
 
                                             "%s.bpickle" % self.service_name)
59
 
        super(MonitorService, self).__init__(config)
60
 
        self.plugins = self.get_plugins()
61
 
 
62
 
    def get_plugins(self):
63
 
        return [namedClass("landscape.monitor.%s.%s"
64
 
                           % (plugin_name.lower(), plugin_name))()
65
 
                for plugin_name in self.config.plugin_factories]
66
 
 
67
 
    def startService(self):
68
 
        super(MonitorService, self).startService()
69
 
 
70
 
        # If this raises ServiceUnknownError, we should do something nice.
71
 
        self.remote_broker = RemoteBroker(self.bus)
72
 
 
73
 
        self.registry = MonitorPluginRegistry(self.remote_broker, self.reactor,
74
 
                                              self.config, self.bus,
75
 
                                              self.persist,
76
 
                                              self.persist_filename)
77
 
        self.dbus_service = MonitorDBusObject(self.bus, self.registry)
78
 
        DBusSignalToReactorTransmitter(self.bus, self.reactor)
79
 
 
80
 
        for plugin in self.plugins:
81
 
            self.registry.add(plugin)
82
 
 
83
 
        self.flush_call_id = self.reactor.call_every(
84
 
            self.config.flush_interval, self.registry.flush)
85
 
 
86
 
        def broker_started():
87
 
            self.remote_broker.register_plugin(self.dbus_service.bus_name,
88
 
                                               self.dbus_service.object_path)
89
 
            self.registry.broker_started()
90
 
 
91
 
        broker_started()
92
 
        self.bus.add_signal_receiver(broker_started, "broker_started")
93
 
 
94
 
    def stopService(self):
95
 
        """Stop the monitor.
96
 
 
97
 
        The monitor is flushed to ensure that things like persist
98
 
        databases get saved to disk.
99
 
        """
100
 
        self.registry.flush()
101
 
        if self.flush_call_id:
102
 
            self.reactor.cancel_call(self.flush_call_id)
103
 
            self.flush_call_id = None
104
 
        super(MonitorService, self).stopService()
105
 
 
106
 
 
107
 
def run(args):
108
 
    run_landscape_service(MonitorConfiguration, MonitorService, args,
109
 
                          MonitorDBusObject.bus_name)