| 1 |
# |
| 2 |
# siteconfig.tcl - Site specific configuration script tracking the modulefile |
| 3 |
# evaluations to generate a log entry each time a module is loaded or |
| 4 |
# unloaded with an information to know if module is auto-loaded or not. |
| 5 |
# |
| 6 |
# Author: Xavier Delaruelle <[email protected]> |
| 7 |
# Compatibility: Modules v4.2+ |
| 8 |
# |
| 9 |
# Installation: put this file in the 'etc' directory of your Modules |
| 10 |
# installation. Refer to the "Modulecmd startup" section in the |
| 11 |
# module(1) man page to get this location. |
| 12 |
|
| 13 |
proc execLogger {msg} { |
| 14 |
# ensure logger command is executed with system libs |
| 15 |
if {[info exists ::env(LD_LIBRARY_PATH)]} { |
| 16 |
set ORIG_LD_LIBRARY_PATH $::env(LD_LIBRARY_PATH) |
| 17 |
unset ::env(LD_LIBRARY_PATH) |
| 18 |
} |
| 19 |
exec logger -t module $msg |
| 20 |
# restore current user lib setup |
| 21 |
if {[info exists ORIG_LD_LIBRARY_PATH]} { |
| 22 |
set ::env(LD_LIBRARY_PATH) $ORIG_LD_LIBRARY_PATH |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
proc logModfileInterp {cmdstring code result op} { |
| 27 |
# parse context |
| 28 |
lassign $cmdstring cmdname modfile modname |
| 29 |
set mode [currentState mode] |
| 30 |
if {[info level] > 1} { |
| 31 |
set caller [lindex [info level -1] 0] |
| 32 |
} else { |
| 33 |
set caller {} |
| 34 |
} |
| 35 |
|
| 36 |
# only log load and unload modulefile evaluation |
| 37 |
if {$mode in {load unload}} { |
| 38 |
|
| 39 |
# add info on load mode to know if module is auto-loaded or not |
| 40 |
if {$mode eq {load} && $caller eq {cmdModuleLoad}} { |
| 41 |
upvar 1 uasked uasked |
| 42 |
set extra ", \"auto\": [expr {$uasked ? {false} : {true}}]" |
| 43 |
} else { |
| 44 |
set extra {} |
| 45 |
} |
| 46 |
|
| 47 |
# produced log entry (formatted as a JSON record) |
| 48 |
execLogger "{ \"user\": \"[get-env USER]\", \"mode\": \"$mode\",\ |
| 49 |
\"module\": \"$modname\"${extra} }" |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
# run log procedure after each modulefile interpretation call |
| 54 |
trace add execution execute-modulefile leave logModfileInterp |
| 55 |
|
| 56 |
proc logModuleCmd {cmdstring code result op} { |
| 57 |
# parse context |
| 58 |
set args [lassign $cmdstring cmdname] |
| 59 |
if {[info level] > 1} { |
| 60 |
set caller [lindex [info level -1] 0] |
| 61 |
} else { |
| 62 |
set caller {} |
| 63 |
} |
| 64 |
|
| 65 |
# skip duplicate log entry when ml command calls module |
| 66 |
if {$cmdname ne {module} || $caller ne {ml}} { |
| 67 |
|
| 68 |
# produced log entry (formatted as a JSON record) |
| 69 |
execLogger "{ \"user\": \"[get-env USER]\", \"cmd\": \"$cmdname\",\ |
| 70 |
\"args\": \"$args\" }" |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
# run log procedure after each module or ml command |
| 75 |
trace add execution module leave logModuleCmd |
| 76 |
trace add execution ml leave logModuleCmd |
| 77 |
|
| 78 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |