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