| Server IP : 61.19.30.66 / Your IP : 216.73.216.15 Web Server : Apache/2.2.22 (Ubuntu) System : Linux klw 3.11.0-15-generic #25~precise1-Ubuntu SMP Thu Jan 30 17:39:31 UTC 2014 x86_64 User : www-data ( 33) PHP Version : 5.3.10-1ubuntu3.48 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : ON | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /usr/share/pyshared/landscape/monitor/ |
Upload File : |
"""
A monitor that collects data on network activity, and sends messages
with the inbound/outbound traffic per interface per step interval.
"""
import time
from landscape.lib.network import get_network_traffic, is_64
from landscape.accumulate import Accumulator
from landscape.monitor.plugin import MonitorPlugin
class NetworkActivity(MonitorPlugin):
"""
Collect data regarding a machine's network activity.
"""
message_type = "network-activity"
persist_name = message_type
run_interval = 30
_rollover_maxint = 0
max_network_items_to_exchange = 200
def __init__(self, network_activity_file="/proc/net/dev",
create_time=time.time):
self._source_file = network_activity_file
# accumulated values for sending out via message
self._network_activity = {}
# our last traffic sample for calculating a traffic delta
self._last_activity = {}
self._create_time = create_time
# We don't rollover on 64 bits, as 16 exabytes is a lot.
if not is_64():
self._rollover_maxint = pow(2, 32)
def register(self, registry):
super(NetworkActivity, self).register(registry)
self._accumulate = Accumulator(self._persist, self.registry.step_size)
self.call_on_accepted("network-activity", self.exchange, True)
def create_message(self):
network_activity = {}
items = 0
for interface, data in list(self._network_activity.items()):
if data:
network_activity[interface] = []
while data and items < self.max_network_items_to_exchange:
item = data.pop(0)
network_activity[interface].append(item)
items += 1
if items >= self.max_network_items_to_exchange:
break
if not network_activity:
return
return {"type": "network-activity", "activities": network_activity}
def send_message(self, urgent):
message = self.create_message()
if not message:
return
self.registry.broker.send_message(message, urgent=urgent)
def exchange(self, urgent=False):
self.registry.broker.call_if_accepted("network-activity",
self.send_message, urgent)
def _traffic_delta(self, new_traffic):
"""
Given network activity metrics across all interfaces, calculate
and return the delta data transferred for inbound and outbound
traffic. Returns a tuple of interface name, outbound delta,
inbound delta.
"""
for interface in new_traffic:
traffic = new_traffic[interface]
if interface in self._last_activity:
previous_out, previous_in = self._last_activity[interface]
delta_out = traffic["send_bytes"] - previous_out
delta_in = traffic["recv_bytes"] - previous_in
if delta_out < 0:
delta_out += self._rollover_maxint
if delta_in < 0:
delta_in += self._rollover_maxint
# If it's still zero or less, we discard the value. The next
# value will be compared to the current traffic, and
# hopefully things will catch up.
if delta_out <= 0 and delta_in <= 0:
continue
yield interface, delta_out, delta_in
self._last_activity[interface] = (
traffic["send_bytes"], traffic["recv_bytes"])
for interface in self._last_activity.keys():
if interface not in new_traffic:
del self._last_activity[interface]
def run(self):
"""
Sample network traffic statistics and store them into the
accumulator, recording step data.
"""
new_timestamp = int(self._create_time())
new_traffic = get_network_traffic(self._source_file)
for interface, delta_out, delta_in in self._traffic_delta(new_traffic):
out_step_data = self._accumulate(
new_timestamp, delta_out, "delta-out-%s" % interface)
in_step_data = self._accumulate(
new_timestamp, delta_in, "delta-in-%s" % interface)
# there's only data when we cross a step boundary
if not (in_step_data and out_step_data):
continue
steps = self._network_activity.setdefault(interface, [])
steps.append(
(in_step_data[0], int(in_step_data[1]), int(out_step_data[1])))