修改sar(系统活动报告程序)的日志保存天数

1. 需求:

  服务器目前版本的sar 日志保存天数默认是31天,时间长了会占用存储资源,需要调整一下:

  # sar 命令隶属于sysstat工具包,安装过程略

  # Sysstat 是一个软件包,包含监测系统性能及效率的一组工具,这些工具对于我们收集系统性能数据,比如CPU使用率、硬盘和网络吞吐数据,这些数据的收集和分析,有利于我们判断系统是否正常运行,是提高系统运行效率、安全运行服务器的得力助手。

~]# rpm -qcf $(which sar)    # 查看配置文件
/etc/cron.d/sysstat
/etc/sysconfig/sysstat
/etc/sysconfig/sysstat.ioconf

 ~]# sar -V    #查看版本

sysstat version 9.0.4
(C) Sebastien Godard (sysstat <at> orange.fr)

2. Sysstat 软件包集成如下工具:

◆iostat:统计并报告你的设备的CPU状态和I/O状态数据。

◆mpstat:监控和显示关于CPU的细节信息。
◆pidstat:统计正在运行的进程/任务的CPU、内存等信息。
◆sar:保存和报告不同资源(CPU、内存、输入输出、网络、内核等)的详细信息。
◆sadc:系统活动数据收集器,用于为sar收集后台的数据。
◆sa1:读取和存储sadc的数据文件的二进制数据。
◆sa2:和sar协作,用于总结每日报告。
◆Sadf:以不同的格式(CSV或XML)显示sar生成的数据。
◆Sysstat:解释sysstat的各种作用。
◆nfsiostat-sysstat:统计NFS协议的网络文件系统的 I/O状态数据。
◆cifsiostat:统计CIFS协议的网络文件系统的 I/O状态数据。

 

安装完,sysstat默认就是随机启动的。
可以通过以下命令来检查:

  centos6及以下: chkconfig –list|grep sysstat

  centos7.x:systemctl status sysstat

而且会在日志保存目录/var/log/sa/中产生当天的日志文件:这也说明sysstat运行正常

~]# ll /var/log/sa
总用量 21284
-rw-r--r-- 1 root root 412428 4月   8 15:19 sa01
-rw-r--r-- 1 root root 412428 4月   8 15:19 sa02
-rw-r--r-- 1 root root 412428 4月   8 15:19 sa03
-rw-r--r-- 1 root root 412428 4月   8 15:19 sa04
-rw-r--r-- 1 root root 412428 4月   8 15:19 sa05

sa1程序会将每天的数据保存到不同的文件中。
会发现文件都是以sa或sar开头,后跟一个数字。这个数字表示一个月中的第几天,以sa开头的文件是以sadc动态保存着的二进制数据,以sar开头的文件是该日对应的由sa2转储的文本数据。

3、分析sar运行步骤
前面分析sar的轮转并没有在日志轮转那里,但是发现sar日志是每十分钟保存一次数据,这次需要用到cron功能。
进入/etc/cron.d/目录中,查看定时任务文件:

~]# less /etc/cron.d/sysstat 
# Run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib64/sa/sa1 1 1
# 0 * * * * root /usr/lib64/sa/sa1 600 6 &
# Generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib64/sa/sa2 -A

从这里就能看出很多东西。
动态写入数据,产生日志的工具是sa1,每10分钟运行一次(如果想修改运行频率,就在这里修改)
sa2每晚23:53分运行,英文注释是说每天晚上23:53分会产生一个日志汇总。但后面我们发现它还有清理过期日志的作用。

我们可以分别打开这两个脚本看看:

~]# cat  /usr/lib64/sa/sa1
#!/bin/sh
# /usr/lib64/sa/sa1
# (C) 1999-2012 Sebastien Godard (sysstat <at> orange.fr)
#
#@(#) sysstat-10.1.5
#@(#) sa1: Collect and store binary data in system activity data file.
#

# Set default value for some variables.
# Used only if ${SYSCONFIG_DIR}/sysstat doesn't exist!
HISTORY=0
SADC_OPTIONS=""
DDIR=/var/log/sa
DATE=`date +%d`
CURRENTFILE=sa${DATE}
CURRENTDIR=`date +%Y%m`
SYSCONFIG_DIR=/etc/sysconfig
umask 0022
[ -r ${SYSCONFIG_DIR}/sysstat ] && . ${SYSCONFIG_DIR}/sysstat
if [ ${HISTORY} -gt 28 ]
then
        cd ${DDIR} || exit 1
        [ -d ${CURRENTDIR} ] || mkdir -p ${CURRENTDIR}
        # If ${CURRENTFILE} exists and is a regular file, then make sure
        # the file was modified this day (and not e.g. month ago)
        # and move it to ${CURRENTDIR}
        [ ! -L ${CURRENTFILE} ] &&
                [ -f ${CURRENTFILE} ] &&
                [ "`date +%Y%m%d -r ${CURRENTFILE}`" = "${CURRENTDIR}${DATE}" ] &&
                mv -f ${CURRENTFILE} ${CURRENTDIR}/${CURRENTFILE}
        touch ${CURRENTDIR}/${CURRENTFILE}
        # Remove the "compatibility" link and recreate it to point to
        # the (new) current file
        rm -f ${CURRENTFILE}
        ln -s ${CURRENTDIR}/${CURRENTFILE} ${CURRENTFILE}
else
        # If ${CURRENTFILE} exists, is a regular file and is from a previous
        # month then delete it so that it is recreated by sadc afresh
        [ -f ${CURRENTFILE} ] && [ "`date +%Y%m -r ${CURRENTFILE}`" -lt "${CURRENTDIR}" ] && rm -f ${CURRENTFILE}
fi
ENDIR=/usr/lib64/sa
cd ${ENDIR}
[ "$1" = "--boot" ] && shift && BOOT=y || BOOT=n
if [ $# = 0 ] && [ "${BOOT}" = "n" ]
then
# Note: Stats are written at the end of previous file *and* at the
# beginning of the new one (when there is a file rotation) only if
# outfile has been specified as '-' on the command line...
        exec ${ENDIR}/sadc -F -L ${SADC_OPTIONS} 1 1 -
else
        exec ${ENDIR}/sadc -F -L ${SADC_OPTIONS} $* -
fi

发现里面程序是个脚本,就是调用sadc命令进行动态写入。所以真正发挥作用的是sadc工具。

 

 ~]# cat  /usr/lib64/sa/sa2                        
#!/bin/sh
# /usr/lib64/sa/sa2
# (C) 1999-2012 Sebastien Godard (sysstat <at> orange.fr)
#
#@(#) sysstat-10.1.5
#@(#) sa2: Write a daily report
#
S_TIME_FORMAT=ISO ; export S_TIME_FORMAT
umask 0022
prefix=/usr
exec_prefix=/usr
# Add a trailing slash so that 'find' can go through this directory if it's a symlink
DDIR=/var/log/sa/
SYSCONFIG_DIR=/etc/sysconfig
YESTERDAY=
DATE=`date ${YESTERDAY} +%d`
CURRENTFILE=sa${DATE}
CURRENTRPT=sar${DATE}
HISTORY=28          #修改此处
COMPRESSAFTER=31
ZIP="bzip2"
[ -r ${SYSCONFIG_DIR}/sysstat ] && . ${SYSCONFIG_DIR}/sysstat
if [ ${HISTORY} -gt 28 ]
then
        CURRENTDIR=`date ${YESTERDAY} +%Y%m`
        cd ${DDIR} || exit 1
        [ -d ${CURRENTDIR} ] || mkdir -p ${CURRENTDIR}
        # Check if ${CURRENTFILE} is the correct file created at ${DATE}
        # Note: using `-ge' instead of `=' since the file could have
        # the next day time stamp because of the file rotating feature of sadc
        [ -f ${CURRENTFILE} ] &&
                [ "`date +%Y%m%d -r ${CURRENTFILE}`" -ge "${CURRENTDIR}${DATE}" ] || exit 0
        # If the file is a regular file, then move it to ${CURRENTDIR}
        [ ! -L ${CURRENTFILE} ] &&
                mv -f ${CURRENTFILE} ${CURRENTDIR}/${CURRENTFILE} &&
                        ln -s ${CURRENTDIR}/${CURRENTFILE} ${CURRENTFILE}
        touch ${CURRENTDIR}/${CURRENTRPT}
        # Remove the "compatibility" link and recreate it to point to
        # the (new) current file
        rm -f ${CURRENTRPT}
        ln -s ${CURRENTDIR}/${CURRENTRPT} ${CURRENTRPT}
        CURRENTDIR=${DDIR}/${CURRENTDIR}
else
        CURRENTDIR=${DDIR}
fi
RPT=${CURRENTDIR}/${CURRENTRPT}
ENDIR=/usr/bin
DFILE=${CURRENTDIR}/${CURRENTFILE}
[ -f "$DFILE" ] || exit 0
cd ${ENDIR}
[ -L ${RPT} ] && rm -f ${RPT}
${ENDIR}/sar $* -f ${DFILE} > ${RPT}
find ${DDIR} \( -name 'sar??' -o -name 'sa??' -o -name 'sar??.xz' -o -name 'sa??.xz' -o -name 'sar??.gz' -o -name 'sa??.gz' -o -name 'sar??.bz2' -o -name 'sa??.bz2' \) \
        -mtime +"${HISTORY}" -exec rm -f {} \;
find ${DDIR} \( -name 'sar??' -o -name 'sa??' \) -type f -mtime +"${COMPRESSAFTER}" \
        -exec ${ZIP} {} \; > /dev/null 2>&1
# Remove broken links
for f in `find ${DDIR} \( -name 'sar??' -o -name 'sa??' \) -type l`; do
        [ -e $f ] || rm -f $f
done
cd ${DDIR}
rmdir [0-9]????? > /dev/null 2>&1
exit 0

可以看到:

 sa2里面的内容正是我们所寻找的。
HISTORY=28 #历史28天,这正是我们所要修改的,但是先别急,再看下面一句
[ -r /etc/sysconfig/sysstat ] && . /etc/sysconfig/sysstat
#判断/etc/sysconfig/sysstat是否存在,如果存在就使里面的环境生效。
我们打开这个文件看看
~]# cat /etc/sysconfig/sysstat                                    
# sysstat-10.1.5 configuration file.

# How long to keep log files (in days).
# If value is greater than 28, then log files are kept in
# multiple directories, one for each month.   #日志文件保存时间(以天为单位),如果值大于28,则日志文件将保存在多个目录,每个月一个。

HISTORY=28 # 修改此处
# Compress (using gzip or bzip2) sa and sar files older than (in days):
COMPRESSAFTER=31

# Parameters for the system activity data collector (see sadc manual page)
# which are used for the generation of log files.
SADC_OPTIONS="-S DISK"

# Compression program to use.
ZIP="bzip2"

由此看来修改sar 的日志保存时间,修改 /etc/sysconfig/sysstat 和cat /usr/lib64/sa/sa2 文件中的 HISTORY 参数即可,需要保存多长时间改为多长时间即可 (单位:天)

纵观整个过程,其实最主要的工具就是sadc,其它的都是调用它来实现,或者就是一些脚本写成的工具。

4、 查看日志文件

日志文件是二进制的,直接打开没办法看。
为sar命令指定-f以选择要读取的文件。如果想读1日的数据,可以使用命令sar -f /var/log/sa/sa01来读取sa01以查看当天的数据,这里会打开当天全部的数据,基本上没办法看。
我们通常都是想看某一时段的数据,这时就需要添加开始时间和结束时间:
使用-s和-e参数(即开始和结束)来指定具体时间以缩小查询的范围。
请注意,-s并不是包含性的,所以必须从所选择的开始时间中多减去十分钟。

~]# sar -f /var/log/sa/sa01
Linux 3.10.0-514.21.1.el7.x86_64 (python-test)  2021年04月01日  _x86_64_        (4 CPU)

00时00分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
00时10分01秒     all      0.09      0.00      0.12      0.00      0.00     99.80
00时20分01秒     all      0.09      0.00      0.11      0.00      0.00     99.80
00时30分01秒     all      0.09      0.00      0.11      0.00      0.00     99.80
00时40分02秒     all      0.09      0.00      0.11      0.00      0.00     99.80
00时50分01秒     all      0.09      0.00      0.11      0.00      0.00     99.79
01时00分01秒     all      0.08      0.00      0.11      0.01      0.00     99.80
01时10分01秒     all      0.09      0.00      0.11      0.00      0.00     99.79
01时20分01秒     all      0.09      0.00      0.11      0.00      0.00     99.80
01时30分01秒     all      0.08      0.00      0.12      0.00      0.00     99.79
01时40分01秒     all      0.09      0.00      0.12      0.00      0.00     99.79
01时50分01秒     all      0.09      0.00      0.12      0.00      0.00     99.79
02时00分01秒     all      0.09      0.00      0.12      0.00      0.00     99.79

 

5、总结:

sa1 是调用sadc将性能数据收集到二进制日志文件中的一个Shell 脚本,而且在cron中被计划执行,每隔十分钟运行一次;
sa2 是将当日二进制日志文件中所有的数据转储到文本文件,并根据保存天数清除过期日志文件的一个shell脚本,在cron中每晚23:53分定期执行;
sadc 是系统动态数据收集工具,收集的数据被写一个二进制的文件中,也是其中最重要的工具,其它工具基本上都是调用它来实现功能;
sadf 显示被sar通过多种格式收集的数据;

参考:http://haibing.org/90

 

posted @ 2021-04-08 16:36  听&夏  阅读(1064)  评论(0编辑  收藏  举报