-
Notifications
You must be signed in to change notification settings - Fork 2
/
syslog.class.php
627 lines (570 loc) · 17.8 KB
/
syslog.class.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
* The Syslog class is a syslog client implementation in PHP
* following the RFC 3164, 5424, 5425, 5426 rules.
* This class is compatible with PHP logger constants for severity and facility.
* Default value are UDP connection with RFC3164 mode
*
* Facility values:
* LOG_KERN kernel messages
* LOG_USER user-level messages
* LOG_MAIL mail system
* LOG_DAEMON system daemons
* LOG_AUTH security/authorization messages
* LOG_SYSLOG messages generated internally by syslogd
* LOG_LPR line printer subsystem
* LOG_NEWS network news subsystem
* LOG_UUCP UUCP subsystem
* LOG_CRON clock daemon
* LOG_AUTHPRIV security/authorization messages
* LOG_FTP FTP daemon
* LOG_NTP NTP subsystem
* LOG_AUDIT log audit
* LOG_LOG_ALERT log alert
* LOG_CLOCK clock daemon
* LOG_LOCAL0 local user 0 (local0) (default value)
* LOG_LOCAL1 local user 1 (local1)
* LOG_LOCAL2 local user 2 (local2)
* LOG_LOCAL3 local user 3 (local3)
* LOG_LOCAL4 local user 4 (local4)
* LOG_LOCAL5 local user 5 (local5)
* LOG_LOCAL6 local user 6 (local6)
* LOG_LOCAL7 local user 7 (local7)
*
* Severity values:
* LOG_EMERG Emergency: system is unusable
* LOG_ALERT Alert: action must be taken immediately
* LOG_CRIT Critical: critical conditions
* LOG_ERR Error: error conditions
* LOG_WARNING Warning: warning conditions
* LOG_NOTICE Notice: normal but significant condition (default value)
* LOG_INFO Informational: informational messages
* LOG_DEBUG Debug: debug-level messages
*
* Protocols:
* NET_SYSLOG_UDP udp protocol. Defaut behaviour
* NET_SYSLOG_TCP tcp protocol
* NET_SYSLOG_SSL ssl protocol. CA File can optionnaly be set
* NET_SYSLOG_TLS tls protocol
*
*
* Usage
*
* require_once('syslog.class.php');
* $syslog = new Net_Syslog($hostname = "", $appname = NET_SYSLOG_NILVALUE,
* $protocol = NET_SYSLOG_UDP, $_procid = NET_SYSLOG_NILVALUE);
* $syslog->logger($priority = LOG_LOCAL0 + LOG_NOTICE, $content = "");
* or
* $syslog->logger542X($priority = 133, $content = "Default content",
* $msgid = NET_SYSLOG_NILVALUE, $structured_data = NET_SYSLOG_NILVALUE);
* or
* $syslog->logger3164($priority = 133, $content = "Default content");
*
* Examples
*
* Example 1
*
* require_once('syslog.class.php');
* $syslog = new Net_Syslog();
* $syslog->logger(LOG_LOCAL0 + LOG_NOTICE, 'Syslog message');
*
*
* Example 2
*
* require_once('syslog.class.php');
* $syslog = new Net_Syslog('myserver', 'MyApp', NET_SYSLOG_TCP);
* $syslog->logger(LOG_LOCAL0 + LOG_NOTICE, 'Syslog message');
*
*
* Example 3
*
* require_once('syslog.class.php');
* $syslog = new Net_Syslog();
* $syslog->setHostname('myserver');
* $syslog->setRFC(NET_SYSLOG_RFC542X);
* $syslog->setAppname('MyApp');
* $syslog->setServer('192.168.0.12');
* $syslog->logger(LOG_LOCAL0 + LOG_NOTICE, 'Syslog message');
*
* Example 4
*
* require_once('syslog.class.php');
* $syslog = new Net_Syslog("myserver", "MyApp", NET_SYSLOG_SSL);
* $syslog->setCAFile("ca.crt");
* $syslog->logger(LOG_CRON + LOG_NOTICE, "Syslog message");
*
* Prerequisites
*
* - Sockets support must be enabled.
* * In Linux and *nix environments, the extension is enabled at
* compile time using the --enable-sockets configure option
* * In Windows, PHP Sockets can be activated by un-commenting
* extension=php_sockets.dll in php.ini
*
*
* TODO : RFC 5848, RFC 6587, Permanent socket
*
* PHP version 5
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU LesserGeneral Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Networking
* @package Net_Syslog
* @author Laurent Vromman <laurent@vromman.org>
* @copyright 2013 Laurent Vromman
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 3
* @link http://pear.php.net/package/Net_Syslog
*/
// {{{ constants
/**
* Set default linux network interface
*/
define("NET_SYSLOG_LINUX_NETWORD_INTERFACE", "eth0");
/**
* Set NILVALUE as defined in RFC 5424
*/
define("NET_SYSLOG_NILVALUE", "-");
/**
* Add missing LOG_FTP level log to existing PHP log levels
*/
defined('LOG_FTP') or define("LOG_FTP", 88);
/**
* Add missing LOG_NTP level log to existing PHP log levels
*/
defined('LOG_NTP') or define("LOG_NTP", 96);
/**
* Add missing LOG_LOG_AUDIT level log to existing PHP log levels
*/
defined('LOG_LOG_AUDIT') or define("LOG_LOG_AUDIT", 104);
/**
* Add missing LOG_ALERT level log to existing PHP log levels
*/
defined('LOG_ALERT') or define("LOG_ALERT", 112);
/**
* Add missing LOG_CLOCK level log to existing PHP log levels
*/
defined('LOG_CLOCK') or define("LOG_CLOCK", 120);
/**
* TCP connection mode
*/
define("NET_SYSLOG_TCP", "tcp");
/**
* UDP connection mode
*/
define("NET_SYSLOG_UDP", "udp");
/**
* SSL connection mode
*/
define("NET_SYSLOG_SSL", "ssl");
/**
* TLS connection mode
*/
define("NET_SYSLOG_TLS", "tls");
/**
* Compatibility for RFC 5424, 5425 and 5426
*/
define("NET_SYSLOG_RFC542X", 1);
/**
* Compatibility for RFC 3164
*/
define("NET_SYSLOG_RFC3164", 0);
// }}}
/**
* Short description for class
*
* Long description for class (if any)...
*
* @category Networking
* @package Net_Syslog
* @author Laurent Vromman <laurent@vromman.org>
* @copyright 2013 Laurent Vromman
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 3
* @version SVN: $Id$
* @link http://pear.php.net/package/Net_Syslog
*/
class Net_Syslog
{
/**
* Sender hostname
*
* No domain name, only a-z A-Z 0-9 and other authorized characters
*
* @access private
* @var string
*/
private $_hostname;
/**
* Syslog remote server address
*
* @access private
* @var string
*/
private $_server;
/**
* Syslog remote server port
*
* Standard syslog port is 514 or 6514 for RFC 5425 (TLS)
*
* @access private
* @var integer
*/
private $_port;
/**
* Protocol to syslog server
*
* Allowed values are :
* - NET_SYSLOG_UDP : UDP
* - NET_SYSLOG_TCP : TCP
* - NET_SYSLOG_SSL : SSL
* - NET_SYSLOG_TLS : TLS
*
* @access private
* @var string
*/
private $_protocol;
/**
* Socket used by class
*
* @access private
* @var object
*/
private $_socket;
/**
* filename for CA Certificate used in SSL connection, if necessary
*
* @access private
* @var string
*/
private $_cafile;
/**
* ProcID as defined in RFC 5424
*
* @access private
* @var string
*/
private $_procid;
/**
* AppName as defined in RFC 5424
*
* @access private
* @var string
*/
private $_appname;
/**
* Constructor of class
*
* @param string $hostname Optional. Sender hostname.
* @param string $appname Optional. AppName as defined in RFC 5424.
* @param string $protocol Optional. Can be NET_SYSLOG_UDP, NET_SYSLOG_TCP, NET_SYSLOG_SSL or NET_SYSLOG_TLS.
* @param string $procid Optional. ProcID as defined in RFC 5424.
*
* @access public
* @see Net_Syslog
*/
public function __construct($hostname = "", $appname = NET_SYSLOG_NILVALUE,
$protocol = NET_SYSLOG_UDP, $procid = NET_SYSLOG_NILVALUE
) {
$this->_rfc = NET_SYSLOG_RFC3164;
$this->_socket = FALSE;
$this->_server = '127.0.0.1';
$this->setProcid($procid);
$this->setAppname($appname);
$this->_hostname = $hostname;
if (strlen($hostname) == 0) {
if (isset($_SERVER["SERVER_NAME"])) {
$hostname = $_SERVER["SERVER_NAME"];
if($this->_rfc == NET_SYSLOG_RFC3164)
$hostname = substr($hostname, 0, strpos($hostname.".", "."));
}
elseif (isset($_SERVER["SERVER_ADDR"])) {
$hostname = $_SERVER["SERVER_ADDR"];
}
else {
if($this->_rfc == NET_SYSLOG_RFC3164)
$hostname = "server";
else
$hostname = NET_SYSLOG_NILVALUE;
}
}
$this->setHostname($hostname);
$this->setProtocol($protocol);
if (!in_array($this->_protocol,
array(NET_SYSLOG_UDP, NET_SYSLOG_TCP, NET_SYSLOG_SSL, NET_SYSLOG_TLS)
)
) {
$this->_protocol = NET_SYSLOG_UDP;
}
// RFC5425
if($this->_protocol == NET_SYSLOG_TLS)
$this->_port = 6514;
else
$this->_port = 514;
}
/**
* Function to get local server address when not available
*
* @return string Local server address
*
* @access private
*/
private function getServerAddress()
{
if(array_key_exists('SERVER_ADDR', $_SERVER))
return $_SERVER['SERVER_ADDR'];
elseif(array_key_exists('LOCAL_ADDR', $_SERVER))
return $_SERVER['LOCAL_ADDR'];
else {
// Running CLI
if(stristr(PHP_OS, 'WIN')) {
return gethostbyname(php_uname("n"));
}
else {
$ifconfig = shell_exec('/sbin/ifconfig '.NET_SYSLOG_LINUX_NETWORD_INTERFACE);
preg_match('/addr:([\d\.]+)/', $ifconfig, $match);
return $match[1];
}
}
}
/**
* RFC Mode setter
*
* @param int $rfc The RFC mode. Can be NET_SYSLOG_RFC542X or NET_SYSLOG_RFC3164
*
* @access public
*/
public function setRFC($rfc)
{
$this->_rfc = $rfc;
}
/**
* Sender hostname setter
*
* @param string $hostname No domain name, only a-z A-Z 0-9 and other authorized characters
*
* @access public
*/
public function setHostname($hostname)
{
$this->_hostname = substr($hostname, 0, 255);
if(strlen($this->_hostname) == 0) $this->_hostname = NET_SYSLOG_NILVALUE;
}
/**
* Remote server address setter
*
* @param string $server Can be an ip or a network name
*
* @access public
*/
public function setServer($server)
{
$this->_server = $server;
}
/**
* Remote server port setter
*
* @param int $port TCP or UDP port on the remote syslog server
*
* @access public
*/
public function setPort($port)
{
if ((intval($port) > 0) && (intval($port) < 65536)) {
$this->_port = intval($port);
}
}
/**
* Protocol setter
*
* @param string $protocol Can be NET_SYSLOG_UDP, NET_SYSLOG_TCP, NET_SYSLOG_SSL or NET_SYSLOG_TLS
*
* @access public
*/
public function setProtocol($protocol)
{
if (in_array($protocol, array(NET_SYSLOG_UDP, NET_SYSLOG_TCP, NET_SYSLOG_SSL, NET_SYSLOG_TLS))) {
$this->_protocol = $protocol;
}
}
/**
* CA File setter
*
* @param string $cafile Filename for CA Certificate used in SSL connection, if necessary
*
* @access public
*/
public function setCAFile($cafile)
{
$this->_cafile = $cafile;
}
/**
* ProcID setter
*
* @param string $procid ProcID as defined in RFC 5424
*
* @access public
*/
public function setProcid($procid)
{
$this->_procid = substr($procid, 0, 128);
if(strlen($this->_procid) == 0) $this->_procid = NET_SYSLOG_NILVALUE;
}
/**
* AppName setter
*
* @param string $appname AppName as defined in RFC 5424
*
* @access public
*/
public function setAppname($appname)
{
$this->_appname = substr($appname, 0, 48);
if(strlen($this->_appname) == 0) $this->_appname = NET_SYSLOG_NILVALUE;
}
/**
* Open the socket to connect to the remote syslog server
*
* @access private
*/
private function openSocket ()
{
if ($this->_socket)
$this->closeSocket();
$contextOptions = array();;
if($this->_protocol == NET_SYSLOG_SSL && $this->_cafile != NULL) {
//http://php.net/manual/en/context.ssl.php
$contextOptions = array(
'ssl' => array(
'cafile' => $this->_cafile
)
);
}
$sslContext = stream_context_create($contextOptions);
$this->_socket = stream_socket_client(
$this->_protocol."://".$this->_server.":".$this->_port,
$errno,
$errstr,
ini_get("default_socket_timeout"),
STREAM_CLIENT_CONNECT,
$sslContext);
if (!$this->_socket) {
throw new Exception("ERROR: $errno - $errstr");
}
}
/**
* Close the socket to the remote syslog server
*
* @access private
*/
private function closeSocket ()
{
fclose($this->_socket);
$this->_socket = NULL;
}
/**
* Function to send a log message RFC3164 compliant
*
* @param int $priority Optional. Priority of message. Is a sum of Severity and Criticity.
* @param string $content Optional. Message content.
*
* @access public
* @see Net_Syslog
*/
public function logger3164($priority = 133, $content = "Default content")
{
$rfc = $this->_rfc;
$this->_rfc = NET_SYSLOG_RFC3164;
$this->logger($priority, $content);
$this->_rfc = $rfc;
}
/**
* Function to send a log message RFC542X compliant
*
* @param int $priority Optional. Priority of message. Is a sum of Severity and Criticity.
* @param string $content Optional. Message content.
* @param string $msgid Optional. MsgID of the message, according to RFC5424.
* @param string $structured_data Optional. Structured data of the message, according to RFC5424.
*
* @access public
* @see Net_Syslog
*/
public function logger542X($priority = 133, $content = "Default content",
$msgid = "-", $structured_data = "-"
) {
$rfc = $this->_rfc;
$this->_rfc = NET_SYSLOG_RFC542X;
$this->logger($priority, $content, $msgid = "-", $structured_data);
$this->_rfc = $rfc;
}
/**
* Function to send a log message. RFC3164 or 542X chosen according to $_rfc parameter.
*
* @param int $priority Optional. Priority of message. Is a sum of Severity and Criticity.
* @param string $content Optional. Message content.
* @param string $msgid Optional. MsgID of the message, according to RFC5424. Ignored in RFC3164 mode.
* @param string $structured_data Optional. Structured data of the message, according to RFC5424. Ignored in RFC3164 mode.
*
* @access public
* @see Net_Syslog
*/
public function logger($priority = 133, $content = "Default content",
$msgid = "-", $structured_data = "-"
) {
$this->_content = $content;
if(strlen($msgid) == 0) $msgid = NET_SYSLOG_NILVALUE;
if(strlen($structured_data) == 0) $structured_data = NET_SYSLOG_NILVALUE;
$facility = floor($priority/8);
$severity = $priority - $facility * 8;
if (0 > $severity || $severity > 7) {
throw new Exception("ERROR: unrecognized severity value : $severity");
}
if (0 > $facility || $facility > 23) {
throw new Exception("ERROR: unrecognized facility value : $facility");
}
$timestamp = date("c");
$pri = "<$priority>";
if($this->_rfc == NET_SYSLOG_RFC542X) {
$timestamp = date("c");
$syslog_version = "1 ";
}
else {
$actualtime = time();
$timestamp = date("M ", $actualtime)
.substr(date(" j", $actualtime), -2)
.date(" H:i:s", $actualtime);
$syslog_version = "";
}
$header = $pri.$syslog_version.$timestamp." ".$this->_hostname." ";
if($this->_rfc == NET_SYSLOG_RFC542X) {
$header .= $this->_appname." ".$this->_procid." ".substr($msgid, 0, 32);
$message = $header. " ".$structured_data." ".$content;
}
else {
// RFC 3164 : Tagname max len : 32
// RFC 3164 : Message max len : 1024
if(strlen($this->_appname) > 0)
$tag = substr($this->_appname, 0 , 32).": ";
$message = substr($header.$tag.$content, 0, 1024);
}
$this->openSocket();
// RFC 5425
if($this->_rfc == NET_SYSLOG_RFC542X && $this->_protocol == NET_SYSLOG_TLS) {
$message = strlen($message)." ".$message;
}
fwrite($this->_socket, $message);
$this->closeSocket();
}
}
?>