def read_args(): """ Read arguments. """ parser = argparse.ArgumentParser() parser.add_argument("-j", "--job", action="store_true", default=False, help='Sample running job info with command "bjobs -u all -r -UF".') parser.add_argument("-q", "--queue", action="store_true", default=False, help='Sample queue info with command "bqueues".') parser.add_argument("-H", "--host", action="store_true", default=False, help='Sample host info with command "bhosts".') parser.add_argument("-l", "--load", action="store_true", default=False, help='Sample host load info with command "lsload".') parser.add_argument("-u", "--user", action="store_true", default=False, help='Sample user info with command "busers".') parser.add_argument("-i", "--interval", type=int, default=0, help='Specify the sampling interval, unit is second. Sampling only once by default".') args = parser.parse_args() if args.interval < 0: common.print_error('*Error*: interval "' + str(args.interval) + '": Cannot be less than "0".') sys.exit(1) return(args.job, args.queue, args.host, args.load, args.user, args.interval)
def get_sql_table_count(db_file, orig_conn, table_name): """ How many lines of the database table. """ count = 'N/A' (result, conn, curs) = connect_preprocess(db_file, orig_conn) if result == 'failed': return (count) try: command = "SELECT count(*) FROM '" + str(table_name) + "'" curs.execute(command) return_list = curs.fetchall() return_tuple = return_list[0] count = return_tuple[0] curs.close() if orig_conn == '': conn.close() except Exception as error: common.print_error( '*Error* (get_sql_table_count) : Failed on getting table count fro table "' + str(table_name) + '" on db_file "' + str(db_file) + '": ' + str(error)) return (count)
def connect_db_file(db_file, mode='read'): result = 'passed' conn = '' if mode == 'write': journal_db_file = str(db_file) + '-journal' if os.path.exists(journal_db_file) and (mode == 'write'): common.print_warning( '*Warning*: database file "' + str(db_file) + '" is on another connection, will not connect it.') result = 'locked' return (result, conn) elif mode == 'read': if not os.path.exists(db_file): common.print_error('*Error*: "' + str(db_file) + '" No such database file.') result = 'failed' return (result, conn) try: conn = sqlite3.connect(db_file) except Exception as error: common.print_error('*Error*: Failed on connecting database file "' + str(db_file) + '": ' + str(error)) result = 'failed' return (result, conn)
def insert_into_sql_table(db_file, orig_conn, table_name, value_string, commit=True): """ Insert new value into sql table. """ (result, conn, curs) = connect_preprocess(db_file, orig_conn, mode='write') if (result == 'failed') or (result == 'locked'): return try: command = "INSERT INTO '" + str(table_name) + "' VALUES " + str( value_string) curs.execute(command) curs.close() if commit: conn.commit() if orig_conn == '': conn.close() except Exception as error: common.print_error( '*Error* (insert_into_sql_table) : Failed on inserting specified values into table "' + str(table_name) + '" on db file "' + str(db_file) + '": ' + str(error))
def get_sql_table_list(db_file, orig_conn): """ Get all of the tables from the specified db file. """ table_list = [] (result, conn, curs) = connect_preprocess(db_file, orig_conn) if result == 'failed': return (table_list) try: command = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" results = curs.execute(command) all_items = results.fetchall() for item in all_items: (key, ) = item table_list.append(key) curs.close() if orig_conn == '': conn.close() except Exception as error: common.print_error( '*Error* (get_sql_table_list) : Failed on getting table list on db_file "' + str(db_file) + '": ' + str(error)) return (table_list)
def create_sql_table(db_file, orig_conn, table_name, init_string, commit=True): """ Create a table if it not exists, initialization the setting. """ (result, conn, curs) = connect_preprocess(db_file, orig_conn, mode='write') if (result == 'failed') or (result == 'locked'): return try: command = "CREATE TABLE IF NOT EXISTS '" + str( table_name) + "' " + str(init_string) curs.execute(command) curs.close() if commit: conn.commit() if orig_conn == '': conn.close() except Exception as error: common.print_error( '*Error* (create_sql_table) : Failed on creating table "' + str(table_name) + '" on db file "' + str(db_file) + '": ' + str(error))
def delete_sql_table_rows(db_file, orig_conn, table_name, row_id, begin_line, end_line, commit=True): """ Delete specified table rows (from begin_line to end_line). """ (result, conn, curs) = connect_preprocess(db_file, orig_conn, mode='write') if (result == 'failed') or (result == 'locked'): return try: command = "DELETE FROM '" + str(table_name) + "' WHERE " + str( row_id) + " IN (SELECT " + str(row_id) + " FROM '" + str( table_name) + "' ORDER BY " + str(row_id) + " LIMIT " + str( begin_line) + "," + str(end_line) + ")" curs.execute(command) curs.close() if commit: conn.commit() if orig_conn == '': conn.close() except Exception as error: common.print_error( '*Error* (drop_sql_table) : Failed on deleting table "' + str(table_name) + '" lines ' + str(begin_line) + '-' + str(end_line) + ': ' + str(error))
def get_sql_table_key_list(db_file, orig_conn, table_name): """ Get all of the tables from the specified db file. """ key_list = [] (result, conn, curs) = connect_preprocess(db_file, orig_conn) if result == 'failed': return (key_list) try: command = "SELECT * FROM '" + str(table_name) + "'" curs.execute(command) key_list = [tuple[0] for tuple in curs.description] curs.close() if orig_conn == '': conn.close() except Exception as error: common.print_error( '*Error* (get_sql_table_key_list) : Failed on getting table key list on db_file "' + str(db_file) + '": ' + str(error)) return (key_list)
def get_sql_table_data(db_file, orig_conn, table_name, key_list=[], limit=0): """ With specified db_file-table_name, get all data from specified key_list. """ data_dic = {} (result, conn, curs) = connect_preprocess(db_file, orig_conn) if result == 'failed': return (data_dic) try: command = "SELECT * FROM '" + str(table_name) + "'" if limit != 0: command = str(command) + ' limit ' + str(limit) results = curs.execute(command) all_items = results.fetchall() table_key_list = [tuple[0] for tuple in curs.description] curs.close() if orig_conn == '': conn.close() if len(key_list) == 0: key_list = table_key_list else: for key in key_list: if key not in table_key_list: common.print_error('*Error* (get_sql_table_data) : "' + str(key) + '": invalid key on specified key list.') return (data_dic) for item in all_items: value_list = list(item) for i in range(len(table_key_list)): key = table_key_list[i] if key in key_list: value = value_list[i] if key in data_dic.keys(): data_dic[key].append(value) else: data_dic[key] = [ value, ] except Exception as error: common.print_error( '*Error* (get_sql_table_data) : Failed on getting table info from table "' + str(table_name) + '" of db_file "' + str(db_file) + '": ' + str(error)) return (data_dic)
def check_job(self, job): command = 'bjobs -UF ' + str(job) job_dic = lsf_common.get_lsf_bjobs_uf_info(command) if job_dic[job]['status'] != 'RUN': common.print_error('*Error*: Job "' + str(job) + '" is not running, cannot get process status.') sys.exit(1) else: if not job_dic[job]['pids']: common.print_error('*Error*: Not find PIDs information for job "' + str(job) + '".') sys.exit(1) return(job_dic, job_dic[job]['pids'])
def __init__(self, job_sampling, queue_sampling, host_sampling, load_sampling, user_sampling, interval): self.job_sampling = job_sampling self.queue_sampling = queue_sampling self.host_sampling = host_sampling self.load_sampling = load_sampling self.user_sampling = user_sampling self.interval = interval self.db_path = str(config.db_path) + '/monitor' job_db_path = str(self.db_path) + '/job' if not os.path.exists(job_db_path): try: os.system('mkdir -p ' + str(job_db_path)) except Exception as error: common.print_error('*Error*: Failed on creating sqlite job db directory "' + str(job_db_path) + '".') common.print_error(' ' + str(error)) sys.exit(1)
def check_pid(self, pid): pid_list = [] command = 'pstree -p ' + str(pid) (return_code, stdout, stderr) = common.run_command(command) for line in str(stdout, 'utf-8').split('\n'): line = line.strip() if re.findall('\((\d+)\)', line): tmp_pid_list = re.findall('\((\d+)\)', line) if tmp_pid_list: pid_list.extend(tmp_pid_list) if not pid_list: common.print_error('*Error*: No valid pid was found.') sys.exit(1) return(pid_list)
def read_args(): """ Read in arguments. """ parser = argparse.ArgumentParser() parser.add_argument('-j', '--job', default='', help='Specify the LSF jobid you want to trace on remote host.') parser.add_argument('-p', '--pid', default='', help='Specify the pid you want to trace on local host.') args = parser.parse_args() if (not args.job) and (not args.pid): common.print_error('*Error*: "--job" or "--pid" must be specified.') sys.exit(1) return(args.job, args.pid)
def drop_sql_table(db_file, orig_conn, table_name, commit=True): """ Drop table if it exists. """ (result, conn, curs) = connect_preprocess(db_file, orig_conn, mode='write') if (result == 'failed') or (result == 'locked'): return try: command = "DROP TABLE IF EXISTS '" + str(table_name) + "'" curs.execute(command) curs.close() if commit: conn.commit() if orig_conn == '': conn.close() except Exception as error: common.print_error( '*Error* (drop_sql_table) : Failed on drop table "' + str(table_name) + '" from db_file "' + str(db_file) + '": ' + str(error))