| OLD | NEW |
| (Empty) |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Classes representing the monitoring interface for tasks or devices.""" | |
| 6 | |
| 7 | |
| 8 class Target(object): | |
| 9 """Abstract base class for a monitoring target. | |
| 10 | |
| 11 A Target is a "thing" that should be monitored, for example, a device or a | |
| 12 process. The majority of the time, a single process will have only a single | |
| 13 Target. | |
| 14 | |
| 15 Do not directly instantiate an object of this class. | |
| 16 Use the concrete child classes instead: | |
| 17 * TaskTarget to monitor a job or tasks running in (potentially) many places; | |
| 18 * DeviceTarget to monitor a host machine that may be running a task. | |
| 19 """ | |
| 20 | |
| 21 def __init__(self): | |
| 22 # Subclasses should list the updatable target fields here. | |
| 23 self._fields = tuple() | |
| 24 | |
| 25 def _populate_target_pb(self, metric): | |
| 26 """Populate the 'target' embedded message field of a metric protobuf.""" | |
| 27 raise NotImplementedError() | |
| 28 | |
| 29 def to_dict(self): | |
| 30 """Return target field values as a dictionary.""" | |
| 31 return {field: getattr(self, field) for field in self._fields} | |
| 32 | |
| 33 def update(self, target_fields): | |
| 34 """Update values of some target fields given as a dict.""" | |
| 35 for field, value in target_fields.iteritems(): | |
| 36 if field not in self._fields: | |
| 37 raise AttributeError('Bad target field: %s' % field) | |
| 38 # Make sure the attribute actually exists in the object. | |
| 39 getattr(self, field) | |
| 40 setattr(self, field, value) | |
| 41 | |
| 42 | |
| 43 class DeviceTarget(Target): | |
| 44 """Monitoring interface class for monitoring specific hosts or devices.""" | |
| 45 | |
| 46 def __init__(self, region, role, network, hostname): | |
| 47 """Create a Target object exporting info about a specific device. | |
| 48 | |
| 49 Args: | |
| 50 region (str): physical region in which the device is located. | |
| 51 role (str): role of the device. | |
| 52 network (str): virtual network on which the device is located. | |
| 53 hostname (str): name by which the device self-identifies. | |
| 54 """ | |
| 55 super(DeviceTarget, self).__init__() | |
| 56 self.region = region | |
| 57 self.role = role | |
| 58 self.network = network | |
| 59 self.hostname = hostname | |
| 60 self.realm = 'ACQ_CHROME' | |
| 61 self.alertable = True | |
| 62 self._fields = ('region', 'role', 'network', 'hostname') | |
| 63 | |
| 64 def _populate_target_pb(self, metric): | |
| 65 """Populate the 'network_device' embedded message of a metric protobuf. | |
| 66 | |
| 67 Args: | |
| 68 metric (metrics_pb2.MetricsData): the metric proto to be populated. | |
| 69 """ | |
| 70 # Note that this disregards the pop, asn, role, and vendor fields. | |
| 71 metric.network_device.metro = self.region | |
| 72 metric.network_device.role = self.role | |
| 73 metric.network_device.hostgroup = self.network | |
| 74 metric.network_device.hostname = self.hostname | |
| 75 metric.network_device.realm = self.realm | |
| 76 metric.network_device.alertable = self.alertable | |
| 77 | |
| 78 | |
| 79 class TaskTarget(Target): | |
| 80 """Monitoring interface class for monitoring active jobs or processes.""" | |
| 81 | |
| 82 def __init__(self, service_name, job_name, region, hostname, task_num=0): | |
| 83 """Create a Target object exporting info about a specific task. | |
| 84 | |
| 85 Args: | |
| 86 service_name (str): service of which this task is a part. | |
| 87 job_name (str): specific name of this task. | |
| 88 region (str): general region in which this task is running. | |
| 89 hostname (str): specific machine on which this task is running. | |
| 90 task_num (int): replication id of this task. | |
| 91 """ | |
| 92 super(TaskTarget, self).__init__() | |
| 93 self.service_name = service_name | |
| 94 self.job_name = job_name | |
| 95 self.region = region | |
| 96 self.hostname = hostname | |
| 97 self.task_num = task_num | |
| 98 self._fields = ('service_name', 'job_name', 'region', | |
| 99 'hostname', 'task_num') | |
| 100 | |
| 101 def _populate_target_pb(self, metric): | |
| 102 """Populate the 'task' embedded message field of a metric protobuf. | |
| 103 | |
| 104 Args: | |
| 105 metric (metrics_pb2.MetricsData): the metric proto to be populated. | |
| 106 """ | |
| 107 metric.task.service_name = self.service_name | |
| 108 metric.task.job_name = self.job_name | |
| 109 metric.task.data_center = self.region | |
| 110 metric.task.host_name = self.hostname | |
| 111 metric.task.task_num = self.task_num | |
| OLD | NEW |