def get_list(self, table, fields, where=None): if isinstance(where, dict) and where: conditions = [] for k, v in where.items(): if isinstance(v, list): conditions.append("%s IN (%s)" % (k, ','.join(v))) elif isinstance(v, str) or isinstance(v, unicode): conditions.append("%s='%s'" % (k, v)) elif isinstance(v, int): conditions.append("%s=%s" % (k, v)) sql = "select %s from %s where %s" % (",".join(fields), table, ' AND '.join(conditions)) elif not where: sql = "SELECT %s FROM %s" % (','.join(fields), table) try: util.WriteLog('db').info("sql: %s" % sql) self.execute(sql) result = self.cur.fetchall() if result: result = [ dict((k, row[i]) for i, k in enumerate(fields)) for row in result ] else: result = {} return result except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def get_list_where_and_not_where(self, table, fields, where, not_where): if isinstance(where, dict) and where: conditions = [] for k, v in where.items(): conditions.append("%s='%s'" % (k, v)) if isinstance(not_where, dict) and not_where: conditions_1 = [] for k, v in not_where.items(): conditions_1.append("%s != '%s'" % (k, v)) sql = "select %s from %s where %s AND %s" % ( ",".join(fields), table, ' AND '.join(conditions), ' AND '.join(conditions_1)) try: self.execute(sql) result = self.cur.fetchall() if result: result = [ dict((k, row[i]) for i, k in enumerate(fields)) for row in result ] else: result = {} return result except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def cabinetupdate(): cabinet_columns = [ 'id', 'name', 'idc_id', 'bandwidth', 'money', 'u_num', 'server_num', 'switch_num' ] if request.method == 'GET': id = request.args.get('id') where = "id=" + id data = db.get_one(cabinet_columns, where, 'cabinet') return json.dumps(data) else: data = request.form.to_dict() for k, v in data.items(): if v == '': return json.dumps({'code': 1, 'errmsg': '不能为空'}) where = "id=" + data['id'] columns = ['id', 'name'] idcs = db.get_list(columns, 'idc') idcs = dict((idc['id'], idc['name']) for idc in idcs) for k, v in idcs.items(): if data['idc_id'] == v: data['idc_id'] = k util.WriteLog('infoLogger').warning('%s update cabinet_id %s' % (session['username'], data['id'])) return json.dumps(db.update(data, where, 'cabinet'))
def send_ding_talk_warning(data): tmp_2 = achieve_warning_toolkit_value('offline_ding_talk') if tmp_2 == 'no': return 'no' for i in data: program = { "msgtype": "markdown", "markdown": { "title": "%s" % (i['title']), "text": "%s" % (i['text']) }, "at": { "isAtAll": False } } try: tmp_3 = random.choice(tmp_2) url = '%s%s' % (tmp_3['toolkit_url'], tmp_3['toolkit_key']) headers = {'Content-Type': 'application/json'} f = requests.post(url, data=json.dumps(program), headers=headers) except: util.WriteLog('send_ding_talk_warning').info( "Execute '%s' error: %s" % (i, 'False')) return 'ok'
def delete(self, table, where): if isinstance(where, dict) and where: conditions = [] for k, v in where.items(): conditions.append("%s='%s'" % (k, v)) sql = "delete from %s where %s" % (table, ' AND '.join(conditions)) try: sql = "select %s from %s" % (",".join(fields), table) util.WriteLog('db').info("sql: %s" % sql) self.execute(sql) return self.execute(sql) except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def cabinetdelete(): id = request.form.get('id') columns = ['id', 'name'] where = 'id=' + id cabinet = json.loads(db.get_one(columns, where, 'cabinet')) util.WriteLog('infoLogger').warning('%s delete cabinet %s' % (session['username'], cabinet['name'])) return json.dumps(db.delete(where, 'cabinet'))
def virtualadd(): if request.method == 'GET': return render_template('assets/virtuals/virtualadd.html', display=dis) else: data = request.form.to_dict() util.WriteLog('infoLogger').warning( '%s add virtual %s' % (session['username'], data['hostname'])) return json.dumps(db.create(data, 'virtuals'))
def idcdelete(): id = request.form.get('id') columns = ['id', 'name'] where = "id=" + id idc = json.loads(db.get_one(columns, where, 'idc')) util.WriteLog('infoLogger').warning('%s delete idc %s' % (session['username'], idc['name'])) return json.dumps(db.delete(where, 'idc'))
def check_dell_racadm_ip(client_ip, ip_addr): check_ip_fields = ["ip_addr"] check_ip_where = {'ip_addr': ip_addr} if DB().get_one('monitored_ipaddr', check_ip_fields, check_ip_where): return 'yes' else: util.WriteLog('ip_info').warn( "Not allow ip_addr: %s from client_ip: %s" % (ip_addr, client_ip)) return 'no'
def serverdelete(): id = request.form.get('id') columns = ['id', 'hostname'] where = 'id=' + id hostname = json.loads(db.get_one(columns, where, 'server')) util.WriteLog('infoLogger').warning( '%s delete server hostname %s' % (session['username'], hostname['hostname'])) return json.dumps(db.delete(where, 'server'))
def virtualdel(): id = request.form.get('id') columns = ['id', 'hostname'] where = "id=" + id hostname = json.loads(db.get_one(columns, where, 'virtuals')) util.WriteLog('infoLogger').warning( '%s delete virtual hostname %s' % (session['username'], hostname['hostname'])) return json.dumps(db.delete(where, 'virtuals'))
def update(self, table, fields): data = ",".join(["%s='%s'" % (k, v) for k, v in fields.items()]) sql = "update %s set %s where id=%s " % (table, data, fields["id"]) try: return self.execute(sql) except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def dell_racadm_achieve_ip_info(client_ip, ip_addr): ip_fields = [ "id", "ip_addr", "remote_card_id", "card_type_name", "card_user_name", "card_user_passwd" ] ip_where = {'ip_addr': ip_addr} ip_info = DB().get_one('monitored_ipaddr', ip_fields, ip_where) if ip_info: if ip_info['remote_card_id']: achieve_remote_card_info_fields = [ "card_type_name", 'card_user_name', 'card_user_passwd' ] achieve_remote_card_info_where = {'id': ip_info['remote_card_id']} achieve_remote_card_info = DB().get_one( 'remote_card_info', achieve_remote_card_info_fields, achieve_remote_card_info_where) if achieve_remote_card_info: if ip_info['card_type_name'] != '====': achieve_remote_card_info['card_type_name'] = ip_info[ 'card_type_name'] if ip_info['card_user_name'] != '====': achieve_remote_card_info['card_user_name'] = ip_info[ 'card_user_name'] if ip_info['card_user_passwd'] != '====': achieve_remote_card_info['card_user_passwd'] = ip_info[ 'card_user_passwd'] achieve_remote_card_info['id'] = ip_info['id'] return achieve_remote_card_info else: util.WriteLog('ip_info').warn( "ip_info achieve error.Table remote_card_info remote_card_id is none . ip_addr: %s from client_ip: %s remote_card_id: %s" % (ip_addr, client_ip, ip_info['remote_card_id'])) return 'no' else: util.WriteLog('ip_info').warn( "ip_info achieve error remote_card_id is none . ip_addr: %s from client_ip: %s" % (ip_addr, client_ip)) return 'no' else: util.WriteLog('ip_info').warn( "ip_info achieve error ip_addr: %s from client_ip: %s" % (ip_addr, client_ip)) return 'no'
def users_delete(): id = request.args.get('id') columns = ['id', 'name'] where = "id=" + id user = db.get_one(columns, where, 'users') if db.user_del(id): util.WriteLog('infoLogger').warning( '%s delete user %s' % (session['username'], user['name'])) return redirect('/users/') return 'del user failed!'
def get_list(self, table, fields): sql = "select %s from %s" % (",".join(fields), table) try: util.WriteLog('db').info("sql: %s" % sql) self.execute(sql) result = self.cur.fetchall() if result: result = [ dict((k, row[i]) for i, k in enumerate(fields)) for row in result ] else: result = {} return result except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def users_add(): user_data = request.form.to_dict() user_data['create_time'] = datetime.now() user_data['last_time'] = datetime.now() user_data['password'] = db.calc_md5(user_data['password']) user_data['repwd'] = db.calc_md5(user_data['repwd']) error = db.user_regedit_check(user_data) if error['status'] == 0: db.user_add(user_data) util.WriteLog('infoLogger').warning( '%s add user %s' % (session['username'], user_data['name'])) return json.dumps(error)
def login(): if request.method == 'POST': username = request.form.get('username') password = db.calc_md5(request.form.get('password')) if db.auth_user(username, password): session['username'] = username session['role'] = db.get_user_role(username) util.WriteLog('infoLogger').info('%s is login' % (session['username'])) return redirect('/user_center/') return render_template('login.html', errormsg='Account or passowrd error!') return render_template('login.html')
def check_dell_racadm_info(ip_addr, connect_user, connect_password, command_body): if command_body != 'no': result_version = os.popen( ("timeout 60 racadm --nocertwarn -r %s -u %s -p %s %s" % (ip_addr, connect_user, connect_password, command_body))).read() if result_version: return result_version else: util.WriteLog('racadm_connect').warn( "racadm connect error, maybe username or password is error . IP_addr:%s card_user_name:%s card_user_passwd:%s command body:%s" % (ip_addr, connect_user, connect_password, command_body)) return 'no'
def users_update(): if request.method == 'GET': id = request.args.get('id') user = db.get_user_by_id(id) user.pop('create_time') user.pop('last_time') return json.dumps(dict(user)) else: userinfo = request.form.to_dict() data = db.user_update(userinfo['name'], userinfo) util.WriteLog('infoLogger').info( '%s changed userinfo %s' % (session['username'], userinfo['name'])) return json.dumps(data)
def chpwdoneself(): userinfo = request.form.to_dict() data = db.change_pass(session['username'], userinfo['username'], userinfo['oldpasswd'], userinfo['newpasswd']) user_email = json.loads( db.get_one(['email'], 'name="%s"' % session['username'], 'users'))['email'] if data['code'] == 1: send_mail.delay([user_email], '个人密码修改失败', data['errmsg']) elif data['code'] == 0: send_mail.delay([user_email], '个人密码修改成功', data['errmsg']) util.WriteLog('infoLogger').info('%s changed his password' % (session['username'])) return json.dumps(data)
def create(self, table, data): fields, values = [], [] for k, v in data.items(): fields.append(k) values.append("'%s'" % v) sql = "insert into %s (%s) values (%s)" % (table, ",".join(fields), ",".join(values)) try: return self.execute(sql) except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def updateoneself(): userinfo = request.form.to_dict() user_data = dict({ 'name_cn': userinfo['name_cn'], 'email': userinfo['email'], 'mobile': userinfo['mobile'] }) data = db.user_update_oneself(userinfo['name'], user_data) if data['code'] == 0: send_mail.delay(userinfo['email'], '个人资料修改成功', '您的个人资料已修改') elif data['code'] == 1: send_mail.delay(userinfo['email'], '个人资料修改失败', '您的个人资料修改失败') util.WriteLog('infoLogger').info('%s changed his userinfo' % (session['username'])) return json.dumps(data)
def update_where(self, table, fields, where): if isinstance(where, dict) and where: conditions = [] for k, v in where.items(): conditions.append("%s='%s'" % (k, v)) data = " and ".join(["%s='%s'" % (k, v) for k, v in fields.items()]) sql = "update %s set %s where %s " % (table, data, ' AND '.join(conditions)) try: return self.execute(sql) except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def check_max_last_id(self, table, fields): sql = "select %s from %s where id=(select max(id) from %s)" % ( ",".join(fields), table, table) try: #util.WriteLog('db').info("sql: %s" % sql) self.execute(sql) result = self.cur.fetchone() if result: result = dict((k, result[i]) for i, k in enumerate(fields)) else: result = {} return result except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def cabinetadd(): if request.method == 'GET': idc_columns = [ 'id', 'name', 'address', 'adminer', 'phone', 'cabinet_num', 'switch_num' ] idcs = db.get_list(idc_columns, 'idc') idcinfo = [] for idc in idcs: idcinfo.append({'id': idc['id'], 'name': idc['name']}) return render_template('assets/cabinet/cabinetadd.html', idcinfo=idcinfo, display=dis) else: data = request.form.to_dict() util.WriteLog('infoLogger').warning( '%s add cabinet %s' % (session['username'], data['name'])) return json.dumps(db.create(data, 'cabinet'))
def idcupdate(): idc_columns = [ 'id', 'name', 'address', 'adminer', 'phone', 'cabinet_num', 'switch_num' ] if request.method == 'GET': id = request.args.get('id') where = "id=" + id data = db.get_one(idc_columns, where, 'idc') return json.dumps(data) else: data = request.form.to_dict() for k, v in data.items(): if v == '': return json.dumps({'code': 1, 'errmsg': '不能为空'}) where = "id=" + data['id'] util.WriteLog('infoLogger').warning('%s update idc_id %s' % (session['username'], data['id'])) return json.dumps(db.update(data, where, 'idc'))
def virtualupdate(): if request.method == 'GET': columns = [ 'id', 'qufu', 'platform', 'serverid', 'hostname', 'lan_ip', 'wan_ip', 'cpu', 'mem', 'disk', 'master_slave', 'server_id' ] id = request.args.get('id') where = 'id=' + id virtual = db.get_one(columns, where, 'virtuals') return json.dumps(virtual) else: data = request.form.to_dict() for k, v in data.items(): if v == '': return json.dumps({'code': 1, 'errmsg': '不能为空'}) where = "id=" + data['id'] util.WriteLog('infoLogger').warning('%s update virtual_id %s' % (session['username'], data['id'])) return json.dumps(db.update(data, where, 'virtuals'))
def serverupdate(): server_columns = [ 'id', 'hostname', 'lan_ip', 'wan_ip', 'cpu', 'mem', 'disk', 'virtual_nums', 'virtual_names', 'idc_id', 'cabinet_id' ] if request.method == 'GET': id = request.args.get('id') where = "id=" + id data = db.get_one(server_columns, where, 'server') return json.dumps(data) else: data = request.form.to_dict() for k, v in data.items(): if v == '': return json.dumps({'code': 1, 'errmsg': '不能为空'}) where = "id=" + data['id'] util.WriteLog('infoLogger').warning('%s update server_id %s' % (session['username'], data['id'])) return json.dumps(db.update(data, where, 'server'))
def check(self, table, fields, where): if isinstance(where, dict) and where: conditions = [] for k, v in where.items(): conditions.append("%s='%s'" % (k, v)) sql = "select %s from %s where %s" % (','.join(fields), table, ' AND '.join(conditions)) try: self.execute(sql) result = self.cur.fetchone() if result: result = dict((k, result[i]) for i, k in enumerate(fields)) else: result = "" return result except: util.WriteLog('db').info("Execute '%s' error: %s" % (sql, traceback.format_exc())) finally: self.close_db()
def chpwdadmin(): userinfo = request.form.to_dict() data = db.change_pass_admin(userinfo['username'], userinfo['newpasswd']) util.WriteLog('infoLogger').info( '%s changed %s passowrd' % (session['username'], userinfo['username'])) return json.dumps(data)