def perf_searchVolumeStatic(request): resource_id = '' if request.GET.has_key('resource_id'): resource_id = request.GET['resource_id'] if request.POST.has_key('resource_id'): resource_id = request.POST['resource_id'] counter_name = 'cpu_util' if request.GET.has_key('counter_name'): counter_name = request.GET['counter_name'] if request.POST.has_key('counter_name'): counter_name = request.POST['counter_name'] print(counter_name) tmpDate = DateTimeUtil.dayBefore(7) result = {} catagories = [] series = [] tmpMax = {} tmpMaxList = [] # tmpMin={} # tmpMinList=[] # tmpAvg={} # tmpAvgList=[] #find volumes by instanceid volumeList = volumes.objects.all().using('cinder').filter( instance_uuid=resource_id) print(volumeList) tmpVolumeIds = [] for vol in volumeList: tmpVolumeIds.append(vol.id) statList = MongoEngineUtil.findStaticsByTimestampAndCounter_nameAndResourceId( tmpDate, counter_name, tmpVolumeIds) for stat in statList: str = DateTimeUtil.datetime_toString(stat.timestamp, "%Y-%m-%d") catagories.append(str) tmpMaxList.append(stat.max) tmpMax['data'] = tmpMaxList tmpMax['type'] = 'line' tmpMax['name'] = 'max' series.append(tmpMax) result['categories'] = catagories result['series'] = series # print(staticsList) return HttpResponse(json.dumps(result))
def perf_searchVolumeStatic(request): resource_id='' if request.GET.has_key('resource_id'): resource_id=request.GET['resource_id']; if request.POST.has_key('resource_id'): resource_id=request.POST['resource_id']; counter_name='cpu_util' if request.GET.has_key('counter_name'): counter_name=request.GET['counter_name']; if request.POST.has_key('counter_name'): counter_name=request.POST['counter_name']; print(counter_name) tmpDate=DateTimeUtil.dayBefore(7); result={} catagories=[] series=[] tmpMax={} tmpMaxList=[] # tmpMin={} # tmpMinList=[] # tmpAvg={} # tmpAvgList=[] #find volumes by instanceid volumeList=volumes.objects.all().using('cinder').filter(instance_uuid=resource_id) print(volumeList) tmpVolumeIds=[] for vol in volumeList: tmpVolumeIds.append(vol.id) statList=MongoEngineUtil.findStaticsByTimestampAndCounter_nameAndResourceId(tmpDate,counter_name,tmpVolumeIds) for stat in statList: str=DateTimeUtil.datetime_toString(stat.timestamp,"%Y-%m-%d") catagories.append(str) tmpMaxList.append(stat.max) tmpMax['data']=tmpMaxList tmpMax['type']='line' tmpMax['name']='max' series.append(tmpMax) result['categories']=catagories result['series']=series # print(staticsList) return HttpResponse(json.dumps(result))
def perf_searchNetworkStatic(request): resource_id = '' if request.GET.has_key('resource_id'): resource_id = request.GET['resource_id'] if request.POST.has_key('resource_id'): resource_id = request.POST['resource_id'] counter_name = 'cpu_util' if request.GET.has_key('counter_name'): counter_name = request.GET['counter_name'] if request.POST.has_key('counter_name'): counter_name = request.POST['counter_name'] print(counter_name) tmpDate = DateTimeUtil.dayBefore(7) result = {} catagories = [] series = [] tmpMax = {} tmpMaxList = [] # tmpMin={} # tmpMinList=[] # tmpAvg={} # tmpAvgList=[] statList = MongoEngineUtil.findNetworkStaticsByTimestampAndCounter_nameAndResourceId( tmpDate, counter_name, resource_id) for stat in statList: str = DateTimeUtil.datetime_toString(stat.timestamp, "%Y-%m-%d") catagories.append(str) tmpMaxList.append(stat.max) tmpMax['data'] = tmpMaxList tmpMax['type'] = 'line' tmpMax['name'] = 'max' series.append(tmpMax) result['categories'] = catagories result['series'] = series # print(staticsList) return HttpResponse(json.dumps(result))
def perf_searchNetworkStatic(request): resource_id='' if request.GET.has_key('resource_id'): resource_id=request.GET['resource_id']; if request.POST.has_key('resource_id'): resource_id=request.POST['resource_id']; counter_name='cpu_util' if request.GET.has_key('counter_name'): counter_name=request.GET['counter_name']; if request.POST.has_key('counter_name'): counter_name=request.POST['counter_name']; print(counter_name) tmpDate=DateTimeUtil.dayBefore(7); result={} catagories=[] series=[] tmpMax={} tmpMaxList=[] # tmpMin={} # tmpMinList=[] # tmpAvg={} # tmpAvgList=[] statList=MongoEngineUtil.findNetworkStaticsByTimestampAndCounter_nameAndResourceId(tmpDate,counter_name,resource_id) for stat in statList: str=DateTimeUtil.datetime_toString(stat.timestamp,"%Y-%m-%d") catagories.append(str) tmpMaxList.append(stat.max) tmpMax['data']=tmpMaxList tmpMax['type']='line' tmpMax['name']='max' series.append(tmpMax) result['categories']=catagories result['series']=series # print(staticsList) return HttpResponse(json.dumps(result))
def perf_searchStaticByVm(request): resource_id = '' if request.GET.has_key('resource_id'): resource_id = request.GET['resource_id'] if request.POST.has_key('resource_id'): resource_id = request.POST['resource_id'] print(resource_id) tmpDate = DateTimeUtil.dayBefore(7) counter_name = 'cpu_util' if request.GET.has_key('counter_name'): counter_name = request.GET['counter_name'] if request.POST.has_key('counter_name'): counter_name = request.POST['counter_name'] print(counter_name) #staticsList=mongoUtil.findStaticsByTimestampAndCounter_name(timestamp,counter_name,0,10) # staticsList=mongoUtil.findLimitStaticsByTimestampAndCounter_name(timestamp,counter_name,1) # resource_id='7dcc77ae-6bc6-44fe-a24b-f6580f9fcce1' # res=MongoEngineUtil.findResourcesById('7dcc77ae-6bc6-44fe-a24b-f6580f9fcce1') # print(res) resList = [resource_id] staticsList = MongoEngineUtil.findStaticsByTimestampAndCounter_nameAndResourceId( tmpDate, counter_name, resList) result = {} catagories = [] series = [] tmpMax = {} tmpMaxList = [] tmpMin = {} tmpMinList = [] tmpAvg = {} tmpAvgList = [] for stat in staticsList: str = DateTimeUtil.datetime_toString(stat.timestamp, "%Y-%m-%d") catagories.append(str) tmpMaxList.append(stat.max) tmpMinList.append(stat.min) tmpAvgList.append(stat.avg) tmpMax['data'] = tmpMaxList tmpMax['type'] = 'line' tmpMax['name'] = 'max' tmpMin['data'] = tmpMinList tmpMin['type'] = 'line' tmpMin['name'] = 'min' tmpAvg['data'] = tmpAvgList tmpAvg['type'] = 'line' tmpAvg['name'] = 'avg' series.append(tmpMax) series.append(tmpMin) series.append(tmpAvg) # result['legends']=['ggg'] # result['categories']=[1,2,3] # result['series']=[55,2,45] # for stat in staticsList: result['categories'] = catagories result['series'] = series # print(staticsList) return HttpResponse(json.dumps(result))
# print(resList[0].id) modelList=Statistics86400.objects.filter(timestamp__gte=timestamp,counter_name=counter_name,resource_id__icontains=resource_id)\ .order_by('timestamp'); return modelList if __name__ == '__main__': mongoUtil=MongoEngineUtil() # mongoUtil.testSave(); # mongoUtil.testFind() # project_id='bd78fccc0a1b48a8bf09797a9c5031a9' # resList=mongoUtil.findResourcesByProject_id(project_id); # for res in resList: # print(res.resource_name) tmpDate=DateTimeUtil.dayBefore(7); str=DateTimeUtil.datetime_toString(tmpDate,"%Y-%m-%d") # str = '2016-01-11' beginTimestamp = datetime.datetime.strptime(str,'%Y-%m-%d') tmpDate=DateTimeUtil.dayBefore(1); str=DateTimeUtil.datetime_toString(tmpDate,"%Y-%m-%d") endTimestamp = datetime.datetime.strptime(str,'%Y-%m-%d') counter_name='cpu_util' # count=mongoUtil.findStaticsCountByTimestampAndCounter_name(timestamp,counter_name) # print(count) staticsList=mongoUtil.findLimitStaticsByTimestampAndCounter_name(beginTimestamp,endTimestamp,counter_name,50,'desc','avg') print(staticsList) for stat in staticsList: # print stat.avg print stat.max
def perf_searchStaticByVm(request): resource_id='' if request.GET.has_key('resource_id'): resource_id=request.GET['resource_id']; if request.POST.has_key('resource_id'): resource_id=request.POST['resource_id']; print(resource_id) tmpDate=DateTimeUtil.dayBefore(7); counter_name='cpu_util' if request.GET.has_key('counter_name'): counter_name=request.GET['counter_name']; if request.POST.has_key('counter_name'): counter_name=request.POST['counter_name']; print(counter_name) #staticsList=mongoUtil.findStaticsByTimestampAndCounter_name(timestamp,counter_name,0,10) # staticsList=mongoUtil.findLimitStaticsByTimestampAndCounter_name(timestamp,counter_name,1) # resource_id='7dcc77ae-6bc6-44fe-a24b-f6580f9fcce1' # res=MongoEngineUtil.findResourcesById('7dcc77ae-6bc6-44fe-a24b-f6580f9fcce1') # print(res) resList=[resource_id] staticsList=MongoEngineUtil.findStaticsByTimestampAndCounter_nameAndResourceId(tmpDate,counter_name,resList) result={} catagories=[] series=[] tmpMax={} tmpMaxList=[] tmpMin={} tmpMinList=[] tmpAvg={} tmpAvgList=[] for stat in staticsList: str=DateTimeUtil.datetime_toString(stat.timestamp,"%Y-%m-%d") catagories.append(str) tmpMaxList.append(stat.max) tmpMinList.append(stat.min) tmpAvgList.append(stat.avg) tmpMax['data']=tmpMaxList tmpMax['type']='line' tmpMax['name']='max' tmpMin['data']=tmpMinList tmpMin['type']='line' tmpMin['name']='min' tmpAvg['data']=tmpAvgList tmpAvg['type']='line' tmpAvg['name']='avg' series.append(tmpMax) series.append(tmpMin) series.append(tmpAvg) # result['legends']=['ggg'] # result['categories']=[1,2,3] # result['series']=[55,2,45] # for stat in staticsList: result['categories']=catagories result['series']=series # print(staticsList) return HttpResponse(json.dumps(result))
def findTopPerfStatics(request): tmpDate=DateTimeUtil.dayBefore(7); str=DateTimeUtil.datetime_toString(tmpDate,"%Y-%m-%d") bdt=str if request.GET.has_key('bdt'): bdt=request.GET['bdt']; if request.POST.has_key('bdt'): bdt=request.POST['bdt']; edt=str if request.GET.has_key('edt'): edt=request.GET['edt']; if request.POST.has_key('edt'): edt=request.POST['edt']; counter_name='cpu_util' if request.GET.has_key('counter_name'): counter_name=request.GET['counter_name']; if request.POST.has_key('counter_name'): counter_name=request.POST['counter_name']; order='aesc' if request.GET.has_key('order'): order=request.GET['order']; if request.POST.has_key('order'): order=request.POST['order']; sortby='avg' if request.GET.has_key('sortby'): sortby=request.GET['sortby']; if request.POST.has_key('sortby'): sortby=request.POST['sortby']; limit=10 if request.GET.has_key('limit'): limit=request.GET['limit']; if request.POST.has_key('limit'): limit=request.POST['limit']; print counter_name print order print sortby print limit # str = '2016-01-11' bdtime = datetime.datetime.strptime(bdt,'%Y-%m-%d') edtime = datetime.datetime.strptime(edt,'%Y-%m-%d') #get record count staticsList=MongoEngineUtil.findLimitStaticsByTimestampAndCounter_name(bdtime,edtime,counter_name,int(limit),order,sortby) perfList=[] tmpStat={} for stat in staticsList: #counter name is network.incoming.bytes.rate # instance-0000322e-3f6c30d6-e029-49c6-ab20-aa502fdbe557-tape7c7968e-bf uuid = stat.resource_id if counter_name.__contains__('network'): uuid=stat.resource_id[18:54] elif counter_name.__contains__('volume'): volList=volumes.objects.all().using('cinder').filter(id=stat.resource_id) if volList: uuid=volList[0].instance_uuid # print(uuid) res=MongoEngineUtil.findResourcesById(uuid) #get instance info insList=instances.objects.all().filter(uuid=uuid) if insList: tmpStat['vm_state']=insList[0].vm_state tmpStat['launched_at']=insList[0].launched_at tmpStat['scheduled_at']=insList[0].scheduled_at tmpStat['terminated_at']=insList[0].terminated_at tmpStat['vcpus']=insList[0].vcpus tmpStat['memory_mb']=insList[0].memory_mb tmpStat['host']=insList[0].host tmpStat['root_gb']=insList[0].root_gb image=images.objects.all().using('glance').filter(id=insList[0].image_ref) print(image) if image: tmpStat['image']=image[0].name else: tmpStat['image']='null' proList=project.objects.all().using('keystone').filter(id=insList[0].project_id) if proList: tmpStat['project_name']=proList[0].name tmpStat['resource_name']=res.resource_name tmpStat['counter_name']=stat.counter_name tmpStat['counter_name']=stat.counter_name tmpStat['resource_id']=stat.resource_id tmpStat['timestamp']=datetime.datetime.strftime(stat.timestamp,'%Y-%m-%d %H:%M:%S') tmpStat['avg']=stat.avg tmpStat['unit']=stat.unit tmpStat['count']=stat.count tmpStat['max']=stat.max tmpStat['user_id']=stat.user_id tmpStat['val']=stat.val tmpStat['min']=stat.min tmpStat['sum']=stat.sum tmpStat['period_start']=datetime.datetime.strftime(stat.period_start,'%Y-%m-%d %H:%M:%S') tmpStat['period_end']=datetime.datetime.strftime(stat.period_end,'%Y-%m-%d %H:%M:%S') perfList.append(tmpStat) tmpStat={} result={} result["total"]=result.__len__(); result["rows"]=perfList; # print(json.dumps(result)) return HttpResponse(json.dumps(result,cls = DateTimeEncoder))
modelList=Statistics86400.objects.filter(timestamp__gte=timestamp,counter_name=counter_name,resource_id__icontains=resource_id)\ .order_by('timestamp') return modelList if __name__ == '__main__': mongoUtil = MongoEngineUtil() # mongoUtil.testSave(); # mongoUtil.testFind() # project_id='bd78fccc0a1b48a8bf09797a9c5031a9' # resList=mongoUtil.findResourcesByProject_id(project_id); # for res in resList: # print(res.resource_name) tmpDate = DateTimeUtil.dayBefore(7) str = DateTimeUtil.datetime_toString(tmpDate, "%Y-%m-%d") # str = '2016-01-11' beginTimestamp = datetime.datetime.strptime(str, '%Y-%m-%d') tmpDate = DateTimeUtil.dayBefore(1) str = DateTimeUtil.datetime_toString(tmpDate, "%Y-%m-%d") endTimestamp = datetime.datetime.strptime(str, '%Y-%m-%d') counter_name = 'cpu_util' # count=mongoUtil.findStaticsCountByTimestampAndCounter_name(timestamp,counter_name) # print(count) staticsList = mongoUtil.findLimitStaticsByTimestampAndCounter_name( beginTimestamp, endTimestamp, counter_name, 50, 'desc', 'avg') print(staticsList) for stat in staticsList: # print stat.avg
def findTopPerfStatics(request): tmpDate = DateTimeUtil.dayBefore(7) str = DateTimeUtil.datetime_toString(tmpDate, "%Y-%m-%d") bdt = str if request.GET.has_key('bdt'): bdt = request.GET['bdt'] if request.POST.has_key('bdt'): bdt = request.POST['bdt'] edt = str if request.GET.has_key('edt'): edt = request.GET['edt'] if request.POST.has_key('edt'): edt = request.POST['edt'] counter_name = 'cpu_util' if request.GET.has_key('counter_name'): counter_name = request.GET['counter_name'] if request.POST.has_key('counter_name'): counter_name = request.POST['counter_name'] order = 'aesc' if request.GET.has_key('order'): order = request.GET['order'] if request.POST.has_key('order'): order = request.POST['order'] sortby = 'avg' if request.GET.has_key('sortby'): sortby = request.GET['sortby'] if request.POST.has_key('sortby'): sortby = request.POST['sortby'] limit = 10 if request.GET.has_key('limit'): limit = request.GET['limit'] if request.POST.has_key('limit'): limit = request.POST['limit'] print counter_name print order print sortby print limit # str = '2016-01-11' bdtime = datetime.datetime.strptime(bdt, '%Y-%m-%d') edtime = datetime.datetime.strptime(edt, '%Y-%m-%d') #get record count staticsList = MongoEngineUtil.findLimitStaticsByTimestampAndCounter_name( bdtime, edtime, counter_name, int(limit), order, sortby) perfList = [] tmpStat = {} for stat in staticsList: #counter name is network.incoming.bytes.rate # instance-0000322e-3f6c30d6-e029-49c6-ab20-aa502fdbe557-tape7c7968e-bf uuid = stat.resource_id if counter_name.__contains__('network'): uuid = stat.resource_id[18:54] elif counter_name.__contains__('volume'): volList = volumes.objects.all().using('cinder').filter( id=stat.resource_id) if volList: uuid = volList[0].instance_uuid # print(uuid) res = MongoEngineUtil.findResourcesById(uuid) #get instance info insList = instances.objects.all().filter(uuid=uuid) if insList: tmpStat['vm_state'] = insList[0].vm_state tmpStat['launched_at'] = insList[0].launched_at tmpStat['scheduled_at'] = insList[0].scheduled_at tmpStat['terminated_at'] = insList[0].terminated_at tmpStat['vcpus'] = insList[0].vcpus tmpStat['memory_mb'] = insList[0].memory_mb tmpStat['host'] = insList[0].host tmpStat['root_gb'] = insList[0].root_gb image = images.objects.all().using('glance').filter( id=insList[0].image_ref) print(image) if image: tmpStat['image'] = image[0].name else: tmpStat['image'] = 'null' proList = project.objects.all().using('keystone').filter( id=insList[0].project_id) if proList: tmpStat['project_name'] = proList[0].name tmpStat['resource_name'] = res.resource_name tmpStat['counter_name'] = stat.counter_name tmpStat['counter_name'] = stat.counter_name tmpStat['resource_id'] = stat.resource_id tmpStat['timestamp'] = datetime.datetime.strftime( stat.timestamp, '%Y-%m-%d %H:%M:%S') tmpStat['avg'] = stat.avg tmpStat['unit'] = stat.unit tmpStat['count'] = stat.count tmpStat['max'] = stat.max tmpStat['user_id'] = stat.user_id tmpStat['val'] = stat.val tmpStat['min'] = stat.min tmpStat['sum'] = stat.sum tmpStat['period_start'] = datetime.datetime.strftime( stat.period_start, '%Y-%m-%d %H:%M:%S') tmpStat['period_end'] = datetime.datetime.strftime( stat.period_end, '%Y-%m-%d %H:%M:%S') perfList.append(tmpStat) tmpStat = {} result = {} result["total"] = result.__len__() result["rows"] = perfList # print(json.dumps(result)) return HttpResponse(json.dumps(result, cls=DateTimeEncoder))