5
5
6
6
class PHPMonitoring {
7
7
8
+ const ALERT_FILE = 'alert.lock ' ;
8
9
const CONFIG_FILE = 'config.inc.php ' ;
9
10
const ERROR_LOG = '/var/log/php-monitoring.log ' ;
10
11
var $ config ;
@@ -16,11 +17,10 @@ function init(){
16
17
ini_set ('log_errors ' , 1 );
17
18
ini_set ('error_log ' , self ::ERROR_LOG );
18
19
global $ config ;
19
- $ config_file = dirname (__FILE__ ) . '/ ' . self ::CONFIG_FILE ;
20
- if (!file_exists ($ config_file )){
21
- throw new Exception ("config file $ config_file not found " );
20
+ if (!file_exists ($ this ->getConfigFilePath ())){
21
+ throw new Exception ("config file {$ this ->getConfigFilePath ()} not found " );
22
22
}
23
- require_once $ config_file ;
23
+ require_once $ this -> getConfigFilePath () ;
24
24
$ this ->config = $ config ;
25
25
$ this ->config ['results ' ] = array ();
26
26
}
@@ -92,7 +92,7 @@ function error($msg){
92
92
}
93
93
94
94
/**
95
- * Send a mail alert
95
+ * Send a mail alert once per day
96
96
*/
97
97
function alert (){
98
98
if (
@@ -102,21 +102,52 @@ function alert(){
102
102
){
103
103
throw new Exception ('alert not configured ' );
104
104
}
105
- $ body = '' ;
106
- foreach ($ this ->getServices () as $ service ){
107
- $ body .= $ this ->printService ($ service );
105
+ if (file_exists ($ this ->getAlertFilePath ())){
106
+ if (date ('d ' , filemtime ($ this ->getAlertFilePath ())) != date ('d ' )){
107
+ unlink ($ this ->getAlertFilePath ());
108
+ }else {
109
+ return ;
110
+ }
108
111
}
109
- $ result = mail (
110
- $ this ->config ['alert ' ]['to ' ],
111
- $ this ->config ['alert ' ]['subject ' ],
112
- $ body
113
- );
114
- if (!$ result ){
115
- $ this ->error ('could not send alert ' );
112
+ $ this ->config ['alert ' ]['body ' ] = '' ;
113
+ foreach ($ this ->getServices () as $ service ){
114
+ $ this ->config ['alert ' ]['body ' ] .= $ this ->printService ($ service );
116
115
}
116
+ $ this ->mail ($ this ->config ['alert ' ]);
117
+ touch ($ this ->getAlertFilePath ());
117
118
return $ result ;
118
119
}
119
120
121
+ /**
122
+ * Send a mail using PHP PEAR library
123
+ */
124
+ function mail ($ opts = array ()){
125
+ if (!isset ($ opts ['factory ' ])) throw new Exception ('mail factory not set ' );
126
+ if (!isset ($ opts ['parameters ' ])) throw new Exception ('mail parameters not set ' );
127
+ if (!isset ($ opts ['headers ' ])) throw new Exception ('mail headers not set ' );
128
+ if (!isset ($ opts ['body ' ])) throw new Exception ('mail body not set ' );
129
+ require_once ('Mail.php ' );
130
+ $ mail =& Mail::factory ($ opts ['factory ' ], $ opts ['parameters ' ]);
131
+ $ mail ->send ($ opts ['headers ' ]['To ' ], $ opts ['headers ' ], $ opts ['body ' ]);
132
+ if (PEAR ::isError ($ mail )) {
133
+ throw new Exception ($ mail ->getMessage ());
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Get config file path
139
+ */
140
+ function getConfigFilePath (){
141
+ return dirname (__FILE__ ) . '/ ' . self ::CONFIG_FILE ;
142
+ }
143
+
144
+ /**
145
+ * Get alert file path
146
+ */
147
+ function getAlertFilePath (){
148
+ return dirname (__FILE__ ) . '/ ' . self ::ALERT_FILE ;
149
+ }
150
+
120
151
/**
121
152
* Get services from config
122
153
*/
0 commit comments