check_logfiles: 3.7.5.1
[omd.git] / packages / omd / index.py
blob508c967fef45e52f77d1746c2d6541f54694c88b
1 from mod_python import apache,util
2 import os, pwd, re
4 def site_name(req):
5 return os.path.normpath(req.uri).split("/")[1]
7 def get_version(sitename):
8 return [ l.split()[2] for l
9 in file("/omd/sites/%s/share/omd/omd.info" % sitename).readlines()
10 if l.startswith("OMD_VERSION") ][0]
12 def config_load(sitename):
13 confpath = "/omd/sites/%s/etc/omd/site.conf" % sitename
14 if not os.path.exists(confpath):
15 return {}
17 conf = {}
18 for line in file(confpath):
19 line = line.strip()
20 if line == "" or line[0] == "#":
21 continue
22 var, value = line.split("=", 1)
23 conf[var.strip()[7:]] = value.strip('"').strip("'")
24 return conf
26 def page_welcome(req):
27 req.headers_out.add("Cache-Control", "max-age=7200, public");
28 req.write("""
29 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
30 "http://www.w3.org/TR/html4/frameset.dtd">
31 <html>
32 <head>
33 <title>OMD - Open Monitoring Distribution</title>
34 <style>
35 body {
36 font-family:Verdana,sans-serif;
37 font-size:14px;
38 color:#484848;
39 margin:0;
40 padding:0;
41 min-width:900px;
43 h1 {
44 margin-top:20px;
45 margin-bottom:20px;
46 text-align:center;
48 div {
49 width:100%%;
50 padding:0;
51 margin:0;
53 div#body {
54 height:100%%;
55 width:700px;
56 margin:auto;
58 div.note {
59 border:1px #DADADA solid;
60 background-color:#FFFFDD;
61 padding:5px;
62 padding-left:15px
64 a.gui {
65 border:1px #DADADA solid;
66 height:193px;
67 display:block;
68 width:100%%;
69 color:#484848;
70 text-decoration:none;
71 padding:10px;
72 background-color:#EEEEEE;
73 margin-top:20px;
74 margin-bottom:20px;
75 overflow: hidden;
77 a.gui:hover {
78 background-color:#759FCF
80 a.gui h2 {
81 margin-top:0
83 a.gui img {
84 border:0;
85 float: right;
86 vertical-align:middle;
87 margin-left: 10px;
89 p.footer {
90 text-align:center;
92 </style>
93 </head>
94 <body>
95 <div id="body">
96 <h1>OMD - Open Monitoring Distribution</h1>
97 <b>Version: %s</b>
98 <p>This page gives you a central view on the available GUIs in OMD.
99 Just have a look and feel free to choose your favorite GUI. At the
100 bottom of this page you can find short instructions on how to change
101 the default GUI of OMD.</p>
102 """ % get_version(site_name(req)))
104 for id, title, path, desc in [ ('nagios', 'Classic Nagios GUI',
105 'share/nagios/htdocs',
106 'The classic Nagios GUI is based on CGI program written '
107 'in C. It retrieves its status information from <tt>status.dat</tt>. '
108 'This interface is not longer actively developed and does not perform '
109 'well in large installations.'),
110 ('check_mk', 'Check_MK Multisite',
111 'share/check_mk/web',
112 'Check_MK Multisite is a fast and flexible status GUI written '
113 'in Python. It supports user definable views and is able to '
114 'display the status of several sites in one combined view. It '
115 'uses MK Livestatus for getting the status data from the sites.'),
116 ('thruk', 'Thruk Monitoring Webinterface',
117 'share/thruk',
118 'Thruk is a complete rework of the classic interface '
119 'in Perl. While maintaining the original look and feel it '
120 'brings lots of improvements and new features. Just as Multisite '
121 'it uses MK Livestatus as backend and supports the visualization '
122 'of multiple sites.'),
123 ('icinga', 'Classic Icinga GUI',
124 'share/icinga/htdocs',
125 'Icinga\'s "classical" GUI is a derivate of the classical Nagios GUI '
126 'and has been directly evolved from the original '
127 'CGI programs in C. It has its own look and feel and brings useful '
128 'improvements. It is not bound to Icinga and can be used with the '
129 'other monitoring cores as well.'),
130 ('shinken', 'Shinken WebUI',
131 'share/lib/shinken',
132 'Shinken enhances the ok/non-ok concept of Nagios by adding '
133 'root problems and impacts. Monitored objects also can get a rank '
134 'identifying their importance to the business.'
135 'The Shinken WebUI was designed to visualize these new concepts '
136 'and can not be used with the other monitoring cores.'),
137 ('nagvis', 'NagVis - The visualization addon',
138 'share/nagvis/htdocs',
139 '<p>NagVis is the leading visualization addon for Nagios.</p>'
140 '<p>NagVis can be used to visualize Nagios status data, e.g. '
141 'to display IT processes like a mail system or a '
142 'network infrastructure.</p>'),
143 ('pnp4nagios', 'PNP4Nagios',
144 'share/pnp4nagios/htdocs',
145 'PNP is an addon to Nagios which analyzes performance data '
146 'provided by plugins and stores them automatically into '
147 'RRD-databases (Round Robin Databases, see RRDTool).'),
148 ('wiki', 'DokuWiki',
149 'share/dokuwiki/htdocs',
150 'DokuWiki is a standards compliant, simple to use Wiki, '
151 'mainly aimed at creating documentation of any kind.') ]:
153 if os.path.exists("/omd/sites/%s/%s" % (site_name(req), path)):
154 req.write("""
155 <a class="gui" href="../%s/">
156 <img src="img/%s-small.png" title="%s" />
157 <h2>%s</h2>
159 </a>
160 """ % (id, id, id, title, desc))
162 req.write("""
163 <div class="note">
164 <p>You can replace this page by logging into your sites system account
165 and execute the commands:</p>
166 <pre>omd stop
167 omd config</pre>
168 Then browse to &quot;Web Gui -&gt; DEFAULT_GUI&quot; entry in the list and hit enter. Select
169 the default GUI and quit all dialogs. After that start your site again by
170 executing:
171 <pre>
172 omd start
173 </pre>
174 </div>
175 <p class="footer">
176 Copyright (c) 2010-2011 OMD Team and Contributors -
177 <a href="http://omdistro.org/" title="Official OMD Homepage"
178 target="_blank">omdistro.org</a>
179 </p>
180 </div>
181 </body>
182 </html>""")
184 def load_process_env(req):
186 Load process env into regular python environment
187 Be aware of eventual lists as values
189 for k, v in dict(req.subprocess_env).iteritems():
190 if type(v) == list:
191 v = v[-1]
192 os.environ[k] = v
194 def handler(req):
195 req.content_type = "text/html; charset=UTF-8"
196 req.header_sent = False
197 req.myfile = req.uri.split("/")[-1][:-3]
199 if req.myfile == "error":
200 try:
201 show_apache_log(req)
202 return apache.OK
203 except Exception, e:
204 req.write("<html><body><h1>Internal Error</h1>Cannot output error log: %s</body></html>" % e)
205 return apache.OK
207 sitename = site_name(req)
208 config = config_load(sitename)
209 gui = 'DEFAULT_GUI' in config and config['DEFAULT_GUI'] or 'nagios'
211 load_process_env(req)
213 if gui == 'welcome':
214 page_welcome(req)
215 else:
216 gui_url = '/%s/%s/' % (sitename, gui)
217 util.redirect(req, gui_url)
219 return apache.OK
221 def omd_mode(req):
222 if os.environ.get('OMD_SITE', '') == pwd.getpwuid(os.getuid())[0]:
223 return 'own'
224 else:
225 return 'shared'
227 def show_apache_log(req):
228 if omd_mode(req) == 'own':
229 log_path = '/omd/sites/%s/var/log/apache/error_log' % site_name(req)
230 else:
231 log_path = None
233 req.write("<html><head><style>\n")
234 req.write("b.date { color: #888; font-weight: normal; }\n")
235 req.write("b.level.error { color: #f00; }\n")
236 req.write("b.level.notice { color: #8cc; }\n")
237 req.write("b.level { color: #cc0; }\n")
238 req.write("b.msg.error { background-color: #fcc; color: #c00; }\n")
239 req.write("b.msg.warn { background-color: #ffc; color: #880; }\n")
240 req.write("b.msg { font-weight: normal; }\n")
241 req.write("b.msg b.line { background-color: #fdd; color: black; }\n")
243 req.write("</style><body>\n")
244 req.write("<h1>Internal Server Error</h1>")
245 req.write("<p>An internal error occurred. Details can be found in the Apache error log")
246 if not log_path:
247 req.write(".")
248 else:
249 logfile = file(log_path)
250 lines = logfile.readlines()
251 if len(lines) > 30:
252 lines = lines[-30:]
254 req.write(" (%s)" % log_path)
255 req.write("Here are the last couple of lines from that log file:</p>")
256 req.write("<pre class=errorlog>\n")
257 for line in lines:
258 parts = line.split(']', 2)
259 if len(parts) < 3:
260 parts += [ "" ] * (3 - len(parts))
261 date = parts[0].lstrip('[').strip()
262 level = parts[1].strip().lstrip('[')
263 message = parts[2].strip()
264 message = re.sub("line ([0-9]+)", "line <b class=line>\\1</b>", message)
265 req.write("<b class=date>%s</b> <b class=\"level %s\">%s</b> <b class=\"msg %s\">%s</b>\n" %
266 (date, level, "%-7s" % level, level, message))
267 req.write("</pre>\n")
268 req.write("</body></html>\n")