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

« back to all changes in this revision

Viewing changes to landscape/monitor/deployment.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

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",
 
18
               "UserMonitor"]
 
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
        return parser
 
39
 
 
40
    @property
 
41
    def plugin_factories(self):
 
42
        if self.monitor_plugins == "ALL":
 
43
            return ALL_PLUGINS
 
44
        return [x.strip() for x in self.monitor_plugins.split(",")]
 
45
 
 
46
 
 
47
class MonitorService(LandscapeService):
 
48
    """
 
49
    The core Twisted Service which creates and runs all necessary monitoring
 
50
    components when started.
 
51
    """
 
52
 
 
53
    service_name = "monitor"
 
54
 
 
55
    def __init__(self, config):
 
56
        self.persist_filename = os.path.join(config.data_path,
 
57
                                             "%s.bpickle" % self.service_name)
 
58
        super(MonitorService, self).__init__(config)
 
59
        self.plugins = self.get_plugins()
 
60
 
 
61
    def get_plugins(self):
 
62
        return [namedClass("landscape.monitor.%s.%s"
 
63
                           % (plugin_name.lower(), plugin_name))()
 
64
                for plugin_name in self.config.plugin_factories]
 
65
 
 
66
    def startService(self):
 
67
        super(MonitorService, self).startService()
 
68
 
 
69
        # If this raises ServiceUnknownError, we should do something nice.
 
70
        self.remote_broker = RemoteBroker(self.bus)
 
71
        self.registry = MonitorPluginRegistry(self.reactor, self.remote_broker,
 
72
                                              self.config, self.bus,
 
73
                                              self.persist,
 
74
                                              self.persist_filename)
 
75
        self.dbus_service = MonitorDBusObject(self.bus, self.registry)
 
76
        DBusSignalToReactorTransmitter(self.bus, self.reactor)
 
77
        self.remote_broker.register_plugin(self.dbus_service.bus_name,
 
78
                                           self.dbus_service.object_path)
 
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 stopService(self):
 
87
        """Stop the monitor.
 
88
 
 
89
        The monitor is flushed to ensure that things like persist
 
90
        databases get saved to disk.
 
91
        """
 
92
        self.registry.flush()
 
93
        if self.flush_call_id:
 
94
            self.reactor.cancel_call(self.flush_call_id)
 
95
            self.flush_call_id = None
 
96
        super(MonitorService, self).stopService()
 
97
 
 
98
 
 
99
def run(args):
 
100
    run_landscape_service(MonitorConfiguration, MonitorService, args,
 
101
                          MonitorDBusObject.bus_name)