ruby 并行运行各种stat collection命令以创建单个输出以供以后解析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 并行运行各种stat collection命令以创建单个输出以供以后解析相关的知识,希望对你有一定的参考价值。

require 'pty'


cmds = [
        ['sar_dev', 'sar -n DEV 1'],
        ['sar_edev', 'sar -n EDEV 1'],
        ['sar_tcp', 'sar -n TCP 1'],
        ['sar_etcp', 'sar -n ETCP 1'],
        ['sar_sock', 'sar -n SOCK 1'],
        ['mpstat_irq','mpstat -I SCPU 1'],
        ['mpstat_cpu','mpstat -P ALL 1']
]

loop_cmds = [
        ['power_stats','sudo ipmi-dcmi --get-enhanced-system-power-statistics 2>/dev/null'],
        ['softnet_stat','cat /proc/net/softnet_stat | nl --starting-line-number=0'],
        ['sockstats','cat /proc/net/sockstat | grep TCP'],
        ['varnishstats','sudo varnishstat -n /dev/shm/varnish/ -1'],
        ['haproxystats','echo "show info; show stat" | nc -U  /var/run/haproxy-video_edge_ssl-stats.1.sock'],
        ['haproxy send-Q','sudo netstat -tpn | grep 443 | grep ESTA | awk \'{print $3}\' | sort -n | awk \'NR == 1  { max=$1; min=$1; sum=0; nonzero_count=0 }     { if ($1>max) max=$1; if ($1<min) min=$1; if ($1>0 ) nonzero_count+=1; sum+=$1;}     END {printf "Sum: %d\tMin: %d\tMax: %d\tAverage: %f\tCount: %d\tZero_count: %d\n",sum, min, max, sum/nonzero_count, NR, NR-nonzero_count}\''],
        ['haproxy recv-Q','sudo netstat -tpn | grep 443 | grep ESTA | awk \'{print $2}\' | sort -n | awk \'NR == 1  { max=$1; min=$1; sum=0; nonzero_count=0 }     { if ($1>max) max=$1; if ($1<min) min=$1; if ($1>0 ) nonzero_count+=1; sum+=$1;}     END {printf "Sum: %d\tMin: %d\tMax: %d\tAverage: %f\tCount: %d\tZero_count: %d\n",sum, min, max, sum/nonzero_count, NR, NR - nonzero_count}\''],
        ['varnish send-Q','sudo netstat -tpn | grep 127.0.0.1:80 | grep varnishd | awk \'{print $3}\' | sort -n | awk \'NR == 1  { max=$1; min=$1; sum=0; nonzero_count=0 }     { if ($1>max) max=$1; if ($1<min) min=$1; if ($1>0 ) nonzero_count+=1; sum+=$1;}     END {printf "Sum: %d\tMin: %d\tMax: %d\tAverage: %f\tCount: %d\tZero_count: %d\n",sum, min, max, sum/nonzero_count, NR, NR - nonzero_count}\''],
        ['varnish recv-Q','sudo netstat -tpn | grep 127.0.0.1:80 | grep varnishd | awk \'{print $2}\' | sort -n | awk \'NR == 1  { max=$1; min=$1; sum=0; nonzero_count=0 }     { if ($1>max) max=$1; if ($1<min) min=$1; if ($1>0 ) nonzero_count+=1; sum+=$1;}     END {printf "Sum: %d\tMin: %d\tMax: %d\tAverage: %f\tCount: %d\tZero_count: %d\n",sum, min, max, sum/nonzero_count, NR, NR - nonzero_count}\'']
]




def shell_execute(caption,cmd)
  PTY.spawn( cmd ) do |stdout, stdin, pid|
    begin
      stdout.each do  |line|
        ts = Time.now.strftime("%s")
        print "#{ts}\t#{caption}\t\t#{line}"
      end
    rescue Errno::EIO
        sleep 1
    end
  end
end


def loop_shell_execute(caption,cmd)
        loop { shell_execute(caption,cmd); sleep 1  }
end

begin

threads = []

cmds.each do |c|
        threads << Thread.new do
                shell_execute(c[0],c[1])
        end

end

loop_cmds.each do |c|
        threads << Thread.new do
                loop_shell_execute(c[0],c[1])
        end
end
threads.each { |thr| thr.join }


rescue PTY::ChildExited
  puts "The child process exited!"
end

以上是关于ruby 并行运行各种stat collection命令以创建单个输出以供以后解析的主要内容,如果未能解决你的问题,请参考以下文章

DBMS_STATS并行和并发收集统计信息

Is Ruby dead? Hell no! - Analyzing RubyGems stats for 2016

Redis-stat 的安装与使用

Redis-stat 的安装与使用

Ruby 多线程

Ruby 多线程