def sent_sms(to_address, from_sign, TemplateCode, TemplateParam): #发送短信接口 try: run_result = Alicloud_sms.SMS.sent_sms(to_address, from_sign, TemplateCode, TemplateParam) save_log( "sent_sms to_address:%s,from_sign:%s,TemplateCode:%s,TemplateParam:%s" % (to_address, from_sign, TemplateCode, TemplateParam)) except Exception as err: raise err else: return run_result
def reset_password(email_address, new_password): #重置Google邮箱账号密码 try: run_result = Gsuit_api_request.APIrequest.update_user_password( email_address, new_password) #邮箱重置密码 save_log("reset_google_password email_address:%s,new_password:%s" % (email_address, new_password)) except Exception as err: raise err else: return run_result
def suspend_user(email_address): #暂停google账户 try: run_result = Gsuit_api_request.APIrequest.suspend_user( email_address) run_result = Gsuit_api_request.APIrequest.update_user_group( email_address, '/ZZ-Resignation') #暂停账号,移动账号至离职人员群组 save_log("suspend_user email_address:%s" % (email_address)) except Exception as err: raise err else: return run_result
def add_user(email_address, mail_password, ad_password, family, given, ou, phone): #添加新员工账户,同时创建AD域账户和google账户 try: run_result = API_Add_User.main(email_address, mail_password, ad_password, family, given, ou, phone) #谷歌账号创建 save_log( "Add_User_New_employee email:%s,mail_password:%s,ad_password:%s,x:%s,m:%s,ou:%s,phone:%s" % (email_address, mail_password, ad_password, family, given, ou, phone)) except Exception as err: raise err else: return run_result
def sent_snmp_trap(product, level, regionId, instanceName, name): # Protocol version to use verID = api.protoVersion2c pMod = api.protoModules[verID] # Build PDU trapPDU = pMod.TrapPDU() pMod.apiTrapPDU.setDefaults(trapPDU) # Traps have quite different semantics among proto versions if verID == api.protoVersion2c: var = [] oid = (1, 3, 6, 1, 4, 1, 12149, 1) val = pMod.OctetString('product:%s' % product) var.append((oid, val)) oid = (1, 3, 6, 1, 4, 1, 12149, 2) val = pMod.OctetString('level:%s' % level) var.append((oid, val)) oid = (1, 3, 6, 1, 4, 1, 12149, 3) val = pMod.OctetString('regionId:%s' % regionId) var.append((oid, val)) oid = (1, 3, 6, 1, 4, 1, 12149, 4) val = pMod.OctetString('instanceName:%s' % instanceName) var.append((oid, val)) oid = (1, 3, 6, 1, 4, 1, 12149, 5) val = pMod.OctetString('name:%s' % name) var.append((oid, val)) pMod.apiTrapPDU.setVarBinds(trapPDU, var) print(var) save_log(str(var)) # Build message trapMsg = pMod.Message() pMod.apiMessage.setDefaults(trapMsg) pMod.apiMessage.setCommunity(trapMsg, 'public') pMod.apiMessage.setPDU(trapMsg, trapPDU) transportDispatcher = AsynsockDispatcher() transportDispatcher.registerTransport( udp.domainName, udp.UdpSocketTransport().openClientMode()) transportDispatcher.sendMessage(encoder.encode(trapMsg), udp.domainName, ('localhost', 162)) transportDispatcher.runDispatcher() transportDispatcher.closeDispatcher()
def wechatwork_robot(product, level, regionId, instanceName, name): now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') paramsList = ["云产品:", "告警级别:", "区域:", "告警节点:", "告警内容:"] valueList = [product, level, regionId, instanceName, name] content = "" headers = {"Content-Type": "text/plain"} title_color = "warning" for i in range(len(paramsList)): content = content + "<font color=\"comment\">" + paramsList[ i] + "</font>" + valueList[i] + "\n" data = { "msgtype": "markdown", "markdown": { "content": "**<font color=\"" + title_color + "\">【阿里云报警小机器人】</font>**\n**通知时间:" + now_time + "**\n" + content, } } r = requests.post(url=webhook_url, headers=headers, json=data) print(r.text) save_log('wechatwork_robot result:%s' % r.text) return r.text
def AliCloud_Alarm(data): try: data = json.loads(data) product = data['product'] level = data['level'] regionId = data['regionId'] instanceName = data['instanceName'] name = data['name'] save_log( 'product:%s ,level:%s ,regionId:%s ,instanceName:%s ,name:%s' % (product, level, regionId, instanceName, name)) run_result = wechatwork_robot(product, level, regionId, instanceName, name) sent_snmp_trap(product, level, regionId, instanceName, name) except Exception as err: save_log("run_result:%s" % err) return err else: save_log("run_result:%s" % run_result) return run_result
logs += str(results) print(logs) with open(os.path.join(save_folder, 'results.txt'), 'w') as txtfile: txtfile.write(logs) return results, trial_id_list, experiment_data if __name__=='__main__': args = get_args() save_path = os.path.join(os.environ["HOME"], "mountdir/nni/experiments/") save_path = os.path.join(save_path, args.id) save_folder = os.path.join(save_path, 'results') if not os.path.exists(save_folder): os.mkdir(save_folder) results, trial_id_list, experiment_data = main(args, save_folder) start_time = experiment_data[trial_id_list[0]][0][0][1] for index in range(len(trial_id_list)-1,-1,-1): if trial_id_list[index] in experiment_data: stop_time = experiment_data[trial_id_list[index]][-1][-1][1] break # gp = gen_report.GenPerfdata(start_time,stop_time) # gp.parse_mem() # gp.parse_cputil() # gp.parse_gpuutil() # gp.parse_gpumem() gen_report.GenReport(save_folder).output() print("Experiment report are saved in \"" + save_folder + '\"') os.system('rm ' + save_folder + '/*.png') if args.logs is True: save_log.save_log(save_folder, args.id) print("Finished.\n======================================================================\n")
def get_data(request): try: if request.META.get('HTTP_X_FORWARDED_FOR'): ip = request.META.get("HTTP_X_FORWARDED_FOR") else: ip = request.META.get("REMOTE_ADDR") except Exception as err: ip = 'unknow IP address' save_log("\n") save_log("Request from IP:%s" % ip) if request.method != "POST": # 定义post接口 save_log("Receive WRONG request, not POST method") return JsonResponse({"msg": "Receive WRONG request, not POST method"}) try: SECRET = request.POST.get("SECRET") API_POST_METHOD = request.POST.get("API_POST_METHOD") #sha256后的SECRET为:ae3e5f4098d40e38846f69bf83cf8f8c18a40fb27f9f7da2aa23f63241089a85 if SECRET == None: run_result = 'SECRET EMPTY' save_log("run_result:%s" % run_result) return JsonResponse({"msg": "%s" % run_result}) save_log("GET SECRET(sha256):%s" % hashlib.sha256(SECRET.encode()).hexdigest()) if (hashlib.sha256(SECRET.encode()).hexdigest( ) != 'ae3e5f4098d40e38846f69bf83cf8f8c18a40fb27f9f7da2aa23f63241089a85' ): run_result = 'Invalid SECRET' save_log("run_result:%s" % run_result) return JsonResponse({"msg": "%s" % run_result}) save_log("SECRET CHECK PERMIT") if API_POST_METHOD == None: run_result = 'API_POST_METHOD EMPTY' save_log("run_result:%s" % run_result) return JsonResponse({"msg": "%s" % run_result}) save_log("GET API_POST_METHOD:%s" % API_POST_METHOD) run_result = 'Invalid API_POST_METHOD code' if API_POST_METHOD == '201909230001': name = request.POST.get("name") run_result = Microsoft_AD.Add_Database__User_Group(name) if API_POST_METHOD == '201909270001': name = request.POST.get("name") run_result = Microsoft_AD.Add_Database__bussiness1_Group(name) if API_POST_METHOD == '201909270002': name = request.POST.get("name") run_result = Microsoft_AD.remove_Database__bussiness1_Group(name) if API_POST_METHOD == '201910280001': name = request.POST.get("name") password = request.POST.get("password") run_result = Microsoft_AD.reset_password(name, password) if API_POST_METHOD == '201910280002': name = request.POST.get("name") password = request.POST.get("password") run_result = Google_Gsuit.reset_password(name, password) if API_POST_METHOD == '201910300001': name = request.POST.get("name") run_result = Google_Gsuit.suspend_user(name) if API_POST_METHOD == '201911280001': name = request.POST.get("name") database_id = request.POST.get("database_id") if database_id == '1': database_group_name = 'database' if database_id == '0': database_group_name = 'database_back_8' run_result = Microsoft_AD.add_database_group( name, database_group_name) if API_POST_METHOD == '202007110001': email_address = request.POST.get("email_address") mail_password = request.POST.get("mail_password") ad_password = request.POST.get("ad_password") family = request.POST.get("family") given = request.POST.get("given") ou = request.POST.get("ou") phone = request.POST.get("phone") run_result = Add_User_New_employee.add_user( email_address, mail_password, ad_password, family, given, ou, phone) if API_POST_METHOD == '202007111001': to_address = request.POST.get("to_address") from_sign = request.POST.get("from_sign") TemplateCode = request.POST.get("TemplateCode") TemplateParam = request.POST.get("TemplateParam") run_result = Alicloud_API.sent_sms(to_address, from_sign, TemplateCode, TemplateParam) except Exception as err: save_log("run_result:%s" % err) return JsonResponse({"msg": "%s" % err}) else: save_log("run_result:%s" % run_result) return JsonResponse({"msg": run_result})
def get_data(request): try: if request.META.get('HTTP_X_FORWARDED_FOR'): ip = request.META.get("HTTP_X_FORWARDED_FOR") else: ip = request.META.get("REMOTE_ADDR") except Exception as err: ip = 'unknow IP address' save_log("\n") save_log("Request from IP:%s" % ip) Request_method = request.method save_log("Request method is: %s" % Request_method) try: data = str(request.body, 'utf-8').encode('utf-8').decode('unicode_escape') save_log("run_result:%s" % data) run_result = AliCloud_Alarm(data) except Exception as err: save_log("run_result:%s" % err) return JsonResponse({"msg": "%s" % err}) else: save_log("run_result:%s" % run_result) return JsonResponse({"msg": run_result})