#For PRTG - default parameter list:
#-deviceID %deviceid -targetHost '%host' -OID '1.3.6.1.2.1.43.10.2.1.4.1.1'
#find a list with OID examples at the following link as they differ by vendor and partly model
#https://www.it-admins.com/it-assets-database/online-manual/printers/
param(
[Parameter(
Mandatory = $true
)][int]$deviceID,
[Parameter(
Mandatory = $true
)][string]$targetHost,
[Parameter(
Mandatory = $true
)][string]$OID = "",
[string]$Community = "public"
)
Import-Module -Name SNMP
[string]$ErrorText = "" #will hold error information to be returned to PRTG text field / sensor message
[int]$ErrorValue = 0 #default no errors initiated with 0 (0 = OK | 1 = Warning | 2 = Error)
[string]$LogFile = "$PSScriptRoot\PrinterLog_$deviceID.log"
[int]$result = -2 #standard response would be negative 2 as initialized integer value
try{
$result = (Get-SNMPdata -IP $targetHost -OID $OID -Community $Community).Data
#if no error occured, we have the current page count as a result
} catch {
$result = -1 #if any error happened, we now have the result set to negative 1
$ErrorText += "error: can't read SNMP OID value from device "
$ErrorValue = 2
}
[int]$returnValue = 0 #default return value, integer, initialized with 0
[string]$LogFileContent = -1 #default logfilecontent is integer, intialized with negative 1
[string]$LogFileDate
[int]$LogFileCount = -1
if ($result -gt 0) {
#we seem to have a page count from SNMP
if (Test-Path $LogFile -PathType Leaf){
#we do have an exisiting LogFile
try{
$LogFileContent = Get-Content $LogFile -Raw
if ($LogFileContent.Length -gt 0){
$LogFileDate = $LogFileContent.Split("|")[0]
$LogFileCount = $LogFileContent.Split("|")[1]
} else {
#LogFile empty
$LogFileCount = 0
$ErrorText += "warning: initial run or no logfile yet "
$ErrorValue = 1
}
} catch {
$LogFileCount = -2 #negative 2 as error catch
} finally {
if ($LogFileCount -gt 0){
#no error occured as for the LogFile reading
if (($result - $LogFileCount) -gt -1){
#it should be either the same value or higher then the last logfile value
$returnValue = $result - $LogFileCount
} else {
#LogFile value is higher then current count - this is weird
#still returning this - but it will show a negative count and should raise a flag
$returnValue = $result - $LogFileCount
$ErrorText += "error: logfile value higher then current printer read "
$ErrorValue = 2
}
} else {
#there was some kind of error in the LogFile read - not an integer value or new Logfile started and result was 0
$ErrorText += "error: can't read logfile for data compare "
$ErrorValue = 2
}
}
} else {
#no LogFile yet - will need one but return value as difference initially will stay 0
#we set the LogFileContent to 0 as this is the current status
$LogFileCount = 0
$ErrorText += "warning: initial run or no logfile yet or wrong data format "
$ErrorValue = 1
}
#we need to write the positive SNMP OID result to the LogFile
#but only if the date differs more then 1x day
[bool]$WriteLog = $false
[string]$CurrentDate = (Get-Date -Format yyyy-MM-dd)
if ($LogFileDate.Length -gt 0){
try{
if ((New-TimeSpan -Start $LogFileDate -End $CurrentDate).Days -gt 1){
#date difference more then 1 day
$WriteLog = $true
} else {
#date difference not sufficient - same day? Future?
if ((New-TimeSpan -Start $LogFileDate -End $CurrentDate).Days -lt 0){
#LogFile newer then current date? That's just wrong
$WriteLog = $true
$ErrorText += "warning: Logfile date above current date $LogFileDate - rewriting log"
$ErrorValue = 1
} else {
#not greater then 1 day (0 and 1) or less must be equal as a result...
#nothing to do as WriteLog is initialized false
}
}
} catch {
#couldn't compare the date - let's write the Log to be save
$WriteLog = $true
}
} else {
#LogFileDate length 0 means we need to write a logfile as it might not exist
$WriteLog = $true
}
if ($WriteLog){
try {
Set-Content -Path $LogFile -Value "$CurrentDate|$result"
$LogFileDate = $CurrentDate
} catch {
#there was an issue writing to the LogFile
$ErrorText += "error: can't write to logfile "
$ErrorValue = 2
}
}
} else {
#no pagecount available due to errors or issues
#uncomment the next line for debugging only
#$returnValue = $result
#the returnValue will now show the current result value and point therefor to the error section
$ErrorText += "error: no pagecount available "
$ErrorValue = 2
}
#Assuming no errors - the text result will be the last LogFile date
if ($ErrorText.Length -eq 0){
$ErrorText = "Last LogFile write date: $LogFileDate"
}
$XML = "<prtg>
<result>
<channel>last relative Page Count</channel>
<value>$returnValue</value>
</result>
<result>
<channel>last total from Printer</channel>
<value>$result</value>
</result>
<result>
<channel>last total from LogFile</channel>
<value>$LogFileCount</value>
</result>
<result>
<channel>Script Error Status</channel>
<value>$ErrorValue</value>
<LimitMode>1</LimitMode>
<LimitMaxWarning>0</LimitMaxWarning>
<LimitMaxError>1</LimitMaxError>
</result>
<text>$ErrorText</text>
</prtg>"
Function WriteXmlToScreen ([xml]$XML) #just to make it clean XML code...
{
$StringWriter = New-Object System.IO.StringWriter;
$XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
$XmlWriter.Formatting = "indented";
$xml.WriteTo($XmlWriter);
$XmlWriter.Flush();
$StringWriter.Flush();
Write-Output $StringWriter.ToString();
}
WriteXmlToScreen $XML;