What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
300.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Custom Sensor with own Message

Votes:

0

Your Vote:

Up

Down

Hi,

in the past hours i am working on a script for monitoring vmware snapshots. Snapshoting is a great technique for backing up esx guests but snapshots also consume lots of storage und memory.

until now we monitor our snapshots via e-mail. at 6 AM a powershell enums all snapshots and generates a excel list which is sent by mail. this is ok but not working very well cause we are not reading our mails daily ;)

now here is a working script vor monitoring snapshot status for each guest. The Script is working fine but the prtg sensor is not going down when snapshot is older then one day. here is the tricky part

if ( $snap.Created -lt (Get-Date).AddDays( -$Age ) ) {
    $datework = $snap.Created -replace ":" , "-"
    Write-Host "2:Error - Snapshotname=" $snap.Name ";  Groesse in MB = " $snap.SizeMB ";  Erzeugt am= " $datework}
}	

in prtg the sensor always shows "up".

can someone give me a hint?

custom-exe custom-sensor error-messages esxi snapshot vmware vmware-monitoring

Created on Aug 15, 2012 6:28:25 AM by  Eugen Vieten (0) 2

Last change on Aug 15, 2012 6:36:05 AM by  Konstantin Wolff [Paessler Support]



11 Replies

Votes:

0

Your Vote:

Up

Down

Hi,
in your if statement, try adding a line with a exit code. The script has to be ended with the proper exit code to make PRTG recognize the script beeing in error state. Your code including the exit code would look something like this:

if ( $snap.Created -lt (Get-Date).AddDays( -$Age ) ) {
    $datework = $snap.Created -replace ":" , "-"
    Write-Host "2:Error - Snapshotname=" $snap.Name ";  Groesse in MB = " $snap.SizeMB ";  Erzeugt am= " $datework
    exit 2}
}

Where the "2" in the exit code represents a system error. Please find all exit codes below.

ValueDescription
0OK
1WARNING
2System Error (e.g. a network/socket error)
3Protocol Error (e.g. web server returns a 404)
4Content Error (e.g. a web page does not contain a required word)

Best regards

Created on Aug 15, 2012 6:43:26 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Hi Konstantin,

thank you for replaying. Im not sure if i understand you corrctly. when i change the following code

If ($snapcount -eq "0"){
	write-host $snapcount ":OK - Last Backup = " $snapcount}
else {
	foreach ( $snap in $snapshots ) {
		if ( $snap.Created -lt (Get-Date).AddDays( -$Age ) ) {
			$datework = $snap.Created -replace ":" , "-"
			#Write-Host "2:Error"
			Write-Host "2:Error - Snapshotname=" $snap.Name ";  Groesse in MB = " $snap.SizeMB ";  Erzeugt am= " $datework
			}			
		}	
	}

into

Write-Host "4:Error"

or

Write-Host "2:Error"

nothing happens. my custom sensor in prtg keeps "ok" and "up". when i add a special message text the text is display correct:

Error - Snapshotname= lala test 123; Groesse in MB= 2053; Erzeugt am= 13.08.2012 18-03-33

i thought changing the first number in the line in 2 or 4 turns the sensor into "down" state? is there an error in my script? or to i have to change something in my sensor?

i followed the custom sensor creation:

—Standard EXE/Script Sensor

The returned data for standard EXE/Script sensors must be in the following format:

value:message

Value has to be a 32 bit integer and will be used as the resulting value for this sensor (e.g. bytes, milliseconds, etc.), message can be any string and will be stored in the database.

The EXE's exit code has to be one of the following values:

ValueDescription
0OK
1WARNING
2System Error (e.g. a network/socket error)
3Protocol Error (e.g. web server returns a 404)
4Content Error (e.g. a web page does not contain a required word)

Created on Aug 15, 2012 6:53:51 AM by  Eugen Vieten (0) 2

Last change on Mar 9, 2018 1:34:13 PM by  Luciano Lingnau [Paessler]



Votes:

0

Your Vote:

Up

Down

Hi,
I'm not refering to the ouput of the script like Write-Host. The script has to be ended with the correct exit code. Following code will make the sensor value 2 and the sensor message "Error":

Write-Host "2:Error"

You will have to provide a explicit exit code like:

Write-Host "2:Error"
exit 2

or

Write-Host "5:Everything OK"
exit 0

What you are doing, is only giving back a value and message nad not providing a exit code, which makes PRTG assume everything is ok regardless of the output.

Created on Aug 15, 2012 7:04:00 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Hi,

ok so far i understand what you say. We do need a correct system return code. But how can i return a "readable message text" too?

When is use Write-Host $snapcount":Error - Snapshotname=" $snap.Name "; Groesse in MB = " $snap.SizeMB "; Erzeugt am= " $datework

i receive a nice formated message with all information i need "Error - Snapshotname=lala test123; Groesse in MB = 2056; Erzeugt am= 2012-08-13 08-09-16

using the "exit 2" code in my script within the if statement i receive Name Port User ---- ---- ---- hostname.domain.tld DOMAIN\USERNAME 2:Error - Snapshotname=lala test123; Groesse in MB = 2056; Erzeugt am= 2012-08-13 08-09-16

for the additional text Name Port User ---- ---- ---- hostname.domain.tld DOMAIN\USERNAME is ok. but my colleagues will be surpised or on worst case they will not understand the error text

Created on Aug 15, 2012 7:28:24 AM by  Eugen Vieten (0) 2

Last change on Aug 15, 2012 7:37:55 AM by  Eugen Vieten (0) 2



Votes:

0

Your Vote:

Up

Down

Hi,
you can use the "Write-Host" statement to return every error message you want. For example:

Write-Host $snapcount":Error - Snapshotname=" $snap.Name ";  Groesse in MB = " $snap.SizeMB ";  Erzeugt am= " $datework
exit 2

Just make sure the message is returned before the exit code. When returning it like above the sensor will show the message and will have the error state.
Best regards

Created on Aug 15, 2012 7:37:03 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

Your Vote:

Up

Down

Hi,

now it works like i is designed. im using the following code

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
	Add-PSSnapin VMware.VimAutomation.Core
}

connect-viserver HOSTNAME-Domain.TLD  -WarningAction SilentlyContinue
$VM = Get-VM $args[0]																# Der Namen des Gastes entspricht dem ersten übergebenenen Parameter args[0]
$snapcount = (@($VM | Get-Snapshot)).Count											# Anzahl der Snapshots für einen Gast herausfinden
$snapshots = Get-Snapshot -VM $VM #| Where { $_.Created -lt (Get-Date).AddDays(-1)}	# Alle Snapshots in ein Array schreiben
$Age = 1																			# Alter definieren ab dem ein Alarm ausgegeben wird

If ($snapcount -eq "0"){
	write-host $snapcount ":OK - Last Backup = " $snapcount}
else {
	foreach ( $snap in $snapshots ) {
		if ( $snap.Created -lt (Get-Date).AddDays( -$Age ) ) {
			$datework = $snap.Created -replace ":" , "-"
			#Write-Host "2:Error"
			Write-Host $snapcount":Error -222 Snapshotname=" $snap.Name ";  Groesse in MB = " $snap.SizeMB ";  Erzeugt am= " $datework
			}			
		}	
	}
	
Disconnect-VIServer HOSTNAME-Domain.TLD -Confirm:$false

If ($snapcount -ne "0"){
exit 2}

now prtg triggers down event for the sensor and the message is written into the log / last message field. but there is some more text infront of my message. is there any way to delete the additional message text Name Port User ---- ---- ---- hostname.domain.tld DOMAIN\USERNAME

Message Text is: Name Port User ---- ---- ---- hostname.domain.tld DOMAIN\USERNAME 2:Error - Snapshotname=lala test123; Groesse in MB = 2056; Erzeugt am= 2012-08-13 08-09-16

Message Text should be Error - Snapshotname=lala test123; Groesse in MB = 2056; Erzeugt am= 2012-08-13 08-09-16

Created on Aug 15, 2012 7:45:17 AM by  Eugen Vieten (0) 2



Votes:

0

Your Vote:

Up

Down

What is the output when running the script on the command line?

Created on Aug 15, 2012 7:56:04 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

Your Vote:

Up

Down

powershell output is:

2:Error Snapshotname= lala test 1q23 ;  Groesse in MB =  2053,16 ;  Erzeugt am=  13.08.2012 18-03-33


PS C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE>

Created on Aug 15, 2012 7:58:44 AM by  Eugen Vieten (0) 2



Votes:

0

Your Vote:

Up

Down

The "additional" part in the returned message might be related to the fact that PRTG is executing the script with the SYSTEM account and when running the same in Powershell you are using your own account. In the sensor settings, please switch the option Security Context to Use Windows credentials of parent device to impersonate the execution of the script.

Created on Aug 15, 2012 8:14:28 AM by  Konstantin Wolff [Paessler Support]



Votes:

0

Your Vote:

Up

Down

it is configured as you say. this configuration is configured sinze starting with this sensor. logging onte the prtg remote probe and checking output in powershell gives the following

powershell output is:

2:Error Snapshotname= lala test 1q23 ;  Groesse in MB =  2053,16 ;  Erzeugt am=  13.08.2012 18-03-33


PS C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE>

Created on Aug 15, 2012 9:24:27 AM by  Eugen Vieten (0) 2



Votes:

0

Your Vote:

Up

Down

I'm afraid I'm running out of ideas here. Can you please try one last thing. Set the account the whole Remote Probe is running under to a non-system account (e.g. your own) but make sure it has at least local admin permissions. Does that work?

Created on Aug 15, 2012 1:51:21 PM by  Konstantin Wolff [Paessler Support]



Please log in or register to enter your reply.


Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.