What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Maintenance mode scripts

Votes:

0

Hello,

I don't know where to post it, and it's not a question, but I wanted to share this with the community: I have been looking for an automated way to pause/put into maintenance mode all our servers.
I don't want to lookup the id for every machine and create an individual file. Since I couldn't find a 'howto' on the net, I have created 2 scripts myself. 1 for Linux & 1 for windows. Am I not a developer/scripter, so the code will be ugly and there will probably be a better way to do stuff, but, here it is:

! Before you can start with either script, you'll need a DEDICATED service account with password set to never expires, make sure this account has permissions to put machines in maintenance mode. Log in with this account, go to the account properties and click "show passhash".

The scripts work as follows:
It connects to the PRTG server to download the list of machines (FQDN based!!!)+ IDs. It then compares the hostname to the name in the file. It then does a webcall to that ID.

LINUX: (bash script, so save as .sh and execute at shutdown)

#/bin/sh
# Script that can automatically pause the sensors on PRTG for xx minutes with reboot
# variables
######setup details, access related.

#auth is retrieved from the logged in user - should be a service account, because hash changes when password changes
auth="username=SERVICEACCOUNT&passhash=1111111111"
PRTGHost="prtg.fqdn.dom"
#duration of pause
durationinminutes="10"
localdir="/scripts/prtg/maintmode"
if [ ! -d "/scripts" ]; then
	mkdir /scripts
fi
if [ ! -d  "/scripts/prtg" ]; then
	mkdir /scripts/prtg
fi
if [ ! -d  $localdir ]; then
	mkdir $localdir
fi
PRTGUrl="http://$PRTGHost/api/table.xml?content=devices&output=csvtable&columns=objid,host&count=2500&$auth"
#Retrieve list of devices
wget --no-check-certificate -O ${localdir}/devices.csv ${PRTGUrl}

#Retrieve FQDN
while read LINE
do
	fqdn=$LINE
done < /proc/sys/kernel/hostname
typeset -l fqdn
shopt -s nocasematch # set environment case insesitive
# Read csv into array
read -d '' -r -a lines < ${localdir}/devices.csv
for i in "${lines[@]}"
do
	IFS=', ' read -ra linearray <<< "$i"
	arrayvalue="${linearray[2]}"
	typeset -l arrayvalue
	#remove quotes from value
	newarray=$(echo $arrayvalue|sed 's/\"//g'|tee)
	
	#echo "$fqdn - $newarray"
	
	if [[ $newarray == $fqdn ]]; then
		#echo "$newarray - IN IF LOOP"
		serverID=$(echo ${linearray[0]}|sed 's/\"//g'|tee) 
		#remove quotes from value
		echo "Server ${linearray[2]} found with ID $serverID"
		PauseURL="http://$PRTGHost/api/pauseobjectfor.htm?id=$serverID&duration=$durationinminutes&pausemsg=Automatically%20paused%20for%20server%20reboot%20(10%20minutes)&$auth"
		echo $PauseURL
		# Put machine in pause
		wget --no-check-certificate -O ${localdir}/pausedir.html $PauseURL 
	fi	
done

WINDOWS: (powershell - so add a .bat file next to it to launch the script, configure with GPO)

$DebugPreference = "SilentlyContinue"

######ignore invalid SSL Certs##########
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

######setup details, access related.
$auth = "username=SERVICEACCOUNT&passhash=1111111111"
$PRTGHost = "prtg.fqdn.dom"
$durationinminutes = "10"

$PRTGUrl = "http://$PRTGHost/api/table.xml?content=devices&output=csvtable&columns=objid,host&count=2500&$auth"
$RETRIEVECSV =Invoke-WebRequest -Uri $PRTGUrl -MaximumRedirection 0 -ErrorAction Ignore
$PRTGCSV = "C:\TEMP\DEVICES.CSV"

#Invoke-WebRequest -Uri $PRTGUrl -MaximumRedirection 0 -ErrorAction Ignore

$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($PRTGUrl,$PRTGCSV)

$CSVFile = Import-Csv -Path $PRTGCSV

$myIPs = @()
# Get IP for lookup
$myIPs += Get-NetIPAddress -AddressFamily IPv4 | where { $_.InterfaceAlias -notmatch 'Loopback'} |Select IPAddress
# Get FQDN for lookup
$t = "" | select ipaddress 
$t.ipaddress = (Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
$myIPs += $t
# Get hostname for lookup
$t = "" | select ipaddress 
$t.ipaddress = hostname
$myIPs += $t


ForEach ($Line in $CSVFile){
    #Write-Debug "$($Line.Host)"
    If ($myIPs -contains "$($Line.Host)"){
        $PauseDate = Get-Date -Format yyyyMMdd-HHmm
        Write-Debug "Machine: $($Line.Host) $($Line.ID)"
        $serverID = $($Line.ID)
        $PauseURL = "http://$PRTGHost/api/pauseobjectfor.htm?id=$serverID&duration=$durationinminutes&pausemsg=Automatically%20paused%20for%20server%20reboot%20(10%20minutes%20-%20$PauseDate)&$auth"
        $PauseServer = Invoke-WebRequest -Uri $PauseURL  -ErrorAction Ignore
        Write-Debug $PauseURL
    }
}

Hopefully this helps you guys out :)

Have a nice day
Danny

pause prtg script

Created on Feb 8, 2019 11:03:35 AM

Last change on Feb 8, 2019 11:59:26 AM by  Dariusz Gorka [Paessler Support]



1 Reply

Votes:

0

Hi there,

I just added some formatting to your topic, in case you want to format it further, check this guide:
https://kb.paessler.com/en/topic/11-what-markup-codes-can-i-use-to-format-my-questions-and-answers-in-the-paessler-knowledge-base

However thanks for the contribution! Hope it helps some other users as well.

Best regards.

Created on Feb 8, 2019 12:00:24 PM by  Dariusz Gorka [Paessler Support]




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.