def satellite_missed_pulp_agent_queues(broker):
    """
    This datasource provides the missed pulp agent queues information on satellite server.

    Note:
        This datasource may be executed using the following command:

        ``insights cat --no-header satellite_missed_pulp_agent_queues``

    Sample output::

        pulp.agent.09008eec-aba6-4174-aa9f-e930004ce5c9:2018-01-16 00:06:13
        pulp.agent.fac7ebbc-ee4f-44b4-9fe0-3f4e42c7f024:2018-01-16 00:06:16
        0

    Returns:
        str: All the missed pulp agent queues and the boolean mark if the data is
            truncated in the last line. If the value of last line is 0,
            it means all the missed queues are returned. If the value of the
            last line is 1, it means there are a lot of missed queues, to
            avoid render error, only the first 10 missed queues are returned.

    Raises:
        SkipComponent: When the error doen't happen or the missed queues have been recreated.

    """
    def _parse_non_existing_queues_in_msg():
        agentq_date_re = re.compile(
            r'^(?P<date>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) \[Protocol\] error Error on attach: Node not found: (?P<agentq>pulp.agent.[0-9a-f-]+)$'
        )
        agent_queue_last_date = {}
        ms_obj = broker[Specs.messages]
        for line in ms_obj.stream():
            if NODE_NOT_FOUND_ERROR in line:
                info, msg = [i.strip() for i in line.split(': ', 1)]
                info_splits = info.rsplit(None, 2)
                if len(info_splits) >= 3 and info_splits[2].startswith('qpidd'):
                    # The timestamp from syslog doesn't contain the year, but the
                    # message itself does - so use that.
                    match = agentq_date_re.search(msg)
                    if match:
                        agent_queue_last_date[match.group('agentq')] = match.group('date')
        return agent_queue_last_date

    def _get_content_host_uuid():
        output = broker[LocalSpecs.content_host_uuids].content
        host_uuids = []
        if len(output) > 3:
            for line in output[2:-1]:
                host_uuids.append(line.strip())
        return host_uuids

    def _get_qpid_queues():
        output = broker[LocalSpecs.qpid_queues].content
        current_queues = []
        if len(output) > 3:
            current_queues = [line.split()[0].strip() for line in output[3:] if line.split()[0].startswith('pulp.agent')]
        return current_queues

    missed_queues_in_log = _parse_non_existing_queues_in_msg()
    if missed_queues_in_log:
        host_uuids = _get_content_host_uuid()
        if host_uuids:
            qpid_queues = _get_qpid_queues()
            missed_queues = []
            too_more_data = 0
            for queue in missed_queues_in_log:
                if queue.split('.')[-1] in host_uuids and queue not in qpid_queues:
                    missed_queues.append('%s:%s' % (queue, missed_queues_in_log[queue]))
                    # only return 10 missed queues in case too long data can't be rendered
                    if len(missed_queues) >= 10:
                        too_more_data = 1
                        break
            if missed_queues:
                missed_queues.append(str(too_more_data))
                return DatasourceProvider(missed_queues, relative_path='insights_commands/satellite_missed_qpid_queues')
    raise SkipComponent
Example #2
0
def yum_updates(_broker):
    """
    This datasource provides a list of available updates on the system.
    It uses the yum python library installed locally, and collects list of
    available package updates, along with advisory info where applicable.

    Sample data returned::

        {
          "releasever": "8",
          "basearch": "x86_64",
          "update_list": {
            "NetworkManager-1:1.22.8-4.el8.x86_64": {
              "available_updates": [
                {
                  "package": "NetworkManager-1:1.22.8-5.el8_2.x86_64",
                  "repository": "rhel-8-for-x86_64-baseos-rpms",
                  "basearch": "x86_64",
                  "releasever": "8",
                  "erratum": "RHSA-2020:3011"
                }
              ]
            }
          },
          "metadata_time": "2021-01-01T09:39:45Z"
        }

    Returns:
        list: List of available updates
    Raises:
        SkipComponent: Raised on systems different than RHEL 7
    """

    if not _broker.get(IsRhel7):
        raise SkipComponent("Yum updates currently only works on RHEL 7")

    with UpdatesManager() as umgr:
        umgr.load()

        response = {
            "releasever": umgr.releasever,
            "basearch": umgr.basearch,
            "update_list": {},
        }

        data = {'package_list': umgr.installed_packages()}
        updates = {}
        for pkg in data["package_list"]:
            (nevra, updates_list) = umgr.updates(pkg)
            updates[nevra] = updates_list
            for (nevra, update_list) in updates.items():
                if update_list:
                    out_list = []
                    for pkg in umgr.sorted_pkgs(update_list):
                        pkg_dict = {
                            "package": umgr.pkg_nevra(pkg),
                            "repository": umgr.pkg_repo(pkg),
                            "basearch": response["basearch"],
                            "releasever": response["releasever"],
                        }
                        erratum = umgr.advisory(pkg)
                        if erratum:
                            pkg_dict["erratum"] = erratum
                        out_list.append(pkg_dict)
                    response["update_list"][nevra] = {"available_updates": out_list}

        ts = umgr.last_update()
        if ts:
            response["metadata_time"] = time.strftime("%FT%TZ", time.gmtime(ts))
    return DatasourceProvider(content=json.dumps(response), relative_path='insights_commands/yum_updates_list')
Example #3
0
   988 /usr/sbin/httpd -DFOREGROUND
  1036 /usr/sbin/httpd -DFOREGROUND
  1037 /usr/sbin/httpd -DFOREGROUND
  1038 /usr/sbin/httpd -DFOREGROUND
  1039 /usr/sbin/httpd -DFOREGROUND
  1040 /usr/local/sbin/httpd -DFOREGROUND
 28218 /usr/bin/java TestSleepMethod1
 28219 java TestSleepMethod1
 28240 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/jre/bin/java TestSleepMethod2
333083 /home/user3/apps/pycharm-2021.1.1/jbr/bin/java -classpath /home/user3/apps/pycharm-2021.1.1/lib/bootstrap.jar:/home/user3/apps/pycharm-2021.1.1/lib/util.jar:/home/user3/apps/pycharm-2021.1.1/lib/jdom.jar:/home/user3/apps/pycharm-2021.1.1/lib/log4j.jar:/home/user3/apps/pycharm-2021.1.1/lib/jna.jar -Xms128m -Xmx2048m -XX:ReservedCodeCacheSize=512m -XX:+UseG1GC -XX:SoftRefLRUPolicyMSPerMB=50 -XX:CICompilerCount=2 -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -ea -Dsun.io.useCanonCaches=false -Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.attach.allowAttachSelf=true -Djdk.module.illegalAccess.silent=true -Dkotlinx.coroutines.debug=off -Dsun.tools.attach.tmp.only=true -XX:ErrorFile=/home/user3/java_error_in_pycharm_%p.log -XX:HeapDumpPath=/home/user3/java_error_in_pycharm_.hprof -Didea.vendor.name=JetBrains -Didea.paths.selector=PyCharm2021.1 -Djb.vmOptionsFile=/home/user3/.config/JetBrains/PyCharm2021.1/pycharm64.vmoptions -Didea.platform.prefix=Python com.intellij.idea.Main
"""

EXPECTED = DatasourceProvider(
    "\n".join([
        "{0} {1}".format(HTTPD_PATH,
                         HTTPD_PKG), "{0} {1}".format(JAVA_PATH_1, JAVA_PKG_2),
        "{0} {1}".format(JAVA_PATH_2, JAVA_PKG_2)
    ]),
    relative_path='insights_commands/package_provides_command')


def test_cmd_and_pkg():
    pseo = PsEoCmd(context_wrap(PS_EO_CMD))
    ps = Ps(None, None, None, None, None, None, pseo)
    broker = dr.Broker()
    broker[HostContext] = FakeContext()
    broker[Ps] = ps

    result = cmd_and_pkg(broker)
    assert result is not None
    assert sorted(result.content) == sorted(EXPECTED.content)
Example #4
0
 def machine_id(broker):
     doc = json.loads(broker[OpenshiftSpecsImpl.namespaces].content)
     v = next(o["metadata"]["uid"] for o in doc["items"] if o["metadata"]["name"] == "kube-system")
     return DatasourceProvider(content=v, relative_path="/etc/insights-client/machine-id")
Example #5
0
 def the_data(broker):
     data = """
     foo bar baz
     baz bar
     """.strip()
     return DatasourceProvider(data, "the_data")
Example #6
0
def ps_eo_cmd(broker):
    """
    Custom datasource to collect the full paths to all running commands on the system
    provided by the ``ps -eo pid,args`` command.  After collecting the data, all of the
    args are trimmed to leave only the command including full path.

    Sample output from the ``ps -eo pid, args`` command::

        PID COMMAND
          1 /usr/lib/systemd/systemd --switched-root --system --deserialize 31
          2 [kthreadd]
          3 [rcu_gp]
          4 [rcu_par_gp]
          6 [kworker/0:0H-events_highpri]
          9 [mm_percpu_wq]
         10 [rcu_tasks_kthre]
         11 /usr/bin/python3 /home/user1/python_app.py
         12 [kworker/u16:0-kcryptd/253:0]

    This datasource trims off the args to minimize possible PII and sensitive information.
    After trimming the data looks like this::

        PID COMMAND
          1 /usr/lib/systemd/systemd
          2 [kthreadd]
          3 [rcu_gp]
          4 [rcu_par_gp]
          6 [kworker/0:0H-events_highpri]
          9 [mm_percpu_wq]
         10 [rcu_tasks_kthre]
         11 /usr/bin/python3
         12 [kworker/u16:0-kcryptd/253:0]

    Returns:
        str: Returns a multiline string in the same format as ``ps`` output

    Raises:
        SkipComponent: Raised if no data is available
    """
    content = broker[LocalSpecs.ps_eo_args].content
    data = []
    data.append('PID COMMAND')
    start = False
    for l in content:
        if 'PID' in l and 'COMMAND' in l:
            start = True
            continue
        if not start:
            continue
        pid, args = l.strip().split(None, 1)
        if ' ' in args:
            cmd, _ = args.split(None, 1)
        else:
            cmd = args
        data.append('{0} {1}'.format(pid, cmd))

    if len(data) > 1:
        return DatasourceProvider('\n'.join(data),
                                  relative_path='insights_commands/ps_eo_cmd')

    raise SkipComponent()
Example #7
0
def cloud_cfg(broker):
    """
    This datasource provides the network configuration information collected
    from ``/etc/cloud/cloud.cfg``.

    Typical content of ``/etc/cloud/cloud.cfg`` file is::

        #cloud-config
        users:
          - name: demo
            ssh-authorized-keys:
              - key_one
              - key_two
            passwd: $6$j212wezy$7H/1LT4f9/N3wpgNunhsIqtMj62OKiS3nyNwuizouQc3u7MbYCarYeAHWYPYb2FT.lbioDm2RrkJPb9BZMN1O/

        network:
            version: 1
            config:
            - type: physical
                name: eth0
                subnets:
                  - type: dhcp
                  - type: dhcp6

        system_info:
            default_user:
            name: user2
            plain_text_passwd: 'someP@assword'
            home: /home/user2

        debug:
            output: /var/log/cloud-init-debug.log
            verbose: true

    Note:
        This datasource may be executed using the following command:

        ``insights cat --no-header cloud_cfg``

    Sample data returned includes only the ``network`` portion of the input file in JSON format::

        {
            "version": 1,
            "config": [
                {
                    "type": "physical",
                    "name": "eth0",
                    "subnets": [
                        {"type": "dhcp"},
                        {"type": "dhcp6"}
                    ]
                }
            ]
        }

    Returns:
        str: JSON string when the ``network`` parameter includes content, else `None` is returned.

    Raises:
        SkipComponent: When the path does not exist or any exception occurs.
    """
    relative_path = '/etc/cloud/cloud.cfg'
    try:
        content = broker[LocalSpecs.cloud_cfg_input].content
        if content:
            content = yaml.load('\n'.join(content), Loader=yaml.SafeLoader)
            network_config = content.get('network', None)
            if network_config:
                return DatasourceProvider(content=json.dumps(network_config),
                                          relative_path=relative_path)
    except Exception as e:
        raise SkipComponent("Unexpected exception:{e}".format(e=str(e)))

    raise SkipComponent('No network section in yaml')