龙空技术网

一键远程自动抓取perf火焰图(附脚本)

98dev 153

前言:

今天兄弟们对“ubuntu提取rar”可能比较讲究,兄弟们都想要剖析一些“ubuntu提取rar”的相关资讯。那么小编同时在网摘上网罗了一些对于“ubuntu提取rar””的相关资讯,希望大家能喜欢,我们一起来了解一下吧!

关于 perf相关内容,抓取命令较多,当需要大量数据时每次输入命令会比较麻烦。

perf与火焰图使用介绍_perf 火焰图_cocoti的博客-CSDN博客

根据一些流程,我们可以将个命令合入到一个脚本中,进行一次性抓取perf

target_ip=10.8.104.xx target_port=22 target_name=arvintarget_workspace="~/perf" local_workspace=$(pwd) target_process=0 running_time_s=30 outputfile="out"

设置目标IP,端口,用户名即可远程抓取perf

支持功能如下:

自动在远端Target上判断是否有可用perf,没有本地上传

自定义远端Target上工作目录

可抓取所有进程,或单进程名字 ./auto-perf.sh xxx

自定义抓取时间(秒) ./auto-perf.sh xxx yyy

抓取完后自动调用浏览器查看数据

使用方法 将压缩包解压到ubuntu系统,或服务器上(服务器不支持图形化,解析完后无法自动打开perf图)

配置好远端的工作目录,及相关ip、端口、用户名

./autoperf.sh 运行。根据提示输入远端密码,由于要配置一些环境,会有多次输入

完成后会在out目录中生成相应的perf数据以及svg可视化文件下载即可分析

暂时无法在飞书文档外展示此内容

脚本源码如下:

#!/bin/sh

target_ip=10.8.104.xx

target_port=22

target_name=arvin

target_workspace="~/perf"

local_workspace=$(pwd)

target_process=0

running_time_s=30

outputfile="out"

echo "current local workspace:\033[34m$local_workspace\033[0m"

mkdir -p $local_workspace

if [ $# -le 2 ]; then

for arg in $*

do

if [ -n "$(echo $arg| sed -n "/^[0-9]\+$/p")" ]; then

running_time_s=$arg

else

target_process=$arg

fi

done

else

echo "Usage:"

echo " ./autoperf.sh [process_name] [capture_time]"

exit

fi

echo "target_ip:\033[35m$target_ip\033[0m"

echo "target_port:\033[35m$target_port\033[0m"

echo "target_name:\033[35m$target_name\033[0m"

echo "target_process:\033[32m$target_process\033[0m"

echo "time:\033[32m$running_time_s(S)\033[0m"

flamegraph_path=$local_workspace"/FlameGraph"

if [ ! -d $flamegraph_path ];then

echo "Install FlameGraph..."

mkdir -p $flamegraph_path

unzip FlameGraph-master.zip -d $flamegraph_path > /dev/ 2>&1

if [ $? -ne 0 ]; then

echo "\033[31mInstall FlameGraph failed.\033[0m"

exit

fi

fi

# upload the ssh-key to target

if [ ! -f "$HOME/.ssh/id_rsa.pub" ];then

echo "Install ssh key..."

ssh-keygen -t rsa

fi

echo "target_ip:\033[33mUpdate ssh key to target_ip:${target_ip} ...\033[0m"

ssh-copy-id -i ~/.ssh/id_rsa.pub -p ${target_port} ${target_name}@${target_ip}

ssh -p ${target_port} ${target_name}@${target_ip} "mkdir -p $target_workspace"

# upload the perf to target, if not the perf

target_return=$(ssh -p ${target_port} ${target_name}@${target_ip} "which perf" | tr -d "\r\n")

if [ "$target_return" = "" ];then

echo "target_ip:\033[33mNo perf on target, push a new one, make sure the perf is OK...\033[0m"

target_return=$(ssh -p ${target_port} ${target_name}@${target_ip} "sudo -S uname --m" | tr -d "\r\n")

echo "current operator system:\033[35m${target_return}\033[0m"

if [ "$target_return" = "x86_64" ] || [ "$target_return" = "aarch64" ];then

scp -r -P ${target_port} $target_return/perf ${target_name}@${target_ip}:${target_workspace}

ssh -p ${target_port} ${target_name}@${target_ip} "chmod 777 ${target_workspace}/perf; sudo -S cp ${target_workspace}/perf /usr/bin; which perf"

if [ $? -ne 0 ]; then

echo "\033[31mPush perf into the target failed.\033[0m"

exit

fi

else

echo "\033[31mNot have the perf tool for ${target_return}\033[0m"

exit

fi

fi

# str_grep="command not found"

# target_return=$(ssh -p ${target_port} ${target_name}@${target_ip} "perf" | tr -d "\r\n")

# result=$(echo $target_return | grep "${str_grep}")

# if [ "$result" != "" ]; then

# echo "target_ip:\033[33mNo perf on target, push a new one, make sure the perf is OK...\033[0m"

# scp -r -P ${target_port} .ws/tools/perf ${target_name}@${target_ip}:/usr/bin/

# ssh -p ${target_port} ${target_name}@${target_ip} "chmod 777 /usr/bin/perf; which perf"

# if [ $? -ne 0 ]; then

# echo "\033[31mPush perf into the target failed.\033[0m"

# exit

# fi

# target_return=$(ssh -p ${target_port} ${target_name}@${target_ip} "perf" | tr -d "\r\n")

# if echo "$target_return" | grep -q "command not found"; then

# cho "\033[31mThe perf is not support for target.\033[0m"

# exit

# fi

# fi

flamegraph=$flamegraph_path"/FlameGraph-master"

echo "Capturing perf data, just wait for $running_time_s seconds..."

# Run perf

if [ "$target_process" != "0" ]; then

pid=$(ssh -p ${target_port} ${target_name}@${target_ip} "ps -aux | grep $target_process" | awk 'NR==1 {print $2}' | tr -d "\r\n")

echo "Will capture the pid : \033[32m"$pid"\033[0m"

if [ "$pid" = "" ]; then

echo "\033[31mNo process is found for $target_process.\033[0m"

exit

fi

if [ -n "$(echo $pid | sed -n "/^[0-9]\+$/p")" ]; then

continue

else

echo "\033[31mGet the pid of $target_process failed, a non-numeric is got.\033[0m"

exit

fi

echo "sudo -S perf record -F 99 -p $pid --call-graph=dwarf --proc-map-timeout 10000 -o $target_workspace/perf.data -- sleep $running_time_s"

ssh -p ${target_port} ${target_name}@${target_ip} "sudo -S perf record -F 99 -p $pid --call-graph=dwarf --proc-map-timeout 10000 -o $target_workspace/perf.data -- sleep $running_time_s"

outputfile=$target_process

else

ssh -p ${target_port} ${target_name}@${target_ip} "sudo -S perf record -F 99 -a --call-graph=dwarf --proc-map-timeout 10000 -o $target_workspace/perf.data -- sleep $running_time_s"

fi

# Dump perf data

cur_datetime="`date +%Y%m%d%H%M%S`"

outputfile=$outputfile".$cur_datetime.perf"

echo $target_workspace$outputfile

ssh -p ${target_port} ${target_name}@${target_ip} "sudo -S perf script -i $target_workspace/perf.data > $target_workspace/$outputfile"

if [ ! -d "output" ];then

mkdir output

fi

cd output

scp -r -P ${target_port} ${target_name}@${target_ip}:$target_workspace/$outputfile .

$local_workspace/FlameGraph/FlameGraph-master/stackcollapse-perf.pl $outputfile | $local_workspace/FlameGraph/FlameGraph-master/flamegraph.pl > $outputfile".svg"

ssh -p ${target_port} ${target_name}@${target_ip} "rm $target_workspace -rf"

# Auto show the svg

which google-chrome > /dev/

if [ $? -eq 0 ]; then

google-chrome `pwd`/$outputfile".svg" &

else

which firefox

if [ $? -eq 0 ]; then

firefox `pwd`/$outputfile".svg" &

fi

fi

cd -

作者丨arvin_xiaoting

来源丨网址:

编辑:IT运维技术圈

腾讯云超值服务器3年599,复制链接注册购买

或者点击原文链接跳转后注册购买

波哥

IT行业近二十年的IT老炮。常年潜伏于国企、各一二线大厂中。硬件集成入行,直至虚拟技术、容器化。岗位历经系统集成、DBA、全栈开发、sre、项目经理、产品经理、部门总监。

主要作品:

IT类资源汇聚门户:

各大短视频平台:98dev

各大主要技术论坛博客:IT运维技术圈

长视频教学作品:《波哥讲网络》《波哥讲git》《波哥讲gitlab》

小程序:IT面试精选

构建技术社区:+V itboge1521 入学习交流群

标签: #ubuntu提取rar