Linux:日志采集

一·日志采集规则4.14
*.*      文件名称
日志类型.日志级别
auth    用户登陆日志(pam产生日志)
authpriv  服务认证日志(sshd认证)
kern   内核日志
cron  定时任务日志
lpr   打印机日志
mail  邮件日志
news  新闻
user   用户相关程序日志
local 1-7   用户自定义日志

日志级别
debug     系统调试信息
info      常规信息
warning   警告xinxi
err       报错(级别低,阻止某个功能不能正常工作)
crit      报错(级别高,阻止了整个软件或整系统不能正常工作)
alert     需要立即修改的信息
emerg     内核奔溃
none      不采集任何日志xinxi

例子:
vim  /etc/rsyslog.conf
auth.debug     /var/log/westos
将用户登陆的调试信息放在/var/log/westos 里
auth.*         
*.*             /var/log/log.all
 ^

(所有日志的所有级别)



日志在内存里,日志是由服务决定的
resyslog
系统常用日志
/var/log/messages     所有日志级别的常规信息(不包含邮件,服务认证,定时任务)
/var/log/maillog      邮件日志
/var/log/secure       服务认证日志
/var/log/cron         定时任务日志
二·日志远程同步

日志同步:自己生成一份日志后主动传送给别人

实验步骤:

在日志发送方:
vim /etc/rsyslog.conf
*.*             @172.25.254.119   日志接受方地址

systemctl  restart  rsyslog



在日志接受方
vim  /etc/rsyslog.conf
15 $ModLoad imudp

16 $UDPServerRun 514


syetemctl  restart  rsyslog

cat  /var/log/messages



重置:

syetemctl  restart  rsyslog

开启防火墙:

systemctl stop firewalld

关闭防火墙:
systemctl stop firewalld
systemctl disable firewalld


三·定义日志采集格式
vim /etc/rsyslog.conf
$template  格式名称,“日志采集格式”
*.info;mail.none;authpriv.none;cron.none

$template  westos,"%timegenerated% %FROMHOST-IP% %syslogtag% %msg%\n  "

*.info;mail.none;authpriv.none;cron.none    /var/log/massages;westos

%timegenerated%             日志生成时间
%FORMHOST-IP%           日志来源主机ip
%syslogtag%                     日志生成程序
%msg%                              日志内容
\n                                         换行


在发送端进行:




在接收端进行:

在发送端进行:



四·journactl                                                    日志查看工具,直接查看内存中的日志

     journalctl  -n 3                                          查看最近前3条


     journalctl  -p err                                        报错


     journalctl  -f                                              进行监控


     journalctl --since 输入时间  --until 输入时间                   输入时间点


    (journalctl  -o verbose  查看日志详细参数  _PID=651(/关键字)           查看日志详细参数

     journactl  _PID=651)





五·对systemd-journald管理:默认此程序只负责对日志查看而不对日志进行保存和采集,那么关机后开机,对日志进行查看,只能查看到开机后的日志,系统之前的日志因为是保存在内存中的,所以关机后被清空了,那么在开机是用journalctl看不到的

如何让systemd-journald保存日志到硬盘中
实验步骤:
mkdir  /var/log/journal                         建立目录
chmod root.systemd-journal /var/log/journal
ls -ld /var/log/journal
chgrp systemd-journald /var/log/jounal
ls -ld /var/log/jounal
chmod g+s(2755) /var/log/journal
killall -l systemd-journald
ls /var/log/journal
journalctl -n 3
date
reboot                                            重启
journalctl

这个实验说明:重启之后也会有之前登陆时间





六·时间同步
实验步骤:
在服务器端共享时间
vim /etc/chrony.conf    
29 local startum 10        开启是时间共享功能并设定共享级别
                           这个参数开启后本机不去同步别人的时间到本机
22 allow 172.25.254.0/24   允许哪些客户端来访问本机共享时间

systemctl  restart  chronyd   重置



在客户端:
vim /etc/chrony.conf

server  172.25.254.119 iburst    119:服务端ip



systemctl restart chronyd

chronyc sources -v


最后结果:

210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||                                                /   xxxx = adjusted offset,
||         Log2(Polling interval) -.             |    yyyy = measured offset,
||                                  \            |    zzzz = estimated error.
||                                   |           |                         
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 172.25.254.119                0   6     0   10y     +0ns[   +0ns] +/-    0ns


七·更改时间(timedatectl  set-time "更改时间")
实验步骤:

timedatectl                                                            查看时间

timedatectl  list-timezones                                     查看所有时区时间


timedatectl   set-timezone  Asia/Shanghai             更改为上海时间
timedatectl   

date


timedatectl  set-local-rtc  0                                     0指的是UTC时间

 vim /etc/adjtime


timedatectl  set-local-rtc  1                                      1指的是本机时间

vim /etc/adjtime


timedatectl   set-time   "需要更改时间"                    更改时间
date




猜你喜欢

转载自blog.csdn.net/le_anny/article/details/79949968