文件被那个进程使用,写数据不是用lsof可以找出来吗,但现实情况是lsof没找出来T_T

背景

centos7 在某一段时间监控报警磁盘使用率达99%,由于监控属于概要形式信息,没有快照信息的监控(能发现某进程的I/O,CPU消耗情况),所以需要在服务器上去定时执行统计命令获取快照信息。

需要通过iostat -dx -k去查看avgqu-sz、await、svctm、%util;

sar -u查看%iowait、%user;

pidstat -d 查看进程I/O读写的快照信息

步骤

  • 生成统计信息文件
cat>/tmp/at_task.sh<<EOF
pidstat -d 2 >/tmp/pidstat_\`date +%F_%T\`.log 2>& 1 &
sar -u 2 >/tmp/sar_\`date +%F_%T\`.log 2>& 1 &
while [ 1 ];do echo -n \`date +%T\` >>/tmp/iostat_\`date +%F\` 2>& 1 && iostat -dx -k 1 1 >>/tmp/iostat_\`date +%F\` 2>& 1; sleep 2; done &
EOF

在while循环中使用iostat的原因是要输出date +%T时间,不然只有数据,没有时间信息也没有什么用

  • 使用at 命令定时执行
at 15:14 today -f /tmp/at_task.sh

出现错误

Can't open /var/run/atd.pid to signal atd. No atd running?

重启atd服务

service atd restart

重新开启at定时任务

at 15:14 today -f /tmp/at_task.sh

job 2 at Wed Mar 13 15:14:00 2019

得到如下快照信息

iostat

15:13:35Linux 3.10.0-862.14.4.el7.x86_64 (ip-xxxxx)     03/13/2019      _x86_64_        (4 CPU)

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
vda 0.12 0.07 17.31 19.41 580.79 90.52 36.57 0.09 2.39 4.42 0.57 0.72 2.63
scd0 0.00 0.00 0.00 0.00 0.00 0.00 6.00 0.00 0.28 0.28 0.00 0.25 0.00

sar

03:14:00 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
03:14:02 PM all 0.25 0.00 0.38 0.00 0.00 99.37
03:14:04 PM all 1.25 0.13 0.63 0.00 0.00 97.99
03:14:06 PM all 0.25 0.13 0.50 0.00 0.00 99.12
03:14:08 PM all 0.50 0.00 0.50 0.63 0.00 98.37

pidstat

03:14:00 PM   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
03:14:02 PM 5700 9089 0.00 6.00 0.00 uxxx
03:14:02 PM 5700 9140 0.00 6.00 0.00 uxxx
03:14:02 PM 5700 9292 0.00 10.00 0.00 uxxx
03:14:02 PM 0 18084 0.00 2.00 0.00 bash

kill 掉收集信息的命令

ps -ef | egrep 'iostat|sar|pidstat|while' | grep -v grep | awk '{print $2}' | xargs -l kill

但ps -ef | egrep 命令没有获取到while循环的pid,不kill掉该while循环,就会一直对/tmp/iostat_2019-03-13写数据-_-

通过lsof 没有定位到打开文件的进程

lsof /tmp/iostat_2019-03-13
[root@ip-10-186-60-117 ~]#
[root@ip-10-186-60-117 ~]#

通过lsof 可以定位到打开mysql-error.log的进程

lsof /opt/mysql/data/5690/mysql-error.log
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 12858 actiontech-universe 1w REG 253,1 6345 20083533 /opt/mysql/data/5690/mysql-error.log
mysqld 12858 actiontech-universe 2w REG 253,1 6345 20083533 /opt/mysql/data/5690/mysql-error.log

可见,某进程只有一只持有某文件的inode,才可以通过lsof查看文件在被那些进程使用

获取写文件的进程号

安装sysemtap

yum -y install systemtap

SystemTap 是对 Linux 内核监控和跟踪的工具

利用systemtap中的inodewatch.stp工具来查找写文件的进程号

得到文件的inode

stat -c '%i' /tmp/iostat_2019-03-13
4210339

获取文件所在设备的major,minor

ls -al /dev/vda1
brw-rw---- 1 root disk 253, 1 Jan 30 13:57 /dev/vda1

得到写文件的pid

stap /usr/share/systemtap/examples/io/inodewatch.stp 253 1  4210339

Checking "/lib/modules/3.10.0-862.14.4.el7.x86_64/build/.config" failed with error: No such file or directory
Incorrect version or missing kernel-devel package, use: yum install kernel-devel-3.10.0-862.14.4.el7.x86_64

根据系统内核版本在kernel-devel rpm build for : Scientific Linux 7网站上下载相应的kernal-devel包

wget ftp://ftp.pbone.net/mirror/ftp.scientificlinux.org/linux/scientific/7.2/x86_64/updates/security/kernel-devel-3.10.0-862.14.4.el7.x86_64.rpm

rpm -ivh kernel-devel-3.10.0-862.14.4.el7.x86_64.rpm

再次执行stap

stap /usr/share/systemtap/examples/io/inodewatch.stp 253 1 4210339

......

Missing separate debuginfos, use: debuginfo-install kernel-3.10.0-862.14.4.el7.x86_64

Pass 2: analysis failed. [man error::pass2]

Number of similar error messages suppressed: 2.

安装debuginfo kernal

debuginfo-install kernel-3.10.0-862.14.4.el7.x86_64
Verifying : kernel-debuginfo-common-x86_64-3.10.0-862.14.4.el7.x86_64 1/3
Verifying : yum-plugin-auto-update-debug-info-1.1.31-50.el7.noarch 2/3
Verifying : kernel-debuginfo-3.10.0-862.14.4.el7.x86_64 3/3 Installed:
kernel-debuginfo.x86_64 0:3.10.0-862.14.4.el7
yum-plugin-auto-update-debug-info.noarch 0:1.1.31-50.el7 Dependency Installed:
kernel-debuginfo-common-x86_64.x86_64 0:3.10.0-862.14.4.el7

再次执行stap

stap /usr/share/systemtap/examples/io/inodewatch.stp 253 1  4210339
ERROR: module version mismatch (#1 SMP Tue Sep 25 14:32:52 CDT 2018 vs #1 SMP Wed Sep 26 15:12:11 UTC 2018), release 3.10.0-862.14.4.el7.x86_64
WARNING: /usr/bin/staprun exited with status: 1
添加 -v查看详细报错
stap -v /usr/share/systemtap/examples/io/inodewatch.stp 253 1 4210339
Pass 1: parsed user script and 471 library scripts using 240276virt/41896res/3368shr/38600data kb, in 300usr/20sys/320real ms.
Pass 2: analyzed script: 2 probes, 12 functions, 8 embeds, 0 globals using 399436virt/196284res/4744shr/197760data kb, in 1540usr/560sys/2106real ms.
Pass 3: using cached /root/.systemtap/cache/f5/stap_f5c0cd780e8a2cac973c9e3ee69fba0c_7030.c
Pass 4: using cached /root/.systemtap/cache/f5/stap_f5c0cd780e8a2cac973c9e3ee69fba0c_7030.ko
Pass 5: starting run.
ERROR: module version mismatch (#1 SMP Tue Sep 25 14:32:52 CDT 2018 vs #1 SMP Wed Sep 26 15:12:11 UTC 2018), release 3.10.0-862.14.4.el7.x86_64
WARNING: /usr/bin/staprun exited with status: 1
Pass 5: run completed in 0usr/10sys/38real ms.
Pass 5: run failed. [man error::pass5]

修改

vim /usr/src/kernels/3.10.0-862.14.4.el7.x86_64/include/generated/compile.h

#define UTS_VERSION "#1 SMP Tue Sep 25 14:32:52 CDT 2018"
改为
#define UTS_VERSION "#1 SMP Wed Sep 26 15:12:11 UTC 2018" rm -rf /root/.systemtap/cache/f5/stap_f5c0cd780e8a2cac973c9e3ee69fba0c_7030*

参考SystemTap - 安装

再次执行

stap /usr/share/systemtap/examples/io/inodewatch.stp 253 1 4210339

iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4671) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4677) vfs_write 0xfd00001/4210339
iostat(4683) vfs_write 0xfd00001/4210339
............

可见已经得到了写/tmp/iostat_date +%F 文件的进程号,但进程号一直在打印出来,因为后台进程iostat -dx -m 的在while循环中的,每隔sleep 2s 后就会执行一次iostat 产生新的pid。

那要怎样才能让iostat -dx -m 停止写/tmp/iostat_date +%F 文件了?除了重启大法好 $_$

rm -rf 也不能终止后台的while iostat进程写文件,删除了文件后,while循环又会生成新的文件

rm -rf  /tmp/iostat_2019-03-1*

stat /tmp/iostat_2019-03-1*
File: ‘/tmp/iostat_2019-03-13’
Size: 146700 Blocks: 512 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 4210339 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-03-14 16:07:26.211888899 +0800
Modify: 2019-03-14 16:18:17.854019793 +0800
Change: 2019-03-14 16:18:17.854019793 +0800

正确做法

cat>/tmp/iostat.sh<<EOF
while [ 1 ];do echo -n \`date +%T\` >>/tmp/iostat_\`date +%F\` 2>& 1 && iostat -dx -m 1 1 >>/tmp/iostat_\`date +%F\` 2>& 1; sleep 2; done &
EOF at now + 1 minute today
bash /tmp/iostat.sh #这样就能方便的获取到进程号pid了
ps -ef | grep iostat
root 8593 1 0 16:16 pts/2 00:00:00 bash /tmp/iostat.sh

参考

I Need the Kernel Source

Linux下如何知道文件被那个进程写

Linux下的IO监控与分析

使用systemtap调查异常io的来源

Linux 查看文件被那个进程写数据的更多相关文章

  1. linux查看文件被哪个进程占用?

    1> 如果文件是端口号 netstat -ntlp | grep portNum [root@localhost root]# netstat -ntlp Active Internet con ...

  2. Linux是cat、tail、head查看文件任意几行的数据

    Linux是cat.tail.head查看文件任意几行的数据 一.使用cat.tail.head组合 1.查看最后100行的数据 cat filename | tail -n 100 2.查看100到 ...

  3. linux查看文件相关指令

    以下内容整理自以下两篇文章: http://www.cnblogs.com/xilifeng/archive/2012/10/13/2722596.html Linux 查看文件内容的命令 http: ...

  4. linux查看文件的编码格式的方法 set fileencoding PYTHON

    linux查看文件的编码格式的方法 set fileencoding   乱码原因:因为你的文件声明为utf-8,并且也应该是用utf-8的编码保存的源文件.但是windows的本地默认编码是cp93 ...

  5. Linux查看文件编码格式及文件编码转换

    Linux查看文件编码格式及文件编码转换   如果你需要在Linux 中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而L ...

  6. Linux 查看某个用户的进程

    Linux 查看某个用户的进程 To view only the processes owned by a specific user, use the following command: top ...

  7. linux 查看文件命令总结

    linux 查看文件命令总结 1.cat 查看文件内容 选项-b 空白行不显示行号.-n,空白行显示 2.more 查看文件内容,通过空格键查看下一页 q键退出查看 3.less 和上同,多了方向键( ...

  8. (转载)Linux查看文件编码格式及文件编码转换

    Linux查看文件编码格式及文件编码转换 时间:2011-04-08作者:woyoo分类:linux评论:0 我友分享: 新浪微博 腾讯微博 搜狐微博 网易微博 开心网 QQ空间 msn 如果你需要在 ...

  9. Win和Linux查看端口和杀死进程

    title: Win和Linux查看端口和杀死进程 date: 2017-7-30 tags: null categories: Linux --- 本文介绍Windows和Linux下查看端口和杀死 ...

随机推荐

  1. Confluence 6 用户宏示例 - Formatted Panel

    下面的用演示了如果还写一个用户宏,并在这个宏中创建一个格式化的面板,并且指定颜色.将会创建下面的面板: (Title)   注意:这个面板的标题为空,如果你没有给这个面板标题参数的话. Macro n ...

  2. pytorch的学习资源

    安装:https://github.com/pytorch/pytorch 文档:http://pytorch.org/tutorials/beginner/blitz/tensor_tutorial ...

  3. Ribbon服务消费者

    springcloud使用到两种消费工具,ribbon和feign ribbon实现了服务的负载均衡 feign默认集成了ribbon,一般情况下使用feign作为消费端 搭建消费者项目(Ribbon ...

  4. Python基础之面向对象进阶二

    一.__getattribute__ 我们一看见getattribute,就想起来前面学的getattr,好了,我们先回顾一下getattr的用法吧! class foo: def __init__( ...

  5. 剑指offer 二叉树的层序遍历

    剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...

  6. django----注意事项

    不用带参数 必须要带参数:

  7. cf861D 字典树+时间戳

    好久没碰字典树之类的题了,搞起来有点生疏 /* 把所有母串的后缀加入字典树中 然后再扫一次所有母串的后缀,把后缀放到字典树中查询,找到第一个访问次数为1的结点返回即可 num在计数时,同一个母串的子串 ...

  8. Python安装、卸载第三方模块

    pip command ModuleName command:用于指定要执行的命令(install:安装,uninstall:卸载) ModuleName:需要安装的模块名称 示例: 安装第三方模块n ...

  9. 滴水穿石-08IO

    1.0 File a:构造方法 package d8; import java.io.File; public class FileGouZao { public static void main(S ...

  10. sharepoint2013 Restore-SPSite 报错,采用数据库还原

    PS C:\Users\spadmin> Restore-SPSite http://hz0xw002049:8099 -Path D:\20170731MossSiteSP.bak -Forc ...