-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogParser.php
More file actions
executable file
·170 lines (131 loc) · 4 KB
/
Copy pathlogParser.php
File metadata and controls
executable file
·170 lines (131 loc) · 4 KB
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
#!/usr/bin/php
<?php
// signal handler function
function sig_handler($signo) {{{
global $handle;
switch ($signo) {
case SIGTERM:
// handle shutdown tasks
unlink('/var/run/logParser.pid');
fclose($handle);
closelog();
exit;
break;
default:
// handle all other signals
}
}}}
$logFile = '/var/log/Daemon.log';
$delimiter = "\n";
$pattern = '/ERROR/i';
if(!(file_exists($logFile) && is_readable($logFile)) ) {
die("Can't Read File".PHP_EOL);
}
$pid = pcntl_fork();
if($pid == -1){
return 1;
}
else if($pid){
file_put_contents('/var/run/logParser.pid',$pid);
exit;
}
else{
// We are the child...
if (posix_setsid() === -1) {
die('Could not setsid');
}
fclose(STDIN); // Close all of the standard
fclose(STDOUT); // file descriptors as we
fclose(STDERR); // are running as a daemon.
set_time_limit (0);
chdir("/");
// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
$oldSize = 0;
$handle = fopen($logFile,'r');
openlog("myLogParser", LOG_PID|LOG_ODELAY , LOG_LOCAL3);
// loop forever
for (;;) {
pcntl_signal_dispatch(); // remember to call the signal dispatch to see if we have any waiting signals
$line = stream_get_line($handle, 10000 , $delimiter);
if($line === FALSE) {
sleep(1);
} else {
$count = preg_match_all($pattern, $line, $matches);
if($count > 0 ) {
$mail = new SMTPSend();
$mail->smtp_mail('admin@mail.com','Error Found',$line);
$mail->close();
unset($mail);
}
}
// Clear the stat cache each time
clearstatcache();
$size = filesize($logFile);
if($oldSize > $size) {
syslog(LOG_ERR,"Oldsize $oldSize Size $size".PHP_EOL);
// We must of rotated close the handle and open a new one
fclose($handle);
$handle = fopen($logFile,'r');
$oldSize = $size = 0;
} else {
$oldSize = $size;
}
}
fclose($handle);
exit;
}
class SMTPSend {
function smtp_mail($to, $subject, $message, $headers = '') {{{
$recipients = explode(',', $to);
$user = 'user@mail.com';
$mailfrom = '<user@mail.com>';
$pass = 'password';
$smtp_host = 'ssl://smtp.gmail.com';
$smtp_port = 465;
if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15))) {
//echo( "Could not connect to smtp host '$smtp_host' ($errno) ($errstr)");
}
if(!$this->server_parse($socket, '220')) return FALSE;
fwrite($socket, 'EHLO '.$smtp_host."\r\n");
if(!$this->server_parse($socket, '250')) return FALSE;
fwrite($socket, 'AUTH LOGIN'."\r\n");
if(!$this->server_parse($socket, '334')) return FALSE;
fwrite($socket, base64_encode($user)."\r\n");
if(!$this->server_parse($socket, '334')) return FALSE;
fwrite($socket, base64_encode($pass)."\r\n");
if(!$this->server_parse($socket, '235')) return FALSE;
fwrite($socket, 'MAIL FROM: '.$mailfrom."\r\n");
if(!$this->server_parse($socket, '250')) return FALSE;
foreach ($recipients as $email) {
fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
if(!$this->server_parse($socket, '250')) return FALSE;
}
fwrite($socket, 'DATA'."\r\n");
if(!$this->server_parse($socket, '354')) return FALSE;
fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
fwrite($socket, '.'."\r\n");
if(!$this->server_parse($socket, '250')) return FALSE;
fwrite($socket, 'QUIT'."\r\n");
fclose($socket);
return TRUE;
}}}
function server_parse($socket, $expected_response) {{{
$server_response = '';
while (substr($server_response, 3, 1) != ' ') {
if (!($server_response = fgets($socket, 256))) {
//echo( "Couldn't get mail server response codes. Please contact the administrator.");
return FALSE;
}
}
if (!(substr($server_response, 0, 3) == $expected_response)) {
//echo( "Unable to send e-mail. Please contact the administrator with the following error message reported by the SMTP server: ".var_export($server_response,TRUE));
return FALSE;
}
return TRUE;
}}}
function close() {{{
// Nothing to do here
}}}
}
?>