-
Notifications
You must be signed in to change notification settings - Fork 9
/
monitor.py
75 lines (69 loc) · 3.08 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from optparse import OptionParser
import ConfigParser
from agent import Agent
from tasks import tasks
from database import database
import pickle
import base64
class Monitor:
def __init__(self, configFile):
config = ConfigParser.ConfigParser()
config.readfp(open(configFile))
self.agent = Agent(configFile)
def list_tasks(self):
print "============================================================================"
print "| TASKS |"
print "============================================================================"
sql = "SELECT tid,task,after,reschedule,delta,args FROM tasks WHERE complete=0 ORDER BY after;"
result = self.agent.db.query(sql)
print '| {0:3} | {1:10} | {2:29} | {3:12} | {4:6} |'.format("TID", "Task", "After", "Reschedule?", "Delta")
print "----------------------------------------------------------------------------"
for row in result:
print '| {0:3} | {1:10} | {2:29} | {3:12d} | {4:6} |'.format(row[0], row[1], row[2], row[3], row[4])
args = ""
if (len(row[5]) > 0):
args = str(pickle.loads(base64.b64decode(row[5])))
print '| args: {0:66} |'.format(args)
print "----------------------------------------------------------------------------"
def add_task(self, name, repeat, delta, args=[]):
print "Adding Task: %s (repeat=%i, delta=%i)" % (name,repeat,delta)
theTask = None
for tt in tasks.TaskTypes:
if (tt.taskName == name):
theTask = tt(self.agent, args)
if theTask == None:
print "Error -- didn't know what task to do"
print "Valid task types are: " + ", ".join(map(lambda x : x.taskName, tasks.TaskTypes))
else:
if (repeat == 0):
theTask.reschedules = False
else:
theTask.reschedules = True
theTask.delta = delta
theTask.schedule()
def main():
monitor = Monitor("config.txt")
# task = tasks.Task(agent.db)
# task.schedule(False)
parser = OptionParser()
parser.add_option("-t", "--tasks", dest="tasks", help="peroform a TASK action", metavar="TASK")
(options, args) = parser.parse_args()
print "Options " + str(options)
print "Args " + str(args)
print ""
if options.tasks:
if options.tasks == "list":
monitor.list_tasks()
elif options.tasks == "add":
if len(args) < 1:
print "Usage: --add task repeat? delta"
print "Valid task types are: " + ", ".join(map(lambda x : x.taskName, tasks.TaskTypes))
else:
if (len(args) == 1):
monitor.add_task(args[0], 0, 0)
elif (len(args) < 4):
monitor.add_task(args[0], int(args[1]), int(args[2]))
else:
monitor.add_task(args[0], int(args[1]), int(args[2]), args[3:])
if __name__ == "__main__":
main()