Skip to content

Commit

Permalink
Add datasource to get jboss versions (#3600)
Browse files Browse the repository at this point in the history
* Add datasource to get jboss versions

Signed-off-by: Chen lizhong <[email protected]>

* Update order
* Refact codes

Signed-off-by: Chen lizhong <[email protected]>
(cherry picked from commit 278aa87)
  • Loading branch information
chenlizhong authored and bfahr committed Nov 29, 2022
1 parent 1e6ec15 commit b13a25b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/custom_datasources_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ insights.specs.datasources.ps
-----------------------------

.. automodule:: insights.specs.datasources.ps
:members: ps_eo_cmd, LocalSpecs
:members: jboss_runtime_versions, ps_eo_cmd, LocalSpecs
:show-inheritance:
:undoc-members:

Expand Down
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ class Specs(SpecSet):
iscsiadm_m_session = RegistryPoint()
jbcs_httpd24_httpd_error_log = RegistryPoint(filterable=True)
jboss_domain_server_log = RegistryPoint(multi_output=True, filterable=True)
jboss_runtime_versions = RegistryPoint()
jboss_standalone_main_config = RegistryPoint(multi_output=True)
jboss_standalone_server_log = RegistryPoint(multi_output=True, filterable=True)
jboss_version = RegistryPoint(multi_output=True)
Expand Down
59 changes: 56 additions & 3 deletions insights/specs/datasources/ps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""
Custom datasources for ps information
"""
import json
import os.path

from insights.core.context import HostContext
from insights.core.dr import SkipComponent
from insights.core.plugins import datasource
Expand All @@ -11,18 +14,18 @@
class LocalSpecs(Specs):
""" Local specs used only by ps datasources """

ps_eo_args = simple_command("/bin/ps -eo pid,args")
ps_eo_args = simple_command("/bin/ps -ewwo pid,args")
""" Returns ps output including pid and full args """


@datasource(LocalSpecs.ps_eo_args, HostContext)
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
provided by the ``ps -ewwo 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::
Sample output from the ``ps -ewwo pid, args`` command::
PID COMMAND
1 /usr/lib/systemd/systemd --switched-root --system --deserialize 31
Expand Down Expand Up @@ -76,3 +79,53 @@ def ps_eo_cmd(broker):
return DatasourceProvider('\n'.join(data), relative_path='insights_commands/ps_eo_cmd')

raise SkipComponent()


@datasource(LocalSpecs.ps_eo_args, HostContext)
def jboss_runtime_versions(broker):
"""
Custom datasource to collect the <JBOSS_HOME>/version.txt.
Sample output from the ``ps -ewwo 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]
8686 java -D[Standalone] -server -verbose:gc -Xms64m -Xmx512m -Djboss.home.dir=/opt/jboss-datagrid-7.3.0-server -Djboss.server.base.dir=/opt/jboss-datagrid-7.3.0-server/standalone
Get the Jboss home directory and read the version.txt::
-Djboss.home.dir=/opt/jboss-datagrid-7.3.0-server
/opt/jboss-datagrid-7.3.0-server/version.txt
Returns:
str: string of dict {<jboss_home>: <content of version.txt>}
Raises:
SkipComponent: Raised if no data is available
"""
content = broker[LocalSpecs.ps_eo_args].content
jboss_home_dirs = set()
data = {}
for l in content:
if 'java ' in l:
jboss_home_labels = ['-jboss-home ', '-Djboss.home.dir=', '-Dcatalina.home=',
'-Dinfinispan.server.home.path=']
for jhl in jboss_home_labels:
if jhl in l:
jboss_home_str = l.split(jhl)[1]
if jboss_home_str.startswith('/'):
jboss_home_dirs.add(jboss_home_str.split()[0])
if jboss_home_dirs:
for one_jboss_home_dir in jboss_home_dirs:
jboss_v_file = os.path.join(one_jboss_home_dir, 'version.txt')
if os.path.exists(jboss_v_file):
with open(jboss_v_file, 'r') as version_file:
data[one_jboss_home_dir] = version_file.read()
if len(data) > 0:
return DatasourceProvider(json.dumps(data), relative_path='insights_commands/jboss_versions')
raise SkipComponent()
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class DefaultSpecs(Specs):
ironic_inspector_log = first_file(["/var/log/containers/ironic-inspector/ironic-inspector.log", "/var/log/ironic-inspector/ironic-inspector.log"])
iscsiadm_m_session = simple_command("/usr/sbin/iscsiadm -m session")
jbcs_httpd24_httpd_error_log = simple_file("/opt/rh/jbcs-httpd24/root/etc/httpd/logs/error_log")
jboss_runtime_versions = ps_datasource.jboss_runtime_versions
journal_header = simple_command("/usr/bin/journalctl --no-pager --header")
kdump_conf = simple_file("/etc/kdump.conf")
kernel_config = glob_file("/boot/config-*")
Expand Down
38 changes: 37 additions & 1 deletion insights/tests/datasources/test_ps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import json
import tempfile
import pytest
import shutil
from mock.mock import Mock

from insights.core.dr import SkipComponent
from insights.core.spec_factory import DatasourceProvider
from insights.specs.datasources.ps import ps_eo_cmd, LocalSpecs
from insights.specs.datasources.ps import ps_eo_cmd, LocalSpecs, jboss_runtime_versions

PS_DATA = """
PID COMMAND
Expand Down Expand Up @@ -36,8 +39,19 @@
PS_EMPTY = """
PID COMMAND
"""
jboss_home_1 = tempfile.mkdtemp(prefix='insights_test')
jboss_home_2 = tempfile.mkdtemp(prefix='insights_test')
PS_JBOSS_VERSION = """
PID COMMAND
1 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
2 [kthreadd]
3 [rcu_gp]
8686 java -D[Standalone] -server -verbose:gc -Xloggc:/opt/jboss-datagrid-7.3.0-server/standalone/log/gc.log -XX:+PrintGCDetails -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=3M -XX:-TraceClassUnloading -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Dorg.jboss.boot.log.file=/opt/jboss-datagrid-7.3.0-server/standalone/log/server.log -Dlogging.configuration=file:/opt/jboss-datagrid-7.3.0-server/standalone/configuration/logging.properties -jar /opt/jboss-datagrid-7.3.0-server/jboss-modules.jar -mp /opt/jboss-datagrid-7.3.0-server/modules org.jboss.as.standalone -Djboss.home.dir={0} -Djboss.server.base.dir=/opt/jboss-datagrid-7.3.0-server/standalone
8880 /usr/lib/jvm/java-1.8.0-oracle/bin/java -D[Process Controller] -server -Xms64m -Xmx512m -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Dorg.jboss.boot.log.file=/home/jboss/jboss-eap-7.1/domain/log/process-controller.log -Dlogging.configuration=file:/home/jboss/jboss-eap-7.1/domain/configuration/logging.properties -jar /home/jboss/jboss-eap-7.1/jboss-modules.jar -mp /home/jboss/jboss-eap-7.1/modules org.jboss.as.process-controller -jboss-home {1} -jvm /usr/lib/jvm/java-1.8.0-oracle/bin/java -mp /home/jboss/jboss-eap-7.1/modules -- -Dorg.jboss.boot.log.file=/home/jboss/jboss-eap-7.1/domain/log/host-controller.log -Dlogging.configuration=file:/home/jboss/jboss-eap-7.1/domain/configuration/logging.properties -server
""".format(jboss_home_1, jboss_home_2)

RELATIVE_PATH = 'insights_commands/ps_eo_cmd'
JBOSS_VERSION_PATH = 'insights_commands/jboss_versions'


def test_ps_eo_cmd():
Expand Down Expand Up @@ -68,3 +82,25 @@ def test_ps_eo_cmd_empty():
with pytest.raises(SkipComponent) as e:
ps_eo_cmd(broker)
assert e is not None


def test_jboss_versions():
# create version.txt file
with open(jboss_home_1 + '/version.txt', 'w') as j_v:
j_v.write("Red Hat JBoss Enterprise Application Platform - Version 7.4.0.GA")
with open(jboss_home_2 + '/version.txt', 'w') as j_v:
j_v.write("Red Hat Data Grid - Version 8.3.1.GA")
ps_eo_args = Mock()
ps_eo_args.content = PS_JBOSS_VERSION.splitlines()
broker = {LocalSpecs.ps_eo_args: ps_eo_args}
result = jboss_runtime_versions(broker)
assert result is not None
assert isinstance(result, DatasourceProvider)
expected_content = {jboss_home_1: 'Red Hat JBoss Enterprise Application Platform - Version 7.4.0.GA',
jboss_home_2: 'Red Hat Data Grid - Version 8.3.1.GA'}
expected = DatasourceProvider(content=json.dumps(expected_content), relative_path=JBOSS_VERSION_PATH)
result_dict = json.loads(result.content[0])
assert result_dict == expected_content
assert result.relative_path == expected.relative_path
shutil.rmtree(jboss_home_1)
shutil.rmtree(jboss_home_2)

0 comments on commit b13a25b

Please sign in to comment.