Top Level Namespace

Defined Under Namespace

Modules: Comana

Constant Summary collapse

USAGE =

! /usr/bin/env ruby coding: utf-8

<<HERE
  sshall
    -g groups

    E.g.,
      sshall -c "ls -l /" HostA HostB
        Indicate each host
      sshall -c "ls -l /" -g GroupA GroupB
        Indicate Hosts with group names
      sshall -c "ls -l /"
        Execute on all hosts when empty targets

      sshall HostA HostB
        Login each hosts when -c option is omitted.

      sshall -u USER -c "ls -l /"
        Login as USER
HERE
OPTIONS =

option analysis

{}
COMMAND_NAME =

Analyze options

File.basename("#{__FILE__}")

Instance Method Summary collapse

Instance Method Details

#hiObject

Output



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'bin/hostinfo', line 123

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

#target_hostsObject

Return selected hosts



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'bin/hostinfo', line 59

def target_hosts
  cs = Comana::ClusterSetting.load_file
  groups = {}
  cs.groups.each do |key, val|
    cluster = key
    groups[key] = val["members"]
  end
  hs = Comana::HostSelector.new groups
  if OPTIONS[:group]
    hosts = []
    ARGV.each do |group|
      begin
        hosts << hs.select_group(group)
      rescue Comana::HostSelector::NoEntryError
        $stderr.puts "Unknown group: #{group}"
      end
    end
    hosts.flatten!
  else
    hosts = ARGV
  end
  hosts = hs.select_all if ARGV.empty?
  hosts
end

#time_ago(uptime) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'bin/hostinfo', line 84

def time_ago(uptime)
  if uptime
    second = (Time.now - uptime).to_i
    if second >= 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