|
15 | 15 |
|
16 | 16 | import fnmatch
|
17 | 17 | import inspect
|
| 18 | +import json |
18 | 19 | import logging
|
19 | 20 | import os
|
20 | 21 | import random
|
|
38 | 39 | from soscollector import __version__
|
39 | 40 | from soscollector.exceptions import ControlPersistUnsupportedException
|
40 | 41 |
|
| 42 | +COLLECTOR_LIB_DIR = '/var/lib/sos-collector' |
| 43 | + |
41 | 44 |
|
42 | 45 | class SosCollector():
|
43 | 46 | '''Main sos-collector class'''
|
@@ -287,6 +290,60 @@ def _fmt_msg(self, msg):
|
287 | 290 | _fmt = _fmt + fill(line, width, replace_whitespace=False) + '\n'
|
288 | 291 | return _fmt
|
289 | 292 |
|
| 293 | + def _load_group_config(self): |
| 294 | + ''' |
| 295 | + Attempts to load the host group specified on the command line. |
| 296 | + Host groups are defined via JSON files, typically saved under |
| 297 | + /var/lib/sos-collector/, although users can specify a full filepath |
| 298 | + on the commandline to point to one existing anywhere on the system |
| 299 | +
|
| 300 | + Host groups define a list of nodes and/or regexes and optionally the |
| 301 | + master and cluster-type options. |
| 302 | + ''' |
| 303 | + if os.path.exists(self.config['group']): |
| 304 | + fname = self.config['group'] |
| 305 | + elif os.path.exists( |
| 306 | + os.path.join(COLLECTOR_LIB_DIR, self.config['group']) |
| 307 | + ): |
| 308 | + fname = os.path.join(COLLECTOR_LIB_DIR, self.config['group']) |
| 309 | + else: |
| 310 | + raise OSError('Group not found') |
| 311 | + |
| 312 | + self.log_debug("Loading host group %s" % fname) |
| 313 | + |
| 314 | + with open(fname, 'r') as hf: |
| 315 | + _group = json.load(hf) |
| 316 | + for key in ['master', 'cluster_type']: |
| 317 | + if _group[key]: |
| 318 | + self.log_debug("Setting option '%s' to '%s' per host group" |
| 319 | + % (key, _group[key])) |
| 320 | + self.config[key] = _group[key] |
| 321 | + if _group['nodes']: |
| 322 | + self.log_debug("Adding %s to node list" % _group['nodes']) |
| 323 | + self.config['nodes'].extend(_group['nodes']) |
| 324 | + |
| 325 | + def write_host_group(self): |
| 326 | + ''' |
| 327 | + Saves the results of this run of sos-collector to a host group file |
| 328 | + on the system so it can be used later on. |
| 329 | +
|
| 330 | + The host group will save the options master, cluster_type, and nodes |
| 331 | + as determined by sos-collector prior to execution of sosreports. |
| 332 | + ''' |
| 333 | + cfg = { |
| 334 | + 'name': self.config['save_group'], |
| 335 | + 'master': self.config['master'], |
| 336 | + 'cluster_type': self.config['cluster_type'], |
| 337 | + 'nodes': [n for n in self.node_list] |
| 338 | + } |
| 339 | + if not os.path.isdir(COLLECTOR_LIB_DIR): |
| 340 | + raise OSError("%s no such directory" % COLLECTOR_LIB_DIR) |
| 341 | + fname = COLLECTOR_LIB_DIR + '/' + cfg['name'] |
| 342 | + with open(fname, 'w') as hf: |
| 343 | + json.dump(cfg, hf) |
| 344 | + os.chmod(fname, 0o644) |
| 345 | + return fname |
| 346 | + |
290 | 347 | def prep(self):
|
291 | 348 | '''Based on configuration, performs setup for collection'''
|
292 | 349 | disclaimer = ("""\
|
@@ -349,6 +406,13 @@ def prep(self):
|
349 | 406 | ' Ignoring request to change user on node')
|
350 | 407 | self.config['become_root'] = False
|
351 | 408 |
|
| 409 | + if self.config['group']: |
| 410 | + try: |
| 411 | + self._load_group_config() |
| 412 | + except Exception as err: |
| 413 | + self.log_error("Could not load specified group %s: %s" |
| 414 | + % (self.config['group'], err)) |
| 415 | + |
352 | 416 | if self.config['master']:
|
353 | 417 | self.connect_to_master()
|
354 | 418 | self.config['no_local'] = True
|
@@ -380,6 +444,13 @@ def prep(self):
|
380 | 444 | self.config['cluster'].setup()
|
381 | 445 | self.config['cluster'].modify_sos_cmd()
|
382 | 446 | self.get_nodes()
|
| 447 | + if self.config['save_group']: |
| 448 | + gname = self.config['save_group'] |
| 449 | + try: |
| 450 | + fname = self.write_host_group() |
| 451 | + self.log_info("Wrote group '%s' to %s" % (gname, fname)) |
| 452 | + except Exception as err: |
| 453 | + self.log_error("Could not save group %s: %s" % (gname, err)) |
383 | 454 | self.intro()
|
384 | 455 | self.configure_sos_cmd()
|
385 | 456 |
|
|
0 commit comments