blob: 6875ae8a51c85cb98ba82167947a17a200ae122b [file] [log] [blame]
Sapan Bhatia56410d52014-12-16 01:08:23 -05001#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4from ceilometerclient import client
5from os import environ as env
6import keystoneclient.v2_0.client as ksclient
7import re
8import datetime
9import time
10from monitor.monitordriver import *
Sapan Bhatiaa2563052015-01-10 04:53:00 +000011import pdb
Sapan Bhatia56410d52014-12-16 01:08:23 -050012
Sapan Bhatia6e08c3e2015-01-07 01:09:58 -050013def object_to_filter(model_name, pk):
Sapan Bhatiaa2563052015-01-10 04:53:00 +000014 from core.models import *
Sapan Bhatia7c10fb22015-01-07 12:35:05 -050015 filter_dict = {
16 'Slice':[Slice, 'tenant_id', 'project_id'],
17 'Sliver':[Sliver, 'instance_id', 'resource_id'],
18 'Site':[Site, 'tenant_id', 'project_id']
19 }
20
21 mod,field,tag = filter_dict[model_name]
22 obj = mod.objects.get(pk=pk)
Sapan Bhatiaa2563052015-01-10 04:53:00 +000023 return '%s=%s'%(tag,getattr(obj,field))
Sapan Bhatia7c10fb22015-01-07 12:35:05 -050024
Sapan Bhatia56410d52014-12-16 01:08:23 -050025
26def cli_to_array(cli_query):
27 '''This converts from the cli list of queries to what is required
28 by the python api.
29 so from:
30 "this<=34;that=foo"
31 to
32 "[{field=this,op=le,value=34},{field=that,op=eq,value=foo}]"
33 '''
34 if cli_query is None:
35 return None
36
37 op_lookup = {'!=': 'ne',
38 '>=': 'ge',
39 '<=': 'le',
40 '>': 'gt',
41 '<': 'lt',
42 '=': 'eq'}
43
44 def split_by_op(string):
45 # two character split (<=,!=)
46 frags = re.findall(r'([[a-zA-Z0-9_.]+)([><!]=)([^ -,\t\n\r\f\v]+)',
47 string)
48 if len(frags) == 0:
49 #single char split (<,=)
50 frags = re.findall(r'([a-zA-Z0-9_.]+)([><=])([^ -,\t\n\r\f\v]+)',
51 string)
52 return frags
53
54 opts = []
55 queries = cli_query.split(';')
56 for q in queries:
57 frag = split_by_op(q)
58 if len(frag) > 1:
59 raise ValueError('incorrect seperator %s in query "%s"' %
60 ('(should be ";")', q))
61 if len(frag) == 0:
62 raise ValueError('invalid query %s' % q)
63 query = frag[0]
64 opt = {}
65 opt['field'] = query[0]
66 opt['op'] = op_lookup[query[1]]
67 opt['value'] = query[2]
68 opts.append(opt)
69 return opts
70
71def meters_to_stats(meters):
72 stats = DashboardStatistics()
73 for m in meters:
Sapan Bhatiaa2563052015-01-10 04:53:00 +000074 timestamp = m.duration_start
75 stats['stat_list'].append({'timestamp':timestamp, 'value':m.sum})
76 stats['sum']+=m.sum
77 stats['average']+=m.sum
78 stats['unit'] = 'ns'
Sapan Bhatia56410d52014-12-16 01:08:23 -050079
Sapan Bhatiaa2563052015-01-10 04:53:00 +000080 stats['average']/=len(meters)
Sapan Bhatia56410d52014-12-16 01:08:23 -050081 return stats
Sapan Bhatia7c10fb22015-01-07 12:35:05 -050082
83
Sapan Bhatia56410d52014-12-16 01:08:23 -050084class CeilometerDriver(MonitorDriver):
Sapan Bhatia6e08c3e2015-01-07 01:09:58 -050085 def get_meter(self, meter, obj, pk, keystone=None):
Sapan Bhatiaa2563052015-01-10 04:53:00 +000086 keystone = {}
87 keystone['os_username']=env['OS_USERNAME']
88 keystone['os_password']=env['OS_PASSWORD']
89 keystone['os_auth_url']=env['OS_AUTH_URL']
90 keystone['os_tenant_name']=env['OS_TENANT_NAME']
91 keystone['os_cacert']=env['OS_CACERT']
92 keystone['os_region_name']=env['OS_REGION_NAME']
Sapan Bhatia56410d52014-12-16 01:08:23 -050093
Sapan Bhatiaa2563052015-01-10 04:53:00 +000094 keystone['username']=env['OS_USERNAME']
95 keystone['password']=env['OS_PASSWORD']
96 keystone['auth_url']=env['OS_AUTH_URL']
97 keystone['tenant_name']=env['OS_TENANT_NAME']
98 keystone['cacert']=env['OS_CACERT']
99 keystone['region_name']=env['OS_REGION_NAME']
Sapan Bhatia56410d52014-12-16 01:08:23 -0500100
Sapan Bhatiaa2563052015-01-10 04:53:00 +0000101 keystone['auth_plugin']=client.AuthPlugin(**keystone)
Sapan Bhatia56410d52014-12-16 01:08:23 -0500102
Sapan Bhatiaa2563052015-01-10 04:53:00 +0000103 ceilometer = client.get_client(2,**keystone)
104
Sapan Bhatia56410d52014-12-16 01:08:23 -0500105 cur_ts = datetime.datetime.fromtimestamp(time.time()-86400)
106 str_ts = cur_ts.strftime('%Y-%m-%dT%H:%M:%S')
Sapan Bhatia7c10fb22015-01-07 12:35:05 -0500107
Sapan Bhatiaab981392015-01-07 12:36:10 -0500108 object_filter = object_to_filter(obj, pk)
Sapan Bhatia56410d52014-12-16 01:08:23 -0500109 filter=';'.join([object_filter,'timestamp>%s'%str_ts])
110 #query = cli_to_array("project_id=124de34266b24f57957345cdb43cc9ff;timestamp>2014-12-11T00:00:00")
111 query = cli_to_array(filter)
112
113 meters = ceilometer.statistics.list(meter,q=query,period=3600)
114
115 stats = meters_to_stats(meters)
116 return stats