Class: Comana::HostInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/comana/hostinspector.rb

Defined Under Namespace

Classes: NoUpdateFile

Constant Summary collapse

PING_MIN_INTERVAL =
1
CACHE_DIR =
"#{ENV['HOME']}/var/comana"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, cache_dir) ⇒ HostInspector

Returns a new instance of HostInspector.



15
16
17
18
# File 'lib/comana/hostinspector.rb', line 15

def initialize(hostname, cache_dir)
  @hostname = hostname
  @cache_dir = "#{cache_dir}/#{@hostname}"
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



12
13
14
# File 'lib/comana/hostinspector.rb', line 12

def hostname
  @hostname
end

Instance Method Details

#fetch(name) ⇒ Object

Return from cached ping data.



127
128
129
# File 'lib/comana/hostinspector.rb', line 127

def fetch(name)
  load_cache(name)
end

#time_updated(name) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/comana/hostinspector.rb', line 131

def time_updated(name)
  ping_file = "#{@cache_dir}/#{name}.yaml"
  if File.exist?  ping_file
    return File.mtime(ping_file)
  else
    return nil
  end
end

#update_cpuinfoObject

dmesg ログ形式でつらい。 /proc/cpuinfo コアごとにでるのでパースめんどう。 lscpu これだと思ったら、CPU MHz がずれてる。ハードウェアで想定される値ではなく、 実際の速度で書かれるらしい。 負荷の有無で値がかわる。



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/comana/hostinspector.rb', line 96

def update_cpuinfo
  #str = `ssh #{@hostname} 'cat /proc/cpuinfo'`
  str = ssh_str('cat /proc/cpuinfo')
  results = []
  cur_index = 0
  results[cur_index] = {}
  lines = str.split("\n")
  lines.each do |line|
    if line =~ /^\s*$/
      cur_index += 1
      results[cur_index] = {}
      next
    end
    key, value = line.split(/\s*:\s*/)
    results[cur_index][key] = value
  end
  write_cache('cpuinfo', results)
end

#update_cwdObject

cwd readlink コマンドが使えるかとも思ったが、シムリンク自体の名前が不明瞭になる。



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/comana/hostinspector.rb', line 40

def update_cwd
  str = ssh_str('ls -l /proc/\*/cwd 2> /dev/null')
  results = {}
  str.split("\n").each do |line|
    items = line.split
    pid = items[8].sub(/^\/proc\//, '').sub(/\/cwd$/, '')
    results[pid] = items[10]
  end

  write_cache('cwd', results)
end

#update_meminfoObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/comana/hostinspector.rb', line 115

def update_meminfo
  str = ssh_str('cat /proc/meminfo')
  results = {}
  lines = str.split("\n")
  lines.each do |line|
    key, value = line.split(/\s*:\s*/)
    results[key] = value
  end
  write_cache('meminfo', results)
end

#update_pingObject

Try ping three times. Return true if at least one time responds. def ping3



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/comana/hostinspector.rb', line 24

def update_ping
  result = false
  3.times do
    command =
      "ping -c 1 -W #{PING_MIN_INTERVAL} #{@hostname} 2> /dev/null 1> /dev/null"
    if system(command)
      result = true
      break
    end
  end

  write_cache("ping", result)
end

#update_psObject

processes %ps auxw USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND ippei 28971 0.0 0.0 103684 3764 ? S 15:19 0:00 sshd: ippei@pts/19 root 1 0.0 0.0 33876 2280 ? Ss 5月17 0:22 /sbin/init 0———1———2———3———4———5———6———7 0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0

% ps -o ‘user pid %cpu %mem command’ USER PID %CPU %MEM COMMAND ippei 19991 0.0 0.2 zsh ippei 23217 0.0 0.0 ps -o user pid %cpu %mem command 0———1———2———3———4———5———6———7 0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0-2-4-6-8-0 auxw だと、 ippei 2948 198 11.8 4495708 3884740 pts/3 Rl Apr01 173494:26 /opt/bin/vasp5212openmpi で桁が崩れることがある。



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/comana/hostinspector.rb', line 68

def update_ps
  str = ssh_str('ps auxw')
  results = {}
  lines = str.split("\n")
  lines.shift  # titles of items
  lines.each do |line|
    items = line.split
    user    = items[0]
    pid     = items[1]
    cpu     = items[2]
    mem     = items[3]
    command = items[10]

    results[pid] = {
      "user"    => user,
      "cpu"     => cpu,
      "mem"     => mem,
      "command" => command
    }
  end
  write_cache('ps', results)
end