1 / 40

Scripting...

Scripting. …on the Windows side. Disclaimer!. These slides represent the work and opinions of the author and do not constitute official positions of any organization sponsoring the author’s work

Download Presentation

Scripting...

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scripting... …on the Windows side

  2. Disclaimer! • These slides represent the work and opinions of the author and do not constitute official positions of any organization sponsoring the author’s work • This material has not been peer reviewed and is presented here as-is with the permission of the author. • The author assumes no liability for any content or opinion expressed in this presentation and or use of content herein. It is not their fault! It is not my fault! It is your fault!

  3. Agenda • External Scripts • Internal Scripts • Arguments • Scripting: Batch files • Wrapped scripts • Scripting: VBA • Internal Scripts

  4. Windows monitoring NSClient++ (from a scripters perspecitve)

  5. Two kinds of scripts • External Scripts • The normal kind of scripts • Can be written in: • Batch • VBA/VBScript (pretty popular on Windows) • Powershell (a rather strange language) • But also: • Perl, python, bash, etc etc… • Internal Scripts • Can interact with (other) internal commands • Can access settings • Can hold state • Can be written in: • Lua • Python (requires python on the machine)

  6. Configuring External Scripts • Enable the check module [/modules] CheckExternalScripts= # Runs the script NRPEServer= # NRPE server • Each script requires a definition [/settings/External Scripts] check_es_test=scripts\test.bat • Options disabled by default (for a reason) allow arguments = false allow nasty characters = false

  7. Configuring Internal Scripts • Enable the check module [/modules] LUAScript= PythonScript= • Each script requires a definition [/settings/LUA/Scripts] <alias>=test.lua [/settings/python/Scripts] <alias>=test.py • Scripts requires NRPE/NSCA (or NSCP) [/modules] NRPEServer=

  8. Allow arguments • Can be configured in many places • Is probably more confusing then it is worth • The server module • Means NO commands can have arguments • The script module • Means NO external script can have arguments

  9. Allow arguments

  10. Writing our first Scripts The first batch script

  11. Writing a Script (Batch) • Output: • Use: echo <text> • Don’t forget @echo off (or all commands will be echoed) • Exit statuses: • Use: exit <code> • 0 = OK • 1 = Warning • 2 = Critical • 3 = Unknown • NSC.ini syntax: [/settings/External Scripts/scripts] my_script=scripts\script.bat • Reference: • http://www.ss64.com/nt/ • Don’t let preconceptions fool you: batch can actually do a lot!

  12. A basic script (batch) @echo off echo CRITICAL: Everything is not going to be fine exit 2

  13. Running from Command Line …\NSClient++\scripts>cmd /ctest.bat CRITICAL: Everything is not going to be fine …\NSClient++\scripts>echo %ERRORLEVEL% 2

  14. Running from NSClient D:\demo>nscp --test NSClient++ 0,4,0,98 2011-09-06 x64 booting... Boot.ini found in: D:/demo//boot.ini Boot order: ini://${shared-path}/nsclient.ini, old://${exe-path}/nsc.ini Activating: ini://${shared-path}/nsclient.ini Creating instance for: ini://${shared-path}:80/nsclient.ini Reading INI settings from: D:/demo//nsclient.ini Loading: D:/demo//nsclient.ini from ini://${shared-path}/nsclient.ini Booted settings subsystem... On crash: restart: NSClientpp Archiving crash dumps in: D:/demo//crash-dumps booting::loading plugins Found: CheckExternalScripts as Processing plugin: CheckExternalScripts.dll as addPlugin(D:/demo//modules/CheckExternalScripts.dll as ) Loading plugin: Check External Scripts as NSClient++ - 0,4,0,98 2011-09-06 Started! Enter command to inject or exit to terminate...

  15. Running from NSClient Enter command to inject or exit to terminate... my_scripts Injecting: my_script... Arguments: Result my_script: WARNING WARNING:Hello World Command Return Status Return Message

  16. Demo Writing our first Scripts

  17. Writing our first Scripts Killing notepad once and or all!

  18. Killing notepad once and for all! TASKKILL [/S dator [/U användarnamn [/P lösenord]]]] { [/FI filter] [/PID process-ID | /IM avbildning] } [/T][/F] Beskrivning: Det här verktyget används för att avsluta en eller flera aktiviteter utifrån process-ID (PID) eller avbildningsnamn. Parameterlista: … /FI filter Använder ett filter för att välja aktiviteter. Jokertecknet * kan användas, t.ex: imagenameeq note* /PID process-ID Anger process-ID för den process som ska avbrytas. Använd kommandot Tasklist för att hämta process-ID /IM avbildning Anger avbildning för den process som för den process som ska avslutas. Jokertecknet * användas för att ange alla aktiviteter eller avbildningar.

  19. KILL!!! @echo off taskkill /im notepad.exe 1>NUL 2>NUL IF ERRORLEVEL 128 GOTO err IF ERRORLEVEL 0 GOTO ok GOTO unknown :unknown echo UNKNOWN: unknown problem killing notepad... exit /B 3 :err echo CRITICAL: Notepad was not killed... exit /B 1 :ok echo OK: Notepad was killed! exit /B 0

  20. Demo Killing notepad…

  21. Wrapped scripts Interlude

  22. Adding a Script (.bat) • NSC.ini syntax: • [External Scripts] • check_bat=scripts\check_test.bat • Or • [Wrapped Scripts] • check_test=check_test.bat

  23. Adding a Script (.VBS) • NSC.ini syntax: • [External Scripts] • check_test=cscript.exe /T:30 /NoLogo scripts\check_test.vbs • Or • [Wrapped Scripts] • check_test=check_test.vbs

  24. Adding a Script (.VBS) w/ libs • NSC.ini syntax: • [External Scripts] • check_test=cscript.exe /T:30 /NoLogo scripts\lib\wrapper.vbs scripts\check_test.vbs • Or • [Wrapped Scripts] • check_test=check_test.vbs

  25. Adding a Script (.PS1) • NSC.ini syntax: • [External Scripts] • check_test=cmd /c echo scripts\check_test.ps1; exit($lastexitcode) | powershell.exe -command - • Or • [Wrapped Scripts] • check_test=check_test.ps1

  26. What is wrapped scripts? […/wrappings] bat=scripts\%SCRIPT% %ARGS% vbs=cscript.exe //T:30 //NoLogo scripts\lib\wrapper.vbs %SCRIPT% %ARGS% ps1=cmd /c echo scripts\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command - […/wrapped scripts] check_test_vbs=check_test.vbs /arg1:1 /variable:1 check_test_ps1=check_test.ps1 arg1 arg2 check_test_bat=check_test.bat $ARG1$ arg2 check_battery=check_battery.vbs check_printer=check_printer.vbs ; So essentially it is a macro! (but a nice one)

  27. Writing your first Scripts Writing a simple VB script

  28. Writing a Script (VBS) • Output: • Use: Wscript.StdOut.WriteLine <text> • Exit statuses: • Use: Wscript.Quit(<code>) • 0 = OK • 1 = Warning • 2 = Critical • 3 = Unknown • NSC.ini syntax: [External Scripts] check_vbs=cscript.exe //T:30 //NoLogo scripts\check_vbs.vbs //T:30 Is the timeout and might need to be changed. • Reference: • http://msdn.microsoft.com/en-us/library/t0aew7h6(VS.85).aspx

  29. Hello_World.vbs wscript.echo ”Hello World" wscript.quit(0)

  30. Object oriented programming (ish) • Set <variable name>=CreateObject(“<COM Object>") • There is A LOT of objects you can create • A nice way to interact with other applications • For instance: • Set objWord = CreateObject("Word.Application") • objWord.Visible = True • Set objDoc = objWord.Documents.Add() • Set objSelection = objWord.Selection • objSelection.Font.Name = “Comic Sans MS" • objSelection.Font.Size = “28" • objSelection.TypeText“Hello World" • objSelection.TypeParagraph() • objSelection.Font.Size = "14" • objSelection.TypeText "" & Date() • objSelection.TypeParagraph()

  31. Demo: Words…

  32. Are we running windows? strFile=”c:\windows” Dim oFSO Set oFSO=CreateObject("Scripting.FileSystemObject") If oFSO.FileExists(strFile) Then wscript.echo”Yaay!" wscript.quit(0) else wscript.echo“Whhh… what the f***!" wscript.quit(2) end if

  33. Demo: Are we running Windows?

  34. Using the library Dissecting a VBScript

  35. Internal Scripts • Can be used to extend NSClient++ • Are very powerful • A good way to: • Alter things you do not like • Create advanced things • Are written in Lua or Python • Possibly unsafe • Runs inside NSClient++

  36. Anatomy of an internal script • Internal scripts are fundamentally different • One script is NOT equals to one function • A script (at startup) can: • Register query (commands) handlers • Register submission (passive checks) handlers • Register exec handlers • Register configuration • Access configuration • Handlers can: • Execute queries (commands) • Submit submissions (passive checks) • Etc etc…

  37. A basic script definit(plugin_id, plugin_alias, script_alias): conf = Settings.get() reg = Registry.get(plugin_id) reg.simple_cmdline('help', get_help) reg.simple_function(‘command', cmd, ‘A command…') conf.set_int('hello', 'python', 42) log(Answer: %d'%conf.get_int('hello','python',-1)) def shutdown(): log(“Shutting down…”)

  38. A basic script defget_help(arguments): return (status.OK, ‘Im not helpful ') def test(arguments): core = Core.get() count = len(arguments) (retcode, retmessage, retperf) = core.simple_query(‘CHECK_NSCP’, []) return (status.OK, ‘Life is good… %d'%count)

  39. Questions? Q&A

  40. Thank You! Michael Medin michael@medin.name http://www.linkedin.com/in/mickem Information about NSClient++ http://nsclient.org Facebook: facebook.com/nsclient Slides, and examples http://nsclient.org/nscp/conferances/2011/nwcna/

More Related