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
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 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. Disclaimer! It is not their fault! It is not my fault! It is your fault!
5. 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) Two kinds of scripts
6. Enable the check module [/modules] CheckExternalScripts= # Runs the script NRPEServer= # NRPE server Each script requires a definition [/settings/External Scripts] check_es_test=scriptsest.bat Options disabled by default (for a reason) allow arguments = false allow nasty characters = false Configuring External Scripts
7. 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= Configuring Internal Scripts
8. 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 Allow arguments
14. D:emo>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... Running from NSClient
15. Enter command to inject or exit to terminate... my_scripts Injecting: my_script... Arguments: Result my_script: WARNING WARNING:Hello World Running from NSClient Command Return Status Return Message
18. 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. Killing notepad once and for all!
19. @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 KILL!!!
30. 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() Object oriented programming (ish)
32. strFile=”c:indows” 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 Are we running windows?
35. 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++ Internal Scripts
36. 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… Anatomy of an internal script
40. 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/ Thank You!
Notas del editor
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