在云计算出现以前,有客户端/服务器计算,集中存储,其中全部数据,软件应用程序和控件都驻留在服务器端。若是用户想要运行程序或访问特定数据,那么他将链接到服务器并得到适当的访问权限并能够开展业务。在此以后出现了分布式计算概念,其中全部计算机都联网在一块儿,而且在须要时共享资源。那么新手怎么学习云计算?下面给你们讲解小白也能玩转日志管理:node
1、日志基础shell
日志服务功能是内核提供的
rsyslog是用户管理日志的工具apache
安装软件vim
#sudo yum -y install rsyslog-5.8.10-6.el6.x86_64安全
启动服务服务器
#/etc/init.d/rsyslog start //rhel5/6
#systemctl start rsyslog //rhel7ssh
查看日志:两个位置分布式
1) /var/log
2)软件自己指定的目录工具
经常使用日志post
(如下都位于/var/log)
message //系统日志
maillog //邮件日志
cron //计划任务
xferlog //vsftpd日志,下载日志
httpd apache日志
secure 安全日志 ssh ftp telent pop3 pam等
lastlog //记录每个帐户最后一次登录的时间,使用lastlog 命令查看日志,(安全相关)
wtmp //查看的是一个月全部帐户的登录状况,使用last命令查看日志 由于wtmp的日志轮转是一月一次,且只轮转一次,关于轮转,后文会讲到
utmp //查看当前登录帐户,用w who 命令查看日志,两个命令看到的结果略有不一样
btmp // 查看错误登录尝试,使用lastb命令查看日志
samba //samba 共享日志
yum.log //yum程序相关的日志,记录安装和卸载
dmesg //开机是核心检查过程当中所产生的信息
boot.log //系统启动过程当中日志记录存放
libvirt // kvm虚拟化的日志
sa //(是一个目录,记录一个月的cpu的使用率,cpu负载,磁盘I/O)用sar命令来查看, -f 参数后跟某天的文件名
tail -f 动态查看日志
也能够用cat 、vim 等来查看日志。
更加详细的能够个人另一篇 日志服务
二 、自定义日志
# vim /etc/rsyslog.conf
*.* /var/log/mylog
kern.err /var/log/kernel.log
*.info;mail.none /var/log/big.log
mail.info /var/log/mail.log
cron.info;cron.!err /var/log/newcron
/etc/rsyslog.conf中都是如下的模式来配置的
日志对象.日志级别 日志文件
查看都有哪些日志对象和日志级别
#man 5 rsyslog.conf (本身翻译的,可能有些地方不必定准确)
日志对像(也叫日志设备)
The facility is one of the following keywords: auth(认证messsage), authpriv(privileges)认证权限、安全权限, cron计划任务, daemon后台守护进程, kern内核, lpr打印机, mail, mark, news新闻服务器, security (same as auth)安全, syslog(系统日志), user(用户), uucp(unix to unix cp) and local0 through local7(用户自定义日志用的).
日志级别
The priority(优先级) is one of the following keywords, in ascending order: debug, info, notice, warning, warn (same as warning), err, error (same as err), crit, alert, emerg, panic (same as emerg). The keywords error, warn and panic are deprecated and should not be used anymore. The priority defines the severity of the message.
优先级从低到高,级别低的包含级别高的,级别高的不包含级别低的。
level syslogd 遇到何种状况(正常、错误)才会记录日志
LOG_EMERG 紧急,致命,服务没法继续运行,如配置文件丢失
LOG_ALERT 报警,须要当即处理,如磁盘空使用95%
LOG_CRIT 致命行为
LOG_ERR 错误行为
LOG_WARNING 警告信息
LOG_NOTICE 普通
LOG_INFO 标准信息
LOG_DEBUG 调试信息,排错所需,通常不建议使用
日志文件
日志存放的位置
/var/log/ 通常的日志都存放在这里
还有一些是服务类的,这些都是服务定义的日志位置,好比/etc/ssh/sshd_conf 文件是ssh的配置文件,他就指定了ssh日志的存放位置。
自定义日志
local0-local7的使用
定义ssh日志为例
1.修改ssh的主配置文件/etc/ssh/sshd_config
SyslogFacility local5 //设置ssh的日志定义由local5设备来记录
2.修改rsyslog的主配置文件/etc/rsyrlog.conf
local5.info /var/log/ssh
3.重启日志服务,从新加载服务配置文件。
自定义日志就完成了,新产生的日志就会到你指定的位置
logger小工具:用于shell脚本
使用命令行写日志到指定的设备及级别
# logger "run......."
# logger -p emerg "run......."
# logger -p authpriv.info "run......."
3、日志轮转
日志轮转也叫日志切割,日志管理的重中之重
日志轮转的配置文件是/etc/logrotate.conf
全局配置
# vim /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
}
#system-specific logs may be also be configured here.
服务类日志轮转
服务类日志轮转的配置文件在/etc/logrotate.d/下
以apache日志轮转为例
/etc/logrotate.d/httpd
/var/log/httpd/*log {
missingok
notifempty //空文件不轮转
sharedscripts //指下边的无论有多少的文件被轮转,只执行一次scripts
delaycompress //压缩相关的
postrotate //开始
/bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true //从新加载配置文件,把产生的信息扔了。
endscript // 结束
}
还能够加上
prerotate
endscript
轮转前
轮转验证
logrotate -f /etc/logrotate.conf 强制轮转,全部的都被轮转了
强制轮转:
#logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
-s 指定最后的日志轮转记录文件位/var/lib/logrotate/logrotate.status
日志轮转小提示
日志轮转中重启服务的重要性:
日志轮转配置文件中重启服务的脚本 是为了把新的日志内容写入到新的日志文件里
由于旧的日志文件被轮转只是改了个名字,INODE并无变,可是日志程序是按日志文件的inode号识别文件的,因此须要重启日志以改变日志文件为新的文件
四 、日志使用案例
1: 统计登陆失败top 5
# grep 'Fail' /var/log/secure |awk '{print $11}' |sort |uniq -c|sort -k1 -n -r |head -5
7 172.16.130.14
6 172.16.130.70
5 172.16.130.56
3 172.16.130.80
2 172.16.130.76
2: 统计登陆成功
# grep 'Accepted' /var/log/secure |awk '{print $(NF-3)}' |sort |uniq -c
4 111.201.131.215
1 116.243.0.213
1 123.120.14.32
3 123.120.38.233
2 221.222.199.175
1 221.222.202.102
3: 查看网卡是否已被驱动
# grep -i eth /var/log/dmesg
[ 0.809104] r8169 Gigabit Ethernet driver 2.3LK-NAPI loaded
[ 0.814193] r8169 0000:02:00.0 eth0: RTL8168g/8111g at 0xffffc9000183e000, 40:8d:5c:9b:3c:17, XID 0c000800 IRQ 25
[ 0.814195] r8169 0000:02:00.0 eth0: jumbo features [frames: 9200 bytes, tx checksumming: ko]
[ 1.724991] bnx2 0000:01:00.0 eth0: Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express found at mem d6000000, IRQ 32,1.725693] bnx2 0000:01:00.1 eth1: Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express found at mem d8000000, IRQ 33,1.726387] bnx2 0000:02:00.0 eth2: Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express found at mem da000000, IRQ 35, 1.727432] bnx2 0000:02:00.1 eth3: Broadcom NetXtreme II BCM5709 10