sos.policies — Policy Interface

sos.policies.DESC = 'desc'

Constants for on-disk preset fields

class sos.policies.GenericPolicy(sysroot=None)[source]

Bases: sos.policies.Policy

This Policy will be returned if no other policy can be loaded. This should allow for IndependentPlugins to be executed on any system

get_msg()[source]

This method is used to prepare the preamble text to display to the user in non-batch mode. If your policy sets self.distro that text will be substituted accordingly. You can also override this method to do something more complicated.

class sos.policies.InitSystem(init_cmd=None, list_cmd=None, query_cmd=None)[source]

Bases: object

Encapsulates an init system to provide service-oriented functions to sos.

This should be used to query the status of services, such as if they are enabled or disabled on boot, or if the service is currently running.

get_service_names(regex)[source]

Get a list of all services discovered on the system that match the given regex.

get_service_status(name)[source]

Returns the status for the given service name along with the output of the query command

is_disabled(name)[source]

Check if a given service name is disabled

is_enabled(name)[source]

Check if given service name is enabled

is_running(name)[source]

Checks if the given service name is in a running state.

This should be overridden by initsystems that subclass InitSystem

is_service(name)[source]

Checks if the given service name exists on the system at all, this does not check for the service status

load_all_services()[source]

This loads all services known to the init system into a dict. The dict should be keyed by the service name, and contain a dict of the name and service status

parse_query(output)[source]

Parses the output returned by the query command to make a determination of what the state of the service is

This should be overriden by anything that subclasses InitSystem

class sos.policies.LinuxPolicy(sysroot=None, init=None)[source]

Bases: sos.policies.Policy

This policy is meant to be an abc class that provides common implementations used in Linux distros

PATH = '/bin:/sbin:/usr/bin:/usr/sbin'
default_runlevel()[source]
distro = 'Linux'
get_arch()[source]
get_local_name()[source]

Returns the name usd in the pre_work step

get_preferred_hash_name()[source]

Returns the string name of the hashlib-supported checksum algorithm to use

get_upload_https_auth(user=None, password=None)[source]

Formats the user/password credentials using basic auth

get_upload_password()[source]

Helper function to determine if we should use the policy default upload password or one provided by the user

get_upload_url()[source]

Helper function to determine if we should use the policy default upload url or one provided by the user

get_upload_url_string()[source]

Used by distro policies to potentially change the string used to report upload location from the URL to a more human-friendly string

get_upload_user()[source]

Helper function to determine if we should use the policy default upload user or one provided by the user

host_name()[source]
init = None
init_kernel_modules()[source]

Obtain a list of loaded kernel modules to reference later for plugin enablement and SoSPredicate checks

is_kernel_smp()[source]
kernel_version()[source]
pre_work()[source]

This function is called prior to collection.

prompt_for_upload_password()[source]

Should be overridden by policies to determine if a password needs to be provided for upload or not

prompt_for_upload_user()[source]

Should be overridden by policies to determine if a user needs to be provided or not

sanitize_filename(name)[source]
upload_archive(archive)[source]

Entry point for sos attempts to upload the generated archive to a policy or user specified location.

Curerntly there is support for HTTPS, SFTP, and FTP. HTTPS uploads are preferred for policy-defined defaults.

Policies that need to override uploading methods should override the respective upload_https(), upload_sftp(), and/or upload_ftp() methods and should NOT override this method.

In order to enable this for a policy, that policy needs to implement the following:

Required:
Class Attrs:
_upload_url The default location to use. Note

these MUST include protocol header

_upload_user Default username, if any else None _upload_password Default password, if any else None _use_https_streaming Set to True if the HTTPS endpoint

supports streaming data

Optional:
Class Attrs:

_upload_directory Default FTP server directory, if any

Methods:
prompt_for_upload_user() Determines if sos should prompt

for a username or not.

get_upload_user() Determines if the default or a

different username should be used

get_upload_https_auth() Format authentication data for

HTTPS uploads

get_upload_url_string() If you want your policy to print

a string other than the default URL for your vendor/distro, override this method

upload_ftp(url=None, directory=None, user=None, password=None)[source]

Attempts to upload the archive to either the policy defined or user provided FTP location.

upload_https()[source]

Attempts to upload the archive to an HTTPS location.

Policies may define whether this upload attempt should use streaming or non-streaming data by setting the use_https_streaming class attr to True

upload_password = None
upload_sftp()[source]

Attempts to upload the archive to an SFTP location.

Due to the lack of well maintained, secure, and generally widespread python libraries for SFTP, sos will shell-out to the system’s local ssh installation in order to handle these uploads.

Do not override this method with one that uses python-paramiko, as the upstream sos team will reject any PR that includes that dependency.

upload_url = None
upload_user = None
vendor = 'None'
class sos.policies.PackageManager(chroot=None, query_command=None, verify_command=None, verify_filter=None, files_command=None)[source]

Bases: object

Encapsulates a package manager. If you provide a query_command to the constructor it should print each package on the system in the following format:

package name|package.version

You may also subclass this class and provide a get_pkg_list method to build the list of packages and versions.

all_files()[source]

Returns a list of files known by the package manager

all_pkgs()[source]

Return a list of all packages.

all_pkgs_by_name(name)[source]

Return a list of packages that match name.

all_pkgs_by_name_regex(regex_name, flags=0)[source]

Return a list of packages that match regex_name.

build_verify_command(self, packages) → str[source]

Generate a command to verify the list of packages given in packages using the native package manager’s verification tool.

The command to be executed is returned as a string that may be passed to a command execution routine (for e.g. sos_get_command_output().

Parameters

packages – a string, or a list of strings giving package names to be verified.

Returns

a string containing an executable command that will perform verification of the given packages.

Returntype

str or NoneType

chroot = None
files = None
get_pkg_list()[source]

Returns a dictionary of packages in the following format:

{'package_name': {'name': 'package_name',
                  'version': 'major.minor.version'}}
pkg_by_name(name)[source]

Return a single package that matches name.

pkg_nvra(pkg)[source]
query_command = None
verify_command = None
verify_filter = None
class sos.policies.Policy(sysroot=None)[source]

Bases: object

PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
add_preset(name=None, desc=None, note=None, opts=SoSOptions(add_preset='', alloptions=False, allow_system_changes=False, all_logs=False, batch=False, build=False, case_id='', chroot='auto', compression_type='auto', config_file='', desc='', debug=False, del_preset='', dry_run=False, enableplugins=[], encrypt_key=None, encrypt_pass=None, experimental=False, label='', list_plugins=False, list_presets=False, list_profiles=False, log_size=25, noplugins=[], noreport=False, no_env_vars=False, no_postproc=False, note='', onlyplugins=[], plugin_timeout=None, plugopts=[], preset='auto', profiles=[], quiet=False, since=None, sysroot=None, threads=4, tmp_dir='', upload=False, upload_url='', upload_directory='', upload_user='', upload_pass='', verbosity=0, verify=False))[source]

Add a new on-disk preset and write it to the configured presets path.

Parameters

preset – the new PresetDefaults to add

check()[source]

This function is responsible for determining if the underlying system is supported by this policy.

default_scl_prefix = ''
del_preset(name='')[source]
del_valid_subclasses()[source]
display_results(archive, directory, checksum, archivestat=None)[source]
dist_version()[source]

Return the OS version

distro = 'Unknown'
find_preset(preset)[source]

Find a preset profile matching the specified preset string.

Parameters

preset – a string containing a preset profile name.

Returns

a matching PresetProfile.

get_archive_name()[source]

This function should return the filename of the archive without the extension.

This uses the policy’s name_pattern attribute to determine the name. There are two pre-defined naming patterns - ‘legacy’ and ‘friendly’ that give names like the following:

legacy - ‘sosreport-tux.123456-20171224185433’ friendly - ‘sosreport-tux-mylabel-123456-2017-12-24-ezcfcop.tar.xz’

A custom name_pattern can be used by a policy provided that it defines name_pattern using a format() style string substitution.

Usable substitutions are:

name - the short hostname of the system label - the label given by –label case - the case id given by –case-id or –ticker-number rand - a random string of 7 alpha characters

Note that if a datestamp is needed, the substring should be set in the name_pattern in the format accepted by strftime().

get_cmd_for_compress_method(method, threads)[source]
get_default_scl_prefix()[source]
get_msg()[source]

This method is used to prepare the preamble text to display to the user in non-batch mode. If your policy sets self.distro that text will be substituted accordingly. You can also override this method to do something more complicated.

get_preferred_archive()[source]

Return the class object of the prefered archive format for this platform

get_preferred_hash_name()[source]

Returns the string name of the hashlib-supported checksum algorithm to use

get_tmp_dir(opt_tmp_dir)[source]
get_valid_subclasses()[source]
host_sysroot()[source]
in_container()[source]

Returns True if sos is running inside a container environment.

is_root()[source]

This method should return true if the user calling the script is considered to be a superuser

load_presets(presets_path=None)[source]

Load presets from disk.

Read JSON formatted preset data from the specified path, or the default location at /var/lib/sos/presets.

Parameters

presets_path – a directory containing JSON presets.

match_plugin(plugin_classes)[source]
msg = 'This command will collect system configuration and diagnostic information from this %(distro)s system.\n\nFor more information on %(vendor)s visit:\n\n %(vendor_url)s\n\nThe generated archive may contain data considered sensitive and its content should be reviewed by the originating organization before being passed to any third party.\n\n%(changes_text)s\n\n%(vendor_text)s\n'
name_pattern = 'legacy'
pkg_by_name(pkg)[source]
post_work()[source]

This function is called after the sosreport has been generated.

pre_work()[source]

This function is called prior to collection.

presets = {'': PresetDefaults(name='' desc='' note='None' opts=(SoSOptions(add_preset='', alloptions=False, allow_system_changes=False, all_logs=False, batch=False, build=False, case_id='', chroot='auto', compression_type='auto', config_file='', desc='', debug=False, del_preset='', dry_run=False, enableplugins=[], encrypt_key=None, encrypt_pass=None, experimental=False, label='', list_plugins=False, list_presets=False, list_profiles=False, log_size=25, noplugins=[], noreport=False, no_env_vars=False, no_postproc=False, note='', onlyplugins=[], plugin_timeout=None, plugopts=[], preset='auto', profiles=[], quiet=False, since=None, sysroot=None, threads=4, tmp_dir='', upload=False, upload_url='', upload_directory='', upload_user='', upload_pass='', verbosity=0, verify=False))}
presets_path = '/var/lib/sos/presets'
probe_preset()[source]

Return a PresetDefaults object matching the runing host.

Stub method to be implemented by derived policy classes.

Returns

a PresetDefaults object.

register_presets(presets, replace=False)[source]

Add new presets to this policy object.

Merges the presets dictionary presets into this Policy object, or replaces the current presets if replace is True.

presets should be a dictionary mapping str preset names to <class PresetDefaults> objects specifying the command line defaults.

Parameters
  • presets – dictionary of presets to add or replace

  • replace – replace presets rather than merge new presets.

set_commons(commons)[source]
set_exec_path()[source]
set_valid_subclasses(subclasses)[source]
property valid_subclasses

list of subclasses that this policy can process

validate_plugin(plugin_class, experimental=False)[source]

Verifies that the plugin_class should execute under this policy

vendor = 'Unknown'
vendor_text = ''
vendor_url = 'http://www.example.com/'
class sos.policies.PresetDefaults(name='', desc='', note=None, opts=SoSOptions(add_preset='', alloptions=False, allow_system_changes=False, all_logs=False, batch=False, build=False, case_id='', chroot='auto', compression_type='auto', config_file='', desc='', debug=False, del_preset='', dry_run=False, enableplugins=[], encrypt_key=None, encrypt_pass=None, experimental=False, label='', list_plugins=False, list_presets=False, list_profiles=False, log_size=25, noplugins=[], noreport=False, no_env_vars=False, no_postproc=False, note='', onlyplugins=[], plugin_timeout=None, plugopts=[], preset='auto', profiles=[], quiet=False, since=None, sysroot=None, threads=4, tmp_dir='', upload=False, upload_url='', upload_directory='', upload_user='', upload_pass='', verbosity=0, verify=False))[source]

Bases: object

Preset command line defaults.

builtin = True

True if this preset if built-in or False otherwise.

delete(presets_path)[source]
desc = None

Human readable preset description

name = None

Preset name, used for selection

note = None

Notes on preset behaviour

opts = SoSOptions(add_preset='', alloptions=False, allow_system_changes=False, all_logs=False, batch=False, build=False, case_id='', chroot='auto', compression_type='auto', config_file='', desc='', debug=False, del_preset='', dry_run=False, enableplugins=[], encrypt_key=None, encrypt_pass=None, experimental=False, label='', list_plugins=False, list_presets=False, list_profiles=False, log_size=25, noplugins=[], noreport=False, no_env_vars=False, no_postproc=False, note='', onlyplugins=[], plugin_timeout=None, plugopts=[], preset='auto', profiles=[], quiet=False, since=None, sysroot=None, threads=4, tmp_dir='', upload=False, upload_url='', upload_directory='', upload_user='', upload_pass='', verbosity=0, verify=False)

Options set for this preset

write(presets_path)[source]

Write this preset to disk in JSON notation.

Parameters

presets_path – the directory where the preset will be written.

class sos.policies.SystemdInit[source]

Bases: sos.policies.InitSystem

is_running(name)[source]

Checks if the given service name is in a running state.

This should be overridden by initsystems that subclass InitSystem

load_all_services()[source]

This loads all services known to the init system into a dict. The dict should be keyed by the service name, and contain a dict of the name and service status

parse_query(output)[source]

Parses the output returned by the query command to make a determination of what the state of the service is

This should be overriden by anything that subclasses InitSystem

sos.policies.get_human_readable(size, precision=2)[source]
sos.policies.import_policy(name)[source]
sos.policies.load(cache={}, sysroot=None)[source]