I have already received a few emails regarding the monitoring of Sophos Email Protection. As I also use email protection in my private environment, this topic is also of interest to me.
The UTM Mail Manager already provides a very useful overview:
However, it is not very practical to have this overview permanently open in the browser, especially as most companies also use monitoring software.
I use the PRTG software for monitoring, so there is also a corresponding script for a PRTG sensor in this article.
From the emails I have received, most people (like me) are most interested in being informed about the status of SMTP queues. So basically these three values:
Unfortunately, the status of the queues cannot be queried via SNMP or RestAPI, so the only remaining options for obtaining the values are SSH (shell access) or screen parsing. I have decided to use SSH and shell access.
It's not necessarily nice when I log in to the UTM via SSH, but at least it's relatively easy to get the number of mails in the SMTP queues.
In order to get the values, I have created a small PowerShell script which establishes the connection to the UTM via SSH. The connection is established with the user "loginuser". Root access is not required by the script.
The following settings are required in the configuration of the UTM for shell access to work:
Under "Allowed networks", you can restrict which networks or hosts have access to the SSH shell of the UTM. In my case, this is only the internal network and the server that executes the script.
The UTM sends an e-mail to the administrator every time an SSH login is successful. In this case it is therefore necessary to switch off the e-mail notification for successful SSH logins, as otherwise a warning is sent by e-mail every time the script is run:
SNMP traps can still be used, the trap receiver can be configured so that successful logins from the IP of the host (which executes the script) are not immediately regarded as problematic (SIEM systems).
The preparation of the UTM is now complete, we will continue with the actual PowerShell script.
PowerShell script for reading the UTM mail queues
The Posh-SSH module is required so that the SMTP queues of the UTM can be read out. This module enables an SSH connection to be established using PowerShell. On current Windows operating systems, the module can be installed quickly using NuGet:
Install modules Posh-SSH
The following script can now be used to read the mails of the SMTP queues, the first three lines must be adapted to your own environment (login user usually does not need to be changed):
$UTMHostNameorIP = "utm.domain.local" $Loginuser = "loginuser" $LoginUserPassword = "SecurePassword1" $LoginUserPasswordSec = ConvertTo-SecureString $LoginUserPassword -AsPlainText -Force $LoginCreds = New-Object System.Management.Automation.PSCredential($Loginuser,$LoginUserPasswordSec) #Connect SSH Session try { $ConnectSSHSession = New-SSHSession -ComputerName $UTMHostNameorIP -Credential $LoginCreds -AcceptKey:$true } catch { write-host "Can't connect to UTM" } #Query Quarantine try { $StrQuarantine = (Invoke-SSHCommand -Command "find /var/storage/chroot-smtp/spool/quarantine -type f | wc -l" -Index $ConnectSSHSession.SessionId).Output [int]$Quarantine = [convert]::ToInt32($StrQuarantine, 10) } catch { write-host "Can't query Quarantine mails" } if ($Quarantine -eq 1 -or $Quarantine -eq 0) { $Quarantine = 0 } else { $Quarantine = ($Quarantine -1)/2 } #Query Output Queue (Spool) try { $StrOutput = (Invoke-SSHCommand -Command "find /var/storage/chroot-smtp/spool/output -type f | wc -l" -Index $ConnectSSHSession.SessionId).Output [int]$Output = [convert]::ToInt32($StrOutput, 10) } catch { write-host "Can't query Output (spool) mails" } if ($Output -eq 1 -or $Output -eq 0) { $Output = 0 } else { $Output = ($Output -1)/2 } #Query corrupt queue try { $StrCorrupt = (Invoke-SSHCommand -Command "find /var/storage/chroot-smtp/spool/corrupt -type f | wc -l" -Index $ConnectSSHSession.SessionId).Output [int]$Corrupt = [convert]::ToInt32($StrCorrupt , 10) } catch { write-host "Can't query corrupt mails" } if ($Corrupt -eq 1 -or $Corrupt -eq 0) { $Corrupt = 0 } else { $Corrupt = ($Corrupt -1)/2 } #Disconnect SSH session $DisconnectSSHSession = Remove-SSHSession -Index $ConnectSSHSession.SessionId #Write Results write-host "Mails in Quarantine: $Quarantine" write-host "Mails in Spool Queue: $Output" write-host "Corrupt Mails: $Corrupt"
The script establishes an SSH connection with the specified user and password to the UTM, reads the number of files from the corresponding spool directories and calculates the number of mails in the queues. Each mail in the respective queue directory consists of 2 files (SMTP header and body), and there is also a lock file in the queue directory. This is taken into account by the script and calculated accordingly.
Here is the output of the script:
For comparison, the values in the UTM mail manager:
The values of the queues are written in variables and can be reused as required.
Monitoring the UTM mail queues with PRTG
I have modified the previous general script slightly so that it can be used to create a PRTG sensor. The first lines must be adapted to your own environment (login user, password and UTM host name). The PoshSSH module on the PRTG server is also required for this script.
Here is the script for a PRTG sensor:
#User with Shell Access to Sophos UTM: $Loginuser = "loginuser" #Password for Loginuser: $LoginUserPassword = "SecretPassword1" $UTMHostnameorIP = "utm.domain.local" #Create Credential Object $LoginUserPasswordSec = ConvertTo-SecureString $LoginUserPassword -AsPlainText -Force $LoginCreds = New-Object System.Management.Automation.PSCredential($Loginuser,$LoginUserPasswordSec) #Confirm Powershell Version. if ($PSVersionTable.PSVersion.Major -lt 3) { Write-Output "" Write-Output "1" Write-Output "Powershell Version is $($PSVersionTable.PSVersion.Major) Requires at least 3. " Write-Output "" Exit } #Connect SSH Session try { $ConnectSSHSession = New-SSHSession -ComputerName $UTMHostnameorIP -Credential $LoginCreds -AcceptKey:$true } catch { Write-Output "" Write-Output "1" Write-Output "Can't connect to UTM" Write-Output "" Exit } #Query Quarantine try { $StrQuarantine = (Invoke-SSHCommand -Command "find /var/storage/chroot-smtp/spool/quarantine -type f | wc -l" -Index $ConnectSSHSession.SessionId).Output [int]$Quarantine = [convert]::ToInt32($StrQuarantine, 10) } catch { Write-Output "" Write-Output "1" Write-Output "Can't query Qurantine mails" Write-Output "" Exit } if ($Quarantine -eq 1 -or $Quarantine -eq 0) { $Quarantine = 0 } else { $Quarantine = ($Quarantine -1)/2 } #Query Output Queue (Spool) try { $StrOutput = (Invoke-SSHCommand -Command "find /var/storage/chroot-smtp/spool/output -type f | wc -l" -Index $ConnectSSHSession.SessionId).Output [int]$Output = [convert]::ToInt32($StrOutput, 10) } catch { Write-Output "" Write-Output "1" Write-Output "Can't query Output (spool) mails" Write-Output "" Exit } if ($Output -eq 1 -or $Output -eq 0) { $Output = 0 } else { $Output = ($Output -1)/2 } #Query corrupt queue try { $StrCorrupt = (Invoke-SSHCommand -Command "find /var/storage/chroot-smtp/spool/corrupt -type f | wc -l" -Index $ConnectSSHSession.SessionId).Output [int]$Corrupt = [convert]::ToInt32($StrCorrupt , 10) } catch { Write-Output "" Write-Output "1" Write-Output "Can't query corrupt mails" Write-Output "" Exit } if ($Corrupt -eq 1 -or $Corrupt -eq 0) { $Corrupt = 0 } else { $Corrupt = ($Corrupt -1)/2 } #Disconnect SSH session $DisconnectSSHSession = Remove-SSHSession -Index $ConnectSSHSession.SessionId #Write Results write-host "" Write host "" Write host "Qurantine Mails" Write host "$($Quarantine)" Write-Host "" Write host "" Write host "Spooled Mails" Write host "$($Output)" Write host "" Write host "" Write host "Corrupt Mails" Write host "$($Corrupt)" Write host "" write-host ""
The script can be copied to the PRTG Curstom Sensors\EXEXML directory after customization:
Before a sensor is created, the script can be tested once and a corresponding PRTG structure should be returned:
If the test is successful, a new sensor can be created:
"Program/Script (Advanced)" is selected as the sensor type:
The new sensor is given a descriptive name and the previously created PS1 file is selected under "Program/Script":
A short time later, the sensor should already be delivering the values, which can now be improved a little:
With corresponding settings for the threshold values of the channels (switch on alarm based on limit values), threshold values for warning and error states can also be entered here for each channel:
Here is a finished view:
Unfortunately, as already mentioned, the login via SSH on the UTM is not so nice. Here you should also think about the notification, if the UTM spools a lot of mails, it could also be because the Exchange server can no longer accept mails. In this case, a notification via SMS would make more sense than via email.
Hallo Zusammen,
ich weiß das Thema ist schon älter aber ich bin gerade dabei den Sensor in PRTG einzurichten. Ich habe alle Varianten (auch aus den Kommentaren) versucht, erhalte aber immer den JSON Fehler: „XML: Das zurückgelieferte XML entspricht nicht dem erwarteten Schema. (Code: PE233) — JSON: Das zurückgelieferte JSON entspricht nicht der erwarteten Struktur (Invalid JSON.). (Code: PE231)“
Kann es sein, dass es mit der aktuellen Version von PRTG Probleme gibt?
Schöne Grüße
Benjamin
Hat jemand auch einen Weg gefunden für die XG?
Hat auch jemand einen Weg für die XG gefunden?
Ich habe auch noch das Problem, dass immer 2 E-Mails im Spooled angezeigt werden. Die Zeile habe ich schon angepasst.
„find /var/storage/chroot-smtp/spool/output -type f -name ‚-D‘ | wc -l“
hi everybody
i found this article , cause i’ve got a „spam campain“ on my Sophos Utm after a stolen password, and i was looking for a method to observe the mailqueue , and to be warn if the queue was too heavy !
i’m using nagios as monitoring tools, then i create a nagios plugin in bash, to connect to the utm sophos using ssh+public key (no password so !)
i can put threshold and to be notify
Thanks for your idea !
bonne année 2022
Guten Morgen,
super Beiträge, auch noch in 2021 :)
Konnte jemand das Problem von Henning und Alexander mit den „zu viel“ angezeigten Einträgen lösen?
Ich hätte auch gerne nur die, die in der Weboberfläche angezeigt werden.
Lieben Gruß
Christian
Hallo Christian,
pro Mail werden mehrere Dateien in dem Spool-Ordner erzeugt, daher die Multiplikation. Es genügt, den find wie folgt auf eine Datei einzugrenzen, habe mich hier im Beispiel auf die „*-D“ Dateien begrenzt.
find /var/storage/chroot-smtp/spool/output -type f -name „*-D“
Sieht jetzt im Vergleich zur GUI gleich aus.
Grüße
Ulrich
Hallo Ulrich,
ich habe das gleiche Problem. Habe es gemäß deinem Vorschlag angepasst, doch leider zeigt er mir auch bei komplett leerem Spool folgendes an:
Spooled Mails
2
Hast du eventuell noch eine Idee woran das liegen kann?
Oder habe ich die Zeile falsch angepasst?
‚find /var/storage/chroot-smtp/spool/output -type f -name „*-D“ | wc -l‘
mit freundlichen Grüßen
Daniel
Super Artikel.
Leider sind im Spool Ordner alte Dateien, die der Mail Manager nicht anzeigt. Wenn man die Files löscht erscheinen sie nach 2 Minuten wieder. Daher wird leider der Wert verfälscht.
Ordner: /var/storage/chroot-smtp/spool/output/1/msglog
Die Files sind älter als ein Jahr. Klein und nicht vom Inhalt relevant (Fehlermeldungen).
Hallo Frank,
vielen Dank für deinen tollen Beitrag!
Als Ergänzung:
Wir verschieben grundsätzlich alle Mails mit doc/docx und xls/xlsx-Files in die Quarantäne, und wollten dies überwachen, da hier eine manuelle Prüfung stattfindet, in der Vergangenheit aber ab und an vergessen wurde.
Dazu haben wir dein Skript erweitert und durchsuchen das Quarantäne-Verzeichnis mit folgendem SSH-Command:
grep -rnw ‚/var/storage/chroot-smtp/spool/quarantine‘ -e ‚Content-Type: application/msword;‘ | wc -l
Hier kann je nach Bedard der Content-Type durch den entsprechenden Mime-Type ersetzt werden.
Nochmals vielen Dank für deine super Beiträge!
Gruß
Bernhard
Hallo Frank,
dank – cooles script – lange danach gesucht.
Folgendes Problem bei mir:
x64/x86 Powershell sind auf unrestricted
das script in der x64 ps ausgeführt liefert korrekte werte zurtück.
das script in der x86 ps ausgeführt liefert:
PS C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML> .\utm-Mailqueue.ps1
1
Can’t connect to UTM
zurück. Soweit ich weiß, nutzt PRTG für PS Sensoren die x86 ps .. ?
Wo liegt (mein) Fehler ?=
Grüße,
Stefan
Hallo Frank,
dank – cooles script – lange danach gesucht.
Folgendes Problem bei mir:
x64/x86 Powershell sind auf unrestricted
das script in der x64 ps ausgeführt liefert korrekte werte zurtück.
das script in der x86 ps ausgeführt liefert:
PS C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML> .\utm-Mailqueue.ps1
1
Can’t connect to UTM
zurück. Soweit ich weiß, nutzt PRTG für PS Sensoren die x86 ps .. ?
Wo liegt (mein) Fehler ?=
Grüße,
Stefan
Wer lesen kann ist klar im Vorteil ;)
“ Frank sagt:7. Februar 2019 um 08:07 “
lösung war:
programmScript psx64.exe Parameter
-f=“PS_UTM_MailManagerSensor.ps1“
Hallo,
habe gerade mal das Skript getestet, jedoch passen bei mir die Zahlen nicht wirklich.
Mails in Quarantine: 11129 (3812)
Mails in Spool Queue: 59 (18)
Corrupt Mails: 0 (0)
In Klammer steht immer die Zahl welche im Mail Manager ersichtlich sind.
Getestet mit der UTM-Version: 9.510-5
cooler Artikel, sehr detailliert, Daumen hoch, Danke Frank
Hallo Frank,
erst einmal Dank für deine tolle Arbeit!
Christian hat geschrieben, dass die Ausgabe nicht in XML erfolgt und dass hierfür Anpassungen notwendig sind.
Ich erhalte die gleiche Fehlermeldung. Kannst Du uns sagen, was für Anpassungen im Skript nötig sind, damit es funktioniert. Danke schon einmal im Voraus!
Gruß Björn
Sorry, habe glatt deinen Kommentar und die Lösung unten übersehen.
Nochmals Danke!
Gruß Björn
Hallo,
cooler Denkanstoß. Wäre ja schön, wenn Sophos endlich solche Werte auch als API oder SNMP anbieten würde. Auch der Clusterstatus wäre wichtig, im Monitoring zu haben.
Wenn jemand ein Bash Script für Nagios / Check_MK hat, wäre eine Hinweis hier auf der Seite klasse.
Danke
Erik
ich war so frei als text File anzubieten.
https://www.freaky-media.de/externeseiten/ablage-frankysweb/sophos-utm-monitoring-der-mail-queues.txt
von Frank zu Frank: kannst es ja später wieder entfernen wen dein WordPress die Tags nicht mehr entfernt
Hallo,
vielen Dank für den Hinweis, die Formatierung passt nun wieder.
Gruß,
Frank
Hallo,
also das ganze müsste dann so angepasst werden.
#Write Results
write-host „“
Write-Host „“
Write-Host „Qurantine Mails“
Write-Host „$($Quarantine)“
Write-Host „Mails“
Write-Host „Mails“
Write-Host „“
Write-Host „“
Write-Host „Spooled Mails“
Write-Host „$($Output)“
Write-Host „Mails“
Write-Host „Mails“
Write-Host „“
Write-Host „“
Write-Host „Corrupt Mails“
Write-Host „$($Corrupt)“
Write-Host „Mails“
Write-Host „Mails“
Write-Host „“
write-host „“
Mit dem dazufügen von :
Write-Host „Mails“
Write-Host „Mails“
Spart mans ich das anpassen der Einheiten nachträglich über das Webinterface.
Direkt ausführen konnte ich es bei mir jedoch nicht, musste den PShelper verwenden damit das als 64bit Script gestartet wird; http://prtgtoolsfamily.com/downloads/sensors „psx64″ und ausführen dann programmScript psx64.exe Parameter
-f=“PS_UTM_MailManagerSensor.ps1“
Hallo Frank,
super Idee, ich habe es gerade versucht einzurichten, aber PRTG meldet folgenden Fehler:
„XML: Das zurückgelieferte XML entspricht nicht dem erwarteten Schema. (Code: PE233) — JSON: Das zurückgelieferte JSON entspricht nicht der erwarteten Struktur (Invalid JSON.). (Code: PE231)“
In der Powershell liefert es korrekt die Daten. Habe die aktuellste Version 18.4.46.1754+ im Einsatz.
Eine Idee woran dies liegen mag?
MfG
Christian
ich sehe gerade, dass das zweite Skript nicht XML ausgibt…
Skript muss wie folgt in der Ausgabe angepasst werden:
#Write Results
write-host „“
Write-Host „“
Write-Host „Qurantine Mails“
Write-Host „$($Quarantine)“
Write-Host „“
Write-Host „“
Write-Host „Spooled Mails“
Write-Host „$($Output)“
Write-Host „“
Write-Host „“
Write-Host „Corrupt Mails“
Write-Host „$($Corrupt)“
Write-Host „“
write-host „“
lol, scheinbar werden xml Inhalte hier auf der Seite automatisch entfernt! :)
Hallo Christian,
ich habe die gleiche Meldung wie Du. Was hast Du genau geändert?
Gruß Björn
Hi Frank,
Wiedermal klasse vorbereitet!
Wäre es möglich in der ersten Ausgabe (ohne PRTG) eine Mail an eine beliebige E-Mail Adresse zu versenden? Uns geht es hier um eine stündliche Anzeige der Ques – das würde uns sehr viel Arbeit sparen ….
Beste Grüße
Hallo Frank,
Danke für den Denk Anstoß. Denke man kann in der utm noch mehr mit ssh anfangen..
Ich seh den Sensor für die waf bei dir. Kannst du uns erhellen was da hinter steckt? Simpel den Aufruf von Owa oder auch mehr infos?
Lg. Sebastian
hey Frank.
Toller Artikel. Ich schleiche um so eine Lösung auch schon immer wieder rum. Nur wir setzen nagios ein. Aber mit deiner Vorlage sollte auch das lösbar sein. Danke für deine tolle Seite!
Hallo Torsten,
auch mit Nagios sollte dies kein Problem darstellen. Ein kleines Shell Script sollte da schon ausreichen, ein bisschen rechnen kann ja die Bash auch.. :-)
Gruß,
Frank