Linux日志分析

内核及系统日志

这种日志数据由系统服务rsyslog统一管理,根据其主配置文件/etc/rsyslog.conf中的设置决定将内核消息及各种系统程序消息记录到什么位置。系统中有相当一部分程序会把自己的日志文件交由rsyslog管理,因而这些程序使用的日志记录也具有相似的格式。

# 查看/etc/rsyslog.conf文件内容

root@ecs:~# cat /etc/rsyslog.conf |grep -v "^#\|^$"
module(load="imuxsock") # provides support for local system logging
module(load="imklog" permitnonkernelfacility="on")
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$RepeatedMsgReduction on
$FileOwner syslog
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022
$PrivDropToUser syslog
$PrivDropToGroup syslog
$WorkDirectory /var/spool/rsyslog
$IncludeConfig /etc/rsyslog.d/*.conf


# 查看/etc/rsyslog.d/下的文件

root@ecs:~# ls /etc/rsyslog.d/
20-ufw.conf  50-default.conf

root@ecs:~# cat /etc/rsyslog.d/50-default.conf  |grep -v "^#\|^$"
auth,authpriv.*			/var/log/auth.log
*.*;auth,authpriv.none		-/var/log/syslog
kern.*				-/var/log/kern.log
mail.*				-/var/log/mail.log
mail.err			/var/log/mail.err
*.emerg				:omusrmsg:*
root@ecs:~# cat /etc/rsyslog.d/20-ufw.conf  |grep -v "^#\|^$"
:msg,contains,"[UFW " /var/log/ufw.log

常用系统日志文件:

路径说明
/var/log/messages记录 Linux 内核消息及各种应用程序的公共日志信息
/var/log/cron记录 crond 计划任务产生的事件信息
/var/log/dmesg记录 Linux 操作系统在引导过程中的各种事件信息
/var/log/maillog记录进入或发出系统的电子邮件活动
/var/log/boot.log系统启动时的日志,包括自启动的服务
/var/log/yum.log包含使用yum安装软件包的信息
/var/log/audit目录包含audit daemon的审计日志。例如:selinux开启的时候,这里就会有关于selinux审计的日志。
/var/log/sa/ 目录包含每日由sysstat软件包收集的sar文件

用户日志

这种日志数据用于记录Linux操作系统用户登录及退出系统的相关信息,包括用户名、登录的终端、登录时间、来源主机、正在使用的进程操作等。
常用用户日志文件:

路径说明
/var/log/lastlog记录每个用户最近的登录事件
/var/log/secure记录用户认证相关的安全事件信息
/var/log/wtmp记录每个用户登录、注销及系统启动和停机事件,加密文件
/var/log/btmp记录失败的、错误的登录尝试及验证事件,加密文件

wtmp和btmp需要使用last命令解密查看:

root@ecs:/var/log# last -10 -f wtmp
root     pts/0        114.245.34.212   Sun Apr 12 21:13   still logged in
root     pts/1        114.245.34.212   Sat Apr 11 11:37 - 17:20  (05:42)
root     pts/0        114.245.34.212   Sat Apr 11 10:08 - 17:20  (07:11)
root     pts/2        111.193.20.113   Fri Apr 10 22:47 - 23:08  (00:20)
root     pts/1        111.193.20.113   Fri Apr 10 22:28 - 23:08  (00:39)
root     pts/0        124.205.245.114  Fri Apr 10 22:27 - 00:39  (02:11)
reboot   system boot  4.15.0-96-generi Fri Apr 10 09:58   still running
root     pts/1        111.193.20.113   Wed Apr  8 16:49 - 20:46  (03:56)
root     pts/0        106.38.39.210    Wed Apr  8 10:40 - 17:09  (06:29)
reboot   system boot  4.15.0-65-generi Wed Apr  8 10:39   still running

root@ecs:/var/log# last -10 -f btmp
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50    gone - no logout
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)
root     ssh:notty    114.116.252.237  Sat Apr 11 05:50 - 05:50  (00:00)

程序日志

有些应用程序会选择由自己独立管理一份日志文件(而不是交给rsyslog服务管理),用于记录本程序运行过程中的各种事件信息。由于这些程序只负责管理自己的日志文件,因此不同程序所使用的日志记录格式可能会存在较大的差异。

服务日志

systemd拥有强大的处理与系统日志记录功能,提供一套集中化管理方案,从而统一打理全部内核及用户级进程的日志信息。这套系统能够收集并管理日志内容,而这也就是我们所用的journal。
Journal的实现归功于journald守护进程,其负责处理由内核、initrd以及服务等产生的信息。通过journalctl工具,访问并操作journal内部的数据。

# 默认输出所有日志,通过空格翻页,j、k键向下、向上滚动行,操作与vim大致相同
root@ecs:/var/log# journalctl 
-- Logs begin at Thu 2019-10-10 10:30:59 CST, end at Sun 2020-04-12 22:03:52 CST. --
Oct 10 10:30:59 host-192-168-1-220 systemd-logind[675]: Power key pressed.
Oct 10 10:30:59 host-192-168-1-220 systemd-logind[675]: Powering Off...
Oct 10 10:30:59 host-192-168-1-220 systemd-logind[675]: System is powering down.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopped target Host and Network Name Lookups.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopping Session 1 of user root.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Closed Load/Save RF Kill Switch Status /dev/rfkill Watch.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopping Authorization Manager...
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopped target Timers.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopped Daily apt upgrade and clean activities.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopped target Graphical Interface.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopping Accounts Service...
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopped Message of the Day.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopped Discard unused blocks once a week.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopping Availability of block devices...
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopped Daily Cleanup of Temporary Directories.
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopping User Manager for UID 0...
Oct 10 10:30:59 host-192-168-1-220 systemd[1]: Stopped Daily apt download activities.
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Stopped target Default.
Oct 10 10:30:59 host-192-168-1-220 blkdeactivate[2062]: Deactivating block devices:
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Stopped target Basic System.
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Stopped target Timers.
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Stopped target Sockets.
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Closed GnuPG cryptographic agent and passphrase cache (restricted).
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Closed GnuPG cryptographic agent and passphrase cache.
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Closed GnuPG cryptographic agent (ssh-agent emulation).
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Closed GnuPG cryptographic agent and passphrase cache (access for web browsers).
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Stopped target Paths.
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Closed GnuPG network certificate management daemon.
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Reached target Shutdown.
Oct 10 10:30:59 host-192-168-1-220 systemd[1530]: Starting Exit the Session...
lines 1-31

# 常用参数介绍
-f, --follow
		只显示最新的日志项,并且不断显示新生成的日志项。 此选项隐含了 -n 选项。
-n, --lines=
		限制显示最新的日志行数。 --pager-end 与 --follow 隐含了此选项。 
    此选项的参数:若为正整数则表示最大行数; 若为 "all" 则表示不限制行数; 
    若不设参数则表示默认值10行。
-r, --reverse
		反转日志行的输出顺序, 也就是最先显示最新的日志。
-x, --catalog
		在日志的输出中增加一些解释性的短文本, 以帮助进一步说明日志的含义、 
    问题的解决方案、支持论坛、 开发文档、以及其他任何内容
-k, --dmesg
		仅显示内核日志。隐含了 -b 选项以及 "_TRANSPORT=kernel" 匹配项。
-u, --unit=UNIT|PATTERN
		仅显示属于特定单元的日志。 也就是单元名称正好等于 UNIT 或者符合 PATTERN 模式的单元。 
    这相当于添加了一个 "_SYSTEMD_UNIT=UNIT" 匹配项(对于 UNIT 来说), 
    或一组匹配项(对于 PATTERN 来说)。
-e, --pager-end
		在分页工具内立即跳转到日志的尾部。 此选项隐含了 -n1000 以确保分页工具不必缓存太多的日志行。 
    不过这个隐含的行数可以被明确设置的 -n 选项覆盖。 注意,此选项仅可用于 less(1) 分页器。

# 常用组合:查看服务最新日志
journalctl  -xe -u "servcie"

命令使用介绍:http://linux.51yip.com/search/journalctl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小森饭

你的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值