Bright-Computing / bic

Bright-Illumina collaboration
GNU General Public License v2.0
4 stars 5 forks source link

Consider to use improved SitePackage.lua #37

Closed fgeorgatos closed 7 years ago

fgeorgatos commented 8 years ago

https://github.com/TACC/Lmod/blob/master/contrib/more_hooks/SitePackage.lua (or from upstream: https://github.com/hpcugent/Lmod-UGent/blob/master/SitePackage.lua )

per @wpoely86:

As it might of interest to other people: with the latest master of Lmod,
you can now differentiate between modules being loaded because the user
requested it and modules being loaded as a dependency of others. We use
this for logging the usage of modules.

As an example I added the load hook we currently use:
https://github.com/TACC/Lmod/blob/master/contrib/more_hooks/SitePackage.lua
In the README you can find a link to the grok patterns we use to feed
this into logstash.
fgeorgatos commented 7 years ago

I think this is only missing because of not delivering contrib directory. easily fixable.

wpoely86 commented 7 years ago

The version we're using can always be found at https://github.com/hpcugent/Lmod-UGent/blob/master/SitePackage.lua

fgeorgatos commented 7 years ago

@wpoely86 : thanks about the pointer. btw. I've tried to applied your version at some point last month, but it didn't work for me as advertised (possibly because it needed customization and I didn't really dive into it), so I reverted to "classic" SitePackage.lua.

Surely, it would be great if we could come up with a fortified variation of it that works for several sites at first go... how easy do you think that would be?!

fgeorgatos commented 7 years ago

to be more precise, the logger functionality wasn't kicking in ; again, it could have been a triviality

wpoely86 commented 7 years ago

@fgeorgatos Do not just copy it. It contains some specific UGent stuff.

The logging functionality just calls /bin/logger. If that is present on the system, it should just work.

fgeorgatos commented 7 years ago

a case like the following one has worked well; a bit simple but does the job right:

[fotis@demo2 ~]$ cat /etc/site/lmod/SitePackage.lua
--------------------------------------------------------------------------
-- Lmod License
--------------------------------------------------------------------------
--
--  Lmod is licensed under the terms of the MIT license reproduced below.
--  This means that Lmod is free software and can be used for both academic
--  and commercial purposes at absolutely no cost.
--
--  ----------------------------------------------------------------------
--
--  Copyright (C) 2008-2014 Robert McLay
--
--  Permission is hereby granted, free of charge, to any person obtaining
--  a copy of this software and associated documentation files (the
--  "Software"), to deal in the Software without restriction, including
--  without limitation the rights to use, copy, modify, merge, publish,
--  distribute, sublicense, and/or sell copies of the Software, and to
--  permit persons to whom the Software is furnished to do so, subject
--  to the following conditions:
--
--  The above copyright notice and this permission notice shall be
--  included in all copies or substantial portions of the Software.
--
--  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
--  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
--  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
--  NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
--  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
--  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
--  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
--  THE SOFTWARE.
--
--------------------------------------------------------------------------

require("strict")
local hook   = require("Hook")
local uname  = require("posix").uname

-- By using the hook.register function, this function "load_hook" is called
-- every time a module is loaded with the file name and the module name.

local s_msgA = {}

function load_hook(t)
   -- the arg t is a table:
   --     t.modFullName:  the module full name: (i.e: gcc/4.7.2)
   --     t.fn:           The file name: (i.e /apps/modulefiles/Core/gcc/4.7.2.lua)

   -- Your site can use this any way that suits.  Here are some possibilities:
   --  a) Write this information into a file in your users directory (say ~/.lmod.d/.save).
   --     Then once a day/week/month collect this data.
   --  b) have this function call syslogd to register that this module was loaded by this
   --     user
   --  c) Write the same information directly to some database.

   -- This is an example writing to syslogd:

   if (mode() ~= "load") then return end
   local user        = os.getenv("USER")
   local host        = uname("%n")
   local currentTime = epoch()
   local msg         = string.format("user=%s module=%s path=%s host=%s time=%f",
                                     user, t.modFullName, t.fn, host, currentTime)
   local a           = s_msgA
   a[#a+1]           = msg
end

hook.register("load",load_hook)

function report_loads()

   local sys         = os.getenv("LMOD_sys") or "Linux"
   if (sys == "Linux") then
      local a = s_msgA
      for i = 1,#a do
         local msg = a[i]
         os.execute("logger -t ModuleUsageTracking -p local0.info " .. msg)
      end
   end

end

ExitHookA.register(report_loads)