|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2014, Paessler AG <support@paessler.com> |
| 3 | +# All rights reserved. |
| 4 | +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 5 | +# following conditions are met: |
| 6 | +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions |
| 7 | +# and the following disclaimer. |
| 8 | +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions |
| 9 | +# and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 10 | +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse |
| 11 | +# or promote products derived from this software without specific prior written permission. |
| 12 | + |
| 13 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 14 | +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 15 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 16 | +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 17 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 19 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 20 | +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 21 | + |
| 22 | +import os |
| 23 | +import gc |
| 24 | +import logging |
| 25 | + |
| 26 | + |
| 27 | +class Postfix(object): |
| 28 | + def __init__(self): |
| 29 | + gc.enable() |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def get_kind(): |
| 33 | + """ |
| 34 | + return sensor kind |
| 35 | + """ |
| 36 | + return "mppostfix" |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def get_sensordef(): |
| 40 | + """ |
| 41 | + Definition of the sensor and data to be shown in the PRTG WebGUI |
| 42 | + """ |
| 43 | + sensordefinition = { |
| 44 | + "kind": Postfix.get_kind(), |
| 45 | + "name": "Postfix Mailqueue", |
| 46 | + "description": "Monitors the mailqueue of a postfix server", |
| 47 | + "help": "Monitors the mailqueue of a postfix server for active, deferred, hold or corrupt mail", |
| 48 | + "tag": "mppostfixsensor", |
| 49 | + "fields": [], |
| 50 | + "groups": [] |
| 51 | + } |
| 52 | + return sensordefinition |
| 53 | + |
| 54 | + def check(self): |
| 55 | + spoolcmd = os.popen("postconf -h queue_directory") |
| 56 | + spooldir = spoolcmd.readline().replace('\n', '') |
| 57 | + spoolcmd.close() |
| 58 | + deferredcmd = os.popen("test -d %s/deferred && find %s/deferred -type f | wc -l" % (spooldir, spooldir)) |
| 59 | + deferredcnt = deferredcmd.readline().replace('\n', '') |
| 60 | + deferredcmd.close() |
| 61 | + activecmd = os.popen("test -d %s/active && find %s/active -type f | wc -l" % (spooldir, spooldir)) |
| 62 | + activecnt = activecmd.readline().replace('\n', '') |
| 63 | + activecmd.close() |
| 64 | + holdcmd = os.popen("test -d %s/hold && find %s/hold -type f | wc -l" % (spooldir, spooldir)) |
| 65 | + holdcnt = holdcmd.readline().replace('\n', '') |
| 66 | + holdcmd.close() |
| 67 | + corruptcmd = os.popen("test -d %s/corrupt && find %s/corrupt -type f | wc -l" % (spooldir, spooldir)) |
| 68 | + corruptcnt = corruptcmd.readline().replace('\n', '') |
| 69 | + corruptcmd.close() |
| 70 | + channel_list = [ |
| 71 | + { |
| 72 | + "name": "Deferred mails", |
| 73 | + "mode": "integer", |
| 74 | + "unit": "Count", |
| 75 | + "limitmaxwarning": 40, |
| 76 | + "limitmaxerror": 50, |
| 77 | + "limitmode": 1, |
| 78 | + "value": deferredcnt |
| 79 | + }, |
| 80 | + { |
| 81 | + "name": "Active mails", |
| 82 | + "mode": "integer", |
| 83 | + "unit": "Count", |
| 84 | + "value": activecnt |
| 85 | + }, |
| 86 | + { |
| 87 | + "name": "Hold mails", |
| 88 | + "mode": "integer", |
| 89 | + "unit": "Count", |
| 90 | + "value": holdcnt |
| 91 | + }, |
| 92 | + { |
| 93 | + "name": "Corrupt mails", |
| 94 | + "mode": "integer", |
| 95 | + "unit": "Count", |
| 96 | + "value": corruptcnt |
| 97 | + }, |
| 98 | + ] |
| 99 | + return channel_list |
| 100 | + |
| 101 | + @staticmethod |
| 102 | + def get_data(data, out_queue): |
| 103 | + postfix = Postfix() |
| 104 | + try: |
| 105 | + postfixdata = postfix.check() |
| 106 | + data_r = { |
| 107 | + "sensorid": int(data['sensorid']), |
| 108 | + "message": "OK", |
| 109 | + "channel": postfixdata |
| 110 | + } |
| 111 | + logging.debug("Running sensor: %s" % postfix.get_kind()) |
| 112 | + except Exception as e: |
| 113 | + logging.error("Ooops Something went wrong with '%s' sensor %s. Error: %s" % (postfix.get_kind(), |
| 114 | + data['sensorid'], e)) |
| 115 | + data_r = { |
| 116 | + "sensorid": int(data['sensorid']), |
| 117 | + "error": "Exception", |
| 118 | + "code": 1, |
| 119 | + "message": "Postfix failed. %s" % e |
| 120 | + } |
| 121 | + out_queue.put(data_r) |
| 122 | + return 1 |
| 123 | + del postfix |
| 124 | + gc.collect() |
| 125 | + out_queue.put(data_r) |
| 126 | + return 0 |
0 commit comments