blob: 9c53c365a529ea8fe7639bd0e7625ad2aa02629e [file] [log] [blame]
rdudyalab086cf32016-08-11 00:07:45 -04001#
2# Copyright 2014 NEC Corporation. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# 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, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import abc
17
18from oslo_utils import netutils
19import six
20from six.moves.urllib import parse as urlparse
21from stevedore import driver as _driver
22
23from ceilometer.agent import plugin_base
24from ceilometer import sample
25
26
27@six.add_metaclass(abc.ABCMeta)
28class _Base(plugin_base.PollsterBase):
29
30 NAMESPACE = 'network.statistics.drivers'
31 drivers = {}
32
33 @property
34 def default_discovery(self):
35 # this signifies that the pollster gets its resources from
36 # elsewhere, in this case they're manually listed in the
37 # pipeline configuration
38 return None
39
40 @abc.abstractproperty
41 def meter_name(self):
42 """Return a Meter Name."""
43
44 @abc.abstractproperty
45 def meter_type(self):
46 """Return a Meter Type."""
47
48 @abc.abstractproperty
49 def meter_unit(self):
50 """Return a Meter Unit."""
51
52 @staticmethod
53 def _parse_my_resource(resource):
54
55 parse_url = netutils.urlsplit(resource)
56
57 params = urlparse.parse_qs(parse_url.query)
58 parts = urlparse.ParseResult(parse_url.scheme,
59 parse_url.netloc,
60 parse_url.path,
61 None,
62 None,
63 None)
64 return parts, params
65
66 @staticmethod
67 def get_driver(scheme):
68 if scheme not in _Base.drivers:
69 _Base.drivers[scheme] = _driver.DriverManager(_Base.NAMESPACE,
70 scheme).driver()
71 return _Base.drivers[scheme]
72
73 def get_samples(self, manager, cache, resources):
74 resources = resources or []
75 for resource in resources:
76 parse_url, params = self._parse_my_resource(resource)
77 ext = self.get_driver(parse_url.scheme)
78 sample_data = ext.get_sample_data(self.meter_name,
79 parse_url,
80 params,
81 cache)
82
83 for data in sample_data or []:
84 if data is None:
85 continue
86 if not isinstance(data, list):
87 data = [data]
88 for (volume, resource_id,
89 resource_metadata, timestamp) in data:
90
91 yield sample.Sample(
92 name=self.meter_name,
93 type=self.meter_type,
94 unit=self.meter_unit,
95 volume=volume,
96 user_id=None,
97 project_id='default_admin_tenant',
98 resource_id=resource_id,
99 timestamp=timestamp,
100 resource_metadata=resource_metadata
101 )