unify the model by which probes are made to collect information about nodes or
[monitor.git] / monitor / parser.py
1 #!/usr/bin/python
2 import pickle
3 import os
4 import getopt
5 import sys
6 import __main__
7 from optparse import OptionParser
8 import config
9
10 def parse_bool(option, opt_str, value, parser):
11         if opt_str in ["--debug"]:
12                 parser.values.debug = int(int(value))
13         elif opt_str in ["--mail"]:
14                 parser.values.mail = int(int(value))
15         elif opt_str in ["--bcc"]:
16                 parser.values.bcc = int(int(value))
17         elif opt_str in ["--policysavedb"]:
18                 parser.values.policysavedb = int(int(value))
19         elif opt_str in ["--squeeze"]:
20                 parser.values.squeeze = int(int(value))
21         else:
22                 print "blue"
23
24 def parseSetDefaults(parser=None):
25         if parser == None:
26                 parser = OptionParser()
27
28         parser.set_defaults(debug = config.debug,
29                                         mail = config.mail,
30                                         bcc  = config.bcc,
31                                         email = config.email,
32                                         run = config.run,
33                                         squeeze = config.squeeze,
34                                         policysavedb = config.policysavedb)
35
36         parser.add_option("", "--debug", dest="debug",
37                           help="Enable debugging", 
38                           type="int",
39                           metavar="[0|1]",
40                           action="callback", 
41                           callback=parse_bool)
42         parser.add_option("", "--mail", dest="mail",
43                           help="Enable sending email",
44                           type="int",
45                           metavar="[0|1]",
46                           action="callback", 
47                           callback=parse_bool)
48         parser.add_option("", "--bcc", dest="bcc",
49                           help="Include BCC to user",
50                           type="int",
51                           metavar="[0|1]",
52                           action="callback", 
53                           callback=parse_bool)
54         parser.add_option("", "--squeeze", dest="squeeze",
55                           help="Squeeze sites or not",
56                           type="int",
57                           metavar="[0|1]",
58                           action="callback", 
59                           callback=parse_bool)
60         parser.add_option("", "--policysavedb", dest="policysavedb",
61                           help="Save the policy event database after a run",
62                           type="int",
63                           metavar="[0|1]",
64                           action="callback", 
65                           callback=parse_bool)
66         parser.add_option("", "--run", dest="run", 
67                           action="store_true",
68                           help="Perform monitor or print configs")
69         parser.add_option("", "--email", dest="email",
70                           help="Specify an email address to use for mail when "+\
71                                         "debug is enabled or for bcc when it is not")
72         return parser
73
74 def parseSetCacheSet(parser=None):
75         if parser == None:
76                 parser = OptionParser()
77
78         parser.set_defaults(cachecalls=True, cachetime=60)
79         parser.add_option("", "--nocache", dest="cachecalls", action="store_false",
80                                         help="When using PLCCache objects, temporarily disable the use of caching. i.e. refresh")
81         parser.add_option("", "--cachetime", dest="cachetime", 
82                                         help="How long to preserve a cached value. Minutes")
83         return parser
84
85 def parseSetNodeSets(parser=None):
86         if parser == None:
87                 parser = OptionParser()
88         
89         parser.set_defaults(node=None, site=None, nodelist=None, nodeselect=None, nodegroup=None)
90         parser.add_option("", "--node", dest="node", metavar="hostname", 
91                                                 help="Provide a single node to operate on")
92         parser.add_option("", "--site", dest="site", metavar="site name",
93                                                 help="Specify a single site to operate on")
94         parser.add_option("", "--nodegroup", dest="nodegroup", metavar="GroupName", 
95                                                 help="Provide the nodegroup for the list of nodes.")
96         parser.add_option("", "--nodelist", dest="nodelist", metavar="FILE", 
97                                                 help="Provide the input file for the list of objects")
98         parser.add_option("", "--nodeselect", dest="nodeselect", metavar="query string", 
99                                                 help="Provide a selection string to return a node list.")
100         return parser
101
102
103 def getParser(parsesets=[], parser=None):
104         if parser == None:
105                 p = OptionParser()
106         else:
107                 p = parser
108
109         if 'cacheset' in parsesets:
110                 p = parseSetCacheSet(p)
111
112         if 'nodesets' in parsesets:
113                 p = parseSetNodeSets(p)
114         if 'defaults' in parsesets:
115                 p = parseSetDefaults(p)
116
117         return p
118         
119 def parse_args(parser):
120         class obj: pass
121         (options, args) = parser.parse_args()
122         o = obj()
123         o.__dict__.update(options.__dict__)
124         o.__dict__['args'] = args
125         #config.update(o)
126         config.updatemodule(config, o)
127         return config
128
129 def print_values(parser):
130         exclude = ['parser']
131         for key in parser.__dict__.keys():
132                 if key not in exclude:
133                         print "%20s == %s" % (key, parser.__dict__[key])
134         
135 def usage(parser):
136         print_values(parser)
137         parser.print_help()