AM nagios/plc2nagios.py
[monitor.git] / config.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
9 config_command = False
10
11 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
12 def parse_bool(option, opt_str, value, parser):
13         if opt_str in ["--debug"]:
14                 parser.values.debug = int(int(value))
15         elif opt_str in ["--mail"]:
16                 parser.values.mail = int(int(value))
17         elif opt_str in ["--bcc"]:
18                 parser.values.bcc = int(int(value))
19         elif opt_str in ["--policysavedb"]:
20                 parser.values.policysavedb = int(int(value))
21         elif opt_str in ["--squeeze"]:
22                 parser.values.squeeze = int(int(value))
23         else:
24                 print "blue"
25
26 def setFileFromList(file, list):
27         f = open(file, 'w')
28         for line in list:
29                 f.write(line + "\n")
30         f.close()
31         return True
32
33 def getListFromFile(file):
34         f = open(file, 'r')
35         list = []
36         for line in f:
37                 line = line.strip()
38                 list += [line]
39         return list
40
41 class config:
42         debug=0
43         mail=0
44         bcc=0
45         email="soltesz@cs.utk.edu"
46         run=False
47         checkopt=False
48         squeeze=0
49         policysavedb=0
50         __file = ".config"
51
52         def __init__(self, parser=None):
53                 if os.path.exists(self.__file): # file exists, read that.
54                         f = open(self.__file, 'r')
55                         o = pickle.load(f)
56                         self.__dict__.update(o)
57                         f.close()
58
59                 if parser == None:
60                         self.parser = OptionParser()
61                 else:
62                         self.parser = parser
63
64                 self.parser.set_defaults(debug = self.debug,
65                                                                 mail = self.mail,
66                                                                 bcc  = self.bcc,
67                                                                 email = self.email,
68                                                                 run = self.run,
69                                                                 checkopt = False,
70                                                                 squeeze = self.squeeze,
71                                                                 policysavedb = self.policysavedb)
72
73                 self.parser.add_option("", "--debug", dest="debug",
74                                                   help="Enable debugging", 
75                                                   type="int",
76                                                   metavar="[0|1]",
77                                                   action="callback", 
78                                                   callback=parse_bool)
79                 self.parser.add_option("", "--mail", dest="mail",
80                                                   help="Enable sending email",
81                                                   type="int",
82                                                   metavar="[0|1]",
83                                                   action="callback", 
84                                                   callback=parse_bool)
85                 self.parser.add_option("", "--bcc", dest="bcc",
86                                                   help="Include BCC to user",
87                                                   type="int",
88                                                   metavar="[0|1]",
89                                                   action="callback", 
90                                                   callback=parse_bool)
91                 self.parser.add_option("", "--squeeze", dest="squeeze",
92                                                   help="Squeeze sites or not",
93                                                   type="int",
94                                                   metavar="[0|1]",
95                                                   action="callback", 
96                                                   callback=parse_bool)
97                 self.parser.add_option("", "--policysavedb", dest="policysavedb",
98                                                   help="Save the policy event database after a run",
99                                                   type="int",
100                                                   metavar="[0|1]",
101                                                   action="callback", 
102                                                   callback=parse_bool)
103                 self.parser.add_option("", "--checkopt", dest="checkopt", 
104                                                   action="store_true",
105                                                   help="print current options")
106                 self.parser.add_option("", "--run", dest="run", 
107                                                   action="store_true",
108                                                   help="Perform monitor or print configs")
109                 self.parser.add_option("", "--email", dest="email",
110                                                   help="Specify an email address to use for mail when "+\
111                                                                  "debug is enabled or for bcc when it is not")
112                 
113                 # config_command is needed to keep subsequent loads of config() from
114                 # trying to parse the arguments that have already been parsed by
115                 # the new main().
116                 if parser == None and config_command:
117                         print "calling parse_args"
118                         self.parse_args()
119
120         def parse_args(self):
121                 #print "self: %s" % self
122                 #import traceback
123                 #print traceback.print_stack()
124                 #print "Ccalling parse_args"
125                 (options, args) = self.parser.parse_args()
126                 #for o in options.__dict__:
127                 #       print "optin: %s == %s" % (o, options.__dict__[o])
128                 self.__dict__.update(options.__dict__)
129                 self.__dict__['args'] = args
130                 self.save(options)
131                 if options.checkopt:
132                         self.usage()
133                 #       print "\nAdd --run to actually perform the command"
134                         sys.exit(1)
135
136         def getListFromFile(self, file):
137                 f = open(file, 'r')
138                 list = []
139                 for line in f:
140                         line = line.strip()
141                         list += [line]
142                 return list
143
144         def print_values(self):
145                 exclude = ['parser']
146                 for key in self.__dict__.keys():
147                         if key not in exclude:
148                                 print "%20s == %s" % (key, self.__dict__[key])
149                 
150         def save(self, options=None):
151                 f = open(self.__file, 'w')
152                 if options == None:
153                         o = {'debug': self.debug, 
154                                  'mail': self.mail, 
155                                  'bcc': self.bcc, 
156                                  'email':self.email,
157                                  'squeeze':self.squeeze,
158                                  'policysavedb':self.policysavedb}
159                 else:
160                         o = options.__dict__
161
162                 pickle.dump(o, f)
163                 f.close()
164
165         def usage(self):
166                 self.print_values()
167                 self.parser.print_help()
168
169
170 def main():
171         """ Start threads, do some housekeeping, then daemonize. """
172         # Defaults
173         global config_command
174         config_command = True
175         config = __main__.config()
176
177         try:
178                 print "acalling parse_args"
179                 config.parse_args()
180                 
181         except Exception, err:
182                 print "Error: %s " %  err
183                 config.usage()
184                 sys.exit(1)
185
186         config.usage()
187
188
189 if __name__ == '__main__':
190         main()