4

PHP程序如何发送syslog到远程服务器

 2 years ago
source link: https://zhangrr.github.io/posts/20211028-php_syslog/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP程序如何发送syslog到远程服务器

2021-10-28 2 分钟阅读

给同事做了个 PHP 接口,转发发送短信的请求,同时要把发送记录发送到远程的 cacti 的 syslog 去

很简单,但是也不简单

首先是 PHP 服务器,是最简化编译的,php -m 查了一下

php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
gettext
hash
iconv
json
libxml
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter

[Zend Modules]

居然没有 socket 模块,没办法,找到源代码,编译一个安装,原有的 php 安装路径是 /export/servers/php

$ tar zxvf php-7.4.0.tar.gz 
$ cd php-7.4.0/sockets
$ /export/servers/php/bin/phpize
$ ./configure --enable-sockets --with-php-config=/export/servers/php/bin/php-config
$ make
$ make install
shell

又看了一眼,是 php-fpm,居然没有 php.ini ,得,再生成一个,放在 /export/servers/php/lib/php.ini

extension_dir = "/export/servers/php740/lib/php/extensions/no-debug-non-zts-20190902/"
extension = sockets.so

然后重启 php-fpm ,重新 php -m 检查,发现有 socket 模块就 ok 了。

接下来就是 php 源代码了

<?php
date_default_timezone_set('Asia/Shanghai');

function send_remote_syslog($message) {
    $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    foreach(explode("\n", $message) as $line) {
      $syslog_message = "<22>" . date('M d H:i:s ') . 'Qi_an_xin sms_log: ' . $line;
      socket_sendto($sock, $syslog_message, strlen($syslog_message), 0, '172.18.31.6', 514);
    }
    socket_close($sock);
}

    send_remote_syslog("ABC 验证码: 39792192");
?>    

这样就可以了,上面代码比较难理解的是<22>,那是报错级别的计算方法,Facility + Severity:

 *   Facility values:
 *      0 kernel messages
 *      1 user-level messages
 *      2 mail system
 *      3 system daemons
 *      4 security/authorization messages
 *      5 messages generated internally by syslogd
 *      6 line printer subsystem
 *      7 network news subsystem
 *      8 UUCP subsystem
 *      9 clock daemon
 *     10 security/authorization messages
 *     11 FTP daemon
 *     12 NTP subsystem
 *     13 log audit
 *     14 log alert
 *     15 clock daemon
 *     16 local user 0 (local0) (default value)
 *     17 local user 1 (local1)
 *     18 local user 2 (local2)
 *     19 local user 3 (local3)
 *     20 local user 4 (local4)
 *     21 local user 5 (local5)
 *     22 local user 6 (local6)
 *     23 local user 7 (local7)
 *
 *   Severity values:
 *     0 Emergency: system is unusable
 *     1 Alert: action must be taken immediately
 *     2 Critical: critical conditions
 *     3 Error: error conditions
 *     4 Warning: warning conditions
 *     5 Notice: normal but significant condition (default value)
 *     6 Informational: informational messages
 *     7 Debug: debug-level messages

计算方法就是 (facility*8 + severity),这里的22+0,可以理解成 local6 ,就是级别6

image-20211028114512589

如果发了一个 “local use 4” 和 Serverity = 5 的消息,那么就是 20×8+5=165 ,包头就是 <165>


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK