运用unix/linux 实用工具监控系统-针对DBA
一.使用sar命令来监控CPU的使用情况
sar 命令有许多不同的选项,通过设置选项可以展示不同的性能信息。带-u 选项的 sar 命令可用来监控 CPU的使用情况。sar 工具是査看 CPU 使用情况快照的有效手段,该快照展示了 CPU 究竟有多忙(100%的使用率并非好事)。定期运行该工具即可得到系统性能的基线,使你得以识别何时系统运作不佳。sar命令有以下好处:
- 为性能优化和监控提供了非常有用的信息
- 信息被记录到磁盘文件里(但并不提供每个进程的具体信息)
- 开销低
- 应用于大多数的 UNIX和 Linux平台
1.1.sar -u (检查cpu的繁忙程度)
[oracle@single ~]$ sar -u 10 8 Linux 3.10.0-1160.el7.x86_64 (single) 2024年11月30日 _x86_64_ (4 CPU) 23时39分32秒 CPU %user %nice %system %iowait %steal %idle 23时39分42秒 all 0.03 0.00 0.13 0.03 0.00 99.82 23时39分52秒 all 0.03 0.00 0.18 0.03 0.00 99.77 23时40分02秒 all 0.20 0.00 0.64 0.03 0.00 99.13 23时40分12秒 all 0.10 0.00 0.10 0.03 0.00 99.77 23时40分22秒 all 0.05 0.00 0.15 0.03 0.00 99.77 23时40分32秒 all 0.03 0.00 0.18 0.03 0.00 99.77 23时40分42秒 all 0.03 0.00 0.13 0.03 0.00 99.82 23时40分52秒 all 0.00 0.00 0.13 0.08 0.00 99.80 平均时间: all 0.06 0.00 0.20 0.03 0.00 99.71 [oracle@single ~]$
复制
1.2.sar -d (找出I/O问题)
[oracle@single ~]$ sar -d 3 2 Linux 3.10.0-1160.el7.x86_64 (single) 2024年11月30日 _x86_64_ (4 CPU) 23时42分28秒 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 23时42分31秒 dev8-0 3.00 0.00 72.00 24.00 0.00 1.56 1.56 0.47 23时42分31秒 dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 23时42分31秒 dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 23时42分31秒 dev253-2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 23时42分31秒 dev253-3 3.00 0.00 72.00 24.00 0.00 1.56 1.56 0.47 23时42分31秒 DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 23时42分34秒 dev8-0 3.00 0.00 72.00 24.00 0.00 1.11 1.11 0.33 23时42分34秒 dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 23时42分34秒 dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 23时42分34秒 dev253-2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 23时42分34秒 dev253-3 3.00 0.00 72.00 24.00 0.00 1.11 1.11 0.33 平均时间: DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 平均时间: dev8-0 3.00 0.00 72.00 24.00 0.00 1.33 1.33 0.40 平均时间: dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 平均时间: dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 平均时间: dev253-2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 平均时间: dev253-3 3.00 0.00 72.00 24.00 0.00 1.33 1.33 0.40 [oracle@single ~]$
复制
1.3.sar -b(检查缓冲区高速缓存)
[oracle@single ~]$ sar -b Linux 3.10.0-1160.el7.x86_64 (single) 2024年11月30日 _x86_64_ (4 CPU) 00时00分01秒 tps rtps wtps bread/s bwrtn/s 00时10分01秒 4.52 0.19 4.33 44.02 101.31 00时20分01秒 7.87 1.35 6.52 95.26 775.06 23时00分01秒 18.13 11.86 6.27 1269.33 476.83 23时10分01秒 5.33 1.47 3.86 114.49 87.46 23时20分01秒 12.12 5.55 6.57 1040.59 702.69 23时30分01秒 3.94 0.05 3.89 3.63 84.56 23时40分01秒 4.24 0.52 3.72 32.37 81.60 平均时间: 8.73 3.62 5.11 433.83 340.14 [oracle@single ~]$
复制
二.使用sar命令和vmstat命令监控调页/交换
发出 vmstat -s命令乃是迅速判定自系统启动以来是否发生过交换活动的手段。如果在swp/in和swp/out列中有非零值,就表明可能出了问题。可以使用sar命令去深究更多的细节,也可以用它检查系统的调页和交换活动。根据所用的系统,任何调页与交换现象的出现都可能是有问题的征兆。在虚拟存储系统中,当非活动用户被从内存移往磁盘时,就会出现调页(小问题);而当由于内存不足造成活动用户被移往磁盘时,就会出现交换(非常大)的问题。
2.1.使用sar -p 报告调页活动
[root@single ~]# sar -p 2 3 Linux 3.10.0-1160.el7.x86_64 (single) 12/01/2024 _x86_64_ (4 CPU) 12:07:18 AM CPU %user %nice %system %iowait %steal %idle 12:07:20 AM all 0.00 0.00 0.13 0.13 0.00 99.74 12:07:22 AM all 0.13 0.00 0.26 0.00 0.00 99.62 12:07:24 AM all 0.00 0.00 0.26 0.00 0.00 99.74 Average: all 0.04 0.00 0.21 0.04 0.00 99.70 [root@single ~]#
复制
2.2.使用sar -w报告切换活动
[root@single ~]# sar -w 3 2 Linux 3.10.0-1160.el7.x86_64 (single) 12/01/2024 _x86_64_ (4 CPU) 12:07:34 AM proc/s cswch/s 12:07:37 AM 1.00 1546.33 12:07:40 AM 0.67 1543.33 Average: 0.83 1544.83 [root@single ~]#
复制
2.3.使用sar -r命令来报告空闲内存和空闲交换空间
[root@single ~]# sar -r 3 2 Linux 3.10.0-1160.el7.x86_64 (single) 12/01/2024 _x86_64_ (4 CPU) 12:12:17 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty 12:12:20 AM 78264 2816960 97.30 0 1238648 7903228 111.48 1426884 1045864 44 12:12:23 AM 78264 2816960 97.30 0 1238648 7903228 111.48 1426888 1045860 44 Average: 78264 2816960 97.30 0 1238648 7903228 111.48 1426886 1045862 44 [root@single ~]#
复制
2.4.使用sar -g选项来报告调页活动
linux中用sar -B,aix环境中则是sar -r
[root@single ~]# sar -B Linux 3.10.0-1160.el7.x86_64 (single) 12/01/2024 _x86_64_ (4 CPU) 12:00:01 AM pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff 12:10:01 AM 56.30 55.79 304.54 0.05 105.58 16.46 0.00 16.30 99.05 Average: 56.30 55.79 304.54 0.05 105.58 16.46 0.00 16.30 99.05 [root@single ~]#
复制
三.使用top命令找出系统上最差的用户
[root@single ~]# top
top - 00:22:34 up 3:13, 4 users, load average: 0.07, 0.04, 0.05
Tasks: 286 total, 1 running, 285 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.5 us, 1.5 sy, 0.0 ni, 97.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2895224 total, 79168 free, 1528536 used, 1287520 buff/cache
KiB Swap: 4194300 total, 4188660 free, 5640 used. 855284 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 191436 3792 2012 S 0.0 0.1 0:05.72 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 0:00.48 ksoftirqd/0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.04 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:05.75 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
11 root rt 0 0 0 0 S 0.0 0.0 0:00.12 watchdog/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.13 watchdog/1
13 root rt 0 0 0 0 S 0.0 0.0 0:00.08 migration/1
14 root 20 0 0 0 0 S 0.0 0.0 0:00.47 ksoftirqd/1
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0H
17 root rt 0 0 0 0 S 0.0 0.0 0:00.10 watchdog/2
18 root rt 0 0 0 0 S 0.0 0.0 0:00.03 migration/2
19 root 20 0 0 0 0 S 0.0 0.0 0:00.71 ksoftirqd/2
21 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/2:0H
22 root rt 0 0 0 0 S 0.0 0.0 0:00.12 watchdog/3
[root@single ~]#
复制
四.使用uptime和mpstat查看CPU负载
[root@single ~]# uptime 00:28:10 up 3:19, 4 users, load average: 0.00, 0.02, 0.05 [root@single ~]# mpstat 1 10 Linux 3.10.0-1160.el7.x86_64 (single) 12/01/2024 _x86_64_ (4 CPU) 12:28:13 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 12:28:14 AM all 0.00 0.00 0.26 0.00 0.00 0.00 0.00 0.00 0.00 99.74 12:28:15 AM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 12:28:16 AM all 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 0.00 99.75 12:28:17 AM all 0.26 0.00 0.51 0.26 0.00 0.00 0.00 0.00 0.00 98.97 12:28:18 AM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 12:28:19 AM all 0.00 0.00 0.26 0.00 0.00 0.00 0.00 0.00 0.00 99.74 12:28:20 AM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 12:28:21 AM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 12:28:22 AM all 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 0.00 99.75 12:28:23 AM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 Average: all 0.03 0.00 0.15 0.03 0.00 0.00 0.00 0.00 0.00 99.80 [root@single ~]#
复制
五.结合使用ps命令和选定的v$视图
哪个进程使用最多的CPU资源 ?
下面的命令列出了使用CPU资源最多的9个用户。
ps -e -o pcpu,pid,user,args | sort -k 3 -r | tail [root@single ~]# ps -e -o pcpu,pid,user,args | sort -k 3 -r | tail 0.0 1560 nobody /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper 0.0 809 libstor+ /usr/bin/lsmd -d 0.0 2606 geoclue /usr/libexec/geoclue -t 5 0.2 1317 dmdba /dm8/bin/dmserver path=/dm8/data/DAMENG/dm.ini -noconsole 0.0 1314 dmdba /dm8/bin/dmap 0.0 789 dbus /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation 0.0 2186 colord /usr/libexec/colord 0.0 793 chrony /usr/sbin/chronyd 0.0 832 avahi avahi-daemon: registering [single-319.local] 0.0 849 avahi avahi-daemon: chroot helper [root@single ~]#
复制
此命令列出了所消耗的%CPU、进程号PID、用户名以及所执行的命令。
如果消耗最多的CPU资源是Oracle用户,便可使用如下查询得到Oracle中的相关进程信息。
要把ps命令获得的系统PID传给下面的查询:
--ps_view.sql
col username format a15
col osuser format a10
col program format a20
set verify off
select a.username,a.osuser,a.program,spid,sid,a.serial#
from v$session a,v$process b
where a.paddr=b.addr
and spid='&spid';
复制
--ps_sql.sql
set verify off
column username format a15
column sql_text format a60
underfine sid
underfine serial#
accept sid prompt 'sid: '
accept serial prompt 'serial#: '
select 'SQL Currently Executing: '
from dual;
select b.username,a.sql_text
from v$sql a,v$session b
where b.sql_address = a.address
and b.sql_hash_value = a.hash_value
and b.sid= &sid
and b.serial# = '&serial';
select 'Open Cursors:'
from dual;
select b.username,a.sql_text
from v$open_cursor a,v$session b
where b.sql_address = a.address
and b.sql_hash_value = a.hash_value
and b.sid = &sid
and b.serial# = '&serial';
复制
六.使用iostat命令辨认磁盘I/O瓶颈
[root@single ~]# iostat Linux 3.10.0-1160.el7.x86_64 (single) 12/01/2024 _x86_64_ (4 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.11 0.00 0.30 0.04 0.00 99.55 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 9.24 503.62 189.18 7075394 2657839 dm-0 1.83 90.94 3.54 1277636 49718 dm-1 0.20 0.43 0.50 6084 7088 dm-2 0.20 0.92 0.34 12897 4715 dm-3 7.61 409.09 184.65 5747381 2594197 [root@single ~]#
复制
下面的脚本列出时间间隔内最忙的10个磁盘。
[root@single ~]# cat diskbusy iostat -x | awk '/^disk/' iostat -x 5 5|grep -v '^ ' |grep -v '^diskk'|awk '{ print $10 ", "$0 }' $* | sort -n | awk -F, '{ print $2 }' | tail [root@single ~]#
复制
最后修改时间:2024-12-01 08:52:31
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【DBA坦白局】第一期:在小城市和一线城市做DBA,是“躺”还是“卷”?
墨天轮编辑部
1378次阅读
2025-04-10 14:17:22
数据库国产化替代深化:DBA的机遇与挑战
代晓磊
919次阅读
2025-04-27 16:53:22
2025 DBA 薪资观察:做 DBA 还香吗?
墨天轮编辑部
840次阅读
2025-04-24 15:53:21
月薪快6万的DBA,有命干不一定有命花
多明戈教你玩狼人杀
603次阅读
2025-04-18 10:36:38
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
345次阅读
2025-04-15 14:48:05
[MYSQL] 服务器出现大量的TIME_WAIT, 每天凌晨就清零了
大大刺猬
190次阅读
2025-04-01 16:20:44
AI时代,DBA的技能会有有哪些改变
白鳝的洞穴
162次阅读
2025-04-15 09:49:34
Oracle EBS R12.2 安装 -- Step by Step
Albert Tan DBA 之路
71次阅读
2025-04-21 15:08:34
35+的我,Oracle DBA不焦虑了,你也可以!
青年数据库学习互助会
67次阅读
2025-04-16 10:33:51
数据库可观测性中的指标与TRACE
白鳝的洞穴
65次阅读
2025-04-17 10:41:54