[Linux]psacct 패키지로 사용자별 프로세스 사용 통계 로그를 수집해 보자!

리눅스 로그를 기록하는 패키지들 중의 하나인 psacct에 대해 알아보겠습니다. psacct 패키지는 사용자별로 프로세스 사용 통계 로그를 수집하는 유틸리티로 , 패키지에 포함된 데몬 및 명령어를 사용하여 프로세스 활동을 모니터링하고 로그를 기록합니다.
  그러나 최근 몇 년 동안 psacct 패키지는 많은 리눅스 배포판에서 deprecated(사용 중단)되었습니다. Rocky9 리눅스를 설치한 후 psacct 데몬의 상태는 디폴트로 “inactive” 되어 있습니다.

[root@rocky9u2 ~]# systemctl status psacct
psacct.service - Kernel process accounting Loaded: loaded (/usr/lib/systemd/system/psacct.service; disabled; preset: disabled) Active: inactive (dead)

  최신의 리눅스 시스템에서는 psacct보다는 systemd의 systemd-journald와 같은 현대적인 로깅 시스템을 사용하거나, auditd와 같은 다른 로깅 및 감사 도구를 선호합니다. 이러한 도구들은 더 강력하고 유연하며, 더 많은 기능을 제공할 수 있습니다.

  매번 강조하지만 패키지 분석을 위해서는 패키지 설치 시 생성된 파일 및 디렉터리를 먼저 확인해 보겠습니다. 특히 환경 설정 파일, 명령어, systemd 관련 파일, 디폴트 저장 파일 등이 중요합니다. 로그 회전을 위한 /etc/logrotate.d/psacct, 사용자 연결 시간의 통계를 출력하는 ac 명령어, 사용자들이 실행한 명령어 정보를 출력하는 lastcomm 명령어, 프로세스 사용 통계를 수집 기능을 on/off시키는 accton 명령어, 프로세스 사용 통계를 요약하는 sa 명령어 및 프로세스 사용 통계를 저장하는 /var/account/pacct 로그 파일 등이 있습니다.

[root@rocky9u2 ~]# dnf repoquery -l psacct
Last metadata expiration check: 2:37:40 ago on Wed 22 Nov 2023 08:18:54 AM KST. /etc/logrotate.d/psacct /usr/bin/ac /usr/bin/lastcomm /usr/lib/.build-id /usr/lib/.build-id/34 /usr/lib/.build-id/34/24ca21fade73d6ae4fbacdbb61b18fc287828d /usr/lib/.build-id/4b /usr/lib/.build-id/4b/1bb60e600b33b7e433184ee4eaa85b07b8073a /usr/lib/.build-id/67 /usr/lib/.build-id/67/26e14580ad511bfd29e66ac7f1c27662ce079a /usr/lib/.build-id/74 /usr/lib/.build-id/74/1bc15dac4212a825a0f8b8b80a2759c36e70db /usr/lib/.build-id/c3 /usr/lib/.build-id/c3/a1024d09c8ea68cd1af9293df99a294cda124e /usr/lib/.build-id/fc /usr/lib/.build-id/fc/e557fac727cd176c3dfc84abde04c1af3973e4 /usr/lib/systemd/system/psacct.service /usr/libexec/psacct/accton-create /usr/sbin/accton /usr/sbin/dump-acct /usr/sbin/dump-utmp /usr/sbin/sa /usr/share/doc/psacct /usr/share/doc/psacct/README /usr/share/info/accounting.info.gz /usr/share/licenses/psacct /usr/share/licenses/psacct/COPYING /usr/share/man/man1/ac.1.gz /usr/share/man/man1/lastcomm.1.gz /usr/share/man/man8/accton.8.gz /usr/share/man/man8/dump-acct.8.gz /usr/share/man/man8/dump-utmp.8.gz /usr/share/man/man8/sa.8.gz /var/account /var/account/pacct

psacct 데몬 시작 및 관련 명령어들에 대한 용도 및 사용법을 알아보겠습니다.
  1. psacct 데몬 시작 및 중지
    – accton [on|off] 명령어와 systemd [start|stop|restart|enable –now|disable –now] 를 사용해서 시작 또는 중지시킬 수 있습니다.
    [root@rocky9u2 ~]# accton on
    Turning on process accounting, file set to the default '/var/account/pacct'.
    [root@rocky9u2 ~]# accton off
    Turning off process accounting.
    [root@rocky9u2 ~]# systemctl enable --now psacct
    Created symlink /etc/systemd/system/multi-user.target.wants/psacct.service /usr/lib/systemd/system/psacct.service.
    [root@rocky9u2 ~]# systemctl disable --now psacct
    Removed "/etc/systemd/system/multi-user.target.wants/psacct.service".
  2. ac 명령어
    – “/var/log/wtmp” 파일의 로그인/로그아웃 시간을 기준으로 사용자별 연결 시간(hr) 레포트를 출력합니다.
    • -d: 일자별로 출력
    • -y: -d 옵션과 함께 연도 출력
    • -p: 사용자별로 출력
    [root@rocky9u2 ~]# ac -d -y
    Nov 8 2023 total 58.01 Nov 9 2023 total 133.10 Nov 10 2023 total 115.44 Nov 11 2023 total 125.49 Nov 12 2023 total 1.71 Nov 14 2023 total 29.94 Nov 15 2023 total 68.50 Nov 17 2023 total 126.04 Nov 18 2023 total 24.00 Nov 21 2023 total 79.44 Today total 31.17
    [root@rocky9u2 ~]# ac -p
    archer 191.98 root 598.83 jeno 2.04 total 792.84 #필드1 : 사용자명 #필드2 : 연결시간(hrs)
  3. lastcomm 명령어
    – 사용자별로 실행한 명령어 및 프로세스 정보 출력
    • --user name: 지정한 사용자만의 정보 출력
    • --command name: 지정한 명령어만의 정보 출력
    • --pid: 출력 화면에 pid 및 ppid 출력
    [root@rocky9u2 ~]# lastcomm
    accton                 root     __         0.00 secs Wed Nov 22 13:55
    (sd-executor)    SF    root     __         0.00 secs Wed Nov 22 13:55
    systemd-gpt-aut  S     root     __         0.00 secs Wed Nov 22 13:55
    vsftpd-generato  S     root     __         0.00 secs Wed Nov 22 13:55
    ln                     root     __         0.00 secs Wed Nov 22 13:55
    ..... MORE .....
    #필드1 : 프로세스의 명령어 이름
    #필드2 : 플래그(`man lastcomm` 참고)
             S - 슈퍼유저가 실행한 명령
             F - 포크 후에 실행되었으나 다음 exec 없이 실행된 명령
             C - PDP-11 호환 모드에서 실행되는 명령(VAX에만 해당)
             D - 코어 파일 생성으로 종료된 명령
             X - 명령이 SIGTERM 신호로 종료되었습니다.
    #필드3 : 사용자 이름
    #필드4 : 접속 단말기(tty, pts)
    #필드5 : 실행 시간
    #필드6 : 프로세스의 시간(주 월 일 시:분)
    

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다