def create_new_survey(self, args, person, ip): event_type = "CREATE_NEW_SURVEY" f_name, l_name = get_username(person) survey_id = get_uuid() img_filename = uploaded_file_security(args["SURVEY_PIC_FILE"], "survey_pic", survey_id) page_type = "SubProperty" location = "/do-survey" role = "All" parent_page = "Survey" if not isinstance(img_filename, str): return img_filename if get_surveys_table(where="NAME='" + args["SURVEY_NAME"] + "'", count=True) > 0: return response_create(json.dumps({"STATUS": "error", "ERROR": "Survey name is already exists."})) if "All" in args["PROJECT"]: args["PROJECT"] = ["All"] self.write_mysql("INSERT INTO surveys VALUES ('{0}','{1}','{2}','{3}','{4}')".format( survey_id, args["SURVEY_NAME"], args["SURVEY_TEXT"], args["SURVEY_EXP"], ",".join([self.get_project_name(i) for i in args["PROJECT"]]) )) self.write_mysql("INSERT INTO pages VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','Enabled')".format( args["SURVEY_NAME"], location, role, img_filename, page_type, parent_page, survey_id, ",".join([self.get_project_name(i) for i in args["PROJECT"]]) )) log = "New survey added by \"{0} {1}\".Name: {2}, ID: {3}.".format(f_name, l_name, args["SURVEY_NAME"], survey_id) write_log_to_mysql(event_type, ip, "INFO", log, self.system_username) self.mysql_commit() return response_create(json.dumps({"STATUS": "OK", "MESSAGE": "New survey added."}))
def get_cdn_resource_data(): domains = get_all_domains_in_cdn() ret = [] for each in domains: domain_id = each['domain_id'] header = {} header['hour'] = get_last_hour() header['id'] = domain_id header['user_id'] = "user_id" header['account_id'] = "user_id" header['project_id'] = each['tenant_id'] header['uuid'] = "uuid" header['region_id'] = get_region() header['parent_id'] = None ret = [] tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() amount = get_onedomain_lastmonth_cdnflows(domain_id) tmp['cdnflow_1_G'] = amount tmp['resource_name'] = "cdnflow" tmp['resource_type'] = "cdnflow" tmp['sum'] = amount tmp['billing_item'] = "cdnflow_1_G" tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() amount = get_onedomain_lastday_bandwidth(domain_id) tmp['cdnbandwidth_1_M'] = amount tmp['resource_name'] = "cdnbandwidth" tmp['resource_type'] = "cdnbandwidth" tmp['sum'] = amount tmp['billing_item'] = "cdnbandwidth_1_M" tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) return ret
def add_new_project(self, args, person, ip): event_type = "ADD_NEW_PROJECT" f_name, l_name = get_username(person) if get_projects_table(where="NAME='" + args["PROJECT_IDENTIFIER"] + "'", count=True) > 0: return response_create(json.dumps({"STATUS": "error", "ERROR": "Project already exists."})) project_id = get_uuid() self.write_mysql("INSERT INTO projects VALUES ('{0}','{1}','{2}','Passive')".format(project_id, args["PROJECT_IDENTIFIER"], args["PROJECT_EXP"])) log = "New project added by \"{0} {1}\".Project ID: {2}, Project Name: {3}.".format(f_name, l_name, project_id, args["PROJECT_IDENTIFIER"]) write_log_to_mysql(event_type, ip, "INFO", log, self.system_username) self.mysql_commit() return response_create(json.dumps({"STATUS": "OK", "MESSAGE": "New project added."}))
def chip0_scan(self, active=0, filter_uuid=None): # step1:chip 1 start passive scan,then start chip0 scan. with closing(self.sdk.scan(chip=0, active=active, filter_uuid=filter_uuid)) as self.sse1: count = 0 for message in self.sse1: if message.startswith('data'): msg = json.loads(message[5:]) if filter_uuid: if active ==0: if "adData" in msg: uuid=tools.get_uuid(msg['adData']) elif "scanData" in msg: uuid=tools.get_uuid(msg["scanData"]) else: uuid=tools.get_uuid(msg['adData']) # 进入开启过滤的扫描结果判断流程 if count < self.filter_count: print('chip0 ', count, message) # for self.key in ['adData','scanData']: # if self.key == 'adData' # uuid=tools.get_uuid(msg[self.key]) if uuid == filter_uuid: self.fail("filter uuid failed") self.logger.debug("filter uuid failed") break else: count += 1 else: self.flag1+=1 self.logger.debug('Step 1:chip0 start scan with filter uuid success.') break else: # 进入不开启过滤的扫描结果判断流程 if count < self.unfilter_count: print('chip0', count, message) count += 1 else: self.flag1 += 1 self.logger.debug('Step 1:chip0 start scan with no filter uuid success.') break
def chip1_scan(self, active=0, filter_uuid=None): # step2:start chip0 scan. with closing(self.sdk.scan(chip=1, active=active, filter_uuid=filter_uuid)) as self.sse2: count = 0 tmp_name = [] for message in self.sse2: if message.startswith('data'): msg = json.loads(message[5:]) if filter_uuid: if active == 0: if "adData" in msg: uuid = tools.get_uuid(msg['adData']) elif "scanData" in msg: uuid = tools.get_uuid(msg["scanData"]) else: uuid = tools.get_uuid(msg['adData']) # 进入开启过滤的扫描结果判断流程 if count < self.filter_count: print('chip1', count, message) uuid = tools.get_uuid(msg['adData']) if uuid == filter_uuid: self.fail("filter uuid failed") self.logger.debug("filter uuid failed") break else: count += 1 else: self.flag2 += 1 self.logger.debug('Step 1:chip1 start scan with filter uuid success.') break else: # 进入不开启过滤的扫描结果判断流程 if count < self.unfilter_count: print('chip1', count, message) count += 1 else: self.flag2 += 1 self.logger.debug('Step 1:chip1 start scan with no filter uuid success.') break
def register(self, args, ip): event_type = "REGISTER" if get_users_table(where="IP='" + ip + "'", count=True) > 0: return response_create( json.dumps({ "STATUS": "error", "ERROR": "Your IP address not permitted." })) if args["PASSWORD"] != args["RE-PASSWORD"]: return response_create( json.dumps({ "STATUS": "error", "ERROR": "Your passwords does not match." })) try: uid = get_uuid() self.write_mysql( "INSERT INTO users(ID,F_NAME,L_NAME,EMAIL,MAJORITY,COUNTRY,PASSWORD,CITY,HOSPITAL,IP) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')" .format(uid, args["FIRSTNAME"], args["LASTNAME"], args["EMAIL"], args["MAJORITY"], args["COUNTRY"], calculate_hash(args["PASSWORD"], "sha256"), args["CITY"], args["HOSPITAL"], ip)) self.write_mysql( "INSERT INTO user_profile(ID) VALUES ('{0}')".format(uid)) log = "New user created.Name: {0}, Surname: {1}, Majority: {2}, Country: {3}, UserID: {4}.".format( args["FIRSTNAME"], args["LASTNAME"], args["MAJORITY"], args["COUNTRY"], uid) write_log_to_mysql(event_type, ip, "INFO", log, self.system_username) self.mysql_commit() return response_create(json.dumps({"STATUS": "OK", "target": "/"})) except mdb.IntegrityError: self.mysql_rollback() return response_create( json.dumps({ "STATUS": "error", "ERROR": "Your account already created.If you forget your password, contact us." })) except Exception as e: self.mysql_rollback() return response_create( json.dumps({ "STATUS": "error", "ERROR": "Query could not be completed.Error: {0}".format(e) }))
def get_resource_data(session): print "info: begin to collector data " last_hour = get_last_hour instances = get_instance_pconn(session) ret = [] for each in instances: header = {} user_id = each['user_id'] project_id = each['project_id'] uuid = each['uuid'] header['hour'] = get_last_hour() header['id'] = each['id'] #instance's id header['user_id'] = user_id header['account_id'] = user_id header['project_id'] = project_id header['uuid'] = uuid #instance's uuid header['region_id'] = get_region() header['parent_id'] = None #print "UUID=%s, user_id=%s, project_id=%s, id=%s"%(uuid, user_id,project_id, id); #print "Cpu_core=%d"%get_cpu_count(uuid); #print "Memory=%d"%get_memory_count(uuid); tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() sum = get_cpu_count_pconn(session, uuid) tmp['billing_item'] = "cpu_1_core" tmp['resource_name'] = "cpu" tmp['resource_type'] = "cpu" tmp['cpu_1_core'] = sum tmp['sum'] = sum tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() tmp['billing_item'] = "instance_1" tmp['resource_name'] = "instance" tmp['resource_type'] = "instance" tmp['instance_1'] = 1 tmp['sum'] = 1 tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() sum = get_memory_count_pconn(session, uuid) tmp['billing_item'] = "memory_1024_M" tmp['resource_name'] = "memory" tmp['resource_type'] = "memory" tmp['memory_1024_M'] = int(sum / 1024) tmp['sum'] = int(sum / 1024) tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) #print "Disk: "; tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() (sizes, size_amount) = get_disks_pconn(session, user_id, project_id) #for each in sizes: # print each; tmp['billing_item'] = "disk_1_G" tmp['resource_name'] = "disk" tmp['resource_type'] = "disk" tmp['disk_1_G'] = size_amount tmp['sum'] = size_amount tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() (snapshots, snapshot_size_amount) = get_snapshotdisk_pconn(session, project_id) #for each2 in snapshots: # print "\t SNAPSHOT: %s"%each2; tmp['billing_item'] = "snapshotdisk_1_G" tmp['resource_name'] = "snapshotdisk" tmp['resource_type'] = "snapshotdisk" tmp['snapshotdisk_1_G'] = snapshot_size_amount tmp['sum'] = snapshot_size_amount tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() #print "Router count=%d"%len(get_user_routers(project_id)); sum = len(get_user_routers_pconn(session, project_id)) tmp['billing_item'] = "router_1" tmp['resource_name'] = "router" tmp['resource_type'] = "router" tmp['router_1'] = sum tmp['sum'] = sum tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() #print "Router bandwidth%d"%0; sum = 0 tmp['billing_item'] = "bandwidth_1_M" tmp['resource_name'] = "bandwidth" tmp['resource_type'] = "bandwidth" tmp['sum'] = sum tmp['bandwidth_1_M'] = sum tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() #print "Floating IP count=%d"%len(get_ip_count(project_id)); sum = len(get_ip_count_pconn(session, project_id)) tmp['ip_1'] = sum tmp['resource_name'] = "ip" tmp['resource_type'] = "ip" tmp['billing_item'] = "ip_1" tmp['sum'] = sum tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() sum = len(get_vpn_pconn(session, project_id)) tmp['vpn_1'] = sum tmp['resource_name'] = "vpn" tmp['resource_type'] = "vpn" tmp['billing_item'] = "vpn_1" tmp['sum'] = sum tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) #vpn=get_vpn(project_id); #if(len(vpn)==0): #print "VPN: \t%s have no vpn settings;"%project_id; # tmp=0; #else: # for each2 in vpn: # print "VPN: \t%s"%each2; #print "CDN: %d"%0; tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() sum = 2 tmp['cdnflow_1_G'] = sum tmp['resource_name'] = "cdnflow" tmp['resource_type'] = "cdnflow" tmp['sum'] = sum tmp['billing_item'] = "cdnflow_1_G" tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() sum = 2048 tmp['cdnbandwidth_1_M'] = (sum / 1024) tmp['resource_name'] = "cdnbandwidth" tmp['resource_type'] = "cdnbandwidth" tmp['sum'] = (sum / 1024) tmp['billing_item'] = "cdnbandwidth_1_M" tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) tmp = {} header['mq_uuid'] = get_uuid() header['resource_id'] = get_uuid() sum = 1 tmp['image_1'] = sum tmp['resource_name'] = "image" tmp['resource_type'] = "image" tmp['sum'] = sum tmp['billing_item'] = "image_1" tmp['status'] = "sending" resource = dict(header, **tmp) ret.append(resource) #print resource; #ret.append(resource); return ret
if __name__=="__main__": import sys; sys.path.append("../"); from tools import get_uuid, get_region; from get_last_hour import get_last_hour; from account_tools import get_all_user; from random import choice; #users= get_all_user(); users=['aa','bb','cc']; resource={}; resource['hour']=get_last_hour(); resource['id'] ="11100000000000000000000000000001"; resource['mq_uuid']=get_uuid(); resource['region_id']=get_region(); resource['user_id']=choice(users); resource['project_id']="project_id"; resource['uuid'] ="instance000000000000000000000001"; resource['cpu_1_core']=16; resource['instance_1']=1; resource['memory_1024_M']=(4096/1024); #FIXME /1024, 这里是硬代码, resource['disk_1_G']= 100; resource['snapshotdisk_1_G']=100; resource['router_1']=3; resource['bandwidth_1_M']=0; resource['ip_1']=4; resource['vpn_1']=4; resource['cdnflow_1_G']=2; resource['cdnbandwidth_1_M']=(2048/1024); #FIXME /1024, 这里也是硬代码,