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 #first verion of this file from Maximilien Bersoult
24 from setuptools
import setup
, find_packages
31 from pwd
import getpwnam
32 from grp
import getgrnam
34 #Some global variables
35 install_scripts_path
= "/usr/bin" #where to install launch scripts. Beter in PATH ;)
36 root_path
= "/" #if the setup.py is call with root, get it
38 #We know that a Python 2.3 or Python3K will fail.
39 #We can say why and quit.
41 python_version
= tuple((int(s
) for s
in platform
.python_version_tuple()))
43 ## Make sure people are using Python 2.3 or higher
44 if python_version
< (2, 4):
45 print "Shinken require as a minimum Python 2.4.x, sorry"
48 if python_version
< (3):
49 print "Shinken is not yet compatible with Python3k, sorry"
53 script
= os
.path
.basename(path
)
59 #Set the default values for the paths
62 paths_and_owners
= {'var' : {'path' : "c:\\shinken\\var", 'owner' : None, 'group' : None },
63 'etc' : {'path' : "c:\\shinken\\etc", 'owner' : None, 'group' : None},
64 'libexec' : {'path' : "c:\\shinken\\libexec", 'owner' : None, 'group' : None},
67 paths_and_owners
= {'var' : {'path' : "/var/lib/shinken/", 'owner' : 'shinken', 'group' : 'shinken' },
68 'etc' : {'path' : "/etc/shinken", 'owner' : 'shinken', 'group' : 'shinken'},
69 'libexec' : {'path' : "/usr/lib/shinken/plugins", 'owner' : 'shinken', 'group' : 'shinken'},
74 #The default file must have good values for the directories:
75 #etc, var and where to push scripts that launch the app.
76 def generate_default_shinken_file():
77 global paths_and_owners
78 global install_scripts_path
80 #Read the in file, it's our template
81 f
= open("bin/default/shinken.in")
85 #then generate the new one with good values
86 etc_path
= paths_and_owners
['etc']['path']
87 var_path
= paths_and_owners
['var']['path']
88 f
= open("bin/default/shinken", "w")
89 buf
= buf
.replace("$ETC$", etc_path
)
90 buf
= buf
.replace("$VAR$", var_path
)
91 buf
= buf
.replace("$SCRIPTS_BIN$", install_scripts_path
)
96 def parse_config_file(config_file
):
97 global paths_and_owners
99 config
= ConfigParser
.ConfigParser()
100 config
.read(config_file
)
101 if config
._sections
== {}:
102 print "Bad or missing config file : %s " % config_file
105 for dir in ['var', 'etc', 'libexec']:
106 paths_and_owners
[dir]['path'] = config
._sections
[dir]['path']
110 paths_and_owners
[dir]['owner'] = config
._sections
[dir]['owner']
111 paths_and_owners
[dir]['group'] = config
._sections
[dir]['group']
114 #I search for the --install-scripts= parameter if present to modify the
115 #default/shinken file
117 print "Argument", arg
118 if re
.search("--install-scripts=", arg
):
119 elts
= arg
.split('=')
121 install_scripts_path
= elts
[1]
122 print "Install script path", install_scripts_path
123 if re
.search("--root=", arg
):
124 elts
= arg
.split('=')
127 print "New root path", root_path
130 #Get the paths and ownsers form the parameter file
131 #and the generate the default/shinken file so the init.d
132 #scripts will have the good values for directories
133 parse_config_file('setup_parameters.cfg')
134 generate_default_shinken_file()
136 etc_path
= paths_and_owners
['etc']['path']
137 var_path
= paths_and_owners
['var']['path']
138 libexec_path
= paths_and_owners
['libexec']['path']
141 if python_version
< (2, 5):
142 required_pkgs
.append('pyro<4')
144 required_pkgs
.append('pyro')
145 if python_version
< (2, 6):
146 required_pkgs
.append('multiprocessing')
151 packages
= find_packages(),
152 package_data
= {'':['*.py','modules/*.py','modules/*/*.py']},
153 description
= "Shinken is a monitoring tool compatible with Nagios configuration and plugins",
154 long_description
=open('README').read(),
155 author
= "Gabes Jean",
156 author_email
= "naparuba@gmail.com",
157 license
= "GNU Affero General Public License",
158 url
= "http://www.shinken-monitoring.org",
160 classifiers
=['Development Status :: 4 - Beta',
161 'Environment :: Console',
162 'Intended Audience :: System Administrators',
163 'License :: OSI Approved :: GNU Affero General Public License v3',
164 'Operating System :: MacOS :: MacOS X',
165 'Operating System :: Microsoft :: Windows',
166 'Operating System :: POSIX',
167 'Programming Language :: Python',
168 'Topic :: System :: Monitoring',
169 'Topic :: System :: Networking :: Monitoring',
176 scripts
= [f
for f
in glob('bin/[!_]*.py')],
177 data_files
=[(etc_path
, ["etc/nagios.cfg",'etc/brokerd.ini', 'etc/brokerd-windows.ini',
178 'etc/commons.cfg', 'etc/conf-windows.cfg', 'etc/host-150.cfg',
179 'etc/services-150h-1500srv.cfg',
180 'etc/nagios.cfg', 'etc/nagios-windows.cfg', 'etc/pollerd.ini',
181 'etc/reactionnerd.ini', 'etc/resource.cfg', 'etc/schedulerd.ini',
182 'etc/schedulerd-windows.ini', 'etc/pollerd-windows.ini',
183 'etc/shinken-specific.cfg', 'etc/shinken-specific-high-availability.cfg',
184 'etc/shinken-specific-load-balanced-only.cfg'
186 ('/etc/init.d', ['bin/init.d/shinken-arbiter', 'bin/init.d/shinken-broker', 'bin/init.d/shinken-poller',
187 'bin/init.d/shinken-reactionner', 'bin/init.d/shinken-scheduler']),
188 ('/etc/default/', ['bin/default/shinken']),
189 (var_path
, ['var/void_for_git']),
190 (libexec_path
, ['libexec/check.sh']),
196 #Ok now the less good part :(
197 #I didn't find any easy way to get it :(
198 #We will chown shinken:shinken for all /etc/shinken
199 #and /var/lib/shinken.
200 def get_uid(user_name
):
202 return getpwnam(user_name
)[2]
203 except KeyError, exp
:
204 print "Error: the user", user_name
, "is unknown"
205 print "Maybe you should create this user"
209 def get_gid(group_name
):
211 return getgrnam(group_name
)[2]
212 except KeyError, exp
:
213 print "Error: the group",group_name
, "is unknown"
214 print "Maybe you should create this group"
218 #Open a /etc/*d.ini file and change the ../var occurence with a good value
219 #from the configuration file
220 def update_ini_file_with_var(path
):
221 global paths_and_owners
222 var_path
= paths_and_owners
['var']['path']
223 update_file_with_string(path
, "../var", var_path
)
226 #Replace the libexec path in common.cfg by the one in the parameter file
227 def update_resource_cfg_file_with_libexec(path
):
228 global paths_and_owners
229 libexec_path
= paths_and_owners
['libexec']['path']
230 update_file_with_string(path
, "/usr/local/shinken/libexec", libexec_path
)
233 #Replace paths in nagios.cfg file
234 def update_cfg_file_with_var_path(path
):
235 global paths_and_owners
236 var_path
= paths_and_owners
['var']['path']
237 update_file_with_string(path
, "/usr/local/shinken/var", var_path
)
242 #Replace the libexec path in common.cfg by the one in the parameter file
243 def update_file_with_string(path
, match
, new_string
):
248 buf
= buf
.replace(match
, new_string
)
254 #If there is another root, it's strange, it must be a special case...
255 if os
.name
!= 'nt' and ('install' in sys
.argv
or 'sdist' in sys
.argv
) and re
.search("--root", ' '.join(sys
.argv
)) == None:
256 for g_dir
in ['etc', 'var']:
257 path
= paths_and_owners
[g_dir
]['path']
258 owner
= paths_and_owners
[g_dir
]['owner']
259 group
= paths_and_owners
[g_dir
]['group']
262 for root
, dirs
, files
in os
.walk(path
):
263 print "Changing the directory", root
, "by", owner
, ":", group
264 os
.chown(root
, uid
, gui
)
266 print "Change owner of the directory", root
+os
.sep
+dir, "by", owner
, ":", group
267 os
.chown(root
+os
.sep
+dir,uid
, gui
)
269 print "Change owner of the file", root
+os
.sep
+name
, "by", owner
, ":", group
270 os
.chown(root
+os
.sep
+name
,uid
, gui
)
274 #Here, even with --root we should change the file with good values
275 if os
.name
!= 'nt' and ('install' in sys
.argv
or 'sdist' in sys
.argv
):
276 #then update the /etc/*d.ini files ../var value with the real var one
277 print "Now updating the /etc/shinken/*d/ini files with the good value for var"
278 update_ini_file_with_var(os
.sep
.join([root_path
, etc_path
, 'brokerd.ini']))
279 update_ini_file_with_var(os
.sep
.join([root_path
, etc_path
, 'schedulerd.ini']))
280 update_ini_file_with_var(os
.sep
.join([root_path
, etc_path
, 'pollerd.ini']))
281 update_ini_file_with_var(os
.sep
.join([root_path
, etc_path
, 'reactionnerd.ini']))
283 #And now the resource.cfg path with the value of libexec path
284 print "Now updating the /etc/shinken/resource.cfg file with good libexec path"
285 update_resource_cfg_file_with_libexec(os
.sep
.join([root_path
, etc_path
, 'resource.cfg']))
287 #And update the nagios.cfg file for all /usr/local/shinken/var value with good one
288 print "Now updating the /etc/shinken/nagios.cfg file with good var path"
289 update_cfg_file_with_var_path(os
.sep
.join([root_path
, etc_path
, 'nagios.cfg']))
290 update_cfg_file_with_var_path(os
.sep
.join([root_path
, etc_path
, 'shinken-specific.cfg']))