Source code for ruxit.api.snapshot

"""
Utilities used to facilitate interfacing with process snapshot
"""
import re


[docs]def python_module(module_name): """ Predicate used to search process snapshot for a group containing Python module e.g: python -m http.server Args: module_name: python module name to find """ _module_cmd_pattern = r'.*-m\s+\b{module_name}\b'.format(module_name=module_name) def _pred(process): _m = re.match(_module_cmd_pattern, process.properties.get('CmdLine', '')) return _m return _pred
[docs]def command_line_regex(pattern): """ Predicate used to search process snapshot for a group that has process with command line matching a specified regex pattern Args: pattern: regex pattern to find. """ def _pred(process): cmdline = process.properties.get('CmdLine', None) return cmdline is not None and re.match(pattern, cmdline) return _pred
[docs]def pgi_name(name): """ Predicate to find process group of a given name. Args: name: name of the group """ return lambda pgi: pgi.group_name == name
[docs]def parse_port_bindings(entity): """ Searches entity processes for a process port bindings data Returns: list of tuple(string of binded IP address, port integer) """ bind_list = list() for process in entity.processes: to_parse = process.properties.get("PortBindings", None) if to_parse is None: continue for entry in to_parse.split(';'): entry_list = entry.split('_') if len(entry_list) != 2: continue bind_list.append((entry_list[0], int(entry_list[1]))) return bind_list