天天看点

Nagios 利用NSClient++的check_nrpe方式使用自定义脚本监控windowsExit

如果在windows下用nagios实现资源监控,则需要用到NSClient++,这里分享一个NSClient++自定义脚本,有需要的朋友参考下。

实现:

首先,编辑NSClient配置文件NSC.ini,去掉以下几行前面的“;”号(去掉注释,使其生效):

NRPEListener.dll

script_dir=scripts\

CheckExternalScripts.dll

代码示例:

@echo off

tasklist |find "%1" >NUL

IF ERRORLEVEL 1 GOTO err

IF ERRORLEVEL 0 GOTO ok

:err

echo CRITICAL: Process does not exist

start "" "%2\%1"

exit /B 2

:ok

echo OK: The process of normal

exit /B 0

在NSC.ini文件中[External Scripts]下面添加如下行:

check_run=scripts\check_run.bat   Process.exe    pro/path

重启NSClient服务。

在nagios监控主机中,添加如下服务信息:

define service{

        use                   generic-service

        host_name             servername

        service_description   check_processname

        check_command         check_nrpe!check_run

        }

重启nagios服务,使配置生效。

监控windows上tcp连接数:

netstat -an | find /C "TCP" > d:\tcpcount.txt

for /f %%i in (d:\tcpcount.txt) do (

set tcpc=%%i

)

if %tcpc% GTR 5000 ( 

echo  criting: tcp connection count %tcpc%

if not %tcpc% GTR 5000 if  %tcpc% GTR 3000 (

echo  warning: tcp connection count %tcpc%

exit /B 1

)  

if not %tcpc% GTR 3000 ( 

echo  ok: tcp connection count %tcpc%

监控windows文件中关键词出现次数:

set FILE=D:\dcsLogs\logback\pxylist\common\common-all.log

set ERROR=OutOfMemoryError

for /f "delims=" %%a in ('type %FILE%^|find /C "%ERROR%"') do set myvar=%%a

if %myvar% NEQ 0 ( 

echo CRITICAL:error count %myvar%

if %myvar% EQU 0 ( 

echo OK:error count %myvar%

监控windows文件中关键词出现次数(增加判断文件是否存在):

setlocal EnableDelayedExpansion

if EXIST %FILE% (

for /f "delims=" %%a in ('type %FILE%^|find /C /I "%ERROR%"') do set myvar=%%a

if !myvar! NEQ 0 ( 

echo CRITICAL:error count !myvar!-OutOfMemory,Please restart pxylist

)else ( 

echo OK:error count !myvar!

)else (

echo CRITICAL:%FILE% is not exist

监控windows上多个进程名各自是否只跑了一个进程:

SET PROCESS1=telegraph.exe

SET PROCESS2=WindowsApplication1.exe

for /f "delims=" %%a in ('tasklist /v /nh /fo csv /fi "IMAGENAME eq %PROCESS1%"^|find /c "%PROCESS1%"') do set myvar=%%a

if %myvar% NEQ 1 (

echo CRITICAL: %PROCESS1% running %myvar% process

for /f "delims=" %%a in ('tasklist /v /nh /fo csv /fi "IMAGENAME eq %PROCESS2%"^|find /c "%PROCESS2%"') do set myvar=%%a

echo CRITICAL: %PROCESS2% running %myvar% process

for /f "delims=" %%a in ('tasklist /nh ^|findstr "%PROCESS1% %PROCESS2%"^|find /v /c "::"') do set myvar=%%a

if %myvar% EQU 2 (

echo OK: %PROCESS1% %PROCESS2% all running 1 process

----------------------------------------------------------------------------------

退出当前批处理脚本或 Cmd.exe 程序(即,命令解释程序)并返回到曾启动 Cmd.exe 的程序或返回到“程序管理器”。

exit [/b] [ExitCode]

/b

退出当前批处理脚本。

ExitCode

指定数字编号。

/?

在命令提示符显示帮助。

如果在批处理脚本之外使用 /b,就会退出 Cmd.exe。

如果使用 /b,Cmd.exe 将 ERRORLEVEL 设置为指定的 ExitCode。如果退出 Cmd.exe,Cmd.exe 则使用指定的 ExitCode 设置进程退出代码。

关于 exit /b [ExitCode]的详解:

功能: Cmd.exe 将 ERRORLEVEL 设置为指定的 ExitCode。如果退出 Cmd.exe,Cmd.exe 则使用指定的 ExitCode 设置进程退出代码。

示例:leo.bat。

echo 命令

exit /b 30194447

然后,我们在 CMD 命令提示符下进行操作:

执行:leo.bat

执行:Echo %errorlevel%

30194447

本文转自leonardos51CTO博客,原文链接:http://blog.51cto.com/leomars/1894581 ,如需转载请自行联系原作者

继续阅读