UPDATE: This demo is for 0.3.x version of Lua script which has been changed a lot in 0.4.x please refer to test.lua for some more update examples.

https://github.com/mickem/nscp/blob/0.4.1/scripts/lua/test_nrpe.lua

Concepts are still similar but syntax and features have greatly been extended...

This is just a quick intro, I will try to add more info here and also try to add more system related functions (like WMI and performance counter access) in the future.

Debugging Lua

Use the print statement to print to the console (can bee sen from nsclient++ /test).

With a Lua script like this loaded:

nscp.register('lua_debug', 'debug')

function debug (command)
    print ('Hello world: ' .. command)
end

Then you run nsclient++ /test:

nsclient++ /test
...
lua_debug Greetings
...
d \nsclient++.cpp(540) Injecting: lua_debug: Greetings
Hello world: lua_debug
e \script_wrapper.hpp(280) No arguments returned from script.
l \nsclient++.cpp(575) No handler for command: 'lua_debug'

A simple script

print('Loading test script...') -- Just print some debug info

nscp.register('check_something', 'something') -- Register a check-command to a function

function something (command)
  -- Check command function (notice arguments are not supported yet)

  -- Inject and run another check command
  code, msg, perf = nscp.execute('CheckCPU','time=5','MaxCrit=5')
  -- Print the resulting code
  print(code .. ': ' .. msg .. ', ' .. perf)
  -- Return the information (slightly modified)
  return code, 'hello from LUA: ' .. msg, perf
end

Structure of a script

First all script register all commands they will use (it is possible to register commands at a later time) So you could have a command that "turn on" other commands, but since there is no "turn off" (ie. remove) it does not make much sense as of yet.

To register command you call the nscp.register function like so:

nscp.register('command_alias', 'function_in_lua_to_use');

This will when the command command_alias is run execute the function_in_lua_to_use in your script. You can have as many commands as you like so the following is possible:

nscp.register('lua_1', 'lua_function_1');
nscp.register('lua_2', 'lua_function_2');
nscp.register('lua_3', 'lua_function_3');
nscp.register('lua_4', 'lua_function_4');
nscp.register('lua_5', 'lua_function_5');

The functions have the following syntax:

function lua_check_function (command)
  print ('Hello world: ' .. command)
  return 'ok', 'Everything is fine!', 'fine=10%;80;90;'

As of now there are no support for arguments but in the future they will be added. Printing from a check_function is useless (apart from debug) so generally don't do that. The return is a variable list If;

  • 3 options are returned they are assumed to be in order: code, message and performance data
  • 2 options are returned they are assumed to be in order: code, message
  • 1 options are returned they are assumed to be in order: code

The code can be:

  • crit (critical)
  • warn (warning)
  • ok (ok)
  • error (critical)

A 'useful' script

-- Register the command
nscp.register('has', 'check_file_exists')

-- Return true if file exists and is readable.
function file_exists(path)
  local file = io.open(path, "rb")
  if file then file:close() end
  return file ~= nil
end

function check_file_exists (command)
  if file_exists('c:\\foo.bar') then 
    return 'ok', 'File exists' 
  else 
    return 'crit', 'File does not exist'
  end
end
Last modified 6 months ago Last modified on 11/15/12 11:04:36