Skip to content

Commit

Permalink
Dev: scripts/health/collect: use ansible to get sysinfo (#1500)
Browse files Browse the repository at this point in the history
This PR continues the #1497 . Additionally to the package information it is
also the system information that read from ansible.
  • Loading branch information
aleksei-burlakov authored Aug 6, 2024
2 parents 2abacf2 + 28a22f6 commit 7e27032
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
24 changes: 24 additions & 0 deletions crmsh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import gzip
import bz2
import lzma
import json
from pathlib import Path
from contextlib import contextmanager, closing
from stat import S_ISBLK
Expand Down Expand Up @@ -3137,4 +3138,27 @@ def time_value_with_unit(time_value):
Check if the time value contains unit
"""
return re.search(r'^\d+[a-z]+$', time_value) is not None


def ansible_installed():
return shutil.which('ansible')


def ansible_facts(module_name) -> dict:
proc = subprocess.run(['ansible', '-m', module_name, 'localhost']
, capture_output=True, text=True)
out = proc.stdout
# output format 'localhost | SUCCESS => { json...'
bracket_pos = out.find('{')
if bracket_pos == -1:
logger.error("Parsing ansible output.")
return {}
is_ok = out[:bracket_pos].find('SUCCESS =>')
if is_ok == -1:
logger.error("Failure calling ansible module.")
return {}
# get the json part
out = out[bracket_pos:]
json_tree = json.loads(out)
return json_tree['ansible_facts']
# vim:ts=4:sw=4:et:
26 changes: 24 additions & 2 deletions scripts/health/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import crmsh.log
crmsh.log.setup_logging()
from crmsh.report import utils
import crmsh.utils

data = crm_script.get_input()

Expand All @@ -32,6 +33,27 @@ def get_user():


def sys_info():
with open('/proc/uptime') as f:
uptime = f.read().split()

if crmsh.utils.ansible_installed():
facts = crmsh.utils.ansible_facts('setup')

return {'system': facts.get("ansible_system"),
'node': facts.get("ansible_hostname"),
'release': facts.get("ansible_kernel"),
'version': facts.get("ansible_kernel_version"),
'machine': facts.get("ansible_machine"),
'processor': facts.get("ansible_architecture"),
'distname': facts.get("ansible_distribution"),
'user': facts.get("ansible_user_id"),
'hostname': facts.get("ansible_nodename"),
'uptime': facts.get("ansible_uptime_seconds"),
'idletime': uptime[1], # :( not in ansible setup module
'loadavg': facts.get("ansible_loadavg").get("15m") # 15 minute average
}

# if ansible is not installed, do it like before
sysname, nodename, release, version, machine = os.uname()
# The first three columns measure CPU and IO utilization of the
# last one, five, and 15 minute periods. The fourth column shows
Expand All @@ -41,8 +63,8 @@ def sys_info():
distname = utils.get_distro_info()
hostname = os.uname()[1]

uptime = open('/proc/uptime').read().split()
loadavg = open('/proc/loadavg').read().split()
with open('/proc/loadavg') as f:
loadavg = f.read().split()

return {'system': system,
'node': node,
Expand Down
40 changes: 13 additions & 27 deletions utils/crm_rpmcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import subprocess
import shutil
from crmsh import utils

def run(cmd):
proc = subprocess.Popen(cmd,
Expand All @@ -23,10 +24,8 @@ def package_data(pkg):
"""
Gathers version and release information about a package.
"""
if shutil.which('ansible'):
rc, data = ansible_package_data(pkg)
if rc == 0:
return data
if utils.ansible_installed():
return ansible_package_data(pkg)

if shutil.which('rpm'):
return rpm_package_data(pkg)
Expand All @@ -36,36 +35,23 @@ def package_data(pkg):

return {'name': pkg, 'error': "unknown package manager"}


_packages = None
def ansible_package_data(pkg) -> tuple[int, dict]:
def ansible_package_data(pkg) -> dict:
"""
Gathers version and release information about a package.
Using ansible.
"""
# if _packages is None, then get it
global _packages
if not _packages:
# if _packages is None, then get it
rc, out, err = run(['ansible', '-m', 'package_facts', 'localhost'])
if rc == -1:
return -1, {}
# output format 'localhost | SUCCESS => { json...'
bracket_pos = out.find('{')
if bracket_pos == -1:
return -1, {}
is_ok = out[:bracket_pos].find('SUCCESS =>')
if is_ok == -1:
return -1, {}

# get the json part
out = out[bracket_pos:]
json_tree = json.loads(out)
# get _packages
_packages = json_tree['ansible_facts']['packages']

if pkg not in _packages:
return 0, {'name': pkg, 'error': "package not installed"}
else:
return 0, _packages[pkg][0]
facts = utils.ansible_facts('package_facts')
_packages = facts.get('packages')

if _packages and pkg in _packages:
return _packages[pkg][0]

return {'name': pkg, 'error': "package not installed"}


def rpm_package_data(pkg):
Expand Down

0 comments on commit 7e27032

Please sign in to comment.