#! /usr/bin/env ruby # coding: utf-8 require "comana" require "optparse" require "pp" ##Analyze options COMMAND_NAME = File.basename("#{__FILE__}") OPTIONS = {} op = OptionParser.new op.banner = <= 86400 result = sprintf("%2s days ago", second / 86400) elsif second >= 3600 result = sprintf("%2s hours ago", second / 3600) elsif second >= 60 result = sprintf("%2s min ago", second / 60) else result = sprintf("%2s sec ago", second) end else #e.g., not exist cache file result = "need_update" end return result end ## Collect information hostinspectors = target_hosts.map { |host| Comana::HostInspector.new(host) } if OPTIONS[:update] Thread.abort_on_exception = true threads = {} hostinspectors.each do |hi| threads[hi] = Thread.start do hi.update_ping if requirement[:ping] hi.update_cwd if requirement[:cwd] hi.update_ps if requirement[:ps] hi.update_cpuinfo if requirement[:cpuinfo] hi.update_meminfo if requirement[:meminfo] end end threads.each do |hi, thread| print "Waiting for #{hi.hostname}...\n" unless OPTIONS[:quiet] thread.join # wait until all processes are completed end end ## Output hostinspectors.each do |hi| # oldest update oldest_key, val = requirement.min_by {|key,val| hi.time_updated(key.to_s)} hostname = hi.hostname timeago = time_ago( hi.time_updated(oldest_key.to_s)) hosttime = sprintf("%-10s (%12s) ", hi.hostname, time_ago( hi.time_updated(oldest_key.to_s))) begin case subcommand when "ping" if hi.fetch('ping') hosttime += "o" else hosttime += "x" end puts hosttime when "alive" puts hostname if hi.fetch('ping') when "cwd" puts hosttime hi.fetch('cwd').each do |pid, cwd| printf(" %5s %s\n", pid, cwd) end when "ps" puts hosttime hi.fetch('ps').each do |pid, ps| printf(" %-10s %5s\n", hi.hostname, pid) end when "cpuinfo" result = hosttime begin cpuinfo = hi.fetch('cpuinfo') result += sprintf("%2d_cores %s\n", cpuinfo.size, cpuinfo[0]["model name"].gsub(/ +/, '_')) rescue result += sprintf("not_obtained\n") end puts result when "meminfo" result = hosttime meminfo = hi.fetch('meminfo') result += sprintf("%s\n", meminfo['MemTotal']) puts result when "load" result = hosttime ps = hi.fetch("ps") cwd = hi.fetch("cwd") pid, process = ps.max_by {|key, val| val['cpu'].to_f} command = File.basename(process['command'].split[0]) result += sprintf("%5s %8s %5.1f %5.1f %20s %s\n", pid, process['user'], process['cpu'], process['mem'], command, cwd[pid]) puts result end rescue Comana::HostInspector::NoUpdateFile puts hosttime + "------------" rescue NoMethodError # 主にデータを取れなかった時用 puts hosttime + "------------" end end