def __parse(line): """ split the line and put the fields into a dict """ dct = dict(time='', region='', user='', role='', type='', operation='', object='', des='') line = line.strip('\n') dct['time'] = time2epoch(local2utc(str2date(line[0:19]))) pos = line.find(': ') vals_ = line[pos + 1:].split(' ') reg_beg = line[:pos].rfind(' ') + 1 dct['region'] = line[reg_beg:pos] dct[__KEYS[1]] = vals_[1] dct[__KEYS[2]] = vals_[2] dct[__KEYS[3]] = vals_[3] dct[__KEYS[4]] = vals_[4] dct[__KEYS[5]] = vals_[5] if len(vals_) == 7: dct['des'] = vals_[6] else: for i in range(len(vals_) - 6): dct['des'] += (vals_[i + 6] + ' ') dct['des'] = dct['des'].strip(' ()') return dct
def gen_out_volume(volume, metadata): """ :param volume: :param metadata: :return: """ tenant_id = volume["tenant_id"] new_tenant = yield get_tenant_by_id(tenant_id) user_id = metadata.get('user', None) new_user = {} if user_id: try: new_user = yield get_user_by_id(user_id) except Exception: metadata["user"] = "" attachments = [] if volume["attatchments_id"]: attach = { "id": volume["attatchments_id"], "vm_id": volume["vm_id"], "volume_id": volume["id"] } attachments.append(attach) volume_status = metadata["status"] if "status" in metadata and metadata[ "status"] else volume['status'] recover_status = metadata.get("recover_status", "") out_volume = { "id": volume['id'], "name": volume['name'], "size_gb": volume['size_gb'], "status": volume_status, "recover-status": recover_status, "location": volume['host'], "type": volume["type"], "attachments": attachments, "tenant": new_tenant, "user": new_user, "metadata": metadata, "created_at": datetimeUtils.time2epoch(volume['created_at']) } raise gen.Return(out_volume)
def __strdate2int(strdate): date = datetimeUtils.str2date2(strdate) return datetimeUtils.time2epoch(date)
status, hostid = commands.getstatusoutput("hostid") LOG.debug("status=%s, output=%s", status, hostid) if status == 0: return hostid else: raise GetHostIdError except Exception as e: LOG.error("get hostid error:%s" % e) LOG.error(trace()) raise GetHostIdError # def license_storage(license_context): # # if os.path.exists(LICENSE_PATH): # status, output = commands.getstatusoutput("rm -rf %s" % LICENSE_PATH) # # if not os.path.exists(LICENSE): # status, output = commands.getstatusoutput("mkdir %s" % LICENSE) # status, output = commands.getstatusoutput("touch %s" % LICENSE_PATH) # # with open(LICENSE_PATH, 'w+') as fileopt: # fileopt.write(license_context) if __name__ == "__main__": # check_license() s = "2020-11-21" created_at = datetimeUtils.str2date2(s) a = datetimeUtils.time2epoch(created_at) print a
}, "user": user, "id": server["id"], "tenant": tenant, "network": network, "network_info": networks, "name": server.get("name"), "des": extend.get("des", ""), "state": status, "recover-status": recover_status, "memory_mb": server.get("memory"), "cores": server.get("cores"), "keepalive": extend.get("keepalive", ""), "sys_volume": meta.get("sys_volume", ""), "image": image_meta, "created_at": datetimeUtils.time2epoch(server.get("created_at")) } raise gen.Return(out_server) @gen.coroutine def list_vm_by_database(**kwargs): """ :param kwargs: 查询条件 key:数据库nova中instances表中存在字段 :return: vm列表 """ db = dbpools.get_pool(dbpools.NOVA_DB) sql_con = '' if kwargs: for item in kwargs: sql_con += " and " + item + " = '%s'" % kwargs.get(item)
def sample_continuance_enhance(counter_name, samples, limit): utc_now = time2epoch(datetime.datetime.utcnow()) for s in samples: s["timestamp"] = time2epoch(s["timestamp"]) if limit <= 2 and samples: return samples elif limit <= 2 and not samples: for i in range(0, limit): samples.append({ "timestamp": utc_now, "counter_unit": None, "counter_volume": None, "counter_name": counter_name, "resource_id": None, "_id": None, "id": uuid_gen(24) }) return samples result = [] interval = METER_INTERVAL[counter_name] if len(samples) > 0: last_sample_time = samples[0]['timestamp'] if (utc_now - last_sample_time) > (limit-1)*interval: cutting_time = utc_now elif (utc_now - last_sample_time) > 2*interval: cutting_time = last_sample_time + ((utc_now - last_sample_time)/interval)*interval cutting_time-=interval else: cutting_time = last_sample_time else: cutting_time = utc_now time_line = [cutting_time - i * interval for i in range(0, limit)] samples_dict = {} for sample_item in samples: if sample_item['resource_id'] in samples_dict: samples_dict[sample_item['resource_id']].append(sample_item) else: samples_dict[sample_item['resource_id']] = [sample_item] if samples_dict: d_vals = samples_dict.values() for time_item in time_line: for d_val in d_vals: t_flag = False t_resource_id = None for d_val_item in d_val: if t_resource_id is None: t_resource_id = d_val_item["resource_id"] if abs(d_val_item["timestamp"]-time_item) < interval/2.0: d_val_item["timestamp"] = time_item result.append(d_val_item) t_flag = True if not t_flag: result.append({ "timestamp": time_item, "counter_unit": None, "counter_volume": None, "counter_name": counter_name, "resource_id": t_resource_id, "_id": None, "id": uuid_gen(24) }) else: for time_item_t in time_line: result.append({ "timestamp": time_item_t, "counter_unit": None, "counter_volume": None, "counter_name": counter_name, "resource_id": None, "_id": None, "id": uuid_gen(24) }) result=sorted(result, key=lambda m: m['timestamp'], reverse=True) return result
def __datetime_to_epochtime(dt): return time2epoch(dt) if dt else None
if meta_item["volume_id"] in volume_mate: volume_mate[meta_item["volume_id"]].update({meta_item["meta_key"]: meta_item["meta_value"]}) else: volume_mate[meta_item["volume_id"]] = {meta_item["meta_key"]: meta_item["meta_value"]} for volume in volumes: mate_item = volume_mate.get(volume["id"], {}) out_volume = yield gen_out_volume(volume, mate_item) out_volumes.append(out_volume) else: for volume in volumes: out_volume = { "id": volume['id'], "name": volume['name'], "size_gb": volume['size_gb'], "created_at": datetimeUtils.time2epoch(volume['created_at']), "type": volume["type"], "location":volume['host'], "tenant_id": volume["tenant_id"], "status": volume["status"] } out_volumes.append(out_volume) raise gen.Return(out_volumes) @gen.coroutine @required("tenant_id", "size") def create_volume(**volume): """ create volume :param volume: A dict of volume :return: