blob: edf2f02b15d3ca7148d49f517af6ee7ac3ce69b7 [file] [log] [blame]
Matteo Scandoloeb0d11c2017-08-08 13:05:26 -07001
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
rdudyalab086cf32016-08-11 00:07:45 -040017#
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
32import abc
33
34from oslo_utils import netutils
35import six
36from six.moves.urllib import parse as urlparse
37from stevedore import driver as _driver
38
39from ceilometer.agent import plugin_base
40from ceilometer import sample
41
42
43@six.add_metaclass(abc.ABCMeta)
44class _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 )