Your SlideShare is downloading. ×
0
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Nagios Conference 2011 - Michael Medin - Workshop: Scripting On The Windows Side
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

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

2,109

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 …

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,109
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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'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
  • Transcript

    • 1. Scripting...<br />…on the Windows side<br />
    • 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. External Scripts<br />Internal Scripts<br />Arguments<br />Scripting: Batch files<br />Wrapped scripts<br />Scripting: VBA<br />Internal Scripts<br />Agenda<br />
    • 4. Windows monitoring<br />NSClient++ (from a scripters perspecitve)<br />
    • 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. 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. Enable the check module<br />[/modules]<br />LUAScript=<br />PythonScript=<br />Each script requires a definition<br />[/settings/LUA/Scripts]<br />&lt;alias&gt;=test.lua<br />[/settings/python/Scripts]<br />&lt;alias&gt;=test.py<br />Scripts requires NRPE/NSCA (or NSCP)<br />[/modules]<br />NRPEServer=<br />Configuring Internal Scripts<br />
    • 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. Allow arguments<br />
    • 10. Writing our first Scripts<br />The first batch script<br />
    • 11. Output:<br />Use: echo &lt;text&gt;<br />Don’t forget @echo off (or all commands will be echoed)<br />Exit statuses:<br />Use: exit &lt;code&gt;<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. @echo off<br />echo CRITICAL: Everything is not going to be fine<br />exit 2<br />A basic script (batch)<br />
    • 13. …NSClient++scripts&gt;cmd /ctest.bat<br />CRITICAL: Everything is not going to be fine<br />…NSClient++scripts&gt;echo %ERRORLEVEL%<br />2<br />Running from Command Line<br />
    • 14. D:demo&gt;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. 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. Demo<br />Writing our first Scripts<br />
    • 17. Writing our first Scripts<br />Killing notepad once and or all!<br />
    • 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. @echo off<br />taskkill /im notepad.exe 1&gt;NUL 2&gt;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. Demo<br />Killing notepad…<br />
    • 21. Wrapped scripts<br />Interlude<br />
    • 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. 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. 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. 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. […/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. Writing your first Scripts<br />Writing a simple VB script<br />
    • 28. Output:<br />Use: Wscript.StdOut.WriteLine &lt;text&gt;<br />Exit statuses:<br />Use: Wscript.Quit(&lt;code&gt;)<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. wscript.echo ”Hello World&quot;<br />wscript.quit(0)<br />Hello_World.vbs<br />
    • 30. Set &lt;variable name&gt;=CreateObject(“&lt;COM Object&gt;&quot;)<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(&quot;Word.Application&quot;)<br />objWord.Visible = True<br />Set objDoc = objWord.Documents.Add()<br />Set objSelection = objWord.Selection<br />objSelection.Font.Name = “Comic Sans MS&quot;<br />objSelection.Font.Size = “28&quot;<br />objSelection.TypeText“Hello World&quot;<br />objSelection.TypeParagraph()<br />objSelection.Font.Size = &quot;14&quot;<br />objSelection.TypeText &quot;&quot; &amp; Date()<br />objSelection.TypeParagraph()<br />Object oriented programming (ish)<br />
    • 31. Demo:<br />Words…<br />
    • 32. strFile=”c:windows”<br />Dim oFSO<br />Set oFSO=CreateObject(&quot;Scripting.FileSystemObject&quot;)<br />If oFSO.FileExists(strFile) Then<br />wscript.echo”Yaay!&quot;<br />wscript.quit(0)<br />else<br />wscript.echo“Whhh… what the f***!&quot;<br />wscript.quit(2)<br />end if<br />Are we running windows?<br />
    • 33. Demo:<br />Are we running Windows?<br />
    • 34. Using the library<br />Dissecting a VBScript<br />
    • 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. 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. definit(plugin_id, plugin_alias, script_alias):<br />conf = Settings.get()<br />reg = Registry.get(plugin_id)<br />reg.simple_cmdline(&apos;help&apos;, get_help)<br />reg.simple_function(‘command&apos;, cmd, ‘A command…&apos;)<br />conf.set_int(&apos;hello&apos;, &apos;python&apos;, 42)<br /> log(Answer: %d&apos;%conf.get_int(&apos;hello&apos;,&apos;python&apos;,-1))<br />def shutdown():<br /> log(“Shutting down…”)<br />A basic script<br />
    • 38. defget_help(arguments):<br /> return (status.OK, ‘Im not helpful &apos;)<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&apos;%count)<br />A basic script<br />
    • 39. Questions?<br />Q&amp;A<br />
    • 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 />

    ×