Matteo Scandolo | eb0d11c | 2017-08-08 13:05:26 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
rdudyala | b086cf3 | 2016-08-11 00:07:45 -0400 | [diff] [blame] | 17 | # |
| 18 | # Copyright 2014 NEC Corporation. All rights reserved. |
| 19 | # |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 21 | # not use this file except in compliance with the License. You may obtain |
| 22 | # a copy of the License at |
| 23 | # |
| 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | # |
| 26 | # Unless required by applicable law or agreed to in writing, software |
| 27 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 28 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 29 | # License for the specific language governing permissions and limitations |
| 30 | # under the License. |
| 31 | |
| 32 | import abc |
| 33 | |
| 34 | from oslo_utils import netutils |
| 35 | import six |
| 36 | from six.moves.urllib import parse as urlparse |
| 37 | from stevedore import driver as _driver |
| 38 | |
| 39 | from ceilometer.agent import plugin_base |
| 40 | from ceilometer import sample |
| 41 | |
| 42 | |
| 43 | @six.add_metaclass(abc.ABCMeta) |
| 44 | class _Base(plugin_base.PollsterBase): |
| 45 | |
| 46 | NAMESPACE = 'network.statistics.drivers' |
| 47 | drivers = {} |
| 48 | |
| 49 | @property |
| 50 | def default_discovery(self): |
| 51 | # this signifies that the pollster gets its resources from |
| 52 | # elsewhere, in this case they're manually listed in the |
| 53 | # pipeline configuration |
| 54 | return None |
| 55 | |
| 56 | @abc.abstractproperty |
| 57 | def meter_name(self): |
| 58 | """Return a Meter Name.""" |
| 59 | |
| 60 | @abc.abstractproperty |
| 61 | def meter_type(self): |
| 62 | """Return a Meter Type.""" |
| 63 | |
| 64 | @abc.abstractproperty |
| 65 | def meter_unit(self): |
| 66 | """Return a Meter Unit.""" |
| 67 | |
| 68 | @staticmethod |
| 69 | def _parse_my_resource(resource): |
| 70 | |
| 71 | parse_url = netutils.urlsplit(resource) |
| 72 | |
| 73 | params = urlparse.parse_qs(parse_url.query) |
| 74 | parts = urlparse.ParseResult(parse_url.scheme, |
| 75 | parse_url.netloc, |
| 76 | parse_url.path, |
| 77 | None, |
| 78 | None, |
| 79 | None) |
| 80 | return parts, params |
| 81 | |
| 82 | @staticmethod |
| 83 | def get_driver(scheme): |
| 84 | if scheme not in _Base.drivers: |
| 85 | _Base.drivers[scheme] = _driver.DriverManager(_Base.NAMESPACE, |
| 86 | scheme).driver() |
| 87 | return _Base.drivers[scheme] |
| 88 | |
| 89 | def get_samples(self, manager, cache, resources): |
| 90 | resources = resources or [] |
| 91 | for resource in resources: |
| 92 | parse_url, params = self._parse_my_resource(resource) |
| 93 | ext = self.get_driver(parse_url.scheme) |
| 94 | sample_data = ext.get_sample_data(self.meter_name, |
| 95 | parse_url, |
| 96 | params, |
| 97 | cache) |
| 98 | |
| 99 | for data in sample_data or []: |
| 100 | if data is None: |
| 101 | continue |
| 102 | if not isinstance(data, list): |
| 103 | data = [data] |
| 104 | for (volume, resource_id, |
| 105 | resource_metadata, timestamp) in data: |
| 106 | |
| 107 | yield sample.Sample( |
| 108 | name=self.meter_name, |
| 109 | type=self.meter_type, |
| 110 | unit=self.meter_unit, |
| 111 | volume=volume, |
| 112 | user_id=None, |
| 113 | project_id='default_admin_tenant', |
| 114 | resource_id=resource_id, |
| 115 | timestamp=timestamp, |
| 116 | resource_metadata=resource_metadata |
| 117 | ) |