def save_in_gnocchi(resource_type, resource_measures_list): # connect to gnocchi try: auth_plugin = auth.GnocchiBasicPlugin(user=gnocchi_username, endpoint=gnocchi_url) gnocchi = client.Client(session_options={'auth': auth_plugin}) except: print("Error occured while autheticating gnocchi credentials!") sys.exit() # send data for r_m in resource_measures_list: # print(r_m) resource_name = resource_type + ":" + r_m["resource_key"] measures = { resource_name: collections.defaultdict(list) } for measure in r_m["measures"]: measures[resource_name][measure["type"]].append({ "timestamp": measure["time"], "value": measure["value"], }) try: try: # sending measures gnocchi.metric.batch_resources_metrics_measures( measures, create_metrics=True) except exceptions.BadRequest: # Create required resource and send again attrs = {"id": resource_name, "host": r_m["resource_key"]} try: try: gnocchi.resource.create(resource_type, attrs) except exceptions.ResourceTypeNotFound: try: # create resource type gnocchi.resource_type.create({ "name": resource_type, "attributes": { "host": { "required": True, "type": "string", }, } }) except exceptions.ResourceTypeAlreadyExists: pass gnocchi.resource.create(resource_type, attrs) except exceptions.ResourceAlreadyExists: pass gnocchi.metric.batch_resources_metrics_measures( measures, create_metrics=True) except Exception as e: print("Unexpected Error!", e) try: auth_plugin = auth.GnocchiBasicPlugin(user=gnocchi_username, endpoint=gnocchi_url) gnocchi = client.Client(session_options={'auth': auth_plugin}) except: print("Error occured while autheticating gnocchi credentials!") sys.exit() continue
def __init__(self, transformers, **kwargs): super(GnocchiCollector, self).__init__(transformers, **kwargs) adapter_options = {'connect_retries': 3} if CONF.gnocchi_collector.gnocchi_auth_type == 'keystone': auth_plugin = ks_loading.load_auth_from_conf_options( CONF, 'gnocchi_collector', ) adapter_options['interface'] = CONF.gnocchi_collector.interface else: auth_plugin = gauth.GnocchiBasicPlugin( user=CONF.gnocchi_collector.gnocchi_user, endpoint=CONF.gnocchi_collector.gnocchi_endpoint, ) adapter_options['region_name'] = CONF.gnocchi_collector.region_name verify = True if CONF.gnocchi_collector.cafile: verify = CONF.gnocchi_collector.cafile elif CONF.gnocchi_collector.insecure: verify = False self._conn = gclient.Client( '1', session_options={'auth': auth_plugin, 'verify': verify}, adapter_options=adapter_options, )
def __init__(self): super(GnocchiFetcher, self).__init__() adapter_options = {'connect_retries': 3} if CONF.fetcher_gnocchi.gnocchi_auth_type == 'keystone': auth_plugin = ks_loading.load_auth_from_conf_options( CONF, FETCHER_GNOCCHI_OPTS, ) adapter_options['interface'] = CONF.fetcher_gnocchi.interface else: auth_plugin = gauth.GnocchiBasicPlugin( user=CONF.fetcher_gnocchi.gnocchi_user, endpoint=CONF.fetcher_gnocchi.gnocchi_endpoint, ) adapter_options['region_name'] = CONF.fetcher_gnocchi.region_name verify = True if CONF.fetcher_gnocchi.cafile: verify = CONF.fetcher_gnocchi.cafile elif CONF.fetcher_gnocchi.insecure: verify = False self._conn = gclient.Client( '1', session_options={ 'auth': auth_plugin, 'verify': verify }, adapter_options=adapter_options, )
def __init__(self, **kwargs): super(GnocchiCollector, self).__init__(**kwargs) adapter_options = {'connect_retries': 3} if CONF.collector_gnocchi.gnocchi_auth_type == 'keystone': auth_plugin = ks_loading.load_auth_from_conf_options( CONF, COLLECTOR_GNOCCHI_OPTS, ) adapter_options['interface'] = CONF.collector_gnocchi.interface else: auth_plugin = gauth.GnocchiBasicPlugin( user=CONF.collector_gnocchi.gnocchi_user, endpoint=CONF.collector_gnocchi.gnocchi_endpoint, ) adapter_options['region_name'] = CONF.collector_gnocchi.region_name verify = True if CONF.collector_gnocchi.cafile: verify = CONF.collector_gnocchi.cafile elif CONF.collector_gnocchi.insecure: verify = False self._conn = gclient.Client( '1', session=custom_session.create_custom_session( { 'auth': auth_plugin, 'verify': verify }, CONF.collector_gnocchi.http_pool_maxsize), adapter_options=adapter_options, )
def init(self): auth_plugin = auth.GnocchiBasicPlugin(user="******", endpoint="http://localhost:8041") self.g = client.Client(session_options={'auth': auth_plugin}) self._resource_type = "qpid_amqp" try: self.g.resource_type.get(self._resource_type) except exceptions.ResourceTypeNotFound: self.g.resource_type.create({ "name": self._resource_type, "attributes": { "host": { "required": True, "type": "string", }, }, })
def __init__(self, *args, **kwargs): super(GnocchiStorage, self).__init__(*args, **kwargs) adapter_options = {'connect_retries': 3} if CONF.storage_gnocchi.gnocchi_auth_type == 'keystone': auth_plugin = ks_loading.load_auth_from_conf_options( CONF, 'storage_gnocchi', ) adapter_options['interface'] = CONF.storage_gnocchi.api_interface else: auth_plugin = gauth.GnocchiBasicPlugin( user=CONF.storage_gnocchi.gnocchi_user, endpoint=CONF.storage_gnocchi.gnocchi_endpoint, ) self._conn = gclient.Client( '1', session_options={'auth': auth_plugin}, adapter_options=adapter_options, ) self._cacher = GnocchiResourceCacher()
def test_metric_listing(self): apname = str(uuid.uuid4()) # PREPARE AN ARCHIVE POLICY self.gnocchi("archive-policy", params="create " "-d granularity:1s,points:86400 %s" % apname) # Create 1005 metrics c = client.Client( 1, session_options={ 'auth': auth.GnocchiBasicPlugin(user="******", endpoint=self.endpoint) }, ) for i in range(1005): c.metric.create({"archive_policy_name": apname}) result = self.gnocchi(u'metric', params=u"list") self.assertGreaterEqual(len(json.loads(result)), 1005) result = self.gnocchi(u'metric', params=u"list" u" --limit 2") self.assertEqual(2, len(json.loads(result)))
def __init__(self, transformers, **kwargs): super(GnocchiCollector, self).__init__(transformers, **kwargs) self.t_gnocchi = self.transformers['GnocchiTransformer'] self.t_cloudkitty = self.transformers['CloudKittyFormatTransformer'] adapter_options = {'connect_retries': 3} if CONF.gnocchi_collector.gnocchi_auth_type == 'keystone': auth_plugin = ks_loading.load_auth_from_conf_options( CONF, 'gnocchi_collector', ) adapter_options['interface'] = CONF.gnocchi_collector.interface else: auth_plugin = gauth.GnocchiBasicPlugin( user=CONF.gnocchi_collector.gnocchi_user, endpoint=CONF.gnocchi_collector.gnocchi_endpoint, ) self._conn = gclient.Client( '1', session_options={'auth': auth_plugin}, adapter_options=adapter_options, )
import collections from gnocchiclient import auth, exceptions from gnocchiclient.v1 import client, metric import yaml import io # Read YAML file with open("/etc/dhi-ojas/gnocchi.yml", 'r') as stream: gnocchi_config = yaml.safe_load(stream) gnocchi_url = gnocchi_config['gnocchi']['endpoint_url'] gnocchi_username = gnocchi_config['gnocchi']['username'] # connect to gnocchi try: auth_plugin = auth.GnocchiBasicPlugin(user=gnocchi_username, endpoint=gnocchi_url) gnocchi = client.Client(session_options={'auth': auth_plugin}) except: print("Error occured while autheticating gnocchi credentials!") sys.exit(2) def get_resources(): resources = gnocchi.resource.list() return resources def get_measures(id): measures = gnocchi.metric.get_measures(id) return measures def save_in_gnocchi(resource_type, resource_measures_list): # connect to gnocchi
def test_get_headers(self): p = auth.GnocchiBasicPlugin("foobar", "http://localhost") self.assertEqual({'Authorization': 'basic Zm9vYmFyOg=='}, p.get_headers(None))
import datetime MCAST_GRP = '225.0.0.37' MCAST_PORT = 1406 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((MCAST_GRP, MCAST_PORT)) mreq = struct.pack('4sl', socket.inet_aton(MCAST_GRP), socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) print("Conectando no Gnocchi...") auth_plugin = auth.GnocchiBasicPlugin(user="******", endpoint="http://10.7.52.84:8041") gnocchi = client.Client(session_options={'auth': auth_plugin}) date_time = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S-03:00') corrente=0 tensao=0 def recebeValor(): valor = sock.recv(1024) #.decode('utf-8') novoValor = struct.unpack('cf',valor) return novoValor def sendGrafana(corrente,tensao_final,potencia_final):