Skip to content
This repository was archived by the owner on Sep 15, 2020. It is now read-only.

Commit 0ac8454

Browse files
committed
[sos-collector] Ability to use 'host group' files
This change adds the ability to use and write JSON-formatted files to use as 'host groups' that define cluster-type, master, and a list of nodes to use for collection. Using the new --group option, sos-collector will look to /var/lib/sos-collector for a host group with a matching name to load and use for execution. Note that users may still use the --nodes option when using --group - the two lists of nodes will be merged. Using the new --save-group option will write out a host group file to /var/lib/sos-collector/ with the same name as the value given to this option. The values written to the host group file will be what was determined for this run of sos-collector; meaning the cluster-type, master node, and node list enumerated from the cluster. This means that regex strings will not be saved to group files, but the results of those regexes will be saved. Fixes: #20 Signed-off-by: Jake Hunsaker <[email protected]>
1 parent bcff17b commit 0ac8454

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

man/en/sos-collector.1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ sos-collector \- Collect sosreports from multiple (cluster) nodes
1212
[\-\-case\-id CASE_ID]
1313
[\-\-cluster\-type CLUSTER_TYPE]
1414
[\-e ENABLE_PLUGINS]
15+
[\-\-group GROUP]
16+
[\-\-save\-group GROUP]
1517
[\-\-insecure-sudo]
1618
[\-k PLUGIN_OPTION]
1719
[\-\-label LABEL]
@@ -104,6 +106,32 @@ Sosreport option. Use this to enable a plugin that would otherwise not be run.
104106

105107
This option supports providing a comma-delimited list of plugins.
106108
.TP
109+
\fB\-\-group\fR GROUP
110+
Specify an existing host group definition to use.
111+
112+
Host groups are pre-defined settings for the cluster-type, master, and nodes options
113+
saved in JSON-formatted files under /var/lib/sos-collector/<GROUP>.
114+
115+
If cluster_type and/or master are set in the group, sos-collector behaves as if
116+
these values were specified on the command-line.
117+
118+
If nodes is defined, sos-collector \fBextends\fR the \fB\-\-nodes\fR option, if set,
119+
with the nodes or regexes listed in the group.
120+
121+
Note that sos-collector will only write group definitions to /var/lib/sos-collector/
122+
however the GROUP value may be a filename for any group definitions that exist outside
123+
of the default location. If you are manually writing these files, use the value \fBnull\fR
124+
when a python NoneType is expected. Caveat: use \fBstring\fR 'none' if setting cluster_type
125+
to none.
126+
.TP
127+
\fB\-\-save\-group\fR GROUP
128+
Save the results of this run of sos-collector to a host group definition.
129+
130+
sos-colllector will write a JSON-formatted file with name GROUP to /var/lib/sos-collector/
131+
with the settings for cluster-type, master, and the node list as discovered by cluster enumeration.
132+
Note that this means regexes are not directly saved to host groups, but the results of matching against
133+
those regexes are.
134+
.TP
107135
\fB\-\-insecure-sudo\fR
108136
Use this option when connecting as a non-root user that has passwordless sudo
109137
configured.

sos-collector

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ if __name__ == '__main__':
5252
help="chroot executed commands to SYSROOT")
5353
parser.add_argument('-e', '--enable-plugins', action="append",
5454
help='Enable specific plugins for sosreport')
55+
parser.add_argument('--group', default=None,
56+
help='Use a predefined group JSON file')
57+
parser.add_argument('--save-group', default='',
58+
help='Save the resulting node list to a group')
5559
parser.add_argument('--image', help=('Specify the container image to use'
5660
' for atomic hosts. Defaults to '
5761
'the rhel7/support-tools image'

soscollector/configuration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def set_defaults(self):
4747
self['no_local'] = False
4848
self['tmp_dir'] = None
4949
self['out_dir'] = '/var/tmp/'
50-
self['nodes'] = None
50+
self['nodes'] = []
5151
self['debug'] = False
5252
self['tmp_dir_created'] = False
5353
self['cluster_type'] = None
@@ -87,6 +87,8 @@ def set_defaults(self):
8787
self['log_size'] = 0
8888
self['host_types'] = []
8989
self['password_per_node'] = False
90+
self['group'] = None
91+
self['save_group'] = ''
9092

9193
def parse_node_strings(self):
9294
'''

soscollector/sos_collector.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import fnmatch
1717
import inspect
18+
import json
1819
import logging
1920
import os
2021
import random
@@ -38,6 +39,8 @@
3839
from soscollector import __version__
3940
from soscollector.exceptions import ControlPersistUnsupportedException
4041

42+
COLLECTOR_LIB_DIR = '/var/lib/sos-collector'
43+
4144

4245
class SosCollector():
4346
'''Main sos-collector class'''
@@ -287,6 +290,60 @@ def _fmt_msg(self, msg):
287290
_fmt = _fmt + fill(line, width, replace_whitespace=False) + '\n'
288291
return _fmt
289292

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+
290347
def prep(self):
291348
'''Based on configuration, performs setup for collection'''
292349
disclaimer = ("""\
@@ -349,6 +406,13 @@ def prep(self):
349406
' Ignoring request to change user on node')
350407
self.config['become_root'] = False
351408

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+
352416
if self.config['master']:
353417
self.connect_to_master()
354418
self.config['no_local'] = True
@@ -380,6 +444,13 @@ def prep(self):
380444
self.config['cluster'].setup()
381445
self.config['cluster'].modify_sos_cmd()
382446
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))
383454
self.intro()
384455
self.configure_sos_cmd()
385456

0 commit comments

Comments
 (0)