blob: 9944a9cf4ec4a9ce3d7ab3408e084163ec7a1ae2 [file] [log] [blame]
S.Çağlar Onur3e92b4d2015-02-09 13:34:11 -05001#!/usr/bin/env python
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -05002# -*- 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 Bhatiac6758b42015-01-10 04:53:00 +000011import pdb
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -050012
Sapan Bhatia3a812f72015-01-07 01:09:58 -050013def object_to_filter(model_name, pk):
Sapan Bhatiac6758b42015-01-10 04:53:00 +000014 from core.models import *
Sapan Bhatiaa3e6e0f62015-01-07 12:35:05 -050015 filter_dict = {
Sapan Bhatiad123b522015-01-12 16:48:19 -050016 'ControllerSlice':[ControllerSlice, 'tenant_id', 'project_id'],
Sapan Bhatiaa3e6e0f62015-01-07 12:35:05 -050017 'Sliver':[Sliver, 'instance_id', 'resource_id'],
Sapan Bhatiad123b522015-01-12 16:48:19 -050018 'ControllerSite':[ControllerSite, 'tenant_id', 'project_id']
Sapan Bhatiaa3e6e0f62015-01-07 12:35:05 -050019 }
20
21 mod,field,tag = filter_dict[model_name]
22 obj = mod.objects.get(pk=pk)
Sapan Bhatiac6758b42015-01-10 04:53:00 +000023 return '%s=%s'%(tag,getattr(obj,field))
Sapan Bhatiaa3e6e0f62015-01-07 12:35:05 -050024
Sapan Bhatia6d7e7bd2014-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 Bhatiac6758b42015-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 Bhatia6d7e7bd2014-12-16 01:08:23 -050079
Sapan Bhatia769a85a2015-01-10 04:55:54 +000080 if (len(meters)):
81 stats['average']/=len(meters)
82 else:
83 stats['average']=0
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -050084 return stats
Sapan Bhatiaa3e6e0f62015-01-07 12:35:05 -050085
86
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -050087class CeilometerDriver(MonitorDriver):
Sapan Bhatia3a812f72015-01-07 01:09:58 -050088 def get_meter(self, meter, obj, pk, keystone=None):
Sapan Bhatiad123b522015-01-12 16:48:19 -050089 if (not keystone):
90 keystone={}
91 keystone['os_username']=env['OS_USERNAME']
92 keystone['os_password']=env['OS_PASSWORD']
93 keystone['os_auth_url']=env['OS_AUTH_URL']
94 keystone['os_tenant_name']=env['OS_TENANT_NAME']
95 keystone['os_cacert']=env['OS_CACERT']
96 keystone['os_region_name']=env['OS_REGION_NAME']
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -050097
Sapan Bhatiad123b522015-01-12 16:48:19 -050098 keystone['username']=env['OS_USERNAME']
99 keystone['password']=env['OS_PASSWORD']
100 keystone['auth_url']=env['OS_AUTH_URL']
101 keystone['tenant_name']=env['OS_TENANT_NAME']
102 keystone['cacert']=env['OS_CACERT']
103 keystone['region_name']=env['OS_REGION_NAME']
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -0500104
Sapan Bhatiac6758b42015-01-10 04:53:00 +0000105 keystone['auth_plugin']=client.AuthPlugin(**keystone)
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -0500106
Sapan Bhatiac6758b42015-01-10 04:53:00 +0000107 ceilometer = client.get_client(2,**keystone)
108
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -0500109 cur_ts = datetime.datetime.fromtimestamp(time.time()-86400)
110 str_ts = cur_ts.strftime('%Y-%m-%dT%H:%M:%S')
Sapan Bhatiaa3e6e0f62015-01-07 12:35:05 -0500111
Sapan Bhatiae1a8b2b2015-01-07 12:36:10 -0500112 object_filter = object_to_filter(obj, pk)
Sapan Bhatia6d7e7bd2014-12-16 01:08:23 -0500113 filter=';'.join([object_filter,'timestamp>%s'%str_ts])
114 #query = cli_to_array("project_id=124de34266b24f57957345cdb43cc9ff;timestamp>2014-12-11T00:00:00")
115 query = cli_to_array(filter)
116
117 meters = ceilometer.statistics.list(meter,q=query,period=3600)
118
119 stats = meters_to_stats(meters)
120 return stats