rsyslog

https://www.cnblogs.com/liuwei-xd/p/11022230.html#%E5%85%AD%E6%97%A5%E5%BF%97%E8%BD%AE%E8%BD%AC

常见系统日志

/var/log/messages 系统主日志文件
/var/log/secure 认证、安全
/var/log/dmesg 和系统启动相关
/var/log/yum.log yum
/var/log/mysqld.log MySQL
/var/log/nginx/  nginx
w 当前登录的用户 /var/log/wtmp

日志级别

日志信息分为以下级别,从上到下级别一次降低
    none    none不是一个等级,它表示不记录服务的所有信息
0     emerg    系统不可用
1    alert    特别留意的报警信息
2    crit    非常严重的状况
3    err        错误信息    
4    warning    警告信息
5    notice    稍微需要注意的信息
6    info    正常信息
7    debug    调试信息,开发人员使用

rsyslog配置

[root@server1 ~]# rpm -qc rsyslog
/etc/logrotate.d/syslog
/etc/rsyslog.conf
/etc/sysconfig/rsyslog


[root@server1 ~]# cat /etc/rsyslog.conf

#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console
#设备名.级别                                            定向到某日志
#.=表示等于某一级别,光.表示大于某个级别,不同设备用;分隔

# 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                                                 :omusrmsg:*

# 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
#local(数字)为自定义设备

自定义日志

logger "xxx"
logger -p 危险等级 "xxx"
logger -p 设备.等级 "xxx"

实验

远程日志管理

让目标机上的日志发送到一台日志服务器统一管理

服务器

关闭防火墙、selinux

#打开模块相应的端口
vim /etc/rsyslog.conf
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514

#重启
service rsyslog restart

#查看监听端口
yum install -y net-tools
netstat -nltup |grep 514

目标机

#修改ssh服务的日志文件的设备载体
vim /etc/ssh/sshd_config 


# Logging
#SyslogFacility AUTH
#SyslogFacility AUTHPRIV
#LogLevel INFO
SyslogFacility LOCAL0
# Authentication:

#重启ssh
service sshd restart

#修改/etc/rsyslog.conf来指定ssh服务的日志发送到远端

# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514

local0.*        @192.168.226.148:514    //一个@表示udp,两个@表示tcp 


#重启rsyslog
service rsyslog restart

轮转切割

日志会随着时间越攒越多,对于硬盘来说是个很大的负担,及时切割丢弃旧的日志文件有利于节省空间

[root@server1 ~]# cat /etc/logrotate.conf
# 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 #切割后时候创建新的日志文件

# use date as a suffix of the rotated file
dateext #使用日期作为文件名的后缀

# 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 and btmp -- we'll rotate them here
/var/log/wtmp {  #具体对应日志的策略,如果有没写的项目则以上面写的为默认配置
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok #丢失不提醒
    monthly
    create 0600 root utmp
    rotate 1
}

标签: none

评论已关闭