def analyze(self, prm): cmd = "netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n" os0.muteshell(cmd, simulate=self.simulate, keepout=True) stdinp_fd = open(os0.setlfilename(os0.bgout_fn), 'r') line = stdinp_fd.readline() total_conn = 0 total_dos_conn = 0 total_overload = 0 while line != "": fields = line.strip().split() # Check for DOS attack if fields[1] == "SYS_SENT": total_dos_conn += int(fields[0]) # Check for overload elif fields[1] == "SYN_RECV" or fields[1] == "TIME_WAIT": total_overload += int(fields[0]) total_conn += int(fields[0]) line = stdinp_fd.readline() stdinp_fd.close() try: os.remove(os0.setlfilename(os0.bgout_fn)) except: pass perc_dos_conn = (total_dos_conn * 100) / total_conn perc_overload = (total_overload * 100) / total_conn prm['total_conn'] = total_conn prm['total_dos_conn'] = total_dos_conn prm['total_overload'] = total_overload prm['perc_dos_conn'] = perc_dos_conn prm['perc_overload'] = perc_overload return prm
def gen_db_list(self, dbtype, user, sqlcmd, ctx): # pdb.set_trace() dblist = [] os0.wlog(" Creating", dbtype, "db list") if dbtype == "psql": cmd = sqlcmd + " -U" + user + " -l" cmdlog = cmd elif dbtype == "mysql": cmd = sqlcmd + " -u " + user + \ " --password="******" -e \"show databases;\" mysql" cmdlog = sqlcmd + " -u " + user + " -e \"show databases;\" mysql" else: cmd = "" cmdlog = cmd os0.trace_debug("$", cmdlog) os0.muteshell(cmd, simulate=self.dry_run, keepout=True) if ctx['db_name']: sel_db = ctx['db_name'] else: sel_db = '.*' if os0.debug_mode: os0.wlog("> DB selection", sel_db) stdinp_fd = open(os0.setlfilename(os0.bgout_fn), 'r') line = stdinp_fd.readline() while line != "": i = line.rfind('\n') if i >= 0: if dbtype == "psql": if line[0:1] == ' ' and line[1:2] != ' ': x = line.split('|') dbname = x[0].strip() if re.match("z[ei].*|demo.*", dbname) and \ re.match(sel_db, dbname): dblist.append(dbname) if os0.debug_mode: os0.wlog("> dblist.append({0})".format(dbname)) elif dbtype == "mysql": dbname = line.strip() if re.match("w.*|mg.*|assioma.*", dbname) and \ re.match(sel_db, dbname): dblist.append(dbname) if os0.debug_mode: os0.wlog("> dblist.append({0})".format(dbname)) line = stdinp_fd.readline() stdinp_fd.close() if not os0.debug_mode and not self.dry_run and os0.bgout_fn != "": os.remove(os0.setlfilename(os0.bgout_fn, 'r')) return dblist
def chdir(self, path): # Change root dir lpath = os0.setlfilename(path) os0.wlog(" [{0}]".format(lpath)) self.set_chdir(lpath) self.ftp_rootdir = lpath self.ftp_dir = ""
def main(): """Tool main""" sts = 0 # pdb.set_trace() ctx = parse_args(sys.argv[1:], version=version(), doc=__doc__) if ctx['do_list']: print ctx['saveset_list'] return sts RI = Restore_Image(ctx) f_alrdy_run = check_if_running(ctx, RI.pid) if f_alrdy_run: os0.wlog("({0}) ***Another instance is running!!!".format(RI.pid)) # Restore files file_r_ctr = 0 file_u_ctr = 0 time_wait = 60 wait_loop = 3 if not f_alrdy_run: fl = RI.extract_fn_2_restore() loop_ctr = wait_loop while loop_ctr > 0: if fl != "": file_r_ctr = file_r_ctr + 1 if os.path.isfile(fl): RI.restore_file(fl) file_u_ctr += 1 if file_u_ctr > 1: wait_loop = 60 loop_ctr = wait_loop else: os0.wlog(" file", fl, "not found!!!") RI.commit_fn_restored() fl = RI.extract_fn_2_restore() if fl == "": os0.wlog(" wait for next db") time.sleep(time_wait) loop_ctr -= 1 if not ctx['dbg_mode'] and os.path.isfile(os0.setlfilename(os0.bgout_fn)): os.remove(os0.setlfilename(os0.bgout_fn)) if not f_alrdy_run: os0.wlog("Restore DB ended." " {0} DB to restore, {1} DB restored ({2}).".format( file_u_ctr, file_u_ctr, RI.pid)) return sts
def check_if_running(ctx, pid): f_alrdy_run = False id_str = ctx['caller'] + ".py" cmd = "ps aux|grep " + id_str os0.muteshell(cmd, keepout=True) stdinp_fd = open(os0.setlfilename(os0.bgout_fn), 'r') rxmatch = "root .* python .*" + id_str + ".*" rxnmatch = "root .* {0} .*".format(pid) line = stdinp_fd.readline() while line != "" and not f_alrdy_run: i = line.rfind('\n') if i >= 0: if re.match(rxmatch, line) and not re.match(rxnmatch, line): f_alrdy_run = True line = stdinp_fd.readline() stdinp_fd.close() if os.path.isfile(os0.setlfilename(os0.bgout_fn)): os.remove(os0.setlfilename(os0.bgout_fn)) return f_alrdy_run
def test_06(self, ctx): if not ctx.get('dry_run', False): if os.path.dirname(__file__) == "": if __file__ == "__main__.py": cmd = "dir " + os0.setlfilename("../") else: cmd = "dir " + os0.setlfilename("./") else: cmd = "dir " + os0.setlfilename(os.path.dirname(__file__)) try: os.remove(os0.setlfilename(os0.bgout_fn)) except: pass os0.muteshell(cmd, keepout=True) self.check_4_tkn_in_stdout(os.path.basename(__file__)) sts = self.Z.test_result(ctx, "os0.dir", True, os.path.isfile(os0.bgout_fn)) if not ctx.get('dry_run', False): if _platform == "win32": cmd = "del" elif _platform == "OpenVMS": cmd = "delete" else: cmd = "rm" cmd = cmd + " " + (os0.setlfilename(os0.bgout_fn)) os0.muteshell(cmd, simulate=True, tlog=True) self.check_4_tkn_in_stdout(cmd) if sts == TEST_SUCCESS: sts = self.Z.test_result(ctx, "os0.del", True, os.path.isfile(os0.bgout_fn)) if not ctx.get('dry_run', False): if not ctx.get('dry_run', False): cmd = "dir" os0.muteshell(cmd) if sts == TEST_SUCCESS: sts = self.Z.test_result(ctx, "os0.dir", False, os.path.isfile(os0.bgout_fn)) return sts
def check_4_tkn_in_stdout(self, token): # Now search for this program name in output; # if found muteshell worked right! found_chunk = False try: stdout_fd = open(os0.setlfilename(os0.bgout_fn, 'r')) f = stdout_fd.read() if f.find(token) >= 0: found_chunk = True stdout_fd.close() except: pass if not found_chunk: os0.wlog("Test failed: muteshell did not work!!!") raise Exception("Test failed: muteshell did not work!!!")
def chdir(self, path): # Change root dir lpath = os0.setlfilename(path) os0.wlog(self.pid, " directory [{0}]".format(lpath)) self.set_chdir(lpath) self.ftp_dir = lpath
def check_4_lfile_dir(self, fsrc, ftgt): f = os0.setlfilename(fsrc, os0.LFN_DIR) if f != ftgt: return False else: return True