rsyslogd服务器及负载均衡

来源:互联网 发布:复杂网络同步 编辑:程序博客网 时间:2024/05/16 05:53

rsyslogd服务器及负载均衡

rsyslog和logrotate服务

  • 参考:
    • http://www.lampbo.org/others/opensource/rsyslog-config-file-detail.html
    • http://bangerlee.blog.chinaunix.net/uid-10540984-id-2609837.html

rsyslog 是一个 syslogd 的多线程增强版。现在Fedora和Ubuntu, rhel6默认的日志系统都是rsyslog了。

rsyslog负责写入日志, logrotate负责备份和删除旧日志, 以及更新日志文件

rsyslog服务

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装
yuminstallrsyslog rsyslog-mysql logrotate
 
# 查看当前rsyslog服务的状态
/etc/init.d/rsyslogstatus
 
# 在centos6中, rsyslog服务默认是开机启动的,我们先看一下它的进程::
ps-ef | greprsyslogd | grep-vgrep
root      1343    1  0 12:09 ?        00:00:00 /sbin/rsyslogd-c 4
 
#注意,这里的服务名是rsyslog!
chkconfig –list | greprsyslog
rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off

从上面命令的输出结果看到rsyslog执行时使用的参数是-c 4.

它的意思是指定rsyslog运行(兼容)的版本号, 这个参数必须是第一个参数, 当然也可以省略, 默认为-c0, (命令行兼容sysklogd)

这个参数是在文件/etc/sysconfig/rsyslog中指定:

1
2
3
4
# Options to syslogd
# syslogd options are deprecated since rsyslog v3
# if you want to use them, switch to compatibility mode 2 by "-c 2"
SYSLOGD_OPTIONS="-c 4"
  • rsyslog接收日志延迟问题

http://www.sectop.com/?p=105

rsyslog处理大量日志接收时,总是会延迟,说有dns解析的问题。然后man了下rsyslogd,发现有-Q -x参数可以屏蔽dns解析。改下启动脚本:

1
SYSLOGD_OPTIONS="-c 5 -Q -x"

rsyslog配置文件

  • 配置文件的基本信息(/etc/rsyslog.conf)

配置文件中有很多内容, 但最主要的是指定需要记录哪些服务和需要记录什么等级的信息:

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
#rsyslog v3 config file
# if you experience problems, check
#http://www.rsyslog.com/troubleshoot for assistance
 
#### MODULES ####    加载 模块
$ModLoad imuxsock.so  –> 模块名  # provides support for local system logging (e.g. via logger command) 本地系统日志
$ModLoad imklog.so               # provides kernel logging support (previously done by rklogd)
#$ModLoad immark.so              # provides –MARK– message capability
 
# Provides UDP syslog reception
# 允许514端口接收使用UDP协议转发过来的日志
$ModLoad imudp.so
$UDPServerRun 514
 
# Provides TCP syslog reception
# 允许514端口接收使用TCP协议转发过来的日志
$ModLoad imtcp.so
$InputTCPServerRun 514
 
#### GLOBAL DIRECTIVES ####
# 定义日志格式默认模板
# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on
 
$IncludeConfig/etc/rsyslog.d/*.conf # 加入/etc/rsyslog.d下配置文件,如:/etc/rsyslog.d/50-default.conf
 
#### RULES ####
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console    关于内核的所有日志都放到/dev/console(控制台)
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
# 记录所有日志类型的info级别以及大于info级别的信息到/var/log/messages,但是mail邮件信息,authpriv验证方面的信息和cron时间任务相关的信息除外
*.info;mail.none;authpriv.none;cron.none               /var/log/messages
# The authpriv file has restricted access.
# authpriv验证相关的所有信息存放在/var/log/secure
authpriv.*                                             /var/log/secure
# Log all the mail messages in one place.
# 邮件的所有信息存放在/var/log/maillog; 这里有一个-符号,表示是使用异步的方式记录, 因为日志一般会比较大
mail.*                                                  -/var/log/maillog
# Log cron stuff
# 计划任务有关的信息存放在/var/log/cron
cron.*                                                 /var/log/cron
# Everybody gets emergency messages
# 记录所有的大于等于emerg级别信息, 以wall方式发送给每个登录到系统的人
*.emerg                          *          #  *代表所有在线用户
# Save news errors of level crit and higher in a special file.
# 记录uucp,news.crit等存放在/var/log/spooler
uucp,news.crit                                         /var/log/spooler
# Save boot messages also to boot.log     启动的相关信息
local7.*                                               /var/log/boot.log
#:rawmsg, contains, "sdns_log" @@192.168.56.7:10514
#:rawmsg, contains, "sdns_log" ~
# ### begin forwarding rule ###  转发规则
# The statement between the begin … end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$WorkDirectory /var/spppl/rsyslog # where to place spool files
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514                    # @@表示通过tcp协议发送  @表示通过udp进行转发
#local3.info  @@localhost:514
#local3.info -/var/log/ipwall/messages.log
#local7.*                                 # @@192.168.56.7:514
# ### end of the forwarding rule ###
  • 配置格式
1
2
# 日志设备(类型).(连接符号)日志级别            日志处理方式(action)
*.info;mail.none;authpriv.none;cron.none  /var/log/messages
  • 日志设备(可以理解为日志类型)
日志设备/类型说明auth–pam产生的日志authpriv–ssh,ftp等登录信息的验证信息cron–时间任务相关kern–内核lpr–打印mail–邮件mark(syslog)–rsyslog服务内部的信息,时间标识news–新闻组user–用户程序产生的相关信息uucp–unix to unix copy, unix主机之间相关的通讯local 1~7–自定义的日志设备
  • 日志级别

从上到下,级别从低到高,记录的信息越来越少 详细的可以查看手册: man 3 syslog

级别说明级别值debug–有调式信息的,日志信息最多7info–一般信息的日志,最常用 notice–最具有重要性的普通条件的信息 warning–警告级别4err–错误级别,阻止某个功能或者模块不能正常工作的信息 crit–严重级别,阻止整个系统或者整个软件不能正常工作的信息 alert–需要立刻修改的信息 emerg–内核崩溃等严重信息 none–什么都不记录 
  • 连接符号
连接符号说明.xxx表示大于等于xxx级别的信息.=xxx表示等于xxx级别的信息.!xxx表示在xxx之外的等级的信息
  • 日志处理方式Actions
    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
    # 记录到普通文件或设备文件
    *.*    /var/log/file.log  # 绝对路径
    *.*    /dev/pts/0
     
    # 测试:
    # logger 命令用于产生日志
    logger -p local3.info 'KadeFor is testing the rsyslog and logger'
     
     
    # 转发到远程
    *.* @192.168.0.1            # 使用UDP协议转发到192.168.0.1的514(默认)端口
    *.* @@192.168.0.1:10514     # 使用TCP协议转发到192.168.0.1的10514(默认)端口
     
    # 发送给用户(需要在线才能收到)
    *.*   root
    *.*   root,kadefor,up01     # 使用,号分隔多个用户
    *.*   *                     # *号表示所有在线用户
     
    # 忽略,丢弃
    local3.*   ~                # 忽略所有local3类型的所有级别的日志
     
    # 执行脚本
    local3.*    ^/tmp/a.sh     # ^号后跟可执行脚本或程序的绝对路径
    # 日志内容可以作为脚本的第一个参数.
    # 可用来触发报警
  • 注意

日志记录的顺序有先后关系!

配置范例

  • 一个标准简单的配置文件
    1
    2
    3
    4
    5
    6
    7
    *.info;mail.none;authpriv.none;cron.none     /var/log/messages
    authpriv.*                                   /var/log/secure
    mail.*                                       /var/log/maillog
    cron.*                                       /var/log/cron
    *.emerg                                       *
    uucp,news.crit                               /var/log/spooler
    local7.*                                     /var/log/boot.log
  • 实例: 指定日志文件, 或者终端
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    greplocal3/etc/rsyslog.conf
    #local3.*                                      /var/log/local3.log
     
    /etc/init.d/rsyslogreload
    #Reloading system logger...                       [  OK  ]
     
    logger -t 'LogTest'-p local3.info 'KadeFor is testing the rsyslog and logger'
     
    cat/var/log/local3.log
    # Jun 10 04:55:52 kadefor LogTest: KadeFor is testing the rsyslog and logger
    # 自己实验日志发送给某个终端
  • 实例: 过滤特定的日志到文件, 忽略(丢弃)包含某个字符串的日志
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 过滤日志, 由:号开头
    :msg, contains, "error"/var/log/error.log
    :msg, contains, "error"~        # 忽略包含error的日志
    :msg, contains, "user nagios"   ~
    :msg, contains, "user kadefor"   ~
    :msg, contains, "module-alsa-sink.c: ALSA woke us up to write new data to the device, but there was actually nothing to write"~
    local3.*    ~
    # PS.
    &   ~       # 忽略所有的日志

把包含'oracle'的日志保存在/var/log/oracle.log

  • 实例: 使用模板来定义日志格式
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    $template myFormat,"%rawmsg%n"
    $ActionFileDefaultTemplate myFormat
     
    #如果不要$ActionFileDefaultTemplate myFormat这一行, 就需要像这样来使用模板:
    #在日志文件后添加模板名, 并用;号分隔
    $template myFormat,"%rawmsg%n"
     
    # The authpriv file has restricted access.
    authpriv.*     /var/log/secure;myFormat
    # Log all the mail messages in one place.
    mail.*         /var/log/maillog;myFormat
    # Log cron stuff
    cron.*         /var/log/cron;myFormat
    # Everybody gets emergency messages
    *.emerg         *
    # Save news errors of level crit and higher in a special file.
    uucp,news.crit /var/log/spooler;myFormat
    # Save boot messages also to boot.log
    local7.*       /var/log/boot.log;myFormat
  • 实例: remote log 远程发送与接收
    1
    2
    3
    4
    # 只要在rsyslog.conf中加入
    *.* @192.168.0.10
    *.* @192.168.0.10:10514     # 带端口号
    *.* @@192.168.0.10          # TCP

但是没有定义保存在远程的哪一个文件啊?

其实保存在什么文件, 那是远程日志服务器接收到日志之后它自己的事情了.

注意:如果要修改为非514的端口, 需要设置SElinux。

远程发送接收配置

配置例1

  • Client(send)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    local3.*   @@192.0.2.1:10514
    # if you need to forward to other systems as well, just add additional config lines:
    # *.*      @@other-server.example.net:10514
     
    # Log anything (except mail) of level info or higher.
    # Don't log private authentication messages!
    *.info;mail.none;authpriv.none;cron.none     /var/log/messages
    # The authpriv file has restricted access.
    authpriv.*                                   /var/log/secure
    # Log all the mail messages in one place.
    mail.*                                       /var/log/maillog
    # Log cron stuff
    cron.*                                       /var/log/cron
    # Everybody gets emergency messages
    *.emerg                                       *
    # Save news errors of level crit and higher in a special file.
    uucp,news.crit                               /var/log/spooler
    # Save boot messages also to boot.log
    local7.*                                     /var/log/boot.log
  • Server(receive)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # for TCP use:
    $modload imtcp
    $InputTCPServerRun 10514
    # for UDP use:
    $modload imudp
    $UDPServerRun 514
     
    # Log anything (except mail) of level info or higher.
    # Don't log private authentication messages!
    *.info;mail.none;authpriv.none;cron.none     /var/log/messages
    # The authpriv file has restricted access.
    authpriv.*                                   /var/log/secure
    # Log all the mail messages in one place.
    mail.*                                       /var/log/maillog
    # Log cron stuff
    cron.*                                       /var/log/cron
    # Everybody gets emergency messages
    *.emerg                                       *
    # Save news errors of level crit and higher in a special file.
    uucp,news.crit                               /var/log/spooler
    # Save boot messages also to boot.log
    local7.*                                     /var/log/boot.log
    local3.*   /var/log/local3.log    # 测试用

配置例2

  • 配置服务端(接收)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # /etc/rsyslog.conf
     
    #在文件开始加上,同时确保514端口能够被客户端用tcp访问
    $ModLoad imtcp.so           # needs to be done just once #使用tcp方式
    $InputTCPMaxSessions 500    # tcp接收连接数为500个
    $InputTCPServerRun 514      # tcp接收信息的端口
    $template logformat,"%TIMESTAMP:::date-mysql% %FROMHOST-IP%%msg%n"   # 定义一个名为logformat模板, 为信息加上日志时间
    $template DynFile,"/var/log/tlog%$year%%$month%%$day%.log"          # 定义日志文件的名称,按照年月日
    :rawmsg, contains, "sdns_log"?DynFile;logformat    # 把rawmsg(也可以使用msg)日志中包含sdns_log标志的信息写到DynFile定义的日志文件里
    :rawmsg, contains, "sdns_log" ~                    # 这个表示丢弃包含sdns_log标志的信息, 一般都加上它, 以免多个日志文件记录重复的日志
  • 配置客户端(发送)
    1
    2
    3
    4
    5
    6
    7
    8
    # /etc/rsyslog.conf
     
    #在文件开始加上
    #把包含sdns_log的信息通过tcp发到192.168.1.2 @@表示tcp @表示udp
    :rawmsg, contains, "sdns_log"      @@192.168.1.2      # 默认514端口
     
    #这个表示丢弃包含sdns_log标志的信息,防止这个信息写到本机的/var/log/message
    :rawmsg, contains, "sdns_log"      ~
  • 测试

在客户端上执行,观察服务端/var/log/目录里是否有tlog*日志产生。

1
logger -p user.info "sdns_log 34334"
  • 补充

如果要把不同服务器发送过来的日志保存到不同的文件, 可以这样操作:

1
2
3
4
:fromhost-ip, isequal, "192.168.0.160"/var/log/host160.log
:FROMHOST-IP, isequal, "192.168.0.161"/var/log/host161.log
:FROMHOST-IP, startswith, "192.168.1."/var/log/network1.log
:FROMHOST-IP, startswith, "192.168.2."/var/log/network2.log
  • 练习
    1. 实现把ssh服务的日志自定义保存到/var/log/newsshd.log (先不做)
    2. mail日志保存在远程日志服务器/var/log/newmail.log
    3. 过滤日志, 如果日志包含有"daydayup", 则执行脚本/tmp/a.sh,脚本内容:
      1
      2
      #!/bin/bash
      echo "KO::**1"> /dev/tty2

logrotate服务

rotate 轮换,日志切换

logrotate是一个日志管理程序,用来把旧的日志文件删除(备份),并创建新的日志文件,这个过程称为"转储"。我们可以根据日志的大小,或者根据其使用的天数来转储。

logrotate启动方式

logrotate 的执行由crond服务实现。在/etc/cron.daily目录中,有个文件logrotate,它实际上是个shell script,用来启动logrotate。logrotate程序每天由cron在指定的时间(/etc/crontab)启动。

因此,使用ps是无法查看到logrotate的。如果它没有起来,就要查看一下crond服务有没有在运行。

在执行logrotate时,需要指定其配置文件/etc/logrotate.conf

这个配置文件的注释写得很清楚,没有必要再罗嗦了。只想强调下面这行,它的作用包含存放在/etc/logrotate.d目录下面的配置文件,不可或缺。如果你安装了一个新的服务,它的日志转储的规则可以建立一个专门的配置文件,放在/etc/logrotate.d下面。它其实也因为下面的这句话,在 logrotate服务启动时被读取。

每个存放在/etc/logrotate.d目录里的文件,都有上面格式的配置信息。在{}中定义的规则,如果与logrotate.conf中的冲突,以/etc/logrotatate.d/中的文件定义的为准。

logrotate启动脚本放在 /etc/cron.daily/logrotate 中,可人工执行命令进行测试:

1
/usr/sbin/logrotate-f/etc/logrotate.conf

dateext表示转储文件会以日期来结束*

配置logrotate

  • logrotate选项说明

日志文件匹配规则中,不要使用/var/log/news/*,这会匹配已经切换过的日志,应使用/var/log/news/*.log

选项用途nocompress,compress不压缩,压缩delaycompress不压缩前一个截断的文件(需要与compress一起用)daily,weekly,monthly每天/周/月轮转一次copytruncate清空原有文件,而不是创建一个新文件create 0644 root utmp新建日志文件,权限、属主、组ifempty即使用空文件也转储notifempty日志文件为空不进行转储olddir <dir>转储后的日志存放目录,必须和当前日志文件在同一个文件系统rotate 5保留最近的5个日志文件minisize 1M必须大于1MB才会转转size 50M超过50MB后轮转日志mail www@my.org轮换后的把日志发给邮箱missingok如果日志文件不存在,不报错prerotate,endscript在logrotate之前执行的命令,如/usr/bin/charrt -a /var/log/logfilepostrotate,endscript在logrotate之后执行的命令,如/usr/bin/charrt +a /var/log/logfile/bin/kill -HUP $(/bin/cat /var/run/rsyslogd.pid 2>/dev/null) &>/dev/null/usr/bin/killall -HUP httpd[ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)sharedscripts共享脚本,表示切换时只执行一次脚本dateext增加日期作为后缀,不然会是一串无意义的数字dateformat .%s切换后文件名,必须配合dateext使用
  • /etc/logrotate.conf
    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
    # see "man logrotate" for details
    # rotate log files weekly
    weekly         #–每周轮转一次
    # keep 4 weeks worth of backlogs
    rotate 4        #–保留四个
    # create new (empty) log files after rotating old ones
    create         #–rotate后,创建一个新的空文件
    # uncomment this if you want your log files compressed
    #compress       #–默认是不压缩的
    # RPM packages drop log rotation information into this directory
    include/etc/logrotate.d   #–这个目录下面配置文件生效
     
    # no packages own wtmp - we'll rotate them here
    /var/log/wtmp{        #–定义/var/log/wtmp这个日志文件
        monthly                #–每月轮转一次,取代了上面的全局设定的每周轮转一次
        minsize 1M              #–定义日志必须要大于1M大小才会去轮转
        create 0664 root utmp   #–新的日志文件的权限,属主,属主
        rotate 1                #–保留一个,取代了上面的全局设定的保留四个
    }
     
    /var/log/btmp{
        missingok      #-如果日志丢失, 不报错
        monthly
        create 0600 root utmp
        rotate 1
    }
  • logrotate.conf范例
    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
    # sample logrotate configuration file
    compress    # 全局设置, 压缩
     
    /var/log/messages{
        rotate 5     # 保留5份日志
        weekly      # 每周轮换一次
        postrotate  # 轮换之后重启syslogd服务
        /usr/bin/killall-HUP syslogd
        # rhel6中为:/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
        # 可查看/etc/logrotate.d/下的配置文件
        endscript
    }
     
    "/var/log/httpd/access.log"/var/log/httpd/error.log {   #  指定多个文件, 如果有特殊字符需要用单引号
        rotate 5
        mail www@my.org
        size 100k        # 超过100k后切换日志, 并把老的日志发送邮件给www@my.org
        sharedscripts   # 共享脚本.下面的postrotate脚本只运行一次.
        postrotate
        /usr/bin/killall-HUP httpd
        endscript
    }
     
    /var/log/news/* {    # 少用通配符, 因会它会包括已经切换过的日志, 要用的话最好在*号后加上扩展名, 如*.log
        monthly
        rotate 2
        olddir/var/log/news/old
        missingok
        postrotate
        kill-HUP'cat /var/run/inn.pid'
        endscript
        nocompress
    }
     
    /var/log/ipwall/messages.log {   #日志路径一定要和rsyslog定义的日志文件路径一致
        rotate 65535             #滚动65535次
        create 0777 syslog adm   #设置权限,方便windows连接samba服务器
        compress                #采用压缩
        size 50M                 #文件大小50M以上的分割日志
        dateext
        dateformat .%s           #定义文件切割后的文件名,必须配合dateext使用
     
        #posrotate/endscript这段脚本使rsyslog程序重新读取配置文件,程序释放对messages.log文件持有的文件描述符
        postrotate
        /bin/kill-HUP $(/bin/cat/var/run/rsyslogd.pid 2>/dev/null) &>/dev/null
        endscript
    }
  • 强制轮转
    1
    2
    3
    4
    5
    6
    logrotate -f /etc/logrotate.conf    # –强制轮转
    logrotate -vf /etc/logrotate.conf   # –再加一个-v参数查看轮转的过程
     
    logger -t 'aaaa''bbbbbb'           #–在日志里加一个内容tag和内容
    tail/var/log/messages
    # Jun 12 20:34:22 kadefor aaaa: bbbbbb
  • /etc/logrotate.d/syslog
    1
    2
    3
    4
    5
    6
    7
    /var/log/messages/var/log/secure/var/log/maillog /var/log/spooler /var/log/boot.log/var/log/cron{
        sharedscripts  #–表示切换时脚本只执行一次
        postrotate     #–表示rotate后执行的脚本
        /bin/kill-HUP `cat/var/run/syslogd.pid 2> /dev/null` 2> /dev/null||true
        /bin/kill-HUP `cat/var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null||true
        endscript      #–表示脚本结束
    }
  • 定制命名

logrotate命名方式确实太单调了,默认通过数字后缀来分割,支持时间格式函数格式,但是只支持 %Y %m %d %s这4个函数,由于logrotate确实不支持“小时:分钟”这样的函数。

无奈只有写了个脚本把%s的秒数换算成日期+时刻这样的惯用格式,并且我们把日志文件按日期分类到各个文件夹里。我们在 /etc/rsyslog.d/rename.sh 创建一个文件,注意更改执行权限 chmod 755 /etc/rsyslog.d/rename.sh。

1
2
3
4
5
#!/bin/bash
/usr/sbin/logrotate/etc/logrotate.conf
cd/var/log/ipwall
/bin/ls|/usr/bin/awk-F'.''/log.*gz/{system("mv "$0" `date -d \"@"$3"\" +%y-%m-%d_%H-%M`.gz")}'
/bin/ls*gz | /usr/bin/awk-F"_"'NR==1{i=$1;system("mkdir -p "i"")}{if($1==i)system("mv "$0" "i"")}'
  • 删除最早的日志文件

防火墙上网日志记录要求最大化保存日志,没有设置rotate回滚次数,上面的脚本按照日期把日志保存到文件夹内,下面的脚本是当磁盘空间接近饱和的时候删除最早那天的日志文件夹,我们在 chmod 755 /etc/rsyslog.d/delbak.sh 创建更更改权限。

1
2
3
4
5
#!/bin/bash
SPACE=$(df/var/log/ipwall| awk'NR==2{print $4}')
SP=20000000
cd/var/log/ipwall
[ $SPACE -le$SP ] && ls-d */ | awk'NR==1{system("rm -rf "$0"")}'
  • crontab -e
    1
    2
    *  * * * * . /etc/profile;/etc/rsyslog.d/rename.sh
    10 0 * * * /etc/rsyslog.d/delbak.sh

PHP客户端

PHP语言编写的一个简单的syslog客户端,通过UDP协议,直接将日志消息发送给 rsyslogd日志服务。

http://xmgu2008.blog.163.com/blog/static/1391223802010518115219906/

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
functionmgsyslog($level,$msg)
{
    $server="192.168.120.67";
    $port=518;
    $facility=1;
    $pid=posix_getpid();
    $process="PHP[${pid}]";
    $actualtime= time();
    $month     =date("M",$actualtime);
    $day       =substr("  ".date("j",$actualtime), -2);
    $hhmmss    =date("H:i:s",$actualtime);
    $timestamp =$month." ".$day." ".$hhmmss;
    $hostname=gethostname();
    $pri   ="<".($facility*8 + $level).">";
    $header=$timestamp." ".$hostname;
    $message=substr($pri.$header." ".$process.": ".$msg, 0, 1024);
 
    $fp=fsockopen("udp://".$server,$port,$errno,$errstr);
    if($fp)
    {
        fwrite($fp,$message);
        fclose($fp);
        returntrue;
    }
    returnfalse;
}

rsyslogv7

rsyslogd v7: http://www.rsyslog.com/

安装

  • CentOS: http://rpms.adiscon.com/v7-stable/
    http://rpms.adiscon.com/v7-stable/rsyslog.repo[rsyslog_v7]name=Adiscon CentOS-$releasever - local packages for $basearchbaseurl=http://rpms.adiscon.com/v7-stable/epel-$releasever/$basearchenabled=1gpgcheck=0gpgkey=http://rpms.adiscon.com/RPM-GPG-KEY-Adisconprotect=1yum search rsyslog[root@localhost yum.repos.d]# rpm -qs rsyslog-7.2.6-3.el6.i686
  • Ubuntu
    To use our repository, follow these steps:    Install our PGP Key into your apt system    sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com AEF0CF8E    gpg --export --armor AEF0CF8E | sudo apt-key add -    Edit your /etc/apt/sources.list and add these lines to the end    For Ubuntu (12) Precise    # Adiscon repository    deb http://ubuntu.adiscon.com/v7-devel precise/    deb-src http://ubuntu.adiscon.com/v7-devel precise/    For Ubuntu (13) Quantal    # Adiscon repository    deb http://ubuntu.adiscon.com/v7-devel quantal/    deb-src http://ubuntu.adiscon.com/v7-devel quantal/    Once done perform these commands to update your apt cache and install the latest rsyslog version    sudo apt-get update && sudo apt-get upgrade    If you receive a message like this while upgrading follow these steps below:    The following packages have been kept back:    rsyslog    0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.    sudo apt-get install rsyslog
最新最早最热
  • 5条评论 
  • 2条新浪微博
  • wwek
    wwek

    非常感谢`

  • cdmonkey
    cdmonkey

    再没有一篇教程比这个更详细了!

  • baicaidaoshi
    baicaidaoshi

    我的rsyslog配置文件内容怎么跟这有点不一样呢?

  • XStar
    XStar

    回复 baicaidaoshi: rsyslog 7强大了许多,配置文件格式也不同了

  • 天天_66797
    天天_66797

    [赞]

多说

0 0