Scripting...<br />…on the Windows side<br />
These slides represent the work and opinions of the author and do not constitute official positions of any organization sp...
External Scripts<br />Internal Scripts<br />Arguments<br />Scripting: Batch files<br />Wrapped scripts<br />Scripting: VBA...
Windows monitoring<br />NSClient++ (from a scripters perspecitve)<br />
External Scripts<br />The normal kind of scripts<br />Can be written in:<br />Batch<br />VBA/VBScript (pretty popular on W...
Enable the check module<br />[/modules]<br />CheckExternalScripts=	# Runs the script<br />NRPEServer=			# NRPE server<br /...
Enable the check module<br />[/modules]<br />LUAScript=<br />PythonScript=<br />Each script requires a definition<br />[/s...
Can be configured in many places<br />Is probably more confusing then it is worth<br />The server module<br />Means NO com...
Allow arguments<br />
Writing our first Scripts<br />The first batch script<br />
Output:<br />Use: echo <text><br />Don’t forget @echo off (or all commands will be echoed)<br />Exit statuses:<br />Use: e...
@echo off<br />echo CRITICAL: Everything is not going to be fine<br />exit 2<br />A basic script (batch)<br />
…NSClient++scripts>cmd /ctest.bat<br />CRITICAL: Everything is not going to be fine<br />…NSClient++scripts>echo %ERRORLEV...
D:demo>nscp --test<br />NSClient++ 0,4,0,98 2011-09-06 x64 booting...<br />Boot.ini found in: D:/demo//boot.ini<br />Boot ...
Enter command to inject or exit to terminate...<br />my_scripts<br />Injecting: my_script...<br />Arguments:<br />Result m...
Demo<br />Writing our first Scripts<br />
Writing our first Scripts<br />Killing notepad once and or all!<br />
TASKKILL [/S dator [/U användarnamn [/P lösenord]]]]<br />         { [/FI filter] [/PID process-ID | /IM avbildning] } [/T...
@echo off<br />taskkill /im notepad.exe 1>NUL 2>NUL<br />IF ERRORLEVEL 128 GOTO err<br />IF ERRORLEVEL 0 GOTO ok<br />GOTO...
Demo<br />Killing notepad…<br />
Wrapped scripts<br />Interlude<br />
NSC.ini syntax:<br />[External Scripts]<br />check_bat=scriptscheck_test.bat<br />Or<br />[Wrapped Scripts]<br />check_tes...
NSC.ini syntax:<br />[External Scripts]<br />check_test=cscript.exe /T:30 /NoLogo scriptscheck_test.vbs<br />Or<br />[Wrap...
NSC.ini syntax:<br />[External Scripts]<br />check_test=cscript.exe /T:30 /NoLogo scriptslibwrapper.vbs scriptscheck_test....
NSC.ini syntax:<br />[External Scripts]<br />check_test=cmd /c echo scriptscheck_test.ps1; exit($lastexitcode) | powershel...
[…/wrappings]<br />bat=scripts%SCRIPT% %ARGS%<br />vbs=cscript.exe //T:30 //NoLogo scriptslibwrapper.vbs %SCRIPT% %ARGS%<b...
Writing your first Scripts<br />Writing a simple VB script<br />
Output:<br />Use: Wscript.StdOut.WriteLine <text><br />Exit statuses:<br />Use: Wscript.Quit(<code>)<br />0 = OK<br />1 = ...
wscript.echo ”Hello World"<br />wscript.quit(0)<br />Hello_World.vbs<br />
Set <variable name>=CreateObject(“<COM Object>")<br />There is A LOT of objects you can create<br />A nice way to interact...
Demo:<br />Words…<br />
strFile=”c:windows”<br />Dim oFSO<br />Set oFSO=CreateObject("Scripting.FileSystemObject")<br />If oFSO.FileExists(strFile...
Demo:<br />Are we running Windows?<br />
Using the library<br />Dissecting a VBScript<br />
Can be used to extend NSClient++<br />Are very powerful<br />A good way to:<br />Alter things you do not like<br />Create ...
Internal scripts are fundamentally different<br />One script is NOT equals to one function<br />A script (at startup) can:...
definit(plugin_id, plugin_alias, script_alias):<br />conf = Settings.get()<br />reg = Registry.get(plugin_id)<br />reg.sim...
defget_help(arguments):<br />	return (status.OK, ‘Im not helpful ')<br />def test(arguments):<br />	core = Core.get()<br ...
Questions?<br />Q&A<br />
Michael Medin<br />michael@medin.name<br />http://www.linkedin.com/in/mickem<br />Information about NSClient++<br />http:/...
Upcoming SlideShare
Loading in …5
×

Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side

2,690 views

Published on

Michael Medin's workshop on Windows scripting for Nagios. The workshop was given during the Nagios World Conference North America held Sept 27-29th, 2011 in Saint Paul, MN. For more information on the conference (including photos and videos), visit: http://go.nagios.com/nwcna

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,690
On SlideShare
0
From Embeds
0
Number of Embeds
109
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Hello my name is Michael Medin.I am from Stockholm, Sweden.I will give this in English just because I think everyone else does...If there are any questions or such just chime in.Feel free to ask questions in Swedish if you wish.
  • Standard Disclaimer - My views (not anyone else&apos;s) - Not peer reviewed so I could be lying to you. - If you 2 billion dollar servers crash: life sucksLets simplify this a bit…
  • cmd /c is because we need to capture the “exit code” normally exit will exit all running shellsWe get the message but no error codeUse echo %ERRORLEVEL% to display the exit code
  • We run the scriptGet the error code (Critical) and the Message (...)But we get no performance data
  • We run the scriptGet the error code (Critical) and the Message (...)But we get no performance data
  • 2 minutes
  • 2 minutes
  • 2 minutes
  • Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side

    1. 1. Scripting...<br />…on the Windows side<br />
    2. 2. These slides represent the work and opinions of the author and do not constitute official positions of any organization sponsoring the author’s work <br />This material has not been peer reviewed and is presented here as-is with the permission of the author.<br />The author assumes no liability for any content or opinion expressed in this presentation and or use of content herein.<br />Disclaimer!<br />It is not their fault!<br />It is not my fault!<br />It is your fault!<br />
    3. 3. External Scripts<br />Internal Scripts<br />Arguments<br />Scripting: Batch files<br />Wrapped scripts<br />Scripting: VBA<br />Internal Scripts<br />Agenda<br />
    4. 4. Windows monitoring<br />NSClient++ (from a scripters perspecitve)<br />
    5. 5. External Scripts<br />The normal kind of scripts<br />Can be written in:<br />Batch<br />VBA/VBScript (pretty popular on Windows)<br />Powershell (a rather strange language)<br />But also:<br />Perl, python, bash, etc etc…<br />Internal Scripts<br />Can interact with (other) internal commands<br />Can access settings<br />Can hold state<br />Can be written in:<br />Lua<br />Python (requires python on the machine)<br />Two kinds of scripts<br />
    6. 6. Enable the check module<br />[/modules]<br />CheckExternalScripts= # Runs the script<br />NRPEServer= # NRPE server<br />Each script requires a definition<br />[/settings/External Scripts]<br />check_es_test=scriptstest.bat<br />Options disabled by default (for a reason)<br />allow arguments = false<br />allow nasty characters = false<br />Configuring External Scripts<br />
    7. 7. Enable the check module<br />[/modules]<br />LUAScript=<br />PythonScript=<br />Each script requires a definition<br />[/settings/LUA/Scripts]<br /><alias>=test.lua<br />[/settings/python/Scripts]<br /><alias>=test.py<br />Scripts requires NRPE/NSCA (or NSCP)<br />[/modules]<br />NRPEServer=<br />Configuring Internal Scripts<br />
    8. 8. Can be configured in many places<br />Is probably more confusing then it is worth<br />The server module<br />Means NO commands can have arguments<br />The script module<br />Means NO external script can have arguments<br />Allow arguments<br />
    9. 9. Allow arguments<br />
    10. 10. Writing our first Scripts<br />The first batch script<br />
    11. 11. Output:<br />Use: echo <text><br />Don’t forget @echo off (or all commands will be echoed)<br />Exit statuses:<br />Use: exit <code><br />0 = OK<br />1 = Warning<br />2 = Critical<br />3 = Unknown<br />NSC.ini syntax:<br />[/settings/External Scripts/scripts]<br />my_script=scriptsscript.bat<br />Reference:<br />http://www.ss64.com/nt/<br />Don’t let preconceptions fool you: batch can actually do a lot!<br />Writing a Script (Batch)<br />
    12. 12. @echo off<br />echo CRITICAL: Everything is not going to be fine<br />exit 2<br />A basic script (batch)<br />
    13. 13. …NSClient++scripts>cmd /ctest.bat<br />CRITICAL: Everything is not going to be fine<br />…NSClient++scripts>echo %ERRORLEVEL%<br />2<br />Running from Command Line<br />
    14. 14. D:demo>nscp --test<br />NSClient++ 0,4,0,98 2011-09-06 x64 booting...<br />Boot.ini found in: D:/demo//boot.ini<br />Boot order: ini://${shared-path}/nsclient.ini, old://${exe-path}/nsc.ini<br />Activating: ini://${shared-path}/nsclient.ini<br />Creating instance for: ini://${shared-path}:80/nsclient.ini<br />Reading INI settings from: D:/demo//nsclient.ini<br />Loading: D:/demo//nsclient.ini from ini://${shared-path}/nsclient.ini<br />Booted settings subsystem...<br />On crash: restart: NSClientpp<br />Archiving crash dumps in: D:/demo//crash-dumps<br />booting::loading plugins<br />Found: CheckExternalScripts as<br />Processing plugin: CheckExternalScripts.dll as<br />addPlugin(D:/demo//modules/CheckExternalScripts.dll as )<br />Loading plugin: Check External Scripts as<br />NSClient++ - 0,4,0,98 2011-09-06 Started!<br />Enter command to inject or exit to terminate...<br />Running from NSClient<br />
    15. 15. Enter command to inject or exit to terminate...<br />my_scripts<br />Injecting: my_script...<br />Arguments:<br />Result my_script: WARNING<br />WARNING:Hello World<br />Running from NSClient<br />Command<br />Return Status<br />Return Message<br />
    16. 16. Demo<br />Writing our first Scripts<br />
    17. 17. Writing our first Scripts<br />Killing notepad once and or all!<br />
    18. 18. TASKKILL [/S dator [/U användarnamn [/P lösenord]]]]<br /> { [/FI filter] [/PID process-ID | /IM avbildning] } [/T][/F]<br />Beskrivning:<br /> Det här verktyget används för att avsluta en eller flera aktiviteter<br /> utifrån process-ID (PID) eller avbildningsnamn.<br />Parameterlista:<br />…<br /> /FI filter Använder ett filter för att välja aktiviteter.<br /> Jokertecknet * kan användas, t.ex:<br />imagenameeq note*<br /> /PID process-ID Anger process-ID för den process som ska avbrytas.<br /> Använd kommandot Tasklist för att hämta process-ID<br /> /IM avbildning Anger avbildning för den process som<br /> för den process som ska avslutas. Jokertecknet *<br /> användas för att ange alla aktiviteter eller<br /> avbildningar.<br />Killing notepad once and for all!<br />
    19. 19. @echo off<br />taskkill /im notepad.exe 1>NUL 2>NUL<br />IF ERRORLEVEL 128 GOTO err<br />IF ERRORLEVEL 0 GOTO ok<br />GOTO unknown<br />:unknown<br />echo UNKNOWN: unknown problem killing notepad...<br />exit /B 3<br />:err<br />echo CRITICAL: Notepad was not killed...<br />exit /B 1<br />:ok<br />echo OK: Notepad was killed!<br />exit /B 0<br />KILL!!!<br />
    20. 20. Demo<br />Killing notepad…<br />
    21. 21. Wrapped scripts<br />Interlude<br />
    22. 22. NSC.ini syntax:<br />[External Scripts]<br />check_bat=scriptscheck_test.bat<br />Or<br />[Wrapped Scripts]<br />check_test=check_test.bat<br />Adding a Script (.bat)<br />
    23. 23. NSC.ini syntax:<br />[External Scripts]<br />check_test=cscript.exe /T:30 /NoLogo scriptscheck_test.vbs<br />Or<br />[Wrapped Scripts]<br />check_test=check_test.vbs<br />Adding a Script (.VBS)<br />
    24. 24. NSC.ini syntax:<br />[External Scripts]<br />check_test=cscript.exe /T:30 /NoLogo scriptslibwrapper.vbs scriptscheck_test.vbs<br />Or<br />[Wrapped Scripts]<br />check_test=check_test.vbs<br />Adding a Script (.VBS) w/ libs<br />
    25. 25. NSC.ini syntax:<br />[External Scripts]<br />check_test=cmd /c echo scriptscheck_test.ps1; exit($lastexitcode) | powershell.exe -command -<br />Or<br />[Wrapped Scripts]<br />check_test=check_test.ps1<br />Adding a Script (.PS1)<br />
    26. 26. […/wrappings]<br />bat=scripts%SCRIPT% %ARGS%<br />vbs=cscript.exe //T:30 //NoLogo scriptslibwrapper.vbs %SCRIPT% %ARGS%<br />ps1=cmd /c echo scripts%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -<br />[…/wrapped scripts]<br />check_test_vbs=check_test.vbs /arg1:1 /variable:1<br />check_test_ps1=check_test.ps1 arg1 arg2<br />check_test_bat=check_test.bat $ARG1$ arg2<br />check_battery=check_battery.vbs<br />check_printer=check_printer.vbs<br />; So essentially it is a macro! (but a nice one)<br />What is wrapped scripts?<br />
    27. 27. Writing your first Scripts<br />Writing a simple VB script<br />
    28. 28. Output:<br />Use: Wscript.StdOut.WriteLine <text><br />Exit statuses:<br />Use: Wscript.Quit(<code>)<br />0 = OK<br />1 = Warning<br />2 = Critical<br />3 = Unknown<br />NSC.ini syntax:<br />[External Scripts]<br />check_vbs=cscript.exe //T:30 //NoLogo scriptscheck_vbs.vbs<br /> //T:30 Is the timeout and might need to be changed.<br />Reference:<br />http://msdn.microsoft.com/en-us/library/t0aew7h6(VS.85).aspx<br />Writing a Script (VBS)<br />
    29. 29. wscript.echo ”Hello World"<br />wscript.quit(0)<br />Hello_World.vbs<br />
    30. 30. Set <variable name>=CreateObject(“<COM Object>")<br />There is A LOT of objects you can create<br />A nice way to interact with other applications<br />For instance:<br />Set objWord = CreateObject("Word.Application")<br />objWord.Visible = True<br />Set objDoc = objWord.Documents.Add()<br />Set objSelection = objWord.Selection<br />objSelection.Font.Name = “Comic Sans MS"<br />objSelection.Font.Size = “28"<br />objSelection.TypeText“Hello World"<br />objSelection.TypeParagraph()<br />objSelection.Font.Size = "14"<br />objSelection.TypeText "" & Date()<br />objSelection.TypeParagraph()<br />Object oriented programming (ish)<br />
    31. 31. Demo:<br />Words…<br />
    32. 32. strFile=”c:windows”<br />Dim oFSO<br />Set oFSO=CreateObject("Scripting.FileSystemObject")<br />If oFSO.FileExists(strFile) Then<br />wscript.echo”Yaay!"<br />wscript.quit(0)<br />else<br />wscript.echo“Whhh… what the f***!"<br />wscript.quit(2)<br />end if<br />Are we running windows?<br />
    33. 33. Demo:<br />Are we running Windows?<br />
    34. 34. Using the library<br />Dissecting a VBScript<br />
    35. 35. Can be used to extend NSClient++<br />Are very powerful<br />A good way to:<br />Alter things you do not like<br />Create advanced things<br />Are written in Lua or Python<br />Possibly unsafe<br />Runs inside NSClient++<br />Internal Scripts<br />
    36. 36. Internal scripts are fundamentally different<br />One script is NOT equals to one function<br />A script (at startup) can:<br />Register query (commands) handlers<br />Register submission (passive checks) handlers<br />Register exec handlers<br />Register configuration<br />Access configuration<br />Handlers can:<br />Execute queries (commands)<br />Submit submissions (passive checks)<br />Etc etc…<br />Anatomy of an internal script<br />
    37. 37. definit(plugin_id, plugin_alias, script_alias):<br />conf = Settings.get()<br />reg = Registry.get(plugin_id)<br />reg.simple_cmdline('help', get_help)<br />reg.simple_function(‘command', cmd, ‘A command…')<br />conf.set_int('hello', 'python', 42)<br /> log(Answer: %d'%conf.get_int('hello','python',-1))<br />def shutdown():<br /> log(“Shutting down…”)<br />A basic script<br />
    38. 38. defget_help(arguments):<br /> return (status.OK, ‘Im not helpful ')<br />def test(arguments):<br /> core = Core.get()<br />count = len(arguments)<br />(retcode, retmessage, retperf) =<br />core.simple_query(‘CHECK_NSCP’, [])<br />return (status.OK, ‘Life is good… %d'%count)<br />A basic script<br />
    39. 39. Questions?<br />Q&A<br />
    40. 40. Michael Medin<br />michael@medin.name<br />http://www.linkedin.com/in/mickem<br />Information about NSClient++<br />http://nsclient.org<br />Facebook: facebook.com/nsclient<br />Slides, and examples<br />http://nsclient.org/nscp/conferances/2011/nwcna/<br />Thank You!<br />

    ×