CloudRadar - mailq check with Python
To create a custom check for CloudRadar to check you mailq size.
This check allows to create a custom check at CloudRadar to check the mailq size. This is only about the number of mails in the queue. If this exceeds a limit, a warning or an error will be send to CloudRadar.
#!/usr/bin/env python
import requests
import json
import sys
import subprocess
import json
import sys
import subprocess
#
# Mailqueue check
#
# Mailqueue check
#
# Your token of the custom check
token = 'XXXXXXX'
token = 'XXXXXXX'
# Name of the custom check. Needs to be created on my.cloduradar.io first!
check = 'mailq'
check = 'mailq'
# URL of cloudradars data hub. No need to change.
url = "https://hub.cloudradar.io/cct/"
url = "https://hub.cloudradar.io/cct/"
# Max numbers of mails in queue before warning
threshold = 4
threshold = 4
alerts = []
warnings = []
payload = dict()
warnings = []
payload = dict()
p = subprocess.Popen('mailq | grep -c "^[A-F0-9]"', stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
(output, err) = p.communicate()
payload[check+'.mailq'] = int(output)
if int(output) > threshold:
alerts.append("%s mails in queue - ERROR / threshold: %d" % (output, threshold))
if int(output) == threshold:
warnings.append("%s mails in queue - WARNING" % output)
if len(alerts) > 0:
# Include alerts that will trigger a message on cloudradar
payload[check+'.alert'] = ",".join(alerts)
if len(warnings) > 0:
# Include warnings that will trigger a message on cloudradar
payload[check+'.warning'] = ",".join(warnings)
alerts.append("%s mails in queue - ERROR / threshold: %d" % (output, threshold))
if int(output) == threshold:
warnings.append("%s mails in queue - WARNING" % output)
if len(alerts) > 0:
# Include alerts that will trigger a message on cloudradar
payload[check+'.alert'] = ",".join(alerts)
if len(warnings) > 0:
# Include warnings that will trigger a message on cloudradar
payload[check+'.warning'] = ",".join(warnings)
headers = { 'X-CustomCheck-Token': token }
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
if response.status_code != 204:
sys.stderr.write("Sending data failed. " + response.text + "\n" )
sys.exit(response.status_code)
sys.stderr.write("Sending data failed. " + response.text + "\n" )
sys.exit(response.status_code)
print("All data sent.")
Kommentare
Kommentar veröffentlichen