f37 -> f39
[infrastructure.git] / nagios / configurator / configurator.py
1 #!/usr/bin/env python
2
3 # WARNING - you might need to set your locale before you can run this script
4 # e.g.
5 # export LANG=en_US.UTF-8
6 # nagios.py ...
7
8 import sys 
9 import getopt
10
11 from NagiosConfig import NagiosConfig
12
13 SERVER="www.planet-lab.org"
14 DIRNAME="/etc/nagios"
15
16 options='h:o:Os:d:nNv'
17
18 usage_string="""Usage : %s [options] plc-id plc-passwd role
19 Outputs a nagios config for watching a given set of sites
20 Also writes a static HTML page for bookmarking site-centric queries to comon
21 Options:
22   -h plc-hostname       To provide an alternate plc hostname
23                         (defaults to %s)
24   -o e-mail             If you provide an e-mail address, all notifications
25                         will be sent there. Otherwise, you'll need admin role
26                         so that we can determine the respective PI's e-mails
27   -O                    same as -e with the plc-id as dest. e-mail
28   -s site-file          You can provide a list of sites in a separate file
29                         expected format is one line per site, site_id first
30                         the default is to list all sites
31   -d dirname            To specify an alternate output dir
32                         (defaults to %s)
33   -n                    Does not generate the nagios config
34   -N                    Does not generate the HTML page
35   -v                    Runs in verbose mode
36 """%(sys.argv[0],SERVER,DIRNAME)
37
38 def usage ():
39     print usage_string
40     sys.exit(1)
41
42
43 def main ():
44     command = sys.argv[0]
45     argv = sys.argv[1:]
46
47     try:
48         opts,args = getopt.getopt(argv, options)
49     except getopt.GetoptError:
50         print "Unknown option"
51         usage()
52
53     plc_server=SERVER
54     output_mail=None
55     opt_same_email=False
56     site_file=None
57     dirname=DIRNAME
58     opt_nagios = True
59     opt_html = True
60     opt_verbose = False
61     
62     for o,a in opts:
63         if o in ['-h']:
64             plc_server = a
65         elif o in ['-o']:
66             output_mail = a
67         elif o in ['-O']:
68             opt_same_email = True
69         elif o in ['-s']:
70             site_file = a
71         elif o in ['-d']:
72             dirname = a
73         elif o in ['-n']:
74             opt_nagios = False
75         elif o in ['-N']:
76             opt_html = False
77         elif o in ['-v']:
78             opt_verbose = True
79         else:
80             print "Unknown option",o
81             usage()
82
83     if not len(args) == 3:
84         usage()
85
86     (plc_id,passwd,role) = args
87     if opt_same_email:
88         output_mail=plc_id
89
90 #    print plc_server,output_mail,dirname,opt_nagios,opt_html
91 #    print plc_id,passwd,role,site_file
92
93     print "Connecting to %s and gathering data"%plc_server
94     config = NagiosConfig (plc_server,plc_id,passwd,role)
95     if opt_verbose:
96         config.set_verbose()
97     config.GatherData (output_mail,site_file)
98
99     if opt_nagios:
100         print "Creating nagios config in %s"%dirname
101         config.WriteNagiosConfig (dirname)
102
103     if opt_html:
104         html_page = "%s/comon.html"%dirname
105         config.WriteComonLinks (html_page)
106
107     
108 ####################
109 if __name__ == "__main__":
110     main ()
111