forked from poppyred/1.6.x
/
cron.notifs.php
executable file
·394 lines (300 loc) · 12.6 KB
/
cron.notifs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
include_once(dirname(__FILE__) . '/class.cronldap.inc');
include_once(dirname(__FILE__) . '/ressources/class.mysql.inc');
include_once(dirname(__FILE__) . '/ressources/logs.inc');
include_once(dirname(__FILE__).'/framework/class.unix.inc');
include_once(dirname(__FILE__)."/framework/frame.class.inc");
include_once(dirname(__FILE__).'/ressources/class.http.pear.inc');
include_once(dirname(__FILE__).'/ressources/class.artica-meta.inc');
if($argv[1]=='--sendmail'){SendMailNotification(null,null,true);die();}
if(preg_match("#--verbose#",implode(" ",$argv))){$_GET["DEBUG"]=true;}
if($argv[1]=='--tests'){send_email_events($argv[2],"system","test");die();}
$unix=new unix();
$pidfile="/etc/artica-postfix/croned.1/cron.notifs.php.pid";
$pidTime="/etc/artica-postfix/croned.1/cron.notifs.php.time";
if($unix->file_time_min($pidTime)<5){die();}
@unlink($pidTime);@file_put_contents($pidTime, time());
$pid=@file_get_contents($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($time>50){
$status[]=@file_get_contents("/proc/$pid/status");
$status[]=@file_get_contents("/proc/$pid/cmdline");
$unix->send_email_events("cron.notif has been killed (ghost process)", "$pid: running since $time minutes\n". @implode("\n", $status), "system");
$kill=$unix->find_program("kill");
unix_system_kill_force($pid);
}else{
writelogs("Already instance executed $pid since $time minutes","MAIN",__FILE__,__LINE__);
die();
}
}
$pid=getmypid();
@mkdir("/etc/artica-postfix/croned.1");
file_put_contents($pidfile,$pid);
events("new pid $pid");
echo events("Starting parsing events...");
ParseEvents();
echo events("Starting Launch notifications");
LaunchNotifs();
echo events("Die");
die();
function ParseReboot(){
$path="/etc/artica-postfix/reboot";
$f=new filesClasses();
$hash=$f->DirListTable($path);
if(!is_array($hash)){return null;}
events(count($hash) . " reboot file(s) notifications...");
$unix=new unix();
$mysql=new mysql();
$TIME1=0;
while (list ($num, $file) = each ($hash)){
if(!is_numeric($file)){@unlink($path.'/'.$file);continue;}
$date=date("Y-m-d H:i:s",$file);
if($TIME1>0){
$difference = ($TIME1 - $file);
$min=round($difference/60);
if($min<5){
@unlink($path.'/'.$file);
continue;
}
}
$array=unserialize(@file_get_contents($path.'/'.$file));
$buffer=$array["buffer"];
if(isset($ALREADY[md5($buffer)])){@unlink($path.'/'.$file);continue;}
$ALREADY[md5($buffer)]=true;
$dmsg=@implode("\n",$array["dmsg"]);
$text="Why artica has rebooted ?\nDetected\n$buffer\n\nDmsg:$dmsg";
$subject="Reboot report";
$subject=addslashes($subject);
$text=addslashes($text);
$sql="INSERT IGNORE INTO events (zDate,hostname,
process,text,context,content,attached_files,recipient,event_id) VALUES(
'$date',
'$mysql->hostname',
'reboot',
'$subject',
'system','$text','$text','','')";
writelogs("New notification: reboot report (". strlen($text)." bytes) $date",__FUNCTION__,__FILE__,__LINE__);
if(!$mysql->QUERY_SQL($sql,'artica_events')){
events("Mysql error keep $path/$file;");
events("Fatal: $mysql->mysql_error",__FUNCTION__,__FILE__,__LINE__);
continue;
}
$TIME1=$file;
@unlink($path.'/'.$file);
}
}
function ParseEvents(){
ParseReboot();
$unix=new unix();
$path="/var/log/artica-postfix/events";
$f=new filesClasses();
$hash=$f->DirListTable($path);
if(!is_array($hash)){return null;}
$users=new usersMenus();
$sock=new sockets();
$ArticaMetaEnabled=$sock->GET_INFO("ArticaMetaEnabled");
echo date('Y-m-d h:i:s')." " .count($hash) . " file(s) notifications...\n";
events(count($hash) . " file(s) notifications...");
if(count($hash)==0){return;}
if($ArticaMetaEnabled==1){
$meta=new artica_meta();
$datasToSend=base64_encode(serialize($meta->GLOBAL_ARRAY));
$ArticaMetaHostname=$meta->ArticaMetaHostname;
events("ArticaMetaEnabled: \"$ArticaMetaEnabled\" -> ($ArticaMetaHostname)");
}
$mysql=new mysql();
while (list ($num, $file) = each ($hash)){
$text=null;
$processname=null;
$date=null;
$context=null;
$subject=null;
$recipient=null;
$FULL_PATH="$path/$file";
if($unix->file_time_min($FULL_PATH)>480){@unlink($FULL_PATH);continue; }
$bigtext=@file_get_contents($FULL_PATH);
echo date('Y-m-d h:i:s')." Parsing $file ". strlen($bigtext)." bytes text\n";
$ini=new Bs_IniHandler();
if(preg_match("#<text>(.+?)</text>#is",$bigtext,$re)){
$text=$re[1];
$bigtext=str_replace($re[0],'',$bigtext);
$bigtext=str_replace("'", "`", $bigtext);
}
if(preg_match("#<attachedfiles>(.+?)</attachedfiles>#is",$bigtext,$re)){
$bigtext=str_replace($re[0],'',$bigtext);
$files_text=addslashes($re[1]);
}
$ini->loadString($bigtext);
$processname=$ini->_params["LOG"]["processname"];
$date=$ini->_params["LOG"]["date"];
$context=$ini->_params["LOG"]["context"];
if($context=="YTowOnt9"){$context="system";}
$subject=$ini->_params["LOG"]["subject"];
$recipient=$ini->_params["LOG"]["recipient"];
if(strlen($text)<2){
$text=$ini->_params["LOG"]["text"];
}
$arrayToSend["context"]=$context;
$arrayToSend["subject"]=$subject;
$arrayToSend["text"]=$text;
$arrayToSend["date"]=$date;
echo date('Y-m-d h:i:s')." Parsing subject $subject ". strlen($text)." bytes text\n";
writelogs("New notification: $subject (". strlen($text)." bytes) $date",__FUNCTION__,__FILE__,__LINE__);
$event_id=time();
//$text=addslashes($text);
$text=str_replace("'", "`", $text);
$subject=str_replace("'", "`", $subject);
$text=addslashes($text);
$subject=addslashes($subject);
if($subject==null){
if(strlen($text)>5){
if(strpos($text, "\n")==0){$subject=$text;}else{
$subject=substr($text, 0,75)."...";
}
}
}
if(strlen($subject)<5){
writelogs("Warning New notification: Subject seems to be empty ! \"$text\" removing $path/$file",__FUNCTION__,__FILE__,__LINE__);
@unlink("$path/$file");
continue;
}
if(strpos($users->hostname, ".")>0){
$ty=explode($users->hostname,".");
$users->hostname=$ty[0];
}
if(strlen($text)<5){$text="No content body as been added for this notification";}
$sql="INSERT IGNORE INTO events (zDate,hostname,process,text,context,content,attached_files,recipient,event_id) VALUES(
'$date',
'$users->hostname',
'$processname',
'$subject',
'$context','$text','$files_text','$recipient','$event_id')";
if(!$mysql->UseMysql){
$sql="INSERT IGNORE INTO events (id,zDate,hostname,
process,text,context,content,attached_files,recipient,event_id) VALUES(
'$event_id','$date',
'$users->hostname',
'$processname',
'$subject',
'$context','$text','$files_text','$recipient','$event_id')";
}
events(date('Y-m-d h:i:s')." run mysql query -> $subject");
if(!$mysql->QUERY_SQL($sql,'artica_events')){
events("Mysql error keep $path/$file;");
events("Fatal: $mysql->mysql_error",__FUNCTION__,__FILE__,__LINE__);
if(preg_match("#Access denied for user.+?using password:#",$mysql->mysql_error)){
events("Access denied for user password: $mysql->mysql_server@$mysql->mysql_admin:$mysql->mysql_password detected");
if(($mysql->mysql_server=="127.0.0.1") OR ($mysql->mysql_server=="localhost")){
$ldap=new clladp();
$unix=new unix();
$ldap->ldap_password=$unix->shellEscapeChars($ldap->ldap_password);
writelogs("Try to change the mysql password: /usr/share/artica-postfix/bin/artica-install --change-mysqlroot --inline root \"secret\"",__FUNCTION__,__FILE__,__LINE__);
exec("/usr/share/artica-postfix/bin/artica-install --change-mysqlroot --inline root \"secret\" 2>&1",$chroot);
writelogs("Chaning password=".@implode("\n",$chroot),__FUNCTION__,__FILE__,__LINE__);
die();
}
}
if(preg_match("#Unknown column#",$mysql->mysql_error)){events("->BuildTables()");$mysql->BuildTables();}
if(preg_match("#Unknown database#",$mysql->mysql_error)){events("->BuildTables()");$mysql->BuildTables();}
if(preg_match("#connect to local MySQL server through socket#",$mysql->mysql_error)){shell_exec("/etc/init.d/artica-postfix start mysql &");}
writelogs("$sql",__FUNCTION__,__FILE__,__LINE__);
break;
}
unlink($path.'/'.$file);
if($ArticaMetaEnabled==1){
$pidfile="/etc/artica-postfix/pids/exec.artica.meta.php.SendStatus.pid";
$sock=new sockets();
$ArticaMetaPoolTimeMin=$sock->GET_INFO("ArticaMetaPoolTimeMin");
if(!is_numeric($ArticaMetaPoolTimeMin)){$ArticaMetaPoolTimeMin=15;}
if($ArticaMetaPoolTimeMin<2){$ArticaMetaPoolTimeMin=15;}
$minutes=file_time_min($pidfile);
if($minutes<round(($ArticaMetaPoolTimeMin/2.5))){
$meta->events(basename($pidfile).":{$minutes}<".round(($ArticaMetaPoolTimeMin/2.5))."Mn, aborting",__FUNCTION__,__FILE__,__LINE__);
return;
}
$http=new httpget();
$meta->events("Send notification \"{$arrayToSend["subject"]}\" to Meta Server",__FUNCTION__,__FILE__,__LINE__);
$metaconsole=$http->send("$ArticaMetaHostname/lic.status.notifs.php","post",array(
"DATAS"=>$datasToSend,
"NOTIF"=>base64_encode(serialize($arrayToSend))
));
events("META CONSOLE: $metaconsole aborting notifications");
if($metaconsole=="FAILED_CONNECT"){
$meta->events("Result:\"$metaconsole\"",__FUNCTION__,__FILE__,__LINE__);
return;
}
if(!is_file("/etc/artica-postfix/artica-meta.tasks")){
if(preg_match("#<TASKS>(.+?)</TASKS>#is",$metaconsole,$re)){
$meta->events("Save tasks to /etc/artica-postfix/artica-meta.tasks",__FUNCTION__,__FILE__,__LINE__);
@file_put_contents("/etc/artica-postfix/artica-meta.tasks",$re[1]);
$cmd=LOCATE_PHP5_BIN()." ".dirname(__FILE__)."/exec.artica.meta.tasks.php >/dev/null 2>&1 &";
$meta->events("TASKS ->$cmd",__FUNCTION__,__FILE__,__LINE__);
shell_exec($cmd);
}
}
}
$text=addslashes($text);
$context=addslashes($context);
$subject=addslashes($subject);
}
if(count($hash)>0){events(count($hash). " events queue parsed...");}
if($ArticaMetaEnabled==1){
if(is_file("/etc/artica-postfix/artica-meta.tasks")){
$NICE=EXEC_NICE();
shell_exec($NICE.LOCATE_PHP5_BIN2()." ".dirname(__FILE__)."/exec.artica.meta.tasks.php &");
}
}
}
function LaunchNotifs(){
$ini=new Bs_IniHandler("/etc/artica-postfix/smtpnotif.conf");
$sa_learn=$ini->_params["SMTP"]["sa-learn"];
$system=$ini->_params["SMTP"]["system"];
$update=$ini->_params["SMTP"]["update"];
$q=new mysql();
$sql="SELECT * FROM `events` WHERE sended=0 ORDER BY zDate DESC LIMIT 0,100";
$results=$q->QUERY_SQL($sql,"artica_events");
while($ligne=@mysql_fetch_array($results,MYSQL_ASSOC)){
$attached_files=unserialize(base64_decode($ligne["attached_files"]));
if(trim($ligne["content"])==null){
$q->QUERY_SQL("DELETE FROM events WHERE ID={$ligne["ID"]}","artica_events");
continue;
}
if(trim($ligne["text"])==null){
$q->QUERY_SQL("DELETE FROM events WHERE ID={$ligne["ID"]}","artica_events");
continue;
}
$context=$ligne["context"];
$ligne["content"]=str_replace('[br]',"\n",$ligne["content"]);
events("New event: {$ligne["text"]}, context=$context");
if($ini->_params["SMTP"][$context]==1){
$ligne["content"]="{$ligne["zDate"]} :{$ligne["process"]}: {$ligne["text"]}\n\n-----------------------------------------------------\n{$ligne["content"]}\n";
events("Notify {$ligne["text"]} -> SendMailNotification()");
SendMailNotification($ligne["content"],"[$context]: {$ligne["text"]}",false,$attached_files,$ligne["recipient"]);
}else{
events("$context is not enabled, notifications disabled");
}
$sql="UPDATE events SET sended=1 WHERE ID={$ligne["ID"]}";
$q->QUERY_SQL($sql,"artica_events");
if(!$q->ok){
events("Mysql error $sql");
}
}
}
function events($text){
$pid=getmypid();
$filename=basename(__FILE__);
$date=date("H:i:s");
$logFile="/var/log/artica-postfix/notifications.debug";
$size=filesize($logFile);
if($size>1000000){unlink($logFile);}
$f = @fopen($logFile, 'a');
$line="$date {$filename}[{$pid}] $text\n";
if($_GET["DEBUG"]){echo $line;}
@fwrite($f,$line);
@fclose($f);
}
//sa-learn
//system
//update
?>