본문 바로가기

리눅스

sar 명령어(sysstat 패키지)

728x90

sar 명령어(sysstat 패키지)

sar(System Activity Reporter)은 리눅스 시스템 성능 모니터링 도구 중 하나로, 시스템 활동에 대한 다양한 통계 및 성능 데이터를 수집하고 보고하는 데 사용됩니다. sar은 sysstat 패키지의 일부로 제공되며 시스템 성능 문제를 진단하고 해결하는 데 유용합니다.

sysstat 패키지 설치

sysstat 패키지를 설치하면 sar 명령어를 사용할 수 있습니다.

  • Debian/Ubuntu
sudo apt-get install -y sysstat
  • CentOS/RHEL
sudo yum install -y sysstat

sar 설정 확인

cat /etc/default/sysstat
#
# Default settings for /etc/init.d/sysstat, /etc/cron.d/sysstat
# and /etc/cron.daily/sysstat files
#

# Should sadc collect system activity informations? Valid values
# are "true" and "false". Please do not put other values, they
# will be overwritten by debconf!
ENABLED="false"

sar 서비스 시작 및 활성화

sudo systemctl restart sysstat

sar 서비스 상태 확인

$ sudo systemctl status sysstat
● sysstat.service - Resets System Activity Logs
     Loaded: loaded (/lib/systemd/system/sysstat.service; disabled; vendor preset: enabled)
     Active: active (exited) since Wed 2023-10-18 15:52:49 KST; 5s ago
       Docs: man:sa1(8)
             man:sadc(8)
             man:sar(1)
    Process: 3220 ExecStart=/usr/lib/sysstat/sa1 --boot (code=exited, status=0/SUCCESS)
   Main PID: 3220 (code=exited, status=0/SUCCESS)
        CPU: 8ms

Oct 18 15:52:49 aptmirror systemd[1]: Starting Resets System Activity Logs...
Oct 18 15:52:49 aptmirror systemd[1]: Finished Resets System Activity Logs.

sar 기본 구문

sar [옵션] [시간 간격] [반복 횟수]

sar 사용법

더보기

---

$ sar --help
Usage: sar [ options ] [ <interval> [ <count> ] ]
Main options and reports (report name between square brackets):
        -B      Paging statistics [A_PAGE]
        -b      I/O and transfer rate statistics [A_IO]
        -d      Block devices statistics [A_DISK]
        -F [ MOUNT ]
                Filesystems statistics [A_FS]
        -H      Hugepages utilization statistics [A_HUGE]
        -I { <int_list> | SUM | ALL }
                Interrupts statistics [A_IRQ]
        -m { <keyword> [,...] | ALL }
                Power management statistics [A_PWR_...]
                Keywords are:
                CPU     CPU instantaneous clock frequency
                FAN     Fans speed
                FREQ    CPU average clock frequency
                IN      Voltage inputs
                TEMP    Devices temperature
                USB     USB devices plugged into the system
        -n { <keyword> [,...] | ALL }
                Network statistics [A_NET_...]
                Keywords are:
                DEV     Network interfaces
                EDEV    Network interfaces (errors)
                NFS     NFS client
                NFSD    NFS server
                SOCK    Sockets (v4)
                IP      IP traffic      (v4)
                EIP     IP traffic      (v4) (errors)
                ICMP    ICMP traffic    (v4)
                EICMP   ICMP traffic    (v4) (errors)
                TCP     TCP traffic     (v4)
                ETCP    TCP traffic     (v4) (errors)
                UDP     UDP traffic     (v4)
                SOCK6   Sockets (v6)
                IP6     IP traffic      (v6)
                EIP6    IP traffic      (v6) (errors)
                ICMP6   ICMP traffic    (v6)
                EICMP6  ICMP traffic    (v6) (errors)
                UDP6    UDP traffic     (v6)
                FC      Fibre channel HBAs
                SOFT    Software-based network processing
        -q [ <keyword> [,...] | PSI | ALL ]
                System load and pressure-stall statistics
                Keywords are:
                LOAD    Queue length and load average statistics [A_QUEUE]
                CPU     Pressure-stall CPU statistics [A_PSI_CPU]
                IO      Pressure-stall I/O statistics [A_PSI_IO]
                MEM     Pressure-stall memory statistics [A_PSI_MEM]
        -r [ ALL ]
                Memory utilization statistics [A_MEMORY]
        -S      Swap space utilization statistics [A_MEMORY]
        -u [ ALL ]
                CPU utilization statistics [A_CPU]
        -v      Kernel tables statistics [A_KTABLES]
        -W      Swapping statistics [A_SWAP]
        -w      Task creation and system switching statistics [A_PCSW]
        -y      TTY devices statistics [A_SERIAL]

---

 

  • 주요 옵션
    • -u: CPU 사용률 통계 표시
    • -r: 메모리 사용 통계 표시
    • -q: 로드 평균 및 대기 큐 통계 표시
    • -n: 네트워크 통계 표시
    • -b: 블록 디바이스 통계 표시
    • -d: 디스크 I/O 통계 표시
    • -p: 블록 디바이스와 파티션에 대한 상세 정보 표시
    • -W: 스왑 공간 통계 표시
    • -q: 대기 큐와 로드 평균 통계 표시
    • -P: 프로세스 통계 표시
728x90

사용 예시

1. CPU 사용률 확인

sar -u
$ sar -u 2 3
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

04:02:08 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:02:10 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
04:02:12 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
04:02:14 PM     all      0.00      0.00      0.25      0.00      0.00     99.75
Average:        all      0.00      0.00      0.09      0.00      0.00     99.91

2. 메모리 사용 확인

sar -r
$ sar -r 2 3
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

04:02:34 PM kbmemfree   kbavail kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
04:02:36 PM   3014444   3506248    217804      5.44      5576    673924    464128      5.78    613420    130592         0
04:02:38 PM   3014444   3506248    217804      5.44      5576    673924    464128      5.78    613420    130592         0
04:02:40 PM   3014444   3506248    217804      5.44      5576    673924    464128      5.78    613420    130592         0
Average:      3014444   3506248    217804      5.44      5576    673924    464128      5.78    613420    130592         0

3. 로드 평균 및 대기 큐 확인

sar -q
$ sar -q 2 3
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

04:02:54 PM   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
04:02:56 PM         0       166      0.00      0.38      2.35         0
04:02:58 PM         0       166      0.00      0.37      2.34         0
04:03:00 PM         0       166      0.00      0.37      2.34         0
Average:            0       166      0.00      0.37      2.34         0

4. 네트워크 통계 확인

sar -n DEV
$ sar -n DEV 2 3
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

04:03:48 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
04:03:50 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:03:50 PM    enp0s3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:03:50 PM    enp0s8      0.50      0.50      0.03      0.09      0.00      0.00      0.00      0.00

04:03:50 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
04:03:52 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:03:52 PM    enp0s3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:03:52 PM    enp0s8      0.50      0.50      0.03      0.41      0.00      0.00      0.00      0.00

04:03:52 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
04:03:54 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:03:54 PM    enp0s3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:03:54 PM    enp0s8     11.00     21.50      0.64      3.87      0.00      0.00      0.00      0.00

Average:        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
Average:           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:       enp0s3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:       enp0s8      4.00      7.50      0.23      1.46      0.00      0.00      0.00      0.00

5. 블록 디바이스 및 파티션에 대한 상세 정보 확인

sar -p
$ sar -p 2 3
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

04:04:29 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:04:31 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
04:04:33 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
04:04:35 PM     all      0.00      0.00      0.51      0.00      0.00     99.49
Average:        all      0.00      0.00      0.17      0.00      0.00     99.83

6. 디스크 I/O 통계 확인

sar -d
$ sar -d 2 3
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

04:04:47 PM       DEV       tps     rkB/s     wkB/s     dkB/s   areq-sz    aqu-sz     await     %util
04:04:49 PM     loop0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:49 PM     loop1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:49 PM     loop2      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:49 PM     loop3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:49 PM     loop4      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:49 PM     loop5      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:49 PM       sr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:49 PM       sda      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:49 PM      dm-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

04:04:49 PM       DEV       tps     rkB/s     wkB/s     dkB/s   areq-sz    aqu-sz     await     %util
04:04:51 PM     loop0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:51 PM     loop1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:51 PM     loop2      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:51 PM     loop3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:51 PM     loop4      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:51 PM     loop5      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:51 PM       sr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:51 PM       sda      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:51 PM      dm-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

04:04:51 PM       DEV       tps     rkB/s     wkB/s     dkB/s   areq-sz    aqu-sz     await     %util
04:04:53 PM     loop0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:53 PM     loop1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:53 PM     loop2      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:53 PM     loop3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:53 PM     loop4      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:53 PM     loop5      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:53 PM       sr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:53 PM       sda      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:53 PM      dm-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

Average:          DEV       tps     rkB/s     wkB/s     dkB/s   areq-sz    aqu-sz     await     %util
Average:        loop0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:        loop1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:        loop2      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:        loop3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:        loop4      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:        loop5      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:          sr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:          sda      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:         dm-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

7. 스왑 공간 사용 확인

sar -W
$ sar -W 2 3
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

04:05:11 PM  pswpin/s pswpout/s
04:05:13 PM      0.00      0.00
04:05:15 PM      0.00      0.00
04:05:17 PM      0.00      0.00
Average:         0.00      0.00

8. 프로세스 통계 확인

sar -P ALL
$ sar -P ALL 2 3
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

04:05:56 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:05:58 PM     all      0.00      0.00      0.76      0.00      0.00     99.24
04:05:58 PM       0      0.00      0.00      1.00      0.00      0.00     99.00
04:05:58 PM       1      0.00      0.00      0.52      0.00      0.00     99.48

04:05:58 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:06:00 PM     all      0.00      0.00      0.26      0.00      0.00     99.74
04:06:00 PM       0      0.00      0.00      0.00      0.00      0.00    100.00
04:06:00 PM       1      0.00      0.00      0.52      0.00      0.00     99.48

04:06:00 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:06:02 PM     all      0.00      0.00      0.00      0.00      0.00    100.00
04:06:02 PM       0      0.00      0.00      0.00      0.00      0.00    100.00
04:06:02 PM       1      0.00      0.00      0.00      0.00      0.00    100.00

Average:        CPU     %user     %nice   %system   %iowait    %steal     %idle
Average:        all      0.00      0.00      0.34      0.00      0.00     99.66
Average:          0      0.00      0.00      0.33      0.00      0.00     99.67
Average:          1      0.00      0.00      0.35      0.00      0.00     99.65

sar 작업 스케줄 파일 편집

vim /etc/cron.d/sysstat
# The first element of the path is a directory where the debian-sa1
# script is located
PATH=/usr/lib/sysstat:/usr/sbin:/usr/sbin:/usr/bin:/sbin:/bin

# Activity reports every 10 minutes everyday
5-55/10 * * * * root command -v debian-sa1 > /dev/null && debian-sa1 1 1

# Additional run at 23:59 to rotate the statistics file
59 23 * * * root command -v debian-sa1 > /dev/null && debian-sa1 60 2

sar 데이터 파일 확인

$ file /var/log/sysstat/sa18
/var/log/sysstat/sa18: OpenPGP Secret Key Version 5

cron 작업 추가

sar을 1분마다 실행하고 데이터를 수집하기

crontab -e
* * * * * /usr/lib/sysstat/sa1 1 1

sar 명령어로 데이터 확인

sar -f /var/log/sysstat/sa18
$ sar -f /var/log/sysstat/sa18
Linux 5.15.0-86-generic (aptmirror)     10/18/2023      _x86_64_        (2 CPU)

03:52:49 PM  LINUX RESTART      (2 CPU)

04:24:01 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:25:01 PM     all      0.02      0.00      0.15      0.01      0.00     99.82
04:26:01 PM     all      0.03      0.00      0.14      0.03      0.00     99.80
Average:        all      0.02      0.00      0.15      0.02      0.00     99.81

 

sar 명령어는 시스템의 성능과 사용량에 대한 풍부한 정보를 제공하므로 시스템 모니터링 및 문제 해결에 유용합니다. 원하는 시간 간격과 반복 횟수를 지정하여 명령을 실행하고 결과를 분석하여 시스템의 동작을 평가할 수 있습니다.

 

728x90