Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lmod-UGent.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Name: Lmod
Version: 8.7.57
Release: 1.ug%{?dist}
Release: 2.ug%{?dist}
Summary: Environmental Modules System in Lua

# Lmod-5.3.2/tools/base64.lua is LGPLv2
Expand Down
48 changes: 25 additions & 23 deletions run_lmod_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,27 @@

@author: Ward Poelmans (Ghent University)
"""

import json
import os
import sys
import time
from vsc.utils import fancylogger
from vsc.utils.nagios import NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_WARNING
from vsc.utils.run import run_simple
from vsc.utils.script_tools import ExtendedSimpleOption

# log setup
logger = fancylogger.getLogger(__name__)
fancylogger.logToScreen(True)
fancylogger.setLogLevelInfo()

import logging
logging.basicConfig(level=logging.INFO)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you aware that this is not all facnylogger does? one of the things it does is control the root logger; and this is avail since py3.8 using the force=True in basicconfig

NAGIOS_CHECK_INTERVAL_THRESHOLD = 2 * 60 * 60 # 2 hours


def run_cache_create(modules_root):
"""Run the script to create the Lmod cache"""
lmod_dir = os.environ.get("LMOD_DIR", None)
if not lmod_dir:
raise RuntimeError("Cannot find $LMOD_DIR in the environment.")

cmd = "%s/update_lmod_system_cache_files %s" % (lmod_dir, modules_root)
cmd = f"{lmod_dir}/update_lmod_system_cache_files {modules_root}"
return run_simple(cmd)


Expand All @@ -48,19 +46,19 @@ def get_lmod_config():
if not lmod_cmd:
raise RuntimeError("Cannot find $LMOD_CMD in the environment.")

ec, out = run_simple("%s bash --config-json" % lmod_cmd)
ec, out = run_simple(f"{lmod_cmd} bash --config-json")
if ec != 0:
raise RuntimeError("Failed to get Lmod configuration: %s", out)

try:
lmodconfig = json.loads(out)

config = {
'modules_root': lmodconfig['configT']['mpath_root'],
'cache_dir': lmodconfig['cache'][0][0],
'cache_timestamp': lmodconfig['cache'][0][1],
"modules_root": lmodconfig["configT"]["mpath_root"],
"cache_dir": lmodconfig["cache"][0][0],
"cache_timestamp": lmodconfig["cache"][0][1],
}
logger.debug("Found Lmod config: %s", config)
logging.debug("Found Lmod config: %s", config)
except (ValueError, KeyError, IndexError, TypeError) as err:
raise RuntimeError("Failed to parse the Lmod configuration: %s", err)

Expand All @@ -73,10 +71,14 @@ def main():
Returns the errors if any in a nagios/icinga friendly way.
"""
options = {
'nagios-check-interval-threshold': NAGIOS_CHECK_INTERVAL_THRESHOLD,
'create-cache': ('Create the Lmod cache', None, 'store_true', False),
'freshness-threshold': ('The interval in minutes for how long we consider the cache to be fresh',
'int', 'store', 120),
"nagios-check-interval-threshold": NAGIOS_CHECK_INTERVAL_THRESHOLD,
"create-cache": ("Create the Lmod cache", None, "store_true", False),
"freshness-threshold": (
"The interval in minutes for how long we consider the cache to be fresh",
"int",
"store",
120,
),
}
opts = ExtendedSimpleOption(options)

Expand All @@ -85,28 +87,28 @@ def main():

if opts.options.create_cache:
opts.log.info("Updating the Lmod cache")
exitcode, msg = run_cache_create(config['modules_root'])
exitcode, msg = run_cache_create(config["modules_root"])
if exitcode != 0:
logger.error("Lmod cache update failed: %s", msg)
logging.error("Lmod cache update failed: %s", msg)
opts.critical("Lmod cache update failed")
sys.exit(NAGIOS_EXIT_CRITICAL)

opts.log.info("Checking the Lmod cache freshness")
timestamp = os.stat(config['cache_timestamp'])
timestamp = os.stat(config["cache_timestamp"])

# give a warning when the cache is older then --freshness-threshold
if (time.time() - timestamp.st_mtime) > opts.options.freshness_threshold * 60:
errmsg = "Lmod cache is not fresh"
logger.warn(errmsg)
logging.warn(errmsg)
opts.warning(errmsg)
sys.exit(NAGIOS_EXIT_WARNING)

except RuntimeError as err:
logger.exception("Failed to update Lmod cache: %s", err)
logging.exception("Failed to update Lmod cache: %s", err)
opts.critical("Failed to update Lmod cache. See logs.")
sys.exit(NAGIOS_EXIT_CRITICAL)
except Exception as err: # pylint: disable=W0703
logger.exception("critical exception caught: %s", err)
logging.exception("critical exception caught: %s", err)
opts.critical("Script failed because of uncaught exception. See logs.")
sys.exit(NAGIOS_EXIT_CRITICAL)

Expand All @@ -116,5 +118,5 @@ def main():
opts.epilogue("Lmod cache is still fresh.")


if __name__ == '__main__':
if __name__ == "__main__":
main()