Fix: we are not sure the key() values() will return the same order for customs dict...
[shinken.git] / bin / shinken-reactionner
blob4f55816cb04fc8930442e8619d79f9620b4f8112
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 #This class is an application for launch actions
23 #like notifications or event handlers
24 #The actionner listen configuration from Arbiter in a port (first argument)
25 #the configuration gived by arbiter is schedulers where actionner will take
26 #actions.
27 #When already launch and have a conf, actionner still listen to arbiter (one
28 #a timeout) if arbiter wants it to have a new conf, actionner forgot old
29 #chedulers (and actions into) take new ones and do the (new) job.
31 import sys, os
32 import getopt
33 import ConfigParser
35 ## Make sure people are using Python 2.5 or higher
36 if sys.version_info < (2,4):
37 print "Shinken requires as a minimum Python 2.4.x, sorry"
38 sys.exit(1)
39 elif sys.version_info >= (3,):
40 print "Shinken is not yet compatible with Python3k, sorry"
41 sys.exit(1)
44 #Pyro 4 dbg
45 sys.path.insert(0,'.')
48 #Try to load shinken lib.
49 #Maybe it's not in our python path, so we detect it
50 #it so (it's a untar install) we add .. in the path
51 try :
52 from shinken.util import to_bool
53 if hasattr(sys.modules['__main__'], '__file__'):
54 my_path = os.path.abspath(sys.modules['__main__'].__file__)
55 elts = os.path.dirname(my_path).split(os.sep)[:-1]
56 elts.append('shinken')
57 sys.path.append(os.sep.join(elts))
58 except ImportError:
59 if hasattr(sys.modules['__main__'], '__file__'):
60 #Now add in the python path the shinken lib
61 #if we launch it in a direct way and
62 #the shinken is not a python lib
63 my_path = os.path.abspath(sys.modules['__main__'].__file__)
64 elts = os.path.dirname(my_path).split(os.sep)[:-1]
65 sys.path.append(os.sep.join(elts))
66 elts.append('shinken')
67 sys.path.append(os.sep.join(elts))
69 try:
70 import shinken.pyro_wrapper
71 except ImportError:
72 print "Shinken require the Python Pyro module. Please install it."
73 sys.exit(1)
75 Pyro = shinken.pyro_wrapper.Pyro
78 from shinken.satellite import Satellite
79 from shinken.util import to_int, to_bool
81 VERSION = "0.4"
84 #Our main APP class
85 class Reactionner(Satellite):
86 do_checks = False #I do not do checks
87 do_actions = True #just actions like notifications
88 #default_port = 7769
90 properties = {
91 'workdir' : {'default' : '/usr/local/shinken/var', 'pythonize' : None, 'path' : True},
92 'pidfile' : {'default' : '/usr/local/shinken/var/reactionnerd.pid', 'pythonize' : None, 'path' : True},
93 'port' : {'default' : '7769', 'pythonize' : to_int},
94 'host' : {'default' : '0.0.0.0', 'pythonize' : None},
95 'user' : {'default' : 'shinken', 'pythonize' : None},
96 'group' : {'default' : 'shinken', 'pythonize' : None},
97 'idontcareaboutsecurity' : {'default' : '0', 'pythonize' : to_bool}
101 ################### Process launch part
102 def usage(name):
103 print "Shinken Reactionner Daemon, version %s, from :" % VERSION
104 print " Gabes Jean, naparuba@gmail.com"
105 print " Gerhard Lausser, Gerhard.Lausser@consol.de"
106 print "Usage: %s [options] [-c configfile]" % name
107 print "Options:"
108 print " -c, --config"
109 print "\tConfig file."
110 print " -d, --daemon"
111 print "\tRun in daemon mode"
112 print " -r, --replace"
113 print "\tReplace previous running scheduler"
114 print " -h, --help"
115 print "\tPrint detailed help screen"
116 print " --debug"
117 print "\tDebug File. Default : no use (why debug a bug free program? :) )"
121 #lets go to the party
122 if __name__ == "__main__":
123 #Manage the options
124 try:
125 opts, args = getopt.getopt(sys.argv[1:], "hrdc::w", ["help", "replace", "daemon", "config=", "debug=", "easter"])
126 except getopt.GetoptError, err:
127 # print help information and exit:
128 print str(err) # will print something like "option -a not recognized"
129 usage(sys.argv[0])
130 sys.exit(2)
131 #Default params
132 config_file = None
133 is_daemon=False
134 do_replace=False
135 debug=False
136 debug_file=None
137 for o, a in opts:
138 if o in ("-h", "--help"):
139 usage(sys.argv[0])
140 sys.exit()
141 elif o in ("-r", "--replace"):
142 do_replace = True
143 elif o in ("-c", "--config"):
144 config_file = a
145 elif o in ("-d", "--daemon"):
146 is_daemon = True
147 elif o in ("--debug"):
148 debug = True
149 debug_file = a
150 else:
151 print "Sorry, the option",o, a, "is unknown"
152 usage(sys.argv[0])
153 sys.exit()
156 p = Reactionner(config_file, is_daemon, do_replace, debug, debug_file)
157 #import cProfile
158 p.main()
159 #command = """p.main()"""
160 #cProfile.runctx( command, globals(), locals(), filename="var/Shinken.profile" )