2016-04-25 00:47:31 +00:00
|
|
|
module ApplicationHelper
|
2016-10-02 04:19:10 +00:00
|
|
|
require 'net/ping'
|
2016-09-23 09:28:53 +00:00
|
|
|
|
|
|
|
def get_df
|
|
|
|
df = Array.new
|
|
|
|
df_each = Hash.new
|
|
|
|
command = String.new
|
|
|
|
command << "df -hP"
|
|
|
|
puts command
|
|
|
|
output = `#{command}`.split("\n")
|
|
|
|
output.each do |t|
|
|
|
|
next if t.equal? output.first
|
2016-09-26 09:06:53 +00:00
|
|
|
begin
|
|
|
|
s = t.split(' ')
|
|
|
|
df_each['Filesystem'] = s[0]
|
|
|
|
df_each['Size'] = s[1]
|
|
|
|
df_each['Used'] = s[2]
|
|
|
|
df_each['Avail'] = s[3]
|
|
|
|
df_each['Use%'] = s[4]
|
|
|
|
df_each['Mounted on'] = s[5]
|
|
|
|
df << df_each
|
|
|
|
df_each = Hash.new
|
|
|
|
rescue => ex
|
|
|
|
puts ex
|
|
|
|
end
|
2016-09-23 09:28:53 +00:00
|
|
|
end
|
|
|
|
return df
|
|
|
|
end
|
|
|
|
|
2016-09-27 03:14:39 +00:00
|
|
|
def get_du(dir = @current_dir)
|
|
|
|
du = Array.new
|
|
|
|
du_each = Hash.new
|
|
|
|
command = String.new
|
|
|
|
avail = 0.0
|
|
|
|
if dir.eql? "/"
|
|
|
|
command = "sudo df /"
|
|
|
|
s = `#{command}`.split("\n")[1].split(" ")
|
|
|
|
avail = s[2].to_f + s[3].to_f
|
|
|
|
dir = ""
|
|
|
|
else
|
|
|
|
command << "sudo du -s #{dir}"
|
|
|
|
begin
|
|
|
|
avail = `#{command}`.split(" ")[0].to_f
|
|
|
|
rescue
|
|
|
|
# some directory is not connected
|
|
|
|
avail = `#{command}`.split(" ")[1].to_f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
command = ""
|
|
|
|
command << "sudo du -s #{dir}/*"
|
|
|
|
puts command
|
|
|
|
output = `#{command}`.split("\n")
|
|
|
|
output.each do |t|
|
|
|
|
begin
|
2016-09-28 16:39:22 +00:00
|
|
|
du_each['size'] = t.split(" ")[0].to_i
|
2016-09-27 03:14:39 +00:00
|
|
|
du_each['usage'] = t.split(" ")[0].to_f / avail
|
|
|
|
du_each['file_name'] = t.split(" ")[1].split("/").last
|
|
|
|
du << du_each
|
|
|
|
du_each = Hash.new
|
|
|
|
rescue
|
|
|
|
# directory is not connected
|
2016-09-28 16:39:22 +00:00
|
|
|
du_each['size'] = 0
|
2016-09-27 03:14:39 +00:00
|
|
|
du_each['usage'] = 0.0
|
|
|
|
du_each['file_name'] = t.split(" ")[1].split("/").last.split("'")[0]
|
|
|
|
du << du_each
|
|
|
|
du_each = Hash.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if du.length == 0
|
2016-09-28 16:39:22 +00:00
|
|
|
du_each['size'] = 0
|
2016-09-27 03:14:39 +00:00
|
|
|
du_each['usage'] = 1.0
|
|
|
|
du_each['file_name'] = "empty"
|
|
|
|
du << du_each
|
|
|
|
end
|
2016-10-02 04:19:10 +00:00
|
|
|
|
2016-09-28 16:39:22 +00:00
|
|
|
du.sort_by! { |k| k['usage'] }
|
|
|
|
du.reverse!
|
2016-09-27 03:14:39 +00:00
|
|
|
return du
|
|
|
|
end
|
|
|
|
|
2016-09-24 09:00:27 +00:00
|
|
|
def volumes
|
|
|
|
volumes = Array.new
|
|
|
|
volume = Hash.new
|
2016-09-24 10:31:27 +00:00
|
|
|
node = Node.take
|
2016-09-24 09:00:27 +00:00
|
|
|
df = get_df
|
2016-09-26 08:10:11 +00:00
|
|
|
# error check : node is nil
|
|
|
|
if node.nil?
|
|
|
|
return volumes
|
|
|
|
end
|
2016-09-24 09:00:27 +00:00
|
|
|
command = String.new
|
2016-09-24 10:31:27 +00:00
|
|
|
command << "sshpass -p#{node.user_password} ssh #{node.user_name}@#{node.host_ip} gluster volume info"
|
2016-09-24 09:00:27 +00:00
|
|
|
puts command
|
|
|
|
output = `#{command}`.split("\n")
|
|
|
|
output << "\n"
|
|
|
|
output.each do |t|
|
|
|
|
next if t.equal? output.first
|
|
|
|
if t.include? ":"
|
|
|
|
temp = t.split(":")
|
|
|
|
volume[temp[0]] = temp[1]
|
|
|
|
else
|
|
|
|
volume['Mount State'] = "unmounted"
|
|
|
|
df.each do |u|
|
|
|
|
next if !u['Filesystem'].include? volume['Volume Name'].delete(' ')
|
|
|
|
volume['Mount State'] = "mounted"
|
|
|
|
volume['Mount Point'] = u['Mounted on']
|
|
|
|
end
|
|
|
|
volumes << volume
|
|
|
|
volume = Hash.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return volumes
|
|
|
|
end
|
2016-09-24 10:31:27 +00:00
|
|
|
|
2016-09-24 16:24:28 +00:00
|
|
|
def files(dir)
|
|
|
|
files = Array.new
|
|
|
|
file = Hash.new
|
2016-09-26 09:06:53 +00:00
|
|
|
command = String.new
|
|
|
|
command << "ls #{dir} -l"
|
|
|
|
puts command
|
|
|
|
output = `#{command}`.split("\n")
|
2016-09-24 16:24:28 +00:00
|
|
|
output.each do |t|
|
|
|
|
next if t.equal? output.first
|
2016-09-26 09:06:53 +00:00
|
|
|
begin
|
|
|
|
s = t.split(" ")
|
|
|
|
file["auth"] = s[0]
|
|
|
|
file["size"] = s[4]
|
|
|
|
file["date"] = s[5] + " " + s[6] + " " + s[7]
|
|
|
|
file["name"] = s[8]
|
|
|
|
files << file
|
|
|
|
file = Hash.new
|
|
|
|
rescue => ex
|
|
|
|
puts ex
|
|
|
|
end
|
2016-09-24 16:24:28 +00:00
|
|
|
end
|
|
|
|
return files
|
|
|
|
end
|
|
|
|
|
2016-10-02 04:19:10 +00:00
|
|
|
def ping_test?(host)
|
|
|
|
check = Net::Ping::External.new(host)
|
|
|
|
return check.ping?
|
|
|
|
end
|
2016-04-25 00:47:31 +00:00
|
|
|
end
|