~free.ekanayaka/landscape-client/jaunty-1.5.2.1-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to landscape/monitor/networkactivity.py

  • Committer: Free Ekanayaka
  • Date: 2010-06-16 10:01:20 UTC
  • Revision ID: free.ekanayaka@canonical.com-20100616100120-k185twz19yf1qqmh
* New upstream version (LP: #594594):
  - A new includes information about active network devices and their
    IP address in sysinfo output (LP: #272344).
  - A new plugin collects information about network traffic (#LP :284662).
  - Report information about which packages requested a reboot (LP: #538253).
  - Fix breakage on Lucid AMIs having no ramdisk (LP: #574810).
  - Migrate the inter-process communication system from DBus to Twisted AMP.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
A monitor that collects data on network activity, and sends messages
 
3
with the inbound/outbound traffic per interface per step interval.
 
4
"""
 
5
 
 
6
import time
 
7
 
 
8
from landscape.lib.network import get_network_traffic
 
9
from landscape.accumulate import Accumulator
 
10
 
 
11
from landscape.monitor.plugin import MonitorPlugin
 
12
 
 
13
 
 
14
class NetworkActivity(MonitorPlugin):
 
15
    """
 
16
    Collect data regarding a machine's network activity.
 
17
    """
 
18
 
 
19
    message_type = "network-activity"
 
20
    persist_name = message_type
 
21
    run_interval = 30
 
22
 
 
23
    def __init__(self, network_activity_file="/proc/net/dev",
 
24
                 create_time=time.time):
 
25
        self._source_file = network_activity_file
 
26
        # accumulated values for sending out via message
 
27
        self._network_activity = {}
 
28
        # our last traffic sample for calculating a traffic delta
 
29
        self._last_activity = {}
 
30
        self._create_time = create_time
 
31
 
 
32
    def register(self, registry):
 
33
        super(NetworkActivity, self).register(registry)
 
34
        self._accumulate = Accumulator(self._persist, self.registry.step_size)
 
35
        self.call_on_accepted("network-activity", self.exchange, True)
 
36
 
 
37
    def create_message(self):
 
38
        network_activity = self._network_activity
 
39
        if not network_activity:
 
40
            return
 
41
        self._network_activity = {}
 
42
        return {"type": "network-activity", "activities": network_activity}
 
43
 
 
44
    def send_message(self, urgent):
 
45
        message = self.create_message()
 
46
        if not message:
 
47
            return
 
48
        self.registry.broker.send_message(message, urgent=urgent)
 
49
 
 
50
    def exchange(self, urgent=False):
 
51
        self.registry.broker.call_if_accepted("network-activity",
 
52
                                              self.send_message, urgent)
 
53
 
 
54
    def _traffic_delta(self, new_traffic):
 
55
        """
 
56
        Given network activity metrics across all interfaces, calculate
 
57
        and return the delta data transferred for inbound and outbound
 
58
        traffic. Returns a tuple of interface name, outbound delta,
 
59
        inbound delta.
 
60
        """
 
61
        for interface in new_traffic:
 
62
            traffic = new_traffic[interface]
 
63
            if interface in self._last_activity:
 
64
                previous_out, previous_in = self._last_activity[interface]
 
65
                delta_out = traffic["send_bytes"] - previous_out
 
66
                delta_in = traffic["recv_bytes"] - previous_in
 
67
                if not delta_out and not delta_in:
 
68
                    continue
 
69
                yield interface, delta_out, delta_in
 
70
            self._last_activity[interface] = (
 
71
                traffic["send_bytes"], traffic["recv_bytes"])
 
72
 
 
73
    def run(self):
 
74
        """
 
75
        Sample network traffic statistics and store them into the
 
76
        accumulator, recording step data.
 
77
        """
 
78
        new_timestamp = int(self._create_time())
 
79
        new_traffic = get_network_traffic(self._source_file)
 
80
        for interface, delta_out, delta_in in self._traffic_delta(new_traffic):
 
81
            out_step_data = self._accumulate(
 
82
                new_timestamp, delta_out, "delta-out-%s"%interface)
 
83
 
 
84
            in_step_data = self._accumulate(
 
85
                new_timestamp, delta_in, "delta-in-%s"%interface)
 
86
 
 
87
            # there's only data when we cross a step boundary
 
88
            if not (in_step_data and out_step_data):
 
89
                continue
 
90
 
 
91
            steps = self._network_activity.setdefault(interface, [])
 
92
            steps.append(
 
93
                (in_step_data[0], int(in_step_data[1]), int(out_step_data[1])))