Show More
@@ -1,693 +1,714 b'' | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | ||
|
3 | # Copyright (C) 2017-2017 RhodeCode GmbH | |
|
4 | # | |
|
5 | # This program is free software: you can redistribute it and/or modify | |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
|
7 | # (only), as published by the Free Software Foundation. | |
|
8 | # | |
|
9 | # This program is distributed in the hope that it will be useful, | |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
12 | # GNU General Public License for more details. | |
|
13 | # | |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
|
16 | # | |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
|
20 | ||
|
21 | ||
|
1 | 22 | import os |
|
2 | 23 | import sys |
|
3 | 24 | import time |
|
4 | 25 | import platform |
|
5 | 26 | import pkg_resources |
|
6 | 27 | import logging |
|
7 | 28 | import string |
|
8 | 29 | |
|
9 | 30 | |
|
10 | 31 | log = logging.getLogger(__name__) |
|
11 | 32 | |
|
12 | 33 | |
|
13 | 34 | psutil = None |
|
14 | 35 | |
|
15 | 36 | try: |
|
16 | 37 | # cygwin cannot have yet psutil support. |
|
17 | 38 | import psutil as psutil |
|
18 | 39 | except ImportError: |
|
19 | 40 | pass |
|
20 | 41 | |
|
21 | 42 | |
|
22 | 43 | _NA = 'NOT AVAILABLE' |
|
23 | 44 | |
|
24 | 45 | STATE_OK = 'ok' |
|
25 | 46 | STATE_ERR = 'error' |
|
26 | 47 | STATE_WARN = 'warning' |
|
27 | 48 | |
|
28 | 49 | STATE_OK_DEFAULT = {'message': '', 'type': STATE_OK} |
|
29 | 50 | |
|
30 | 51 | |
|
31 | 52 | # HELPERS |
|
32 | 53 | def percentage(part, whole): |
|
33 | 54 | whole = float(whole) |
|
34 | 55 | if whole > 0: |
|
35 | 56 | return round(100 * float(part) / whole, 1) |
|
36 | 57 | return 0.0 |
|
37 | 58 | |
|
38 | 59 | |
|
39 | 60 | def get_storage_size(storage_path): |
|
40 | 61 | sizes = [] |
|
41 | 62 | for file_ in os.listdir(storage_path): |
|
42 | 63 | storage_file = os.path.join(storage_path, file_) |
|
43 | 64 | if os.path.isfile(storage_file): |
|
44 | 65 | try: |
|
45 | 66 | sizes.append(os.path.getsize(storage_file)) |
|
46 | 67 | except OSError: |
|
47 | 68 | log.exception('Failed to get size of storage file %s', |
|
48 | 69 | storage_file) |
|
49 | 70 | pass |
|
50 | 71 | |
|
51 | 72 | return sum(sizes) |
|
52 | 73 | |
|
53 | 74 | |
|
54 | 75 | class SysInfoRes(object): |
|
55 | 76 | def __init__(self, value, state=STATE_OK_DEFAULT, human_value=None): |
|
56 | 77 | self.value = value |
|
57 | 78 | self.state = state |
|
58 | 79 | self.human_value = human_value or value |
|
59 | 80 | |
|
60 | 81 | def __json__(self): |
|
61 | 82 | return { |
|
62 | 83 | 'value': self.value, |
|
63 | 84 | 'state': self.state, |
|
64 | 85 | 'human_value': self.human_value, |
|
65 | 86 | } |
|
66 | 87 | |
|
67 | 88 | def get_value(self): |
|
68 | 89 | return self.__json__() |
|
69 | 90 | |
|
70 | 91 | def __str__(self): |
|
71 | 92 | return '<SysInfoRes({})>'.format(self.__json__()) |
|
72 | 93 | |
|
73 | 94 | |
|
74 | 95 | class SysInfo(object): |
|
75 | 96 | |
|
76 | 97 | def __init__(self, func_name, **kwargs): |
|
77 | 98 | self.func_name = func_name |
|
78 | 99 | self.value = _NA |
|
79 | 100 | self.state = None |
|
80 | 101 | self.kwargs = kwargs or {} |
|
81 | 102 | |
|
82 | 103 | def __call__(self): |
|
83 | 104 | computed = self.compute(**self.kwargs) |
|
84 | 105 | if not isinstance(computed, SysInfoRes): |
|
85 | 106 | raise ValueError( |
|
86 | 107 | 'computed value for {} is not instance of ' |
|
87 | 108 | '{}, got {} instead'.format( |
|
88 | 109 | self.func_name, SysInfoRes, type(computed))) |
|
89 | 110 | return computed.__json__() |
|
90 | 111 | |
|
91 | 112 | def __str__(self): |
|
92 | 113 | return '<SysInfo({})>'.format(self.func_name) |
|
93 | 114 | |
|
94 | 115 | def compute(self, **kwargs): |
|
95 | 116 | return self.func_name(**kwargs) |
|
96 | 117 | |
|
97 | 118 | |
|
98 | 119 | # SysInfo functions |
|
99 | 120 | def python_info(): |
|
100 | 121 | value = dict(version=' '.join(platform._sys_version()), |
|
101 | 122 | executable=sys.executable) |
|
102 | 123 | return SysInfoRes(value=value) |
|
103 | 124 | |
|
104 | 125 | |
|
105 | 126 | def py_modules(): |
|
106 | 127 | mods = dict([(p.project_name, p.version) |
|
107 | 128 | for p in pkg_resources.working_set]) |
|
108 | 129 | value = sorted(mods.items(), key=lambda k: k[0].lower()) |
|
109 | 130 | return SysInfoRes(value=value) |
|
110 | 131 | |
|
111 | 132 | |
|
112 | 133 | def platform_type(): |
|
113 | 134 | from rhodecode.lib.utils import safe_unicode, generate_platform_uuid |
|
114 | 135 | |
|
115 | 136 | value = dict( |
|
116 | 137 | name=safe_unicode(platform.platform()), |
|
117 | 138 | uuid=generate_platform_uuid() |
|
118 | 139 | ) |
|
119 | 140 | return SysInfoRes(value=value) |
|
120 | 141 | |
|
121 | 142 | |
|
122 | 143 | def uptime(): |
|
123 | 144 | from rhodecode.lib.helpers import age, time_to_datetime |
|
124 | 145 | from rhodecode.translation import TranslationString |
|
125 | 146 | |
|
126 | 147 | value = dict(boot_time=0, uptime=0, text='') |
|
127 | 148 | state = STATE_OK_DEFAULT |
|
128 | 149 | if not psutil: |
|
129 | 150 | return SysInfoRes(value=value, state=state) |
|
130 | 151 | |
|
131 | 152 | boot_time = psutil.boot_time() |
|
132 | 153 | value['boot_time'] = boot_time |
|
133 | 154 | value['uptime'] = time.time() - boot_time |
|
134 | 155 | |
|
135 | 156 | date_or_age = age(time_to_datetime(boot_time)) |
|
136 | 157 | if isinstance(date_or_age, TranslationString): |
|
137 | 158 | date_or_age = date_or_age.interpolate() |
|
138 | 159 | |
|
139 | 160 | human_value = value.copy() |
|
140 | 161 | human_value['boot_time'] = time_to_datetime(boot_time) |
|
141 | 162 | human_value['uptime'] = age(time_to_datetime(boot_time), show_suffix=False) |
|
142 | 163 | |
|
143 | 164 | human_value['text'] = u'Server started {}'.format(date_or_age) |
|
144 | 165 | return SysInfoRes(value=value, human_value=human_value) |
|
145 | 166 | |
|
146 | 167 | |
|
147 | 168 | def memory(): |
|
148 | 169 | from rhodecode.lib.helpers import format_byte_size_binary |
|
149 | 170 | value = dict(available=0, used=0, used_real=0, cached=0, percent=0, |
|
150 | 171 | percent_used=0, free=0, inactive=0, active=0, shared=0, |
|
151 | 172 | total=0, buffers=0, text='') |
|
152 | 173 | |
|
153 | 174 | state = STATE_OK_DEFAULT |
|
154 | 175 | if not psutil: |
|
155 | 176 | return SysInfoRes(value=value, state=state) |
|
156 | 177 | |
|
157 | 178 | value.update(dict(psutil.virtual_memory()._asdict())) |
|
158 | 179 | value['used_real'] = value['total'] - value['available'] |
|
159 | 180 | value['percent_used'] = psutil._common.usage_percent( |
|
160 | 181 | value['used_real'], value['total'], 1) |
|
161 | 182 | |
|
162 | 183 | human_value = value.copy() |
|
163 | 184 | human_value['text'] = '%s/%s, %s%% used' % ( |
|
164 | 185 | format_byte_size_binary(value['used_real']), |
|
165 | 186 | format_byte_size_binary(value['total']), |
|
166 | 187 | value['percent_used'],) |
|
167 | 188 | |
|
168 | 189 | keys = value.keys()[::] |
|
169 | 190 | keys.pop(keys.index('percent')) |
|
170 | 191 | keys.pop(keys.index('percent_used')) |
|
171 | 192 | keys.pop(keys.index('text')) |
|
172 | 193 | for k in keys: |
|
173 | 194 | human_value[k] = format_byte_size_binary(value[k]) |
|
174 | 195 | |
|
175 | 196 | if state['type'] == STATE_OK and value['percent_used'] > 90: |
|
176 | 197 | msg = 'Critical: your available RAM memory is very low.' |
|
177 | 198 | state = {'message': msg, 'type': STATE_ERR} |
|
178 | 199 | |
|
179 | 200 | elif state['type'] == STATE_OK and value['percent_used'] > 70: |
|
180 | 201 | msg = 'Warning: your available RAM memory is running low.' |
|
181 | 202 | state = {'message': msg, 'type': STATE_WARN} |
|
182 | 203 | |
|
183 | 204 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
184 | 205 | |
|
185 | 206 | |
|
186 | 207 | def machine_load(): |
|
187 | 208 | value = {'1_min': _NA, '5_min': _NA, '15_min': _NA, 'text': ''} |
|
188 | 209 | state = STATE_OK_DEFAULT |
|
189 | 210 | if not psutil: |
|
190 | 211 | return SysInfoRes(value=value, state=state) |
|
191 | 212 | |
|
192 | 213 | # load averages |
|
193 | 214 | if hasattr(psutil.os, 'getloadavg'): |
|
194 | 215 | value.update(dict( |
|
195 | 216 | zip(['1_min', '5_min', '15_min'], psutil.os.getloadavg()))) |
|
196 | 217 | |
|
197 | 218 | human_value = value.copy() |
|
198 | 219 | human_value['text'] = '1min: {}, 5min: {}, 15min: {}'.format( |
|
199 | 220 | value['1_min'], value['5_min'], value['15_min']) |
|
200 | 221 | |
|
201 | 222 | if state['type'] == STATE_OK and value['15_min'] > 5: |
|
202 | 223 | msg = 'Warning: your machine load is very high.' |
|
203 | 224 | state = {'message': msg, 'type': STATE_WARN} |
|
204 | 225 | |
|
205 | 226 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
206 | 227 | |
|
207 | 228 | |
|
208 | 229 | def cpu(): |
|
209 | 230 | value = {'cpu': 0, 'cpu_count': 0, 'cpu_usage': []} |
|
210 | 231 | state = STATE_OK_DEFAULT |
|
211 | 232 | |
|
212 | 233 | if not psutil: |
|
213 | 234 | return SysInfoRes(value=value, state=state) |
|
214 | 235 | |
|
215 | 236 | value['cpu'] = psutil.cpu_percent(0.5) |
|
216 | 237 | value['cpu_usage'] = psutil.cpu_percent(0.5, percpu=True) |
|
217 | 238 | value['cpu_count'] = psutil.cpu_count() |
|
218 | 239 | |
|
219 | 240 | human_value = value.copy() |
|
220 | 241 | human_value['text'] = '{} cores at {} %'.format( |
|
221 | 242 | value['cpu_count'], value['cpu']) |
|
222 | 243 | |
|
223 | 244 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
224 | 245 | |
|
225 | 246 | |
|
226 | 247 | def storage(): |
|
227 | 248 | from rhodecode.lib.helpers import format_byte_size_binary |
|
228 | 249 | from rhodecode.model.settings import VcsSettingsModel |
|
229 | 250 | path = VcsSettingsModel().get_repos_location() |
|
230 | 251 | |
|
231 | 252 | value = dict(percent=0, used=0, total=0, path=path, text='') |
|
232 | 253 | state = STATE_OK_DEFAULT |
|
233 | 254 | if not psutil: |
|
234 | 255 | return SysInfoRes(value=value, state=state) |
|
235 | 256 | |
|
236 | 257 | try: |
|
237 | 258 | value.update(dict(psutil.disk_usage(path)._asdict())) |
|
238 | 259 | except Exception as e: |
|
239 | 260 | log.exception('Failed to fetch disk info') |
|
240 | 261 | state = {'message': str(e), 'type': STATE_ERR} |
|
241 | 262 | |
|
242 | 263 | human_value = value.copy() |
|
243 | 264 | human_value['used'] = format_byte_size_binary(value['used']) |
|
244 | 265 | human_value['total'] = format_byte_size_binary(value['total']) |
|
245 | 266 | human_value['text'] = "{}/{}, {}% used".format( |
|
246 | 267 | format_byte_size_binary(value['used']), |
|
247 | 268 | format_byte_size_binary(value['total']), |
|
248 | 269 | value['percent']) |
|
249 | 270 | |
|
250 | 271 | if state['type'] == STATE_OK and value['percent'] > 90: |
|
251 | 272 | msg = 'Critical: your disk space is very low.' |
|
252 | 273 | state = {'message': msg, 'type': STATE_ERR} |
|
253 | 274 | |
|
254 | 275 | elif state['type'] == STATE_OK and value['percent'] > 70: |
|
255 | 276 | msg = 'Warning: your disk space is running low.' |
|
256 | 277 | state = {'message': msg, 'type': STATE_WARN} |
|
257 | 278 | |
|
258 | 279 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
259 | 280 | |
|
260 | 281 | |
|
261 | 282 | def storage_inodes(): |
|
262 | 283 | from rhodecode.model.settings import VcsSettingsModel |
|
263 | 284 | path = VcsSettingsModel().get_repos_location() |
|
264 | 285 | |
|
265 | 286 | value = dict(percent=0, free=0, used=0, total=0, path=path, text='') |
|
266 | 287 | state = STATE_OK_DEFAULT |
|
267 | 288 | if not psutil: |
|
268 | 289 | return SysInfoRes(value=value, state=state) |
|
269 | 290 | |
|
270 | 291 | try: |
|
271 | 292 | i_stat = os.statvfs(path) |
|
272 | 293 | value['free'] = i_stat.f_ffree |
|
273 | 294 | value['used'] = i_stat.f_files-i_stat.f_favail |
|
274 | 295 | value['total'] = i_stat.f_files |
|
275 | 296 | value['percent'] = percentage(value['used'], value['total']) |
|
276 | 297 | except Exception as e: |
|
277 | 298 | log.exception('Failed to fetch disk inodes info') |
|
278 | 299 | state = {'message': str(e), 'type': STATE_ERR} |
|
279 | 300 | |
|
280 | 301 | human_value = value.copy() |
|
281 | 302 | human_value['text'] = "{}/{}, {}% used".format( |
|
282 | 303 | value['used'], value['total'], value['percent']) |
|
283 | 304 | |
|
284 | 305 | if state['type'] == STATE_OK and value['percent'] > 90: |
|
285 | 306 | msg = 'Critical: your disk free inodes are very low.' |
|
286 | 307 | state = {'message': msg, 'type': STATE_ERR} |
|
287 | 308 | |
|
288 | 309 | elif state['type'] == STATE_OK and value['percent'] > 70: |
|
289 | 310 | msg = 'Warning: your disk free inodes are running low.' |
|
290 | 311 | state = {'message': msg, 'type': STATE_WARN} |
|
291 | 312 | |
|
292 | 313 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
293 | 314 | |
|
294 | 315 | |
|
295 | 316 | def storage_archives(): |
|
296 | 317 | import rhodecode |
|
297 | 318 | from rhodecode.lib.utils import safe_str |
|
298 | 319 | from rhodecode.lib.helpers import format_byte_size_binary |
|
299 | 320 | |
|
300 | 321 | msg = 'Enable this by setting ' \ |
|
301 | 322 | 'archive_cache_dir=/path/to/cache option in the .ini file' |
|
302 | 323 | path = safe_str(rhodecode.CONFIG.get('archive_cache_dir', msg)) |
|
303 | 324 | |
|
304 | 325 | value = dict(percent=0, used=0, total=0, items=0, path=path, text='') |
|
305 | 326 | state = STATE_OK_DEFAULT |
|
306 | 327 | try: |
|
307 | 328 | items_count = 0 |
|
308 | 329 | used = 0 |
|
309 | 330 | for root, dirs, files in os.walk(path): |
|
310 | 331 | if root == path: |
|
311 | 332 | items_count = len(files) |
|
312 | 333 | |
|
313 | 334 | for f in files: |
|
314 | 335 | try: |
|
315 | 336 | used += os.path.getsize(os.path.join(root, f)) |
|
316 | 337 | except OSError: |
|
317 | 338 | pass |
|
318 | 339 | value.update({ |
|
319 | 340 | 'percent': 100, |
|
320 | 341 | 'used': used, |
|
321 | 342 | 'total': used, |
|
322 | 343 | 'items': items_count |
|
323 | 344 | }) |
|
324 | 345 | |
|
325 | 346 | except Exception as e: |
|
326 | 347 | log.exception('failed to fetch archive cache storage') |
|
327 | 348 | state = {'message': str(e), 'type': STATE_ERR} |
|
328 | 349 | |
|
329 | 350 | human_value = value.copy() |
|
330 | 351 | human_value['used'] = format_byte_size_binary(value['used']) |
|
331 | 352 | human_value['total'] = format_byte_size_binary(value['total']) |
|
332 | 353 | human_value['text'] = "{} ({} items)".format( |
|
333 | 354 | human_value['used'], value['items']) |
|
334 | 355 | |
|
335 | 356 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
336 | 357 | |
|
337 | 358 | |
|
338 | 359 | def storage_gist(): |
|
339 | 360 | from rhodecode.model.gist import GIST_STORE_LOC |
|
340 | 361 | from rhodecode.model.settings import VcsSettingsModel |
|
341 | 362 | from rhodecode.lib.utils import safe_str |
|
342 | 363 | from rhodecode.lib.helpers import format_byte_size_binary |
|
343 | 364 | path = safe_str(os.path.join( |
|
344 | 365 | VcsSettingsModel().get_repos_location(), GIST_STORE_LOC)) |
|
345 | 366 | |
|
346 | 367 | # gist storage |
|
347 | 368 | value = dict(percent=0, used=0, total=0, items=0, path=path, text='') |
|
348 | 369 | state = STATE_OK_DEFAULT |
|
349 | 370 | |
|
350 | 371 | try: |
|
351 | 372 | items_count = 0 |
|
352 | 373 | used = 0 |
|
353 | 374 | for root, dirs, files in os.walk(path): |
|
354 | 375 | if root == path: |
|
355 | 376 | items_count = len(dirs) |
|
356 | 377 | |
|
357 | 378 | for f in files: |
|
358 | 379 | try: |
|
359 | 380 | used += os.path.getsize(os.path.join(root, f)) |
|
360 | 381 | except OSError: |
|
361 | 382 | pass |
|
362 | 383 | value.update({ |
|
363 | 384 | 'percent': 100, |
|
364 | 385 | 'used': used, |
|
365 | 386 | 'total': used, |
|
366 | 387 | 'items': items_count |
|
367 | 388 | }) |
|
368 | 389 | except Exception as e: |
|
369 | 390 | log.exception('failed to fetch gist storage items') |
|
370 | 391 | state = {'message': str(e), 'type': STATE_ERR} |
|
371 | 392 | |
|
372 | 393 | human_value = value.copy() |
|
373 | 394 | human_value['used'] = format_byte_size_binary(value['used']) |
|
374 | 395 | human_value['total'] = format_byte_size_binary(value['total']) |
|
375 | 396 | human_value['text'] = "{} ({} items)".format( |
|
376 | 397 | human_value['used'], value['items']) |
|
377 | 398 | |
|
378 | 399 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
379 | 400 | |
|
380 | 401 | |
|
381 | 402 | def storage_temp(): |
|
382 | 403 | import tempfile |
|
383 | 404 | from rhodecode.lib.helpers import format_byte_size_binary |
|
384 | 405 | |
|
385 | 406 | path = tempfile.gettempdir() |
|
386 | 407 | value = dict(percent=0, used=0, total=0, items=0, path=path, text='') |
|
387 | 408 | state = STATE_OK_DEFAULT |
|
388 | 409 | |
|
389 | 410 | if not psutil: |
|
390 | 411 | return SysInfoRes(value=value, state=state) |
|
391 | 412 | |
|
392 | 413 | try: |
|
393 | 414 | value.update(dict(psutil.disk_usage(path)._asdict())) |
|
394 | 415 | except Exception as e: |
|
395 | 416 | log.exception('Failed to fetch temp dir info') |
|
396 | 417 | state = {'message': str(e), 'type': STATE_ERR} |
|
397 | 418 | |
|
398 | 419 | human_value = value.copy() |
|
399 | 420 | human_value['used'] = format_byte_size_binary(value['used']) |
|
400 | 421 | human_value['total'] = format_byte_size_binary(value['total']) |
|
401 | 422 | human_value['text'] = "{}/{}, {}% used".format( |
|
402 | 423 | format_byte_size_binary(value['used']), |
|
403 | 424 | format_byte_size_binary(value['total']), |
|
404 | 425 | value['percent']) |
|
405 | 426 | |
|
406 | 427 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
407 | 428 | |
|
408 | 429 | |
|
409 | 430 | def search_info(): |
|
410 | 431 | import rhodecode |
|
411 | 432 | from rhodecode.lib.index import searcher_from_config |
|
412 | 433 | |
|
413 | 434 | backend = rhodecode.CONFIG.get('search.module', '') |
|
414 | 435 | location = rhodecode.CONFIG.get('search.location', '') |
|
415 | 436 | |
|
416 | 437 | try: |
|
417 | 438 | searcher = searcher_from_config(rhodecode.CONFIG) |
|
418 | 439 | searcher = searcher.__class__.__name__ |
|
419 | 440 | except Exception: |
|
420 | 441 | searcher = None |
|
421 | 442 | |
|
422 | 443 | value = dict( |
|
423 | 444 | backend=backend, searcher=searcher, location=location, text='') |
|
424 | 445 | state = STATE_OK_DEFAULT |
|
425 | 446 | |
|
426 | 447 | human_value = value.copy() |
|
427 | 448 | human_value['text'] = "backend:`{}`".format(human_value['backend']) |
|
428 | 449 | |
|
429 | 450 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
430 | 451 | |
|
431 | 452 | |
|
432 | 453 | def git_info(): |
|
433 | 454 | from rhodecode.lib.vcs.backends import git |
|
434 | 455 | state = STATE_OK_DEFAULT |
|
435 | 456 | value = human_value = '' |
|
436 | 457 | try: |
|
437 | 458 | value = git.discover_git_version(raise_on_exc=True) |
|
438 | 459 | human_value = 'version reported from VCSServer: {}'.format(value) |
|
439 | 460 | except Exception as e: |
|
440 | 461 | state = {'message': str(e), 'type': STATE_ERR} |
|
441 | 462 | |
|
442 | 463 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
443 | 464 | |
|
444 | 465 | |
|
445 | 466 | def hg_info(): |
|
446 | 467 | from rhodecode.lib.vcs.backends import hg |
|
447 | 468 | state = STATE_OK_DEFAULT |
|
448 | 469 | value = human_value = '' |
|
449 | 470 | try: |
|
450 | 471 | value = hg.discover_hg_version(raise_on_exc=True) |
|
451 | 472 | human_value = 'version reported from VCSServer: {}'.format(value) |
|
452 | 473 | except Exception as e: |
|
453 | 474 | state = {'message': str(e), 'type': STATE_ERR} |
|
454 | 475 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
455 | 476 | |
|
456 | 477 | |
|
457 | 478 | def svn_info(): |
|
458 | 479 | from rhodecode.lib.vcs.backends import svn |
|
459 | 480 | state = STATE_OK_DEFAULT |
|
460 | 481 | value = human_value = '' |
|
461 | 482 | try: |
|
462 | 483 | value = svn.discover_svn_version(raise_on_exc=True) |
|
463 | 484 | human_value = 'version reported from VCSServer: {}'.format(value) |
|
464 | 485 | except Exception as e: |
|
465 | 486 | state = {'message': str(e), 'type': STATE_ERR} |
|
466 | 487 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
467 | 488 | |
|
468 | 489 | |
|
469 | 490 | def vcs_backends(): |
|
470 | 491 | import rhodecode |
|
471 | 492 | value = map( |
|
472 | 493 | string.strip, rhodecode.CONFIG.get('vcs.backends', '').split(',')) |
|
473 | 494 | human_value = 'Enabled backends in order: {}'.format(','.join(value)) |
|
474 | 495 | return SysInfoRes(value=value, human_value=human_value) |
|
475 | 496 | |
|
476 | 497 | |
|
477 | 498 | def vcs_server(): |
|
478 | 499 | import rhodecode |
|
479 | 500 | from rhodecode.lib.vcs.backends import get_vcsserver_service_data |
|
480 | 501 | |
|
481 | 502 | server_url = rhodecode.CONFIG.get('vcs.server') |
|
482 | 503 | enabled = rhodecode.CONFIG.get('vcs.server.enable') |
|
483 | 504 | protocol = rhodecode.CONFIG.get('vcs.server.protocol') or 'http' |
|
484 | 505 | state = STATE_OK_DEFAULT |
|
485 | 506 | version = None |
|
486 | 507 | workers = 0 |
|
487 | 508 | |
|
488 | 509 | try: |
|
489 | 510 | data = get_vcsserver_service_data() |
|
490 | 511 | if data and 'version' in data: |
|
491 | 512 | version = data['version'] |
|
492 | 513 | |
|
493 | 514 | if data and 'config' in data: |
|
494 | 515 | conf = data['config'] |
|
495 | 516 | workers = conf.get('workers', '?') |
|
496 | 517 | |
|
497 | 518 | connection = 'connected' |
|
498 | 519 | except Exception as e: |
|
499 | 520 | connection = 'failed' |
|
500 | 521 | state = {'message': str(e), 'type': STATE_ERR} |
|
501 | 522 | |
|
502 | 523 | value = dict( |
|
503 | 524 | url=server_url, |
|
504 | 525 | enabled=enabled, |
|
505 | 526 | protocol=protocol, |
|
506 | 527 | connection=connection, |
|
507 | 528 | version=version, |
|
508 | 529 | text='', |
|
509 | 530 | ) |
|
510 | 531 | |
|
511 | 532 | human_value = value.copy() |
|
512 | 533 | human_value['text'] = \ |
|
513 | 534 | '{url}@ver:{ver} via {mode} mode[workers:{workers}], connection:{conn}'.format( |
|
514 | 535 | url=server_url, ver=version, workers=workers, mode=protocol, |
|
515 | 536 | conn=connection) |
|
516 | 537 | |
|
517 | 538 | return SysInfoRes(value=value, state=state, human_value=human_value) |
|
518 | 539 | |
|
519 | 540 | |
|
520 | 541 | def rhodecode_app_info(): |
|
521 | 542 | import rhodecode |
|
522 | 543 | edition = rhodecode.CONFIG.get('rhodecode.edition') |
|
523 | 544 | |
|
524 | 545 | value = dict( |
|
525 | 546 | rhodecode_version=rhodecode.__version__, |
|
526 | 547 | rhodecode_lib_path=os.path.abspath(rhodecode.__file__), |
|
527 | 548 | text='' |
|
528 | 549 | ) |
|
529 | 550 | human_value = value.copy() |
|
530 | 551 | human_value['text'] = 'RhodeCode {edition}, version {ver}'.format( |
|
531 | 552 | edition=edition, ver=value['rhodecode_version'] |
|
532 | 553 | ) |
|
533 | 554 | return SysInfoRes(value=value, human_value=human_value) |
|
534 | 555 | |
|
535 | 556 | |
|
536 | 557 | def rhodecode_config(): |
|
537 | 558 | import rhodecode |
|
538 | 559 | import ConfigParser as configparser |
|
539 | 560 | path = rhodecode.CONFIG.get('__file__') |
|
540 | 561 | rhodecode_ini_safe = rhodecode.CONFIG.copy() |
|
541 | 562 | |
|
542 | 563 | try: |
|
543 | 564 | config = configparser.ConfigParser() |
|
544 | 565 | config.read(path) |
|
545 | 566 | parsed_ini = config |
|
546 | 567 | if parsed_ini.has_section('server:main'): |
|
547 | 568 | parsed_ini = dict(parsed_ini.items('server:main')) |
|
548 | 569 | except Exception: |
|
549 | 570 | log.exception('Failed to read .ini file for display') |
|
550 | 571 | parsed_ini = {} |
|
551 | 572 | |
|
552 | 573 | rhodecode_ini_safe['server:main'] = parsed_ini |
|
553 | 574 | |
|
554 | 575 | blacklist = [ |
|
555 | 576 | 'rhodecode_license_key', |
|
556 | 577 | 'routes.map', |
|
557 | 578 | 'pylons.h', |
|
558 | 579 | 'pylons.app_globals', |
|
559 | 580 | 'pylons.environ_config', |
|
560 | 581 | 'sqlalchemy.db1.url', |
|
561 | 582 | 'channelstream.secret', |
|
562 | 583 | 'beaker.session.secret', |
|
563 | 584 | 'rhodecode.encrypted_values.secret', |
|
564 | 585 | 'rhodecode_auth_github_consumer_key', |
|
565 | 586 | 'rhodecode_auth_github_consumer_secret', |
|
566 | 587 | 'rhodecode_auth_google_consumer_key', |
|
567 | 588 | 'rhodecode_auth_google_consumer_secret', |
|
568 | 589 | 'rhodecode_auth_bitbucket_consumer_secret', |
|
569 | 590 | 'rhodecode_auth_bitbucket_consumer_key', |
|
570 | 591 | 'rhodecode_auth_twitter_consumer_secret', |
|
571 | 592 | 'rhodecode_auth_twitter_consumer_key', |
|
572 | 593 | |
|
573 | 594 | 'rhodecode_auth_twitter_secret', |
|
574 | 595 | 'rhodecode_auth_github_secret', |
|
575 | 596 | 'rhodecode_auth_google_secret', |
|
576 | 597 | 'rhodecode_auth_bitbucket_secret', |
|
577 | 598 | |
|
578 | 599 | 'appenlight.api_key', |
|
579 | 600 | ('app_conf', 'sqlalchemy.db1.url') |
|
580 | 601 | ] |
|
581 | 602 | for k in blacklist: |
|
582 | 603 | if isinstance(k, tuple): |
|
583 | 604 | section, key = k |
|
584 | 605 | if section in rhodecode_ini_safe: |
|
585 | 606 | rhodecode_ini_safe[section] = '**OBFUSCATED**' |
|
586 | 607 | else: |
|
587 | 608 | rhodecode_ini_safe.pop(k, None) |
|
588 | 609 | |
|
589 | 610 | # TODO: maybe put some CONFIG checks here ? |
|
590 | 611 | return SysInfoRes(value={'config': rhodecode_ini_safe, 'path': path}) |
|
591 | 612 | |
|
592 | 613 | |
|
593 | 614 | def database_info(): |
|
594 | 615 | import rhodecode |
|
595 | 616 | from sqlalchemy.engine import url as engine_url |
|
596 | 617 | from rhodecode.model.meta import Base as sql_base, Session |
|
597 | 618 | from rhodecode.model.db import DbMigrateVersion |
|
598 | 619 | |
|
599 | 620 | state = STATE_OK_DEFAULT |
|
600 | 621 | |
|
601 | 622 | db_migrate = DbMigrateVersion.query().filter( |
|
602 | 623 | DbMigrateVersion.repository_id == 'rhodecode_db_migrations').one() |
|
603 | 624 | |
|
604 | 625 | db_url_obj = engine_url.make_url(rhodecode.CONFIG['sqlalchemy.db1.url']) |
|
605 | 626 | |
|
606 | 627 | try: |
|
607 | 628 | engine = sql_base.metadata.bind |
|
608 | 629 | db_server_info = engine.dialect._get_server_version_info( |
|
609 | 630 | Session.connection(bind=engine)) |
|
610 | 631 | db_version = '.'.join(map(str, db_server_info)) |
|
611 | 632 | except Exception: |
|
612 | 633 | log.exception('failed to fetch db version') |
|
613 | 634 | db_version = 'UNKNOWN' |
|
614 | 635 | |
|
615 | 636 | db_info = dict( |
|
616 | 637 | migrate_version=db_migrate.version, |
|
617 | 638 | type=db_url_obj.get_backend_name(), |
|
618 | 639 | version=db_version, |
|
619 | 640 | url=repr(db_url_obj) |
|
620 | 641 | ) |
|
621 | 642 | |
|
622 | 643 | human_value = db_info.copy() |
|
623 | 644 | human_value['url'] = "{} @ migration version: {}".format( |
|
624 | 645 | db_info['url'], db_info['migrate_version']) |
|
625 | 646 | human_value['version'] = "{} {}".format(db_info['type'], db_info['version']) |
|
626 | 647 | return SysInfoRes(value=db_info, state=state, human_value=human_value) |
|
627 | 648 | |
|
628 | 649 | |
|
629 | 650 | def server_info(environ): |
|
630 | 651 | import rhodecode |
|
631 | 652 | from rhodecode.lib.base import get_server_ip_addr, get_server_port |
|
632 | 653 | |
|
633 | 654 | value = { |
|
634 | 655 | 'server_ip': '%s:%s' % ( |
|
635 | 656 | get_server_ip_addr(environ, log_errors=False), |
|
636 | 657 | get_server_port(environ) |
|
637 | 658 | ), |
|
638 | 659 | 'server_id': rhodecode.CONFIG.get('instance_id'), |
|
639 | 660 | } |
|
640 | 661 | return SysInfoRes(value=value) |
|
641 | 662 | |
|
642 | 663 | |
|
643 | 664 | def usage_info(): |
|
644 | 665 | from rhodecode.model.db import User, Repository |
|
645 | 666 | value = { |
|
646 | 667 | 'users': User.query().count(), |
|
647 | 668 | 'users_active': User.query().filter(User.active == True).count(), |
|
648 | 669 | 'repositories': Repository.query().count(), |
|
649 | 670 | 'repository_types': { |
|
650 | 671 | 'hg': Repository.query().filter( |
|
651 | 672 | Repository.repo_type == 'hg').count(), |
|
652 | 673 | 'git': Repository.query().filter( |
|
653 | 674 | Repository.repo_type == 'git').count(), |
|
654 | 675 | 'svn': Repository.query().filter( |
|
655 | 676 | Repository.repo_type == 'svn').count(), |
|
656 | 677 | }, |
|
657 | 678 | } |
|
658 | 679 | return SysInfoRes(value=value) |
|
659 | 680 | |
|
660 | 681 | |
|
661 | 682 | def get_system_info(environ): |
|
662 | 683 | environ = environ or {} |
|
663 | 684 | return { |
|
664 | 685 | 'rhodecode_app': SysInfo(rhodecode_app_info)(), |
|
665 | 686 | 'rhodecode_config': SysInfo(rhodecode_config)(), |
|
666 | 687 | 'rhodecode_usage': SysInfo(usage_info)(), |
|
667 | 688 | 'python': SysInfo(python_info)(), |
|
668 | 689 | 'py_modules': SysInfo(py_modules)(), |
|
669 | 690 | |
|
670 | 691 | 'platform': SysInfo(platform_type)(), |
|
671 | 692 | 'server': SysInfo(server_info, environ=environ)(), |
|
672 | 693 | 'database': SysInfo(database_info)(), |
|
673 | 694 | |
|
674 | 695 | 'storage': SysInfo(storage)(), |
|
675 | 696 | 'storage_inodes': SysInfo(storage_inodes)(), |
|
676 | 697 | 'storage_archive': SysInfo(storage_archives)(), |
|
677 | 698 | 'storage_gist': SysInfo(storage_gist)(), |
|
678 | 699 | 'storage_temp': SysInfo(storage_temp)(), |
|
679 | 700 | |
|
680 | 701 | 'search': SysInfo(search_info)(), |
|
681 | 702 | |
|
682 | 703 | 'uptime': SysInfo(uptime)(), |
|
683 | 704 | 'load': SysInfo(machine_load)(), |
|
684 | 705 | 'cpu': SysInfo(cpu)(), |
|
685 | 706 | 'memory': SysInfo(memory)(), |
|
686 | 707 | |
|
687 | 708 | 'vcs_backends': SysInfo(vcs_backends)(), |
|
688 | 709 | 'vcs_server': SysInfo(vcs_server)(), |
|
689 | 710 | |
|
690 | 711 | 'git': SysInfo(git_info)(), |
|
691 | 712 | 'hg': SysInfo(hg_info)(), |
|
692 | 713 | 'svn': SysInfo(svn_info)(), |
|
693 | 714 | } |
General Comments 0
You need to be logged in to leave comments.
Login now