| 1 | from NSCP import Settings, Functions, Core, log, status
|
|---|
| 2 |
|
|---|
| 3 | core = Core.get()
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 | def get_help(arguments):
|
|---|
| 7 | return (status.OK, 'help: Get help')
|
|---|
| 8 |
|
|---|
| 9 | prefix = 'py_'
|
|---|
| 10 | def test(arguments):
|
|---|
| 11 | log('inside test')
|
|---|
| 12 | for a in arguments:
|
|---|
| 13 | log('Got argument: %s'%a)
|
|---|
| 14 | (retcode, msg, perf) = core.simple_query("%snormal"%prefix, [])
|
|---|
| 15 | if msg != "ok got: ":
|
|---|
| 16 | return (status.CRITICAL, "Test failed")
|
|---|
| 17 | (retcode, msg, perf) = core.simple_query("%snormal"%prefix, ["hello"])
|
|---|
| 18 | if msg != "ok got: hello":
|
|---|
| 19 | return (status.CRITICAL, "Test failed")
|
|---|
| 20 | (retcode, msg, perf) = core.simple_query("%snormal"%prefix, ["hello", "world"])
|
|---|
| 21 | if perf != "'args'=2":
|
|---|
| 22 | return (status.CRITICAL, "Test failed: -%s-"%perf)
|
|---|
| 23 | return (status.OK, 'Tests ok')
|
|---|
| 24 |
|
|---|
| 25 | def normal(arguments):
|
|---|
| 26 | log('inside normal')
|
|---|
| 27 | for a in arguments:
|
|---|
| 28 | log(' | Got argument: %s'%a)
|
|---|
| 29 | if arguments:
|
|---|
| 30 | return (status.OK, 'ok got: %s'%arguments[0], "args=%d; "%len(arguments))
|
|---|
| 31 | return (status.OK, 'ok got: ', "args=%d; "%len(arguments))
|
|---|
| 32 |
|
|---|
| 33 | def no_perf(arguments):
|
|---|
| 34 | log('inside no_perf')
|
|---|
| 35 | for a in arguments:
|
|---|
| 36 | log('Got argument: %s'%a)
|
|---|
| 37 | return (status.WARNING, 'I am ok')
|
|---|
| 38 |
|
|---|
| 39 | def no_msg(arguments):
|
|---|
| 40 | log('inside no_perf')
|
|---|
| 41 | for a in arguments:
|
|---|
| 42 | log('Got argument: %s'%a)
|
|---|
| 43 | return (1)
|
|---|
| 44 |
|
|---|
| 45 | def no_ret(arguments):
|
|---|
| 46 | log('inside no_perf')
|
|---|
| 47 | for a in arguments:
|
|---|
| 48 | log('Got argument: %s'%a)
|
|---|
| 49 |
|
|---|
| 50 | def init(alias):
|
|---|
| 51 | if alias:
|
|---|
| 52 | prefix = '%s_'%alias
|
|---|
| 53 |
|
|---|
| 54 | log('Hello World')
|
|---|
| 55 |
|
|---|
| 56 | conf = Settings.get()
|
|---|
| 57 | val = conf.get_string('/modules', 'PythonScript', 'foo')
|
|---|
| 58 |
|
|---|
| 59 | log('Got it: %s'%val)
|
|---|
| 60 |
|
|---|
| 61 | log('Testing to register a function')
|
|---|
| 62 | fun = Functions.get()
|
|---|
| 63 | fun.register_simple('%stest'%prefix, test, 'This is a sample command')
|
|---|
| 64 | fun.register_simple('%snormal'%prefix, normal, 'This is a sample command')
|
|---|
| 65 | fun.register_simple('%snop'%prefix, no_perf, 'No performance data')
|
|---|
| 66 | fun.register_simple('%snom'%prefix, no_msg, 'No performance data')
|
|---|
| 67 | fun.register_simple('%snor'%prefix, no_ret, 'No performance data')
|
|---|
| 68 |
|
|---|
| 69 | fun.simple_cmdline('help', get_help)
|
|---|
| 70 |
|
|---|
| 71 | log('Testing to register settings keys')
|
|---|
| 72 | conf.register_path('hello', 'PYTHON SETTINGS', 'This is stuff for python')
|
|---|
| 73 | conf.register_key('hello', 'python', 'int', 'KEY', 'This is a key', '42')
|
|---|
| 74 |
|
|---|
| 75 | log('Testing to get key (nonexistant): %d' % conf.get_int('hello', 'python', -1))
|
|---|
| 76 | conf.set_int('hello', 'python', 4)
|
|---|
| 77 | log('Testing to get it (after setting it): %d' % conf.get_int('hello', 'python', -1))
|
|---|
| 78 |
|
|---|
| 79 | log('Saving configuration...')
|
|---|
| 80 | conf.save()
|
|---|
| 81 |
|
|---|
| 82 | def shutdown():
|
|---|
| 83 | log('Unloading script...')
|
|---|