def do_showlog(environ, start_response): import config import postgresops from mod_scheduler import scheduledb start_response("200 OK", [("Content-Type", "text/html")]) resp = ["<html><body>\n"] d = cgi.parse_qs(environ["QUERY_STRING"]) if "taskid" in d: import postgresops from mod_scheduler import scheduledb try: scheduledb.connect() postgresops.check_evil(d["taskid"][0]) resp.append("<h2>Log File for Task ID: %s</h2>\n<pre>\n" % d["taskid"][0]) task = scheduledb.get_tasks(where="id = %s" % (d["taskid"][0]), orderby="start_time desc") if len(task) != 1: resp.append("Error 0 or >1 tasks matched!") else: fin = open(task[0].log_file, "r") resp.extend(fin.readlines()) fin.close() except Exception, exp: resp.append("Exception " + str(exp) + " occurred") resp.append("</pre>\n")
def flush_files(): scheduledb.connect() filedb.connect() idstokill = [] postgresops.dbcur.execute("select id from flows.files where status=%s",(filedb.FAIL,)) idstokill.extend([i[0] for i in postgresops.dbcur]) postgresops.dbcur.execute("select id,task_id from flows.files where status=%s",(filedb.INVALID,)) rows = postgresops.dbcur.fetchall() for id,task_id in rows: postgresops.dbcur.execute("select status from schedule.tasks where id=%s",(task_id,)) if ( postgresops.dbcur.rowcount <= 0 or postgresops.dbcur.fetchone()[0] >= scheduledb.ERROR_CRASH ): idstokill.append(id) if len(idstokill)==0: return if not autoconfirm: print "The following files will be removed from the cache: ",idstokill conf = raw_input("Confirm [y|n]? ") if ( conf != 'y' and conf != 'yes'): print "Cancelled by user" sys.exit(10) for id in idstokill: str = postgresops.dbcur.mogrify("DELETE FROM flows.files WHERE id=%s",(id,)) print str postgresops.dbcur.execute(str) postgresops.dbcon.commit() print "";
def flush_tasks(): scheduledb.connect() idstokill = [] postgresops.dbcur.execute("select id from schedule.tasks where status>=%s",(scheduledb.ERROR_CRASH,)) rows = postgresops.dbcur.fetchall() idstocheck = [i[0] for i in rows] while len(idstocheck)>0: id = idstocheck.pop() postgresops.dbcur.execute("select id from schedule.tasks where %s=any(prerequisites)",(id,)) idstocheck.extend([i[0] for i in postgresops.dbcur]) idstokill.append(id) if len(idstokill)==0: return if not autoconfirm: print "The following tasks will be removed from the database: ",idstokill conf = raw_input("Confirm [y|n]? ") if ( conf != 'y' and conf != 'yes'): print "Cancelled by user" sys.exit(10) for id in idstokill: str = postgresops.dbcur.mogrify("DELETE FROM schedule.tasks WHERE id=%s",(id,)) print str postgresops.dbcur.execute(str) postgresops.dbcon.commit() print "";
def do_tasks(environ, start_response): import config from mod_scheduler import scheduledb scheduledb.connect() dbconnected = scheduledb.connected() env = Environment(loader=FileSystemLoader(environ["DOCUMENT_ROOT"])) template = env.get_template("tasks.html") start_response("200 OK", [("Content-Type", "text/html")]) ## getting task info active_tasks = scheduledb.get_tasks( where="status >= %d AND status <= %d" % (scheduledb.WAITING_FOR_INPUT, scheduledb.PAUSED), orderby="status desc" ) error_tasks = scheduledb.get_tasks(where="status >= %d" % (scheduledb.ERROR_CRASH), orderby="end_time desc") done_tasks = scheduledb.get_tasks(where="status = %d" % (scheduledb.DONE), orderby="start_time desc", limit=15) for task in active_tasks + error_tasks + done_tasks: apply_status_task(task) return [ str( template.render( dbconnected=dbconnected, active_tasks=active_tasks, error_tasks=error_tasks, done_tasks=done_tasks ) ) ]
def flush_flows(): scheduledb.connect() filedb.connect() flowdb.connect() postgresops.dbcur.execute("select flowdef,time_from,time_to,source_name,source_id from flows.curflows where status=%s",(flowdb.ERROR,)) idstokill = [] idstokill.extend(postgresops.dbcur.fetchall()) postgresops.dbcur.execute("select flowdef,time_from,time_to,source_name,source_id,task_ids,file_ids from flows.curflows where status!=%s",(flowdb.DONE,)) rows = postgresops.dbcur.fetchall() for flowdef,time_from,time_to,source_name,source_id,task_ids,file_ids in rows: found_death= False for task_id in task_ids: postgresops.dbcur.execute("select status from schedule.tasks where id=%s",(task_id,)) if ( postgresops.dbcur.rowcount <= 0 or postgresops.dbcur.fetchone()[0] >= scheduledb.ERROR_CRASH ): idstokill.append((flowdef,time_from,time_to,source_name,source_id)) found_death = True break if found_death: continue for file_id in file_ids: postgresops.dbcur.execute("select status from flows.files where id=%s",(file_id,)) if ( postgresops.dbcur.rowcount <= 0 or postgresops.dbcur.fetchone()[0] == filedb.FAIL ): idstokill.append((flowdef,time_from,time_to,source_name,source_id)) found_death = True break if len(idstokill)==0: return if not autoconfirm: print "The following flows will be removed from the database:\n"+"\n\t".join([i.__str__() for i in idstokill]) conf = raw_input("Confirm [y|n]? ") if ( conf != 'y' and conf != 'yes'): print "Cancelled by user" sys.exit(10) for flowdef,time_from,time_to,source_name,source_id in idstokill: str = postgresops.dbcur.mogrify("DELETE FROM flows.curflows WHERE flowdef=%s and time_from=%s and time_to=%s and source_name=%s and source_id=%s", (flowdef,time_from,time_to,source_name,source_id)) print str postgresops.dbcur.execute(str) postgresops.dbcon.commit() print "";
def do_admin(environ, start_response): import config from mod_exec import mod_exec_IF start_response('200 OK',[('Content-Type','text/html')]) resp = ['<html><head><meta http-equiv="Refresh" content="10;url=javascript:window.history.back()" /></head><body>'] resp.append("<h2>Admin Command Progress</h2>\n<pre>\n"); d = cgi.parse_qs(environ['QUERY_STRING']) if ( 'modname' in d ): if ( 'action' in d ): resp.append("Module command: %s\n"%d['action'][0]); resp.append("Module name: %s\n\n"%d['modname'][0]) if d['modname'][0] == 'mod_exec' and d['action'][0] == 'start': # different procedure mod_exec_IF.connect(); if ( mod_exec_IF.connected() ): resp.append("mod_exec already responding, please kill first if you wish to restart") else: if (os.path.exists('/etc/init.d/sensezilla')): rcode = os.system('/etc/init.d/sensezilla start') else: rcode = os.system(config.map['mod_exec']['python']+' '+config.map['global']['root_dir']+'/modules/mod_exec/mod_exec.py') resp.append("Started mod_exec, response code %d"%rcode) else: resp.append("Connect to mod_exec...") mod_exec_IF.connect(); if ( mod_exec_IF.connected() ): resp.append("success.\n") if d['modname'][0] == 'mod_exec' and d['action'][0] == 'stop': mod_exec_IF.kill() resp.append("Killed mod_exec and all modules") else: curstate = mod_exec_IF.get_state(d['modname'][0]) if curstate != None: resp.append("Current module state is %d\n"%curstate) if ( d['action'][0] == 'start' ): mod_exec_IF.start(d['modname'][0]) resp.append("Module started\n") elif (d['action'][0] == 'stop' ): mod_exec_IF.stop(d['modname'][0]) resp.append("Module stopped\n") elif (d['action'][0] =='restart'): mod_exec_IF.restart(d['modname'][0]) resp.append("Module restarted\n") else: resp.append("Could not get state of %s (is it unknown to mod_exec?)"%d['modname'][0]) else: resp.append("fail.\n") else: resp.append("No action given\n") elif ( 'action' in d ): if d['action'][0] == 'requeueall': from mod_scheduler import scheduledb resp.append("Connecting to schedule DB...") scheduledb.connect() if not scheduledb.connected(): resp.append("fail.\n") else: errortasks = scheduledb.get_tasks(where='status >= %d'%(scheduledb.ERROR_CRASH),orderby="start_time desc") for task in errortasks: scheduledb.update_task(task,'status',scheduledb.WAITING_FOR_START) return ['<html><script>window.history.back()</script></html>'] elif d['action'][0] == 'requeue': from mod_scheduler import scheduledb import postgresops resp.append("Connecting to schedule DB...") scheduledb.connect() if not scheduledb.connected(): resp.append("fail.\n") else: resp.append("yay\n") if 'task' in d: try: postgresops.check_evil(d['task'][0]) task = scheduledb.get_tasks(where='status >= %d and id = %s'%(scheduledb.ERROR_CRASH,d['task'][0]),orderby="start_time desc") if ( len(task) != 1): resp.append("Error 0 or >1 tasks matched!") else: scheduledb.update_task(task[0],'status',scheduledb.WAITING_FOR_START) return ['<html><script>window.history.back()</script></html>'] except Exception,exp: resp.append("Exception "+str(exp)+" occurred") else: resp.append("Did not provide ID\n")
postgresops.dbcon.commit(); # connect to postgres flowdb.connect() if flowdb.connected(): flowdb.initdb() filedb.initdb() else: print "ERROR: Cannot connect to postgre database" sys.exit(1) #timeouts last_db_check = time.time() last_cache_check = 0 while True: if ( not scheduledb.connected() ): scheduledb.connect() else: if ( time.time() - last_db_check > DB_CHECK_INTERVAL ): check_db() last_db_check = time.time() elif (time.time() - last_cache_check > CACHE_CHECK_INTERVAL): filedb.check_cache() last_cache_check = time.time() time.sleep(0.1)