sysdig是一个强大的开源工具,用于系统级别的分析,探测和排障,它的创建者在介绍它时称之为“strace+tcpdump+lsof+上面点缀着lua樱桃的绝妙酱汁”。抛开幽默不说,sysdig的最棒特性之一在于,它不仅能分析linux系统的“现场”状态,也能将该状态保存为转储文件以供离线检查
sysdig - the definitive system and process troubleshooting tool sysdig is a tool for system troubleshooting, analysis and exploration. It can be used to capture, filter and decode system calls and other OS events. sysdig can be both used to inspect live systems, or to generate trace files that can be analyzed at a later stage. sysdig includes a powerul filtering language, has customizable output, and can be extended through Lua scripts, called chisels.
1、sysdig的安装
系统版本信息如下
[root@VM_Server ~]# cat /etc/redhat-release
centos Linux release 7.6.1810 (Core) [root@VM_Server ~]# uname -r
3.10.0-957.el7.x86_64[root@VM_Server ~]#
1)在线安装
先配置好yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i "s/keepcache=0/keepcache=1/g" /etc/yum.conf
sed -i "s/gpgcheck=1/gpgcheck=0/g" /etc/yum.conf
脚本安装sysdig
curl -s https://s3.amazonaws.com/download.draIOS.com/stable/install-sysdig | bash
2)离线安装
rpm -Uvh zlib-devel-1.2.7-18.el7.x86_64.rpm
rpm -Uvh elfutils-*.rpm
rpm -Uvh dkms-2.8.1-4.20200214git5ca628c.el7.noarch.rpm
rpm -ivh sysdig-0.26.7-x86_64.rpm
2、sysdig的使用
1、sysdig -cl (--list-chisels)列出可用的默认类目
默认有以下几类
[root@VM_Server ~]# sysdig -clCategory: Application 应用
Category: CPU Usage CPU使用量
Category: Errors 错误
Category: I/O
Category: Logs 日志
Category: Misc
Category: Net 网络
Category: Performance 性能
Category: Security 安全
Category: System State 系统状态
Category: Tracers
2、使用-i查看具体的信息
Use the -i flag to get detailed information about a specific chisel
[root@VM_Server ~]# sysdig -i topprocs_file
Category: I/O
-------------
topprocs_file Top processes by R+W disk bytes
Shows the top processes in terms of total (in+out) bytes to disk. This chisel i
s compatible with containers using the sysdig -pc or -pcontainer argument, othe
rwise no container information will be shown.
Args:
(None)
3、用法举例
1)监控交互用户活动用法举例
作为系统管理员想要监控系统中交互的用户活动(如,用户在命令行输入了什么命令,以及用户去了什么目录),这时可以用spy_user “-z” (与“-w”一起使用)为记录文件启用压缩
-z, --compress Used with -w, enables compression for trace files
“-w ”保存sysdig记录到指定的文件
-w, --write=Write the captured events to.
-r, --read=Read the events from.
mkdir -p /log/sysdig/
sysdig -z -w /log/sysdig/spy_users.sysdigcap.gz -c spy_users
例如yuanfan这个用户登录SSH后执行了如下命令
sysdig -c spy_users可以监控到这个用户的操作命令
-r, --read=Read the events from.
sysdig -r /log/sysdig/spy_users.sysdigcap.gz -c spy_users
2)查看占用网络带宽最多的进程
sysdig -c topprocs_net
3)查看R+W读写量最大的文件
sysdig -c topfiles_bytes
4)查看CPU占用量最大的进程
sysdig -c topprocs_cpu
4、总结
sysdig是一个非常强大的工具,本文篇幅有限,其它具体用法可以参考如下几个链接或者自行查阅官方文档
1)https://www.oschina.net/p/sysdig
2)http://www.361way.com/linux-sysdig/4912.html
3)https://github.com/draios/sysdig/wiki/sysdig-user-guide