因为看见许多朋友也都遇到过我这种情况,所以拿出来供大家参考下,我用的loganalyzer版本是Installed version: 3.6.5
这里先贴下效果图:
添加ip字段
1.php文件添加字段
vim ./include/constants_logstream.php +354 修改这个PHP文件
2.视图添加字段
3.数据库添加字段
修改/etc/rsyslog.conf 文件添加一下2行。
$template insertpl,"insert into SystemEvents (Message, Facility, FromHost, FromIP,Priority, DeviceReportedTime, ReceivedAt, InfoUnitID, SysLogTag) values ('%msg%', %syslogfacility%,'%HOSTNAME%', '%fromhost-ip%', %syslogpriority%, '%timereported:::date-mysql%', '%timegenerated:::date-mysql%', %iut%, '%syslogtag%')",SQL *.* :ommysql:localhost,Syslog,logger,123456;insertpl mysql> USE Syslog; mysql> ALTER TABLE SystemEvents ADD FromIP VARCHAR(60) DEFAULT NULL AFTER FromHost;
这里要注意看下database mapping 数据库对应映射关系。要是映射不对,IP列会显示不出来。 rsyslog配置有修改,所以完成要记得重启rsyslog服务。 到这里IP问题基本都可以解决了。rsyslog记录模板化IP字段--mysql写入字段--loganalyzer前端显示。
解决中文显示乱码的问题:
1.源码文件编码改为utf8
编辑网站里的include/functions_common.php文件,找到含有return htmlentities字段的行; return htmlentities($myStr, ENT_NOQUOTES, $content['HeaderDefaultEncoding']);// "UTF-8"); 修改为: return htmlentities($myStr, ENT_NOQUOTES, $content['HeaderDefaultEncoding'], "UTF-8"); 注意小挂号有去除,分号改逗号。
2.视图首选项改为utf8
admin center -->preference-->global options only 设置默认字符为utf-8
3.查看mysql是否编码为utf8
mysql> status ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 3771 Current database: syslog -------------- mysql Ver 14.14 Distrib 5.5.21, for Linux (x86_64) using EditLine wrapper Connection id: 3771 Current database: syslog Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.5.21-log Source distribution Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: latin1 Conn. characterset: latin1 UNIX socket: /tmp/mysql.sock Uptime: 1 hour 38 min 30 sec Threads: 29 Questions: 257366 Slow queries: 3726 Opens: 669 Flush tables: 1 Open tables: 179 Queries per second avg: 43.547
但是这里的客户端字符为latin1,改为utf8后,查看systemevents发现插入的数据全是乱码。
Server version: 5.5.21-log Source distribution Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8
由此发现rsyslog插入mysql用的编码是latin1.
5.客户端插入和数据初始化为utf8 编码
于是在my.cnf里设置为
init_connect='SET NAMES utf8'
强制连接用到编码为utf8重启mysql后解决了乱码的问题