sos.policies — Policy Interface

class sos.policies.Policy(sysroot=None, probe_runtime=True, remote_exec=None)[source]

Bases: object

Policies represent distributions that sos supports, and define the way in which sos behaves on those distributions. A policy should define at minimum a way to identify the distribution, and a package manager to allow for package based plugin enablement.

Policies also control preferred ContainerRuntime()’s, upload support to default locations for distribution vendors, disclaimer text, and default presets supported by that distribution or vendor’s products.

Every Policy will also need at least one “tagging class” for plugins.

Parameters:
  • sysroot (str or None) – Set the sysroot for the system, if not /

  • probe_runtime (bool) – Should the Policy try to load a ContainerRuntime

  • remote_exec (SoSTranport.run_command()) – If this policy is loaded for a remote node, use this to facilitate executing commands via the SoSTransport in use

Variables:
  • os_release_name (str) – The name of the distribution as it appears in the os-release (-esque) file for the NAME variable.

  • os_release_id (str) – The ID variable to match in a distribution’s release file.

  • os_release_file (str) – The filepath of the distribution’s os-release file

  • vendor (str) – The name of the vendor producing the distribution

  • vendor_urls (list of tuples formatted (``description, url)``) – List of URLs for the vendor’s website, or support portal

  • vendor_text (str) – Additional text to add to the banner message

  • name_pattern (str) – The naming pattern to be used for naming archives generated by sos. Values of legacy, and friendly are preset patterns. May also be set to an explicit custom pattern, see get_archive_name()

PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
add_preset(name=None, desc=None, note=None, opts=SoSOptions())[source]

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

Parameters:

preset – the new PresetDefaults to add

classmethod check(remote='')[source]

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

If remote is provided, it should be the contents of os-release from a remote host, or a similar vendor-specific file that can be used in place of a locally available file.

Returns:

True if the Policy should be loaded, else False

Return type:

bool

del_preset(name='')[source]
classmethod display_help(section)[source]
display_results(archive, directory, checksum, archivestat=None, map_file=None)[source]

Display final information about a generated archive

Parameters:
  • archive (str) – The name of the archive that was generated

  • directory (str) – The build directory for sos if –build was used

  • checksum (str) – The checksum of the archive

  • archivestat (os.stat_result) – stat() information for the archive

  • map_file (str) – If sos clean was invoked, the location of the mapping file for this run

dist_version()[source]

Return the OS version

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.

property forbidden_paths

This property is used to determine the list of forbidden paths set by the policy. Note that this property will construct a cumulative list based on all subclasses of a given policy.

Returns:

All patterns of policy forbidden paths

Return type:

list

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

  • rand - a random string of 7 alpha characters

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

Returns:

A name to be used for the archive, as expanded from the Policy name_pattern

Return type:

str

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.os_release_name, that text will be substituted accordingly. You can also override this method to do something more complicated.

Returns:

Formatted banner message string

Return type:

str

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]
in_container()[source]

Are we running inside a container?

Returns:

True if in a container, else False

Return type:

bool

is_root()[source]

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

Returns:

True if user is superuser, else False

Return type:

bool

load_presets(presets_path=None)[source]

Load presets from disk.

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

Parameters:

presets_path – a directory containing JSON presets.

match_plugin(plugin_classes)[source]

Determine what subclass of a Plugin should be used based on the tagging classes assigned to the Plugin

Parameters:

plugin_classes (list) – The classes that the Plugin subclasses

Returns:

The first tagging class that matches one of the Policy’s valid_subclasses

Return type:

PluginDistroTag

msg = 'This command will collect system configuration and diagnostic information from this %(os_release_name)s system.\n\nFor more information on %(vendor)s visit:\n\n  %(vendor_urls)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'
os_release_file = ''
os_release_id = ''
os_release_name = 'Unknown'
pkg_by_name(pkg)[source]

Wrapper to retrieve a package from the Policy’s package manager

Parameters:

pkg (str) – The name of the package

Returns:

The first package that matches pkg

Return type:

str

post_work()[source]

This function is called after the sos report has been generated.

pre_work()[source]

This function is called prior to collection.

presets = {'': PresetDefaults(name='' desc='' note='None' opts=(SoSOptions())}
presets_path = '/etc/sos/presets.d'
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 common host data for the Policy to reference

set_exec_path()[source]
classmethod set_forbidden_paths()[source]

Use this to append policy-specifc forbidden paths that apply to all plugins. Setting this classmethod on an invidual policy will not override subclass-specific paths

validate_plugin(plugin_class, experimental=False)[source]

Verifies that the plugin_class should execute under this policy

Parameters:

plugin_class (PluginDistroTag) – The tagging class being checked

Returns:

True if the plugin_class is allowed by the policy

Return type:

bool

vendor = 'Unknown'
vendor_text = ''
vendor_urls = [('Example URL', 'http://www.example.com/')]
sos.policies.import_policy(name)[source]
sos.policies.load(cache={}, sysroot=None, init=None, probe_runtime=True, remote_exec=None, remote_check='')[source]