def main(): global f_client,g_bquit init() f_client = Fdfs_client('/etc/fdfs/client.conf') signal.signal(signal.SIGTERM,onsignal_term) signal.signal(signal.SIGINT,onsignal_term) logger.info( "begin......" ) del_sql = "delete from PIC_TEMP where id=%d" while 1: if g_bquit: print "i'm quit" break sql = "select ID,PICURL from PIC_TEMP limit 1000" # logger.debug( "sql:%s"%(sql) ) rows = dbutil.select_sql(sql) if rows is None or len(rows)<=0: logger.info("table is empty") continue for row in rows: if g_bquit: print "i'm quit" break # print row[1] pic_dict = json.loads(row[1]) # print pic_dict del_rankingpic( pic_dict ) del_s = del_sql%(row[0]) print del_s dbutil.execute_sql(del_s) time.sleep( sleep_time ) logger.info( "end!!!!!!" )
def add_asset(obj): _args = [] for column in COLUMNS_ADD_ASSET: _args.append(obj.get(column, '')) _sql = 'insert into asset(%s) VALUES(%s);' % (','.join(COLUMNS_ADD_ASSET), ','.join(['%s'] * len(COLUMNS_ADD_ASSET))) _cnt, _ = execute_sql(_sql, _args) return _cnt > 0
def main(): init() r = redis.StrictRedis(host=redis_addr, port=redis_port, db=redis_num) if r is None: logger.error("redis connect failed") return result = None while True: try: result = r.brpop(redis_key) # print type(result[1]) print result[1] dbutil.execute_sql(result[1]) except exceptions.RedisError: logger.error('exception', exc_info = True) return
def create(self): _user = self.get_by_username(self.username) if _user is None: _cnt, _ = execute_sql(self.sql_insert, \ (self.username, self.password, self.telephone, self.age, self.sex, self.status)) return _cnt > 0 return False
def fetch_count(cls, query=''): _sql = cls.sql_fetch_count _args = [] if query.strip() != '': _sql += ' WHERE username like %s' _args.append('%' + query + '%') _cnt, _ret = execute_sql(_sql, _args, True) return _ret[0][0] if _cnt > 0 else 0
def getDATA(cls,ip): _sql = 'select * from moniter where ip = %s and mtime >= %s order by mtime ASC ' mtime =time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time() - 60 * 60 )) _cnt,_res = dbutil.execute_sql(_sql,(ip,mtime),True ) _times = [] _cpu_data = [] _mem_data = [] for _rs in _res: _cpu,_mem,_time = _rs[2],_rs[3],_rs[4] _times.append(_time.strftime('%H:%M')) _cpu_data.append(_cpu) _mem_data.append(_mem) print {'categories':_times,'series':[{'name':'cpu','data':_cpu_data},{'name':'mem','data':_mem_data}]} return {'categories':_times,'series':[{'name':'cpu','data':_cpu_data},{'name':'mem','data':_mem_data}]}
def get_assets(query='', offset=None, limit=None): _machine_rooms = get_machine_rooms() _sql = SQL_FETCH_ALL_ASSET _args = [] if query != '': _sql += ' WHERE hostname like %s ' _args.append('%%%s%%' % query) if limit is not None: _sql += ' LIMIT %s' _args.append(limit) if offset is not None: _sql += ' OFFSET %s' _args.append(offset) _cnt, _assets = execute_sql(_sql, _args, True) return [dict(zip(COLUMS_ASSET, _asset)) for _asset in _assets]
def fetch_all(cls, query='', offset=None, limit=None): _sql = cls.sql_fetch_all % ','.join(cls.columns) _args = [] if query.strip() != '': _sql += ' WHERE username like %s' _args.append('%' + query + '%') # '%%%s%%' % (query, ) if limit is not None: _sql += ' LIMIT %s' _args.append(limit) if offset is not None: _sql += ' OFFSET %s' _args.append(offset) print _sql _cnt, _users = execute_sql(_sql, _args, True) return [User(dict(zip(cls.columns, _user))) for _user in _users]
def get_machine_room(): _cnt, _machines = execute_sql(SQL_FETCH_MACHINE_ROOM, (), is_fetch=True) return dict(_machines)
def modify_user(uid, telephone, age, sex=1, status=0): _cnt, _ = execute_sql(SQL_MODIFY, (telephone, age, sex, status, uid)) return True
def get_users(): count, rt = dbutil.execute_sql(SQL_GET_USERS) return [dict(zip(SQL_GET_USERS_COLUMNS, line)) for line in rt] '''
def get_by_key(cls, key): _sql = cls.sql_get_by_key % ','.join(cls.columns) _cnt, _users = execute_sql(_sql, (key, ), True) return User(dict(zip(cls.columns, _users[0]))) if _cnt > 0 else None
def get_by_username(cls, username): _sql = cls.sql_get_by_username % ','.join(cls.columns) _cnt, _users = execute_sql(_sql, (username, ), True) return dict(zip(cls.columns, _users[0])) if _cnt > 0 else None
def get_asset_count(): _, _cnt = execute_sql(SQL_COUNT_ASSET, (), True) return _cnt[0][0] if _cnt is not None and len(_cnt) > 0 else 0
def delete_asset(uid): _cnt, _ = execute_sql(SQL_DELETE_ASSET_BY_ID, (uid,)) return _cnt > 0
def delete_user(uid): _cnt, _ = execute_sql(SQL_DELETE_BY_ID, (uid, )) return _cnt > 0
def save(self): _sql = 'insert into moniter(ip,mtime,cpu,mem) values (%s,%s,%s,%s)' _cnt, _ = dbutil.execute_sql(_sql, (self.ip, self.mtime, self.cpu, self.mem)) return _cnt > 0
def login(self): _cnt, _ = execute_sql(self.sql_login, (self.username, self.password), True) return _cnt > 0
def record_remedy_status(order_id): sql = ''' update vm_task_allot_impl set remedy = 1 where order_id={0}'''.format(order_id) ret = dbutil.execute_sql(sql)
def remedy(order_id, task_id, remedy_num): sql = '''update vm_task_allot_impl set allot_times = allot_times+{0} where order_id ={1} and task_id={2}'''.format(remedy_num, order_id, task_id) print sql ret = dbutil.execute_sql(sql)
def get_asset_by_id(uid): _cnt, _assets = execute_sql(SQL_GET_ASSET_BY_ID, (uid,), True) return dict(zip(COLUMN_ASSET, _assets[0])) if _cnt > 0 else None
def add_asset(sn, vendor, machine_room_id, model, purchase_date, cpu, ram, disk, os, ip, hostname, admin, business): _asset = get_asset_by_sn(sn) if _asset is None: _cnt, _ = execute_sql(SQL_ASSET_INSERT, (sn, vendor, machine_room_id, model, purchase_date, cpu, ram, disk, os, ip, hostname, admin, business)) return _cnt > 0 return False
def get_user_by_id(uid): _cnt, _users = execute_sql(SQL_GET_BY_ID, (uid, ), True) return dict(zip(COLUMS_USER, _users[0])) if _cnt > 0 else None
def adjust_rantimes(order_id, task_id, ran_times): sql = '''update vm_task_allot_impl set ran_times = ran_times-{0} where order_id ={1} and task_id={2}'''.format(ran_times, order_id, task_id) print sql ret = dbutil.execute_sql(sql)
def get_user_by_username(username): _cnt, _users = execute_sql(SQL_GET_BY_USERNAME, (username, ), True) return dict(zip(COLUMS_USER, _users[0])) if _cnt > 0 else None
def validate_user_login(username, password): _cnt, _ = execute_sql(SQL_VALIDATE_LOGIN, (username, password), True) return _cnt > 0
def execute(cls, sql, args=(), is_fetch=False): return execute_sql(sql, args, is_fetch)
def get_machine_rooms(): _, _machine_rooms = execute_sql(SQL_FETCH_ALL_MACHINE_ROOM, (), True) return dict(_machine_rooms)
def asset_delete(id): DELETE_ASSET_BY_ID_SQL = 'update asset set status=2 where id=%s' dbutil.execute_sql(DELETE_ASSET_BY_ID_SQL,(id,),False) return True
#coding:utf-8 import dbutil res = {} f = open('www_access_20140823.log','r') for line in f: tmp = line.split(' ') ip,status = tmp[0],tmp[8] res[(ip,status)] = res.get((ip,status),0) + 1 for l in sorted(res.items(),key=lambda x:x[1],reverse=True)[:100]: sql = 'insert into log(ip,status,count) values("%s",%s,%s)'%(l[0][0],l[0][1],l[1]) print sql dbutil.execute_sql(sql)
def delete(cls, key): _cnt, _ = execute_sql(cls.sql_delete_by_key, (key,)) return _cnt > 0
def add_user(username, password, telephone, age, sex=1, status=0): _user = get_user_by_username(username) if _user is None: _cnt, _ = execute_sql(SQL_INSERT, (username, password, telephone, age,sex, status)) return _cnt > 0 return False
def delete_user(uid): _cnt, _ = execute_sql(SQL_DELETE_BY_ID, (uid,)) return _cnt > 0
def get_assets(query=''): if query == '': _cnt, _assets = execute_sql(SQL_FETCH_ASSET_ALL, (), True) else: _cnt, _assets = execute_sql(SQL_FETCH_ASSET_BY_SN,('%%%s%%' % query,), True) return [dict(zip(COLUMN_ASSET, _asset)) for _asset in _assets]
def get_user_by_id(uid): _cnt, _users = execute_sql(SQL_GET_BY_ID, (uid,), True) return dict(zip(COLUMN_USER, _users[0])) if _cnt > 0 else None
def get_asset_by_sn(sn): _cnt, _assets = execute_sql(SQL_GET_ASSET_BY_SN, (sn,), True) return dict(zip(COLUMN_ASSET, _assets[0])) if _cnt > 0 else None
def validate_user_login(username, password): _cnt, _ = execute_sql(SQL_VALIDATE_LOGIN, (username, password), True) # 与0比较返回True或者False return _cnt > 0
def modify_asset(uid, cpu, ram, disk, status=0): _cnt, _ = execute_sql(SQL_ASSET_MODIFY, (cpu, ram, disk, status, uid)) return True
def delete(cls, key): _cnt, _ = execute_sql(cls.sql_delete_by_key, (key, )) return _cnt > 0
def get_users(query=''): if query == '': _cnt, _users = execute_sql(SQL_FETCH_ALL, (), True) else: _cnt, _users = execute_sql(SQL_FETCH_BY_USERNAME,('%%%s%%' % query,), True) return [dict(zip(COLUMN_USER, _user)) for _user in _users]
def get_user_by_username(username): _cnt, _users = execute_sql(SQL_GET_BY_USERNAME, (username,), True) return dict(zip(COLUMN_USER, _users[0])) if _cnt > 0 else None
def save(self): _sql = 'insert into moniter(ip,mtime,cpu,mem) values (%s,%s,%s,%s)' _cnt, _ = dbutil.execute_sql(_sql,(self.ip,self.mtime,self.cpu,self.mem)) return _cnt > 0
def update(self): _cnt, _ = execute_sql(self.sql_modify, (self.telephone, self.age, self.id)) return True
#coding:utf-8 import dbutil res = {} f = open('www_access_20140823.log', 'r') for line in f: tmp = line.split(' ') ip, status = tmp[0], tmp[8] res[(ip, status)] = res.get((ip, status), 0) + 1 for l in sorted(res.items(), key=lambda x: x[1], reverse=True)[:100]: sql = 'insert into log(ip,status,count) values("%s",%s,%s)' % ( l[0][0], l[0][1], l[1]) print sql dbutil.execute_sql(sql)