Check ALL volumes for less than % free space

If you have information, script, utility, or idea that can be useful for HostMonitor community, you welcome to share information in this forum.
Post Reply
jivetolkein
Posts: 96
Joined: Thu Jul 19, 2007 4:35 am

Check ALL volumes for less than % free space

Post by jivetolkein »

New to HM, so maybe this isn't an ideal way to do this, but here is a script that will parse all volumes on a remote server, and check that they don't fall below a critical value - if one does, the script returns a Bad status to HM. It always returns the current % of free space.
Many thanks to Rob van der Woude for his original script (and for allowing me to reproduce it here). Set the Critical variable to the % of free space you'd like it to error on. Run as a shell script, and pass the server name to check as a parameter (no \\ needed).
Seems to run from an RMA OK. W2K/W2K3 only - I'm sure getting WMI running on NT4 is possible, but not something I'd like to do to a production server.

Hopefully, somebody can tell me how to pull graphs out of the output data a la Nagiosgraph :-)



>Select Case WScript.Arguments.Count
> Case 0
> ' Default if none specified is local computer (".")
> Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
> Set colItems = objWMIService.ExecQuery( "Select * from
>Win32_ComputerSystem", , 48 )
> For Each objItem in colItems
> strComputer = objItem.Name
> Next
> Case 1
> ' Command line parameter can either be a computer name
> ' or "/?" to request online help
> strComputer = Wscript.Arguments(0)
> if InStr( strComputer, "?" ) > 0 Then Syntax
> Case Else
> ' Maximum is 1 command line parameter
> Syntax
>End Select
>
>'Set some variables
>'Default Status
>strStatus = "UNKNOWN:"
>'Threshold for Error - % free space on a volume
>Critical = 5
>
>Display( strComputer )
>WScript.Quit(0)
>
>
>
>Function Display( strComputer )
> strMsg = ""
> strStatus = "OK:"
> strFree = ""
> On Error Resume Next
> Set objWMIService = GetObject( "winmgmts://" & strComputer &
>"/root/cimv2" )
> If Err.Number Then
> WScript.Echo vbCrLf & "Error # " & CStr( Err.Number ) & _
> " " & Err.Description
> Err.Clear
> Syntax
> End If
> On Error GoTo 0
> ' Set colItems = objWMIService.ExecQuery("Select * from
>Win32_LogicalDisk where MediaType=12",,48)
> Set colItems = objWMIService.ExecQuery("Select * from
>Win32_LogicalDisk where DriveType=3",,48)
> For Each objItem in colItems
> 'Get the free space in terms of a percentage
> Free = Int( 0.5 + ( 100 * objItem.FreeSpace /
>objItem.Size) )
> 'If ANY volume falls below the critical level, set the AHM
>status to BAD
>
> If Free<Critical Then strStatus = "BAD:"
>
>
> 'Add this volumes free space percentage
> strMsg = strMsg & objItem.Name & " " & Free & " %" & " "
>
> Next
> WScript.Echo "Scriptres:" & strStatus & " " & strMsg
>End Function
>
>
>Sub Syntax
> strMsg = vbCrLf & "FreeDiskSpaceAHM.vbs, Version 1.00" & vbCrLf &
>_
> "Display free disk space for all local drives." & vbCrLf
>& _
> vbCrLf & _
> "Usage: CSCRIPT FREESPACE.VBS [ computer_name ]" & _
> vbCrLf & vbCrLf & _
> "Where: " & Chr(34) & "computer_name" & Chr(34) & _
> " is the name of a WMI enabled computer on the network" &
>_
> vbCrLf & vbCrLf & _
> "Adaptation of Rob van der Woude's freespace.vbs" &
>vbCrLf & _
> "http://www.robvanderwoude.com" & vbCrLf & _
> "to provide output in an AHM friendly format" & vbCrLf &
>_
> "Glynn Seymour - 07/2007" & vbCrLF
> WScript.Echo strMsg
> strStatus = "UNKNOWN"
> WScript.Quit(1)
>End Sub
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Re: Check ALL volumes for less than % free space

Post by KS-Soft Europe »

jivetolkein wrote:New to HM, so maybe this isn't an ideal way to do this, but here is a script that will parse all volumes on a remote server, and check that they don't fall below a critical value - if one does, the script returns a Bad status to HM. It always returns the current % of free space.
Great job. Thank you. Very nice script.
jivetolkein wrote:Run as a shell script, and pass the server name to check as a parameter (no \\ needed).
Yes, this script should be started using "Shell script" test method. Also such script with minor modifications can be started using "Active script" test method: http://www.ks-soft.net/hostmon.eng/mfra ... htm#script
"Active Script" test method is designed to run test using different script languages such as Visual Basic Script or Java Script.
jivetolkein wrote:W2K/W2K3 only - I'm sure getting WMI running on NT4 is possible, but not something I'd like to do to a production server.
To start script under NT4, you have to install the WMI Core software package for Windows 95, 98, and Windows NT 4.0, available at available at Microsoft's web site: http://www.microsoft.com/downloads/deta ... F236E0E875
jivetolkein wrote:Hopefully, somebody can tell me how to pull graphs out of the output data a la Nagiosgraph :-)
You may use Log Analyzer to build reports with graphs: http://www.ks-soft.net/hostmon.eng/la/index.htm
Log Analyzer is a graphical tool that visualizes the log data. It parses the contents of a log file and presents the data as a variety of charts representing different test statistics.

Regards,
Max
jivetolkein
Posts: 96
Joined: Thu Jul 19, 2007 4:35 am

Post by jivetolkein »

:D

If you have a chance, could you explain how to get it working as Active Script? I had problems doing this as I needed to pass the server name to check as a parameter - a brief explanation/example would be great, ideally with pros/cons against Shell Script method.
jivetolkein
Posts: 96
Joined: Thu Jul 19, 2007 4:35 am

Post by jivetolkein »

Oh, nearly forgot - it should run OK as a standalone script for testing purpose, just copy it out to a .vbs file and run:

cscript //nologo filename.vbs servername
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

jivetolkein wrote:If you have a chance, could you explain how to get it working as Active Script?
The script has to be written according to some requirements: http://www.ks-soft.net/hostmon.eng/mfra ... htm#script
================
1. script must contain "performtest" function. HM always call function with this name
2. "performtest" function must not have any parameters
3. "performtest" function must return string value. This string contains two parts separated by colon (:). First obligatory part contains test status, it can take following values:

* "Host is alive"
* "No answer"
* "Unknown"
* "Unknown host"
* "Ok"
* "Bad"
* "Bad contents"

Second optional part contains "Reply" value, HostMonitor displays this value in Reply field, writes to log files, uses to displays charts (Log Analyzer), etc.
Several examples:
return "Host is alive:1000 ms" | Jscript; Status: Host is alive; Reply: 1000 ms
return "Unknown host:" | Jscript; Status: Unknown host; Reply: empty
performtest = "Ok:300 Kb" | VBscript; Status: Ok; Reply: 300 Kb
performtest = "Bad:90 %" | VBscript; Status: Bad; Reply: 90 %
================
jivetolkein wrote:I had problems doing this as I needed to pass the server name to check as a parameter
It is a problem, that "Active script" does not support parameters. However, you may use "Translate macros" option. With this option enabled you can use special macro variables in your script. http://www.ks-soft.net/hostmon.eng/mfra ... .htm#macro
For instance, you may specify server name into "Comment" field of te test, and use %CommentLine1% variable within the script's body.
jivetolkein wrote:a brief explanation/example would be great, ideally with pros/cons against Shell Script method.
You can find several simple examples of scripts in HostMonitor's directory "Examples". Also you may browse through the forum to find useful examples, like the following: http://www.ks-soft.net/cgi-bin/phpBB/vi ... php?t=4222

Actually, it is not much difference between "Active Script" and "Shell script" test methods in way of using scripting languages. Personally I preffer to use "Shell script" test method because I do not like Visual basic much. ;-)

Regards,
Max
jivetolkein
Posts: 96
Joined: Thu Jul 19, 2007 4:35 am

Post by jivetolkein »

Ah, thanks - the Comment macro is the missing link for me, that will be very useful - I was struggling to find a way to add a macro value to the script that I could then use as the server FQDN, so that I could replicate the test with replicator across 100+ servers at a time.


Not a huge fan of vbscript either, but 99% of our hosts will be Windows - path of least resistance... it's that or push perl out to most of Europe :-)

One final question - the log analyser/graphing is OK with multiple values, like the "C: 10 % D : 56 %" etc my script returns? I have used nagiosgraph to create rrds of such output before, and used Cacti to graph them so I'm slightly OK with regexps. I'd like to extract say the D drive value and graph it over time - just a quick Yes or No is OK, I've got plenty of reading to do before I get too seriously into the graphing functions
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

One final question - the log analyser/graphing is OK with multiple values, like the "C: 10 % D : 56 %" etc my script returns?
No, Log Analyzer cannot process several values in one Reply field because HostMonitor's tests never return such results.
Quote from the manual
If you want Log Analyzer to process Reply values correctly and display charts, use one of the following formats for Reply value:
- decimal_number (like "123", "0", "6456.45". as decimal separator use symbol that is specified on your system, usually a dot (.) or a comma (,))
- decimal_number + space + "Kb" (like "512 Kb", "64 Kb")
- decimal_number + space + "Mb" (like "1024 Mb", "5 Mb")
- decimal_number + space + "Gb" (like "12 Gb", "4 Gb")
- decimal_number + space + "%" (like "50 %", "99 %")
- decimal_number + space + "ms" (like "100 ms", "5400 ms")
Regards
Alex
russionix
Posts: 15
Joined: Thu Dec 27, 2007 3:00 pm

Script runs through "Let's Try" but not through HM

Post by russionix »

I have a version of this script running on my HostMonitor. The start command I am using is:
cmd /c cscript /E:VBScript %Script% %Params%

When I run the script from the command line or from the "Lets try" tab in the script manager, it returns proper results. It works using both "Test by Agent" and "Test by HostMonitor."

However when I run it as a HM test, it fails. In the Reply I see "Error: Invalid result (Glynn Seymour - 07/2007)"

This is one of the lines from the script (which was written by Glynn Seymour).

Is my command string wrong, or is there something else going on?

Thanks.
russionix
Posts: 15
Joined: Thu Dec 27, 2007 3:00 pm

Problem appears to be with sending parameter and Active RMA

Post by russionix »

More information on my previous post. When I test by "Lets try" and put in the IP of an active RMA in "parameters", the script fails with the following output:
[4:31:52 PM] Agent: Active is going to execute "CheckDrives5" script ...
[4:31:59 PM] Script started, invalid result received:
----------
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Error # 70 Permission denied
FreeDiskSpaceAHM.vbs, Version 1.00
Display free disk space for all local drives.
Usage: CSCRIPT FREESPACE.VBS [ computer_name ]
Where: "computer_name" is the name of a WMI enabled computer on the network
Adaptation of Rob van der Woude's freespace.vbs
http://www.robvanderwoude.com
to provide output in an AHM friendly format
Glynn Seymour - 07/2007
----------

Notice the "Error # 70 Permission denied" - does that refer to having the wrong password for HostMonitor Active RMA, or is it a system permission issue?

Since I am running this as an Active RMA, the remote machine should be able to execute the command without a problem. Any advise on where the problem is?
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

Looks like you have specified wrong value into "Params" input box in "Test Properties" window. To make it work you should specify the target server name only or leave this field blank, if you want to check local machine. If you specify more than 1 parameter, script will give you the error you see.

Regards,
Max
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Re: Problem appears to be with sending parameter and Active

Post by KS-Soft Europe »

russionix wrote:[4:31:59 PM] Script started, invalid result received:
Hm. In previous post you wrote everything works fine... :-?
russionix wrote:Notice the "Error # 70 Permission denied" - does that refer to having the wrong password for HostMonitor Active RMA, or is it a system permission issue?
It is WMI error. If you check remote machine using this script, you have to specify credentials within scripts' body. Please, read the following article for details: http://msdn2.microsoft.com/en-us/library/aa389290.aspx
russionix wrote:Since I am running this as an Active RMA, the remote machine should be able to execute the command without a problem. Any advise on where the problem is?
If script checks the system, where RMA is running, you may leave parameters box blank. In this case script will check the local system (local for RMA) and in this case you do not need to provide credentials within scrips body.

Regards,
Max
jivetolkein
Posts: 96
Joined: Thu Jul 19, 2007 4:35 am

Post by jivetolkein »

Actually, you don't have to specify anything within the script (I adopted this script for Host Monitor with the original authors permission).

Normally, whoever is running this script be it Host Monitor or a separate RMA doesn't have administrative access to the server you are checking if you get this error.

Check the account the RMA is running 'as' is an admin of the server you wish to check. The params box should be the FQDN of the server you wish to check.

I have had some curious results with cross domain checks (rma runs as a user in a different domain to the hosting server) which seem authentication/WMI related, but not had time to really nail them down - worst case, as Max suggests, run an RMA on the box to check, that will fix it. Not had to do that yet though, and we have 100+ servers, 5+ domains and growing...
gold5061

This is seriously too good to be true.

Post by gold5061 »

jivetolkein
Posts: 96
Joined: Thu Jul 19, 2007 4:35 am

Post by jivetolkein »

Thanks for jogging my memory, heres an updated version that lets you specify a volume to ignore (paging file, pre sized db volume that sort of thing):

'Set TimeOut
Wscript.Timeout = 60

Select Case WScript.Arguments.Count
Case 0
' Default if none specified is local computer (".")
Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
For Each objItem in colItems
strComputer = objItem.Name
Next
Case 1
' Command line parameter can either be a computer name
' or "/?" to request online help
strComputer = Wscript.Arguments(0)
if InStr( strComputer, "?" ) > 0 Then Syntax
Case Else
' Maximum is 1 command line parameter
Syntax
End Select

'Set some variables
'Default Status
strStatus = "UNKNOWN:"
'Threshold for Error - % free space on a volume
Critical = 5

Display( strComputer )
WScript.Quit(0)



Function Display( strComputer )
strMsg = ""
strStatus = "OK:"
strFree = ""
On Error Resume Next
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
If Err.Number Then
WScript.Echo vbCrLf & "Error # " & CStr( Err.Number ) & _
" " & Err.Description
Err.Clear
Syntax
End If
On Error GoTo 0
' Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where MediaType=12",,48)
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DriveType=3",,48)
For Each objItem in colItems
'Get the free space in terms of a percentage
Free = Int( 0.5 + ( 100 * objItem.FreeSpace / objItem.Size) )
'If ANY volume falls below the critical level, set the AHM status to BAD

If Free<Critical Then
'Check for exclusion drives...
If (objItem.Name = "D:" and Free<critical) and strStatus <> "BAD:" Then
strStatus = "OK:"
else
strStatus = "BAD:"
end if
End If

'Add this volumes free space percentage
strMsg = strMsg & objItem.Name & " " & Free & " %" & " "

Next
WScript.Echo "Scriptres:" & strStatus & " " & strMsg
End Function


Sub Syntax
strMsg = vbCrLf & "FreeDiskSpaceAHM.vbs, Version 1.00" & vbCrLf & _
"Display free disk space for all local drives." & vbCrLf & _
vbCrLf & _
"Usage: CSCRIPT FREESPACE.VBS [ computer_name ]" & _
vbCrLf & vbCrLf & _
"Where: " & Chr(34) & "computer_name" & Chr(34) & _
" is the name of a WMI enabled computer on the network" & _
vbCrLf & vbCrLf & _
"Adaptation of Rob van der Woude's freespace.vbs" & vbCrLf & _
"http://www.robvanderwoude.com" & vbCrLf & _
"to provide output in an AHM friendly format" & vbCrLf & _
"Glynn Seymour - 07/2007" & vbCrLF
WScript.Echo strMsg
strStatus = "UNKNOWN"
WScript.Quit(1)
End Sub

Never had to ignore more than 1 volume, but I reckon it could be kludged by repeating the IF .. chunk or maybe a little logic in the ignore section... ain't no vbscript pro.
Paul_NHS
Posts: 59
Joined: Wed Feb 25, 2009 6:17 am

Post by Paul_NHS »

Don't forget to check out the alternative Free Hard Disk space test here.
http://www.ks-soft.net/cgi-bin/phpBB/vi ... php?t=4832

cheers, Paul
Post Reply