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