S.Çağlar Onur | 3e92b4d | 2015-02-09 13:34:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
Sapan Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | from ceilometerclient import client |
| 5 | from os import environ as env |
| 6 | import keystoneclient.v2_0.client as ksclient |
| 7 | import re |
| 8 | import datetime |
| 9 | import time |
| 10 | from monitor.monitordriver import * |
Sapan Bhatia | c6758b4 | 2015-01-10 04:53:00 +0000 | [diff] [blame] | 11 | import pdb |
Sapan Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 12 | |
Sapan Bhatia | 3a812f7 | 2015-01-07 01:09:58 -0500 | [diff] [blame] | 13 | def object_to_filter(model_name, pk): |
Sapan Bhatia | c6758b4 | 2015-01-10 04:53:00 +0000 | [diff] [blame] | 14 | from core.models import * |
Sapan Bhatia | a3e6e0f6 | 2015-01-07 12:35:05 -0500 | [diff] [blame] | 15 | filter_dict = { |
Sapan Bhatia | d123b52 | 2015-01-12 16:48:19 -0500 | [diff] [blame] | 16 | 'ControllerSlice':[ControllerSlice, 'tenant_id', 'project_id'], |
Sapan Bhatia | a3e6e0f6 | 2015-01-07 12:35:05 -0500 | [diff] [blame] | 17 | 'Sliver':[Sliver, 'instance_id', 'resource_id'], |
Sapan Bhatia | d123b52 | 2015-01-12 16:48:19 -0500 | [diff] [blame] | 18 | 'ControllerSite':[ControllerSite, 'tenant_id', 'project_id'] |
Sapan Bhatia | a3e6e0f6 | 2015-01-07 12:35:05 -0500 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | mod,field,tag = filter_dict[model_name] |
| 22 | obj = mod.objects.get(pk=pk) |
Sapan Bhatia | c6758b4 | 2015-01-10 04:53:00 +0000 | [diff] [blame] | 23 | return '%s=%s'%(tag,getattr(obj,field)) |
Sapan Bhatia | a3e6e0f6 | 2015-01-07 12:35:05 -0500 | [diff] [blame] | 24 | |
Sapan Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 25 | |
| 26 | def 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 | |
| 71 | def meters_to_stats(meters): |
| 72 | stats = DashboardStatistics() |
| 73 | for m in meters: |
Sapan Bhatia | c6758b4 | 2015-01-10 04:53:00 +0000 | [diff] [blame] | 74 | 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 Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 79 | |
Sapan Bhatia | 769a85a | 2015-01-10 04:55:54 +0000 | [diff] [blame] | 80 | if (len(meters)): |
| 81 | stats['average']/=len(meters) |
| 82 | else: |
| 83 | stats['average']=0 |
Sapan Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 84 | return stats |
Sapan Bhatia | a3e6e0f6 | 2015-01-07 12:35:05 -0500 | [diff] [blame] | 85 | |
| 86 | |
Sapan Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 87 | class CeilometerDriver(MonitorDriver): |
Sapan Bhatia | 3a812f7 | 2015-01-07 01:09:58 -0500 | [diff] [blame] | 88 | def get_meter(self, meter, obj, pk, keystone=None): |
Sapan Bhatia | d123b52 | 2015-01-12 16:48:19 -0500 | [diff] [blame] | 89 | 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 Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 97 | |
Sapan Bhatia | d123b52 | 2015-01-12 16:48:19 -0500 | [diff] [blame] | 98 | 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 Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 104 | |
Sapan Bhatia | c6758b4 | 2015-01-10 04:53:00 +0000 | [diff] [blame] | 105 | keystone['auth_plugin']=client.AuthPlugin(**keystone) |
Sapan Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 106 | |
Sapan Bhatia | c6758b4 | 2015-01-10 04:53:00 +0000 | [diff] [blame] | 107 | ceilometer = client.get_client(2,**keystone) |
| 108 | |
Sapan Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 109 | cur_ts = datetime.datetime.fromtimestamp(time.time()-86400) |
| 110 | str_ts = cur_ts.strftime('%Y-%m-%dT%H:%M:%S') |
Sapan Bhatia | a3e6e0f6 | 2015-01-07 12:35:05 -0500 | [diff] [blame] | 111 | |
Sapan Bhatia | e1a8b2b | 2015-01-07 12:36:10 -0500 | [diff] [blame] | 112 | object_filter = object_to_filter(obj, pk) |
Sapan Bhatia | 6d7e7bd | 2014-12-16 01:08:23 -0500 | [diff] [blame] | 113 | 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 |