Linux操作系统中有三种主要的日志子系统:
(1)连接时间日志
(2)进程统计日志
(3)系统和服务日志
连接时间日志和进程统计日志由rsyslog(旧版是syslog)日志服务驱动,系统和服务日志由相对应的网络服务驱动和管理;
进程统计日志
进程统计日志可以监控用户在服务器上的操作时非常有效,所记录的操作会存入/var/account/pacct文件中;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<code>#accton</code>
<code>accton: no arguments</code>
<code>Usage: accton [OPTION] on|off|ACCOUNTING_FILE</code>
<code>Try </code><code>'accton --help'</code> <code>for</code> <code>more</code> <code>information.</code>
<code># accton --help</code>
<code> </code><code>Turns process accounting on or off, or changes the </code><code>file</code> <code>where this</code>
<code> </code><code>info is saved.</code>
<code> </code><code>OPTIONS:</code>
<code> </code><code>-h, --help Show help and </code><code>exit</code>
<code> </code><code>-V, --version Show version and </code><code>exit</code>
<code> </code><code>ARGUMENTS:</code>
<code> </code><code>on Activate process accounting and use default </code><code>file</code>
<code> </code><code>off Deactivate process accounting</code>
<code> </code><code>ACCOUNTING_FILE Activate (</code><code>if</code> <code>not active) and save information </code><code>in</code>
<code> </code><code>this </code><code>file</code>
<code> </code><code>The system</code><code>'s default process accounting file is '</code><code>/var/account/pacct</code><code>'.</code>
<code> </code><code>Report bugs to <[email protected]></code>
(1)启动进程统计日志情况
<code># accton on</code>
<code>Turning on process accounting, </code><code>file</code> <code>set</code> <code>to the default </code><code>'/var/account/pacct'</code><code>.</code>
(2)显示进程统计日志情况
<code># lastcomm</code>
<code>ksmtuned F root __ 0.00 secs Thu Feb 26 23:05</code>
<code>awk</code> <code>root __ 0.00 secs Thu Feb 26 23:05</code>
<code>pgrep root __ 0.00 secs Thu Feb 26 23:05</code>
<code>sleep</code> <code>root __ 0.00 secs Thu Feb 26 23:04</code>
<code>accton S root pts</code><code>/0</code> <code>0.00 secs Thu Feb 26 23:04</code>
以最后一行为例:
*命令位
*标志位
S表示命令有超级管理员执行
F表示命令有子程序运行,没有使用EXEC
C表示命令运行在PDP-11兼容环境下
X表示命令由SIGTREM信号终止
*用户名
*执行命令的系统
关于lastcomm的用法:
<code># lastcomm --help</code>
<code>Usage: lastcomm [-hpV] [-f </code><code>file</code><code>] [</code><code>command</code><code>] ... [user] ... [terminal] ...</code>
<code> </code><code>[--forwards] [--</code><code>file</code> <code><</code><code>file</code><code>>] [--strict-match] [--print-controls]</code>
<code> </code><code>[--user <name>] [--</code><code>tty</code> <code><name>] [--</code><code>command</code> <code><name>] [--debug]</code>
<code> </code><code>[--show-paging] [--ahz <freq>] [--version] [--help]</code>
<code>The system's default process accounting </code><code>file</code> <code>is </code><code>/var/account/pacct</code><code>.</code>
(3)停止进程统计日志监控
<code># lastcomm off</code>
使用sa命令进行统计
sa命令可以将/var/account/pacct中的数据压缩到/var/log/savacc(基于命令名称进行索引统计)和/etc/log/usracc(基于用户名进行索引统计的)中;
<code># sa -a</code>
<code> </code><code>4557 50640.33re 1.08cp 0avio 27021k</code>
<code> </code><code>1 742.55re 0.58cp 0avio 366208k gnome-shell</code>
<code> </code><code>7 5199.84re 0.23cp 0avio 105595k gmain</code>
re表示示例时间,以分钟为单位;
cp表示系统和用户的使用时间,以分钟为单位;
k表示内核占用CPU的平均时间,一个单元大小为KB;
sh表示命令名称;
avio表示每次执行I/O操作的次数;
sa语法如下:
用法:sa [选项]... [文件]...
主要选项如下。
— -a:打印所有命令的名称(包括那些带有不可打印字符的)。
— -b:将输出按用户和系统时间的总和除以调用次数来进行分类。否则,输出为用户时间和系统时间之和。
— -c:将每个命令使用时间打印为相对于所有命令使用时间的百分比。此外,还有用户、系统和实时。
— -C:将记账文件合并到摘要文件中。如果摘要文件为旧用法,则将它转换为新用法。
— -d:将输出按平均磁盘I/O操作数进行分类。
— -D:将输出按总的磁盘I/O操作数进行分类和打印。
— -f:不要强制进行交互式阈值压缩。此标志必须与-v标志一起使用。
— -I:仅读取原始数据,不读取摘要文件。
— -j:打印每个调用的秒数,而不是每个类别的总的分钟数。
— -k:将输出按平均CPU时间进行分类。
— -K:将输出按CPU存储量整数进行分类和打印。
— -l:将系统时间和用户时间分离,而不是组合它们。
— -m:打印每个用户的进程数和CPU分钟数。
— -n:按调用数对输出进行分类。
— -r:将分类的顺序倒置。
— -s:将记账文件合并到摘要文件中。
— -t:打印每个命令的用时与用户和系统时间和之比。
— -u:暂挂所有其他标志并且打印每个命令的用户数字标识和命令名。
本文转自 bannerpei 51CTO博客,原文链接:http://blog.51cto.com/281816327/1615814,如需转载请自行联系原作者