|
| 1 | +# IM - Infrastructure Manager |
| 2 | +# Copyright (C) 2011 - GRyCAP - Universitat Politecnica de Valencia |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import os.path |
| 18 | +import datetime |
| 19 | +import json |
| 20 | +import yaml |
| 21 | +import logging |
| 22 | + |
| 23 | +from IM.db import DataBase |
| 24 | +from IM.auth import Authentication |
| 25 | +from IM.config import Config |
| 26 | +from IM.VirtualMachine import VirtualMachine |
| 27 | + |
| 28 | + |
| 29 | +class Stats(): |
| 30 | + |
| 31 | + logger = logging.getLogger('InfrastructureManager') |
| 32 | + """Logger object.""" |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def _get_data(str_data, auth=None): |
| 36 | + dic = json.loads(str_data) |
| 37 | + resp = {'creation_date': None} |
| 38 | + if 'creation_date' in dic and dic['creation_date']: |
| 39 | + resp['creation_date'] = str(datetime.datetime.fromtimestamp(float(dic['creation_date']))) |
| 40 | + auth = Authentication.deserialize(dic['auth']) |
| 41 | + resp['icon'] = None |
| 42 | + im_auth = auth.getAuthInfo("InfrastructureManager")[0] |
| 43 | + if 'extra_info' in dic and dic['extra_info'] and "TOSCA" in dic['extra_info']: |
| 44 | + try: |
| 45 | + tosca = yaml.safe_load(dic['extra_info']['TOSCA']) |
| 46 | + icon = tosca.get("metadata", {}).get("icon", "") |
| 47 | + resp['icon'] = os.path.basename(icon)[:-4] |
| 48 | + except Exception: |
| 49 | + Stats.logger.exception("Error loading TOSCA.") |
| 50 | + |
| 51 | + resp['vm_count'] = 0 |
| 52 | + resp['cpu_count'] = 0 |
| 53 | + resp['memory_size'] = 0 |
| 54 | + resp['cloud_type'] = None |
| 55 | + resp['cloud_host'] = None |
| 56 | + resp['hybrid'] = False |
| 57 | + for vm_data in dic['vm_list']: |
| 58 | + vm = VirtualMachine.deserialize(vm_data) |
| 59 | + |
| 60 | + # only get the cloud of the first VM |
| 61 | + if not resp['cloud_type']: |
| 62 | + resp['cloud_type'] = vm.cloud.type |
| 63 | + if not resp['cloud_host']: |
| 64 | + resp['cloud_host'] = vm.cloud.get_url() |
| 65 | + elif resp['cloud_host'] != vm.cloud.get_url(): |
| 66 | + resp['hybrid'] = True |
| 67 | + |
| 68 | + vm_sys = vm.info.systems[0] |
| 69 | + if vm_sys.getValue('cpu.count'): |
| 70 | + resp['cpu_count'] += vm_sys.getValue('cpu.count') |
| 71 | + if vm_sys.getValue('memory.size'): |
| 72 | + resp['memory_size'] += vm_sys.getFeature('memory.size').getValue('M') |
| 73 | + resp['vm_count'] += 1 |
| 74 | + |
| 75 | + if auth is None or im_auth.compare(auth): |
| 76 | + resp['im_user'] = im_auth.get('username', "") |
| 77 | + return resp |
| 78 | + else: |
| 79 | + return None |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + def get_stats(init_date="1970-01-01", auth=None): |
| 83 | + """ |
| 84 | + Get the statistics from the IM DB. |
| 85 | +
|
| 86 | + Args: |
| 87 | +
|
| 88 | + - init_date(str): Only will be returned infrastructure created afther this date. |
| 89 | + - auth(Authentication): parsed authentication tokens. |
| 90 | +
|
| 91 | + Return: a list of dict with the stats. |
| 92 | + """ |
| 93 | + stats = [] |
| 94 | + db = DataBase(Config.DATA_DB) |
| 95 | + if db.connect(): |
| 96 | + res = db.select("SELECT data, date, id FROM inf_list WHERE date > '%s' order by rowid desc;" % init_date) |
| 97 | + for elem in res: |
| 98 | + data = elem[0] |
| 99 | + date = elem[1] |
| 100 | + inf_id = elem[2] |
| 101 | + res = Stats._get_data(data.decode(), auth) |
| 102 | + if res: |
| 103 | + res['inf_id'] = inf_id |
| 104 | + res['last_date'] = str(date) |
| 105 | + stats.append(res) |
| 106 | + |
| 107 | + db.close() |
| 108 | + return stats |
| 109 | + else: |
| 110 | + Stats.logger.error("ERROR connecting with the database!.") |
| 111 | + return None |
0 commit comments