您的位置:首页 > 大数据 > 云计算

新手怎么学习云计算?小白也能玩转日志管理

2019-04-16 17:58 411 查看

在云计算出现之前,有客户端/服务器计算,集中存储,其中所有数据,软件应用程序和控件都驻留在服务器端。如果用户想要运行程序或访问特定数据,那么他将连接到服务器并获得适当的访问权限并可以开展业务。在此之后出现了分布式计算概念,其中所有计算机都联网在一起,并且在需要时共享资源。那么新手怎么学习云计算?下面给大家讲解小白也能玩转日志管理:

一、日志基础

日志服务功能是内核提供的
rsyslog是用户管理日志的工具

安装软件

#sudo yum -y install rsyslog-5.8.10-6.el6.x86_64

启动服务

#/etc/init.d/rsyslog start //rhel5/6
#systemctl start rsyslog //rhel7

查看日志:两个位置

1) /var/log
2)软件本身指定的目录

常用日志

(以下都位于/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…”

三、日志轮转

日志轮转也叫日志切割,日志管理的重中之重
日志轮转的配置文件是/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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: