def post(self, request, *args, **kwargs): filters = [] if 'filters' in request.data.keys(): filters = request.data['filters'] metric = request.data['metric'] pop_id = self.kwargs['popID'] prom_url = getPromIP(pop_id) if prom_url['status'] == 'failed': return Response({'status': prom_url['msg']}, status=status.HTTP_404_NOT_FOUND) ip = socket.gethostbyname(prom_url['addr']) url = "http://" + ip + ":8002/new/?metric=" + metric + "¶ms=" + json.dumps( filters).replace(" ", "") # print url cl = Http() rsp = cl.GET(url, []) print rsp response = {} try: if 'name_space' in rsp: response['status'] = "SUCCESS" response['metric'] = request.data['metric'] response['ws_url'] = "ws://" + ip + ":8002/ws/" + str( rsp['name_space']) else: response['status'] = "FAIL" response['ws_url'] = None except KeyError: response = data pass return Response(response)
def post(self, request, *args, **kwargs): filters = [] psw = socket.gethostbyname('pushgateway') if 'filters' in request.data.keys(): filters = request.data['filters'] metric = request.data['metric'] url = "http://" + psw + ":8002/new/?metric=" + metric + "¶ms=" + json.dumps( filters).replace(" ", "") print url cl = Http() rsp = cl.GET(url, []) print url response = {} try: if 'name_space' in rsp: response['status'] = "SUCCESS" response['metric'] = request.data['metric'] response['ws_url'] = "ws://" + psw + ":8002/ws/" + str( rsp['name_space']) else: response['status'] = "FAIL" response['ws_url'] = None except KeyError: response = data pass return Response(response)
def get(self, request, *args, **kwargs): pop_id = self.kwargs['popID'] prom_url = getPromIP(pop_id) if prom_url['status'] == 'failed': return Response({'status': prom_url['msg']}, status=status.HTTP_404_NOT_FOUND) url = 'http://' + prom_url['addr'] + ':9089/prometheus/configuration' cl = Http() rsp = cl.GET(url, []) print rsp return Response({'config': rsp}, status=status.HTTP_200_OK)
def delete(self, request, *args, **kwargs): queryset = monitoring_rules.objects.all() srvid = self.kwargs['sonata_srv_id'] fq = queryset.filter(service__sonata_srv_id=srvid) print fq print fq.count() if fq.count() > 0: fq.delete() cl = Http() rsp = cl.DELETE( 'http://prometheus:9089/prometheus/rules/' + str(srvid), []) print rsp return Response({'staus': "service's rules removed"}, status=status.HTTP_204_NO_CONTENT) else: return Response({'status': "rules not found"}, status=status.HTTP_404_NOT_FOUND)
def delete(self, request, *args, **kwargs): self.lookup_field = 'sonata_srv_id' queryset = monitoring_services.objects.all() srvid = self.kwargs['sonata_srv_id'] queryset = queryset.filter(sonata_srv_id=srvid) print queryset.count() if queryset.count() > 0: print 'has to be deleted' queryset.delete() cl = Http() rsp = cl.DELETE( 'http://prometheus:9089/prometheus/rules/' + str(srvid), []) print rsp print 'Service ' + srvid + ' removed' return Response({'staus': "service removed"}, status=status.HTTP_204_NO_CONTENT) else: print 'Service ' + srvid + ' not found' return Response({'status': "service not found"}, status=status.HTTP_404_NOT_FOUND)
def post(self, request, *args, **kwargs): srvid = self.kwargs['srvID'] if 'rules' in request.data.keys(): rules = request.data['rules'] else: return Response({'error': 'Undefined rules'}, status=status.HTTP_400_BAD_REQUEST) # Check if service exists srv = monitoring_services.objects.all().filter(sonata_srv_id=srvid) if srv.count() == 0: if srvid != 'generic': return Response({'error': 'Requested Service not found'}, status=status.HTTP_404_NOT_FOUND) else: srvid = 'alerts' # Delete old rule from DB rules_db = monitoring_rules.objects.all().filter( service__sonata_srv_id=srvid) rules_db.delete() # Create prometheus configuration file rls = {} rls['service'] = srvid rls['vnf'] = "To be found..." rls['rules'] = [] rules_status = len(rules) for r in rules: # print json.dumps(r) nt = monitoring_notif_types.objects.all().filter( id=r['notification_type']) if nt.count() == 0: return Response( { 'error': 'Alert notification type does not supported. Action Aborted' }, status=status.HTTP_400_BAD_REQUEST) else: if srvid != "alerts": rule = monitoring_rules(service=srv[0], summary=r['summary'], notification_type=nt[0], name=r['name'], condition=r['condition'], duration=r['duration'], description=r['description']) rule.save() rl = {} rl['name'] = r['name'] rl['description'] = r['description'] rl['summary'] = r['summary'] rl['duration'] = r['duration'] rl['notification_type'] = r['notification_type'] rl['condition'] = r['condition'] rl['labels'] = ["serviceID=\"" + rls['service'] + "\""] rls['rules'].append(rl) if len(rules) > 0: cl = Http() rsp = cl.POST('http://prometheus:9089/prometheus/rules', [], json.dumps(rls)) if rsp == 200: return Response({'status': "success", "rules": rules_status}) else: return Response({'error': 'Rule update failed ' + str(rsp)}) else: return Response({'error': 'No rules defined'})
def post(self, request, *args, **kwargs): if not 'service' in request.data: print 'Received new Service notification: Undefined Service' return Response({'error': 'Undefined Service'}, status=status.HTTP_400_BAD_REQUEST) if not 'functions' in request.data: print 'Received new Service notification: Undefined Functions' return Response({'error': 'Undefined Functions'}, status=status.HTTP_400_BAD_REQUEST) if not 'cloud_services' in request.data: print 'Received new Service notification: Undefined Cloud Services' return Response({'error': 'Undefined Cloud Services'}, status=status.HTTP_400_BAD_REQUEST) if not 'rules' in request.data: print 'Received new Service notification: Undefined Rules' return Response({'error': 'Undefined Rules'}, status=status.HTTP_400_BAD_REQUEST) print 'Received new Service notification: ' + json.dumps(request.data) service = request.data['service'] functions = request.data['functions'] cloud_services = request.data['cloud_services'] rules = request.data['rules'] functions_status = 'NULL' cloud_services_status = 'NULL' metrics_status = 'NULL' rules_status = 'NULL' usr = None if 'sonata_usr' in service: customer = {} customer['email'] = None customer['phone'] = None if 'email' in service['sonata_usr']: customer['email'] = service['sonata_usr']['email'] if 'phone' in service['sonata_usr']: customer['phone'] = service['sonata_usr']['phone'] u = monitoring_users.objects.all().filter( Q(email=customer['email']) & Q(mobile=customer['phone']) & Q(type='cst')) if len(u) == 0: usr = monitoring_users(mobile=customer['phone'], email=customer['email'], type='cst') usr.save() else: usr = u[0] dev = None if 'sonata_dev' in service: developer = {} developer['email'] = None developer['phone'] = None if 'email' in service['sonata_dev']: developer['email'] = service['sonata_dev']['email'] if 'phone' in service['sonata_dev']: developer['phone'] = service['sonata_dev']['phone'] u = monitoring_users.objects.all().filter( Q(email=developer['email']) & Q(mobile=developer['phone']) & Q(type='dev')) if len(u) == 0: dev = monitoring_users(mobile=developer['phone'], email=developer['email'], type='dev') dev.save() else: dev = u[0] s = monitoring_services.objects.all().filter( sonata_srv_id=service['sonata_srv_id']) if s.count() > 0: s.delete() srv_pop_id = '' srv_host_id = '' if service['pop_id']: srv_pop_id = service['pop_id'] pop = monitoring_pops.objects.all().filter( sonata_pop_id=srv_pop_id) if pop.count() == 0: pop = monitoring_pops(sonata_pop_id=srv_pop_id, sonata_sp_id="undefined", name="undefined", prom_url="undefined") # karpa pop.save() if service['host_id']: srv_host_id = service['host_id'] srv = monitoring_services(sonata_srv_id=service['sonata_srv_id'], name=service['name'], description=service['description'], host_id=srv_host_id, pop_id=srv_pop_id) srv.save() if isinstance(usr, monitoring_users): srv.user.add(usr) if isinstance(dev, monitoring_users): srv.user.add(dev) srv.save() for f in functions: fnc_pop_id = f['pop_id'] pop = monitoring_pops.objects.all().filter( sonata_pop_id=fnc_pop_id) if pop.count() == 0: pop = monitoring_pops(sonata_pop_id=fnc_pop_id, sonata_sp_id="undefined", name="undefined", prom_url="undefined") pop.save() functions_status = len(functions) func = monitoring_functions(service=srv, host_id=f['host_id'], name=f['name'], sonata_func_id=f['sonata_func_id'], description=f['description'], pop_id=f['pop_id']) func.save() for m in f['metrics']: metrics_status = len(f['metrics']) metric = monitoring_metrics(function=func, name=m['name'], cmd=m['cmd'], threshold=m['threshold'], interval=m['interval'], description=m['description']) metric.save() for cs in cloud_services: cs_pop_id = cs['pop_id'] pop = monitoring_pops.objects.all().filter(sonata_pop_id=cs_pop_id) if pop.count() == 0: pop = monitoring_pops(sonata_pop_id=cs_pop_id, sonata_sp_id="undefined", name="undefined", prom_url="undefined") pop.save() cloud_services_status = len(cloud_services) css = monitoring_cloud_services( service=srv, csd_name=cs['csd_name'], vdu_id=cs['vdu_id'], cloud_service_record_uuid=cs['cloud_service_record_uuid'], description=cs['description'], pop_id=cs['pop_id']) css.save() for m in cs['metrics']: metrics_status = len(cs['metrics']) metric = monitoring_metrics(cloud_service=cs, name=m['name'], cmd=m['cmd'], threshold=m['threshold'], interval=m['interval'], description=m['description']) metric.save() rls = {} rls['service'] = service['sonata_srv_id'] rls['vnf'] = "To be found..." rls['rules'] = [] for r in rules: # print json.dumps(r) nt = monitoring_notif_types.objects.all().filter( id=r['notification_type']) if nt.count() == 0: return Response( { 'error': 'Alert notification type does not supported. Action Aborted' }, status=status.HTTP_400_BAD_REQUEST) srv.delete() else: rules_status = len(rules) rule = monitoring_rules(service=srv, summary=r['summary'], notification_type=nt[0], name=r['name'], condition=r['condition'], duration=r['duration'], description=r['description']) rule.save() rl = {} rl['name'] = r['name'] rl['description'] = r['description'] rl['summary'] = r['summary'] rl['duration'] = r['duration'] rl['notification_type'] = r['notification_type'] rl['condition'] = r['condition'] rl['labels'] = ["serviceID=\"" + rls['service'] + "\""] rls['rules'].append(rl) if len(rules) > 0: cl = Http() rsp = cl.POST('http://prometheus:9089/prometheus/rules', [], json.dumps(rls)) if rsp == 200: return Response({ 'status': "success", "vnfs": functions_status, "css": cloud_services_status, "metrics": metrics_status, "rules": rules_status }) else: srv.delete() return Response({'error': 'Service update fail ' + str(rsp)}) else: return Response({ 'status': "success", "vnfs": functions_status, "css": cloud_services_status, "metrics": metrics_status, "rules": rules_status })
def postCfgfile(self, confFile): url = 'http://prometheus:9089/prometheus/configuration' cl = Http() rsp = cl.POST(url, [], json.dumps(confFile)) return rsp
def getCfgfile(self): url = 'http://prometheus:9089/prometheus/configuration' cl = Http() rsp = cl.GET(url, []) return rsp
def get(self, request, *args, **kwargs): url = 'http://prometheus:9089/prometheus/configuration' cl = Http() rsp = cl.GET(url, []) print rsp return Response({'config': rsp}, status=status.HTTP_200_OK)
def post(self, request, *args, **kwargs): if not 'service' in request.data: return Response({'error': 'Undefined Service'}, status=status.HTTP_400_BAD_REQUEST) if not 'functions' in request.data: return Response({'error': 'Undefined Functions'}, status=status.HTTP_400_BAD_REQUEST) if not 'rules' in request.data: return Response({'error': 'Undefined Rules'}, status=status.HTTP_400_BAD_REQUEST) service = request.data['service'] functions = request.data['functions'] rules = request.data['rules'] functions_status = 'NULL' metrics_status = 'NULL' rules_status = 'NULL' if not service['sonata_usr_id']: u = monitoring_users.objects.all().filter(sonata_userid='system') else: u = monitoring_users.objects.all().filter( sonata_userid=service['sonata_usr_id']) if u.count() == 0: #add new user usr = monitoring_users(sonata_userid=service['sonata_usr_id']) usr.save() else: usr = u[0] s = monitoring_services.objects.all().filter( sonata_srv_id=service['sonata_srv_id']) if s.count() > 0: s.delete() srv_pop_id = '' srv_host_id = '' if service['pop_id']: srv_pop_id = service['pop_id'] if service['host_id']: srv_host_id = service['host_id'] srv = monitoring_services(sonata_srv_id=service['sonata_srv_id'], name=service['name'], description=service['description'], host_id=srv_host_id, user=usr, pop_id=srv_pop_id) srv.save() for f in functions: functions_status = len(functions) print f['pop_id'] func = monitoring_functions(service=srv, host_id=f['host_id'], name=f['name'], sonata_func_id=f['sonata_func_id'], description=f['description'], pop_id=f['pop_id']) func.save() for m in f['metrics']: metrics_status = len(f['metrics']) metric = monitoring_metrics(function=func, name=m['name'], cmd=m['cmd'], threshold=m['threshold'], interval=m['interval'], description=m['description']) metric.save() rls = {} rls['service'] = service['sonata_srv_id'] rls['vnf'] = "To be found..." rls['rules'] = [] for r in rules: #print json.dumps(r) nt = monitoring_notif_types.objects.all().filter( id=r['notification_type']) if nt.count() == 0: return Response( { 'error': 'Alert notification type does not supported. Action Aborted' }, status=status.HTTP_400_BAD_REQUEST) srv.delete() else: rules_status = len(rules) rule = monitoring_rules(service=srv, summary=r['summary'], notification_type=nt[0], name=r['name'], condition=r['condition'], duration=r['duration'], description=r['description']) rule.save() rl = {} rl['name'] = r['name'] rl['description'] = r['description'] rl['summary'] = r['summary'] rl['duration'] = r['duration'] rl['notification_type'] = r['notification_type'] rl['condition'] = r['condition'] rl['labels'] = ["serviceID=\"" + rls['service'] + "\""] rls['rules'].append(rl) if len(rules) > 0: cl = Http() rsp = cl.POST('http://prometheus:9089/prometheus/rules', [], json.dumps(rls)) if rsp == 200: return Response({ 'status': "success", "vnfs": functions_status, "metrics": metrics_status, "rules": rules_status }) else: srv.delete() return Response({'error': 'Service update fail ' + str(rsp)}) else: return Response({ 'status': "success", "vnfs": functions_status, "metrics": metrics_status, "rules": rules_status })