Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
5de2af 1 <?php
M 2
3 /*
4 Copyright (c) 2013, Marius Cramer, pixcept KG
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class cronjob_monitor_mail_queue extends cronjob {
b1a6a5 32
MC 33     // job schedule
34     protected $_schedule = '*/5 * * * *';
35     protected $_run_at_new = true;
36
37     private $_tools = null;
38
5de2af 39     private function _getIntArray($line) {
M 40         /** The array of float found */
b1a6a5 41
MC 42
5de2af 43         $res = array();
M 44         /* First build a array from the line */
45         $data = explode(' ', $line);
46         /* then check if any item is a float */
47         foreach ($data as $item) {
48             if ($item . '' == (int) $item . '') {
49                 $res[] = $item;
50             }
51         }
52         return $res;
53     }
b1a6a5 54
MC 55     /* this function is optional if it contains no custom code */
56     public function onPrepare() {
57         global $app;
58
59         parent::onPrepare();
60     }
61
62     /* this function is optional if it contains no custom code */
63     public function onBeforeRun() {
64         global $app;
65
66         return parent::onBeforeRun();
67     }
68
69     public function onRunJob() {
70         global $app, $conf;
71
72         /* used for all monitor cronjobs */
73         $app->load('monitor_tools');
74         $this->_tools = new monitor_tools();
75         /* end global section for monitor cronjobs */
76
5de2af 77         /* the id of the server as int */
M 78         $server_id = intval($conf['server_id']);
79
80         /** The type of the data */
81         $type = 'mailq';
82
83         /* Get the data from the mailq */
84         $data['output'] = shell_exec('mailq');
85
86         /*
87          *  The last line has more informations
88          */
89         $tmp = explode("\n", $data['output']);
90         $more = $tmp[sizeof($tmp) - 1];
91         $res = $this->_getIntArray($more);
92         $data['bytes'] = $res[0];
93         $data['requests'] = $res[1];
94
95         /** The state of the mailq. */
96         $state = 'ok';
97         if ($data['requests'] > 2000)
98             $state = 'info';
99         if ($data['requests'] > 5000)
100             $state = 'warning';
101         if ($data['requests'] > 8000)
102             $state = 'critical';
103         if ($data['requests'] > 10000)
104             $state = 'error';
105
106         $res = array();
107         $res['server_id'] = $server_id;
108         $res['type'] = $type;
109         $res['data'] = $data;
110         $res['state'] = $state;
111
112         /*
113          * Insert the data into the database
114          */
115         $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
cc7a82 116             'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)';
MC 117         $app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']);
5de2af 118
M 119         /* The new data is written, now we can delete the old one */
120         $this->_tools->delOldRecords($res['type'], $res['server_id']);
b1a6a5 121
MC 122         parent::onRunJob();
123     }
124
125     /* this function is optional if it contains no custom code */
126     public function onAfterRun() {
127         global $app;
128
129         parent::onAfterRun();
130     }
5de2af 131
M 132 }
133
134 ?>