Sysstat官方介绍:
sysstat包包含各种实用程序,用于监视系统性能和使用活动,这些工具在许多商业unix中都很常见:
iostat报告块设备和分区的CPU统计信息和输入/输出统计信息。
Mpstat报告单个或组合处理器相关的统计信息。
pidstat报告Linux任务(进程)的统计数据:I/O、CPU、内存等。
Tapestat报告连接到系统的磁带机的统计信息。
cifsiostat报告CIFS统计信息。
Sysstat还包含一些工具,您可以通过cron或systemd调度来收集和记录性能和活动数据:
SAR收集、报告和保存系统活动信息(参见下面SAR收集的指标列表)。
Sadc是系统活动数据收集器,用作sar的后端。
Sa1在系统活动每日数据文件中收集和存储二进制数据。它是sadc的前端,设计用于从cron或systemd运行。
Sa2写一份每日活动总结报告。它是sar的前端,设计用于从cron或systemd运行。
sadf以多种格式(CSV, XML, JSON等)显示sar收集的数据,并可用于与其他程序进行数据交换。该命令还可用于使用SVG(可缩放矢量图形)格式为sar收集的各种活动绘制图形。
一、sysstat下载安装
1、下载systat,官网源码地址: https://gitee.com/mirrors/sysstat(本文以v12.7.5为例)
2、上传编译安装
cd sysstat-12.7.5/
./configure --enable-install-cron
make
make install
sysstat安装信息:
......
mkdir -p /usr/local/lib64/sa
mkdir -p /var/log/sa
mkdir -p /usr/local/bin
mkdir -p /usr/local/share/doc/sysstat-12.7.5
mkdir -p /etc/sysconfig
install -m 755 sa1 /usr/local/lib64/sa
install -m 755 sa2 /usr/local/lib64/sa
install -m 755 sadc /usr/local/lib64/sa
install -m 755 sar /usr/local/bin
install -m 755 sadf /usr/local/bin
install -m 755 iostat /usr/local/bin
install -m 755 tapestat /usr/local/bin
install -m 755 mpstat /usr/local/bin
install -m 755 pidstat /usr/local/bin
install -m 755 cifsiostat /usr/local/bin
install -m 644 sysstat.ioconf /etc/sysconfig
install -m 644 sysstat.sysconfig /etc/sysconfig/sysstat
install -m 644 CHANGES /usr/local/share/doc/sysstat-12.7.5
install -m 644 COPYING /usr/local/share/doc/sysstat-12.7.5
install -m 644 CREDITS /usr/local/share/doc/sysstat-12.7.5
install -m 644 README.md /usr/local/share/doc/sysstat-12.7.5
install -m 644 FAQ.md /usr/local/share/doc/sysstat-12.7.5
chown root /var/log/sa
mkdir -p /usr/lib/systemd/system
mkdir -p /usr/lib/systemd/system-sleep
if [ -z "/usr/lib/systemd/system" -o ! -d "/usr/lib/systemd/system" ]; then \
if [ -d /etc/cron.d ]; then \
install -m 644 cron/sysstat.crond /etc/cron.d/sysstat; \
elif [ -d /etc/cron.hourly -a -d /etc/cron.daily ]; then \
install -m 755 cron/sysstat.cron.hourly /etc/cron.hourly/sysstat; \
install -m 755 cron/sysstat.cron.daily /etc/cron.daily/sysstat; \
fi \
fi
if [ \( -z "/usr/lib/systemd/system" -o ! -d "/usr/lib/systemd/system" \) -a ! -d /etc/cron.d ]; then \
if [ ! -d /etc/cron.hourly -o ! -d /etc/cron.daily ]; then \
su root -c "crontab -l > /tmp/crontab-root.save"; \
/usr/bin/cp -a /tmp/crontab-root.save ./crontab-root.`date '+%Y%m%d.%H%M%S'`.save; \
echo "USER'S PREVIOUS CRONTAB SAVED IN CURRENT DIRECTORY (USING .save SUFFIX)."; \
su root -c "crontab cron/crontab"; \
fi \
fi
if [ -n "/usr/lib/systemd/system" -a -d "/usr/lib/systemd/system" ]; then \
install -m 644 sysstat.service /usr/lib/systemd/system; \
install -m 644 cron/sysstat-collect.service /usr/lib/systemd/system; \
install -m 644 cron/sysstat-collect.timer /usr/lib/systemd/system; \
install -m 644 cron/sysstat-summary.service /usr/lib/systemd/system; \
install -m 644 cron/sysstat-summary.timer /usr/lib/systemd/system; \
install -m 644 cron/sysstat-rotate.service /usr/lib/systemd/system; \
install -m 644 cron/sysstat-rotate.timer /usr/lib/systemd/system; \
elif [ -d /etc/rc.d/init.d ]; then \
install -m 755 sysstat /etc/rc.d/init.d/sysstat; \
if [ "n" = "n" ]; then \
if [ -x "/usr/sbin/chkconfig" ]; then \
cd /etc/rc.d/init.d && /usr/sbin/chkconfig --add sysstat; \
else \
[ -d /etc/rc.d/rc2.d ] || mkdir -p /etc/rc.d/rc2.d; \
[ -d /etc/rc.d/rc3.d ] || mkdir -p /etc/rc.d/rc3.d; \
[ -d /etc/rc.d/rc5.d ] || mkdir -p /etc/rc.d/rc5.d; \
cd /etc/rc.d/rc2.d && ln -s -f ../init.d/sysstat S01sysstat; \
cd /etc/rc.d/rc3.d && ln -s -f ../init.d/sysstat S01sysstat; \
cd /etc/rc.d/rc5.d && ln -s -f ../init.d/sysstat S01sysstat; \
fi \
fi \
elif [ -d /etc/rc.d ]; then \
install -m 755 sysstat /etc/rc.d/rc.sysstat; \
if [ "n" = "n" ]; then \
if [ -x "/usr/sbin/chkconfig" ]; then \
cd /etc/rc.d && /usr/sbin/chkconfig --add rc.sysstat; \
else \
[ -d /etc/rc.d/rc2.d ] || mkdir -p /etc/rc.d/rc2.d; \
[ -d /etc/rc.d/rc3.d ] || mkdir -p /etc/rc.d/rc3.d; \
[ -d /etc/rc.d/rc5.d ] || mkdir -p /etc/rc.d/rc5.d; \
cd /etc/rc.d/rc2.d && ln -s -f ../rc.sysstat S01sysstat; \
cd /etc/rc.d/rc3.d && ln -s -f ../rc.sysstat S01sysstat; \
cd /etc/rc.d/rc5.d && ln -s -f ../rc.sysstat S01sysstat; \
fi \
fi \
fi
if [ -n "/usr/lib/systemd/system" -a -n "/usr/lib/systemd/system-sleep" -a -d "/usr/lib/systemd/system-sleep" ]; then \
install -m 755 cron/sysstat.sleep /usr/lib/systemd/system-sleep; \
fi
if [ -n "/usr/lib/systemd/system" -a -x "/usr/bin/systemctl" ]; then \
/usr/bin/systemctl enable sysstat.service; \
fi
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /usr/lib/systemd/system/sysstat.service.
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-collect.timer → /usr/lib/systemd/system/sysstat-collect.timer.
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-summary.timer → /usr/lib/systemd/system/sysstat-summary.timer.
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-rotate.timer → /usr/lib/systemd/system/sysstat-rotate.timer.
3、修改配置文件vim /etc/sysconfig/sysstat
# How long to keep log files (in days).
# If value is greater than 28, then use sadc's option -D to prevent older
# data files from being overwritten. See sadc(8) and sysstat(5) manual pages.
HISTORY=30 --保存日志文件多长时间(以天为单位)
# Compress (using xz, gzip or bzip2) sa and sar files older than (in days):
COMPRESSAFTER=15 --压缩超过n天后的sa和sar文件
4、启动服务
systemctl status sysstat.service
查看/var/log/sa/目录下有“sa当天日期”的文件代表完成安装配置。
二、Sysstat日志查看:
Sysstat日志默认存储在/var/log/sa/路径下,会每天生成一个sa、sar文件以“sa日期”命名。每十分钟收集一次服务器信息至二进制文件sa*,每天生成一个可读的sar文件。
1、sa文件查看:
Sysstat提供sadf工具将sa二进制文件转换成.svg的可视化文件。
使用命令sadf -s 开始时间 -e 截止时间 -t -g 文件名 – -A > 生成可视化文件名.svg
例 截取sa22文件10点到11点的服务器信息生成svg文件:
sadf -s 10:00:00 -e 11:00:00 -t -g sa22 -- -A > sa22-2.svg
生成的.svg文件下载至本地可直接拖入浏览器查看:
2、sar文件查看:
sar文件可直接使用more、cat等命令查看:
more sar26
Linux 3.10.0-1127.el7.x86_64 (demo.novalocal) 2023-12-26 _x86_64_ (4 CPU)
00:00:01 CPU %usr %nice %sys %iowait %steal %irq %soft %guest %gnice %idle
00:10:01 all 0.03 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.96
00:10:01 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 99.99
00:10:01 1 0.03 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.95
00:10:01 2 0.03 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.96
00:10:01 3 0.04 0.00 0.03 0.00 0.00 0.00 0.00 0.00 0.00 99.92
00:20:01 all 0.02 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.96
00:20:01 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 99.99
00:20:01 1 0.01 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 99.98
00:20:01 2 0.04 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.94
00:20:01 3 0.04 0.00 0.03 0.00 0.00 0.00 0.00 0.00 0.00 99.93
00:30:01 all 0.02 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.96
00:30:01 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
00:30:01 1 0.03 0.00 0.03 0.00 0.00 0.00 0.00 0.00 0.00 99.94
00:30:01 2 0.03 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.96
00:30:01 3 0.02 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.96
00:40:01 all 0.02 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 99.96
00:40:01 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
00:40:01 1 0.04 0.00 0.03 0.00 0.00 0.00 0.00 0.00 0.00 99.93
00:40:01 2 0.03 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.95
00:40:01 3 0.01 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 99.98
00:50:01 all 0.02 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 99.96
00:50:01 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 99.99
00:50:01 1 0.03 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.00 99.95