首页 > Linux系统管理 > 用socket 方式望syslog-ng上打日志
2015
11-20

用socket 方式望syslog-ng上打日志

1. 安装syslog-ng

yum -y install syslog-ng

2. 配置syslog-ng

cd /etc/syslog-ng/  
cp syslog-ng.conf syslog-ng.conf_ori
vi /etc/syslog-ng/syslog-ng.conf 
@version: 3.2   
 
source test {  
    udp(ip(0.0.0.0) port(514));  
};  
 
destination d_test{   
    file("/data/log/nginx/$YEAR-$MONTH-$DAY.log" owner(gavingeng) group(gavingeng) perm(0600) dir_perm(0700) create_dirs(yes));   
};  
 
#filter f_test {  
#   level(debug);  
#};  
 
log {  
    source(test);  
#   filter(f_test);  
    destination(d_test);  
};

3. 重启syslog-ng服务

/etc/init.d/syslog-ng restart
# netstat -tunlp|grep syslog
udp        0      0 0.0.0.0:514                 0.0.0.0:*                               10084/syslog-ng

这时我们可以做个测试,灌些日志进去测试

#!/usr/bin/env python  
#coding:utf-8  
#filename:socket_log.py  
 
''''' 
author: gavingeng 
date:   2012-01-18 18:15:13  
'''  
import socket  
import sys   
import traceback  
 
def main():  
    pass  
 
def sendMsg(msg):  
    try:  
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  
        sock.connect(('127.0.0.1',514))  
        sock.sendall(msg)  
        sock.close()  
    except Exception,e:  
        print traceback.print_exc(e)  
 
if __name__=='__main__':  
    if len(sys.argv) < 2 :   
        print "please input message"  
    msg=sys.argv[1]  
    sendMsg(str(msg))

然后在shell中执行:

# for((i=0;i<1000;i++));do value=`echo "$i"|md5sum`;val="$i-----$value";python socket_log.py $val;done

会循环望/data/log/nginx/ 下的日志文件里插日志

最后编辑:
作者:saunix
大型互联网公司linux系统运维攻城狮,专门担当消防员

留下一个回复