module GreenHat::InfoFormat

Info Formatter

Public Instance Methods

cpu_speed() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 122
def cpu_speed
  return nil unless data? :lscpu

  info.lscpu.find { |x| x.include? 'MHz' }.split('               ')
end
cpuinfo_format() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 113
def cpuinfo_format
  info.cpuinfo.join("\n").split("\n\n").map do |cpu|
    all = cpu.split("\n").map do |row|
      row.delete("\t").split(': ')
    end
    { details: all[1..], order: all[0].last }
  end
end
format_df_h()
format_df_inodes()

All Similar to Header and Lines

format_free_m() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 42
def format_free_m
  split_white_space raw
end
format_gitlab_version_manifest_json()
Alias for: format_single_json
format_headers_n_lines() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 14
def format_headers_n_lines
  # Headers to Readable Symbol
  headers = raw.first.split(' ', 6).map(&:downcase).map do |x|
    x.gsub(/\s+/, '_').gsub(/[^0-9A-Za-z_]/, '')
  end.map(&:to_sym)

  output = []

  # Put fields into a Hash based on Location/Key
  raw[1..].map(&:split).each do |row|
    result = {}
    row.each_with_index do |detail, i|
      result[headers[i]] = detail
    end
    output.push result
  end

  self.result = output
end
Also aliased as: format_df_inodes, format_df_h
format_meminfo() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 46
def format_meminfo
  raw.map { |x| x.split(' ', 2) }
end
format_mount(data) click to toggle source
# File lib/greenhat/thing/info_format.rb, line 38
def format_mount(data)
  data.select { |x| x.include? ':' }
end
format_netstat() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 62
def format_netstat
  # Data can return blank or single item array
  conns, sockets = raw.split { |x| x.include? 'Active' }.reject(&:empty?)

  formatted_conns = conns[1..].map do |entry|
    entry.split(' ', 7).map(&:strip).each_with_index.map do |field, idx|
      [format_netstat_conn_headers[idx], field]
    end.to_h
  end

  formatted_sockets = sockets[1..].map do |entry|
    entry.split('  ').map(&:strip).reject(&:blank?).each_with_index.map do |field, idx|
      [format_netstat_socket_headers[idx], field]
    end.to_h
  end

  {
    connections: formatted_conns,
    sockets: formatted_sockets
  }
end
format_netstat_conn_headers() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 50
def format_netstat_conn_headers
  [
    'Proto', 'Recv-Q', 'Send-Q', 'Local Address', 'Foreign Address', 'State', 'PID/Program name'
  ]
end
format_netstat_i() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 84
def format_netstat_i
  headers = raw[1].split
  result = raw[2..].map do |row|
    row.split.each_with_index.map do |x, idx|
      [headers[idx], x]
    end
  end

  result.map(&:to_h)
end
format_netstat_socket_headers() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 56
def format_netstat_socket_headers
  [
    'Proto', 'RefCnt', 'Flags', 'Type', 'State', 'I-Node', 'PID/Program name', 'Path'
  ]
end
format_single_json() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 187
def format_single_json
  Oj.load raw.join("\n")
end
info?() click to toggle source

Is this something that can be formatted by info?

# File lib/greenhat/thing/info_format.rb, line 5
def info?
  methods.include? "format_#{path}".to_sym
end
info_format() click to toggle source

Handle Info Formatting

# File lib/greenhat/thing/info_format.rb, line 10
def info_format
  self.result = send("format_#{path}")
end
manifest_json_format() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 109
def manifest_json_format
  Oj.load info[:gitlab_version_manifest_json].join
end
mount() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 180
def mount
  return nil unless info.key? :mount
  return nil if info.mount.empty?

  MountFormat.parse(info.mount)
end
ps_format() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 99
def ps_format
  headers = info.ps.first.split(' ', 11)
  list = info.ps[1..].each.map do |row|
    row.split(' ', 11).each_with_index.each_with_object({}) do |(v, i), obj|
      obj[headers[i]] = v
    end
  end
  { headers: headers, list: list }
end
split_white_space(data) click to toggle source
# File lib/greenhat/thing/info_format.rb, line 95
def split_white_space(data)
  data.map(&:split)
end
systemctl_color(entry) click to toggle source

Helper to color the status files

# File lib/greenhat/thing/info_format.rb, line 160
def systemctl_color(entry)
  case entry.status
  when 'enabled'  then :green
  when 'static'   then :orange
  when 'disabled' then :red
  else
    :grey
  end
end
systemctl_format() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 148
def systemctl_format
  return nil unless data? :systemctl_unit_files

  all = info.systemctl_unit_files[1..-2].map do |x|
    unit, status = x.split
    { unit: unit, status: status }
  end
  all.reject! { |x| x[:unit].nil? }
  all.sort_by(&:unit)
end
total_memory() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 128
def total_memory
  return nil unless data? :free_m

  value = info.free_m.dig(1, 1).to_i
  number_to_human_size(value * 1024 * 1024)
end
ulimit() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 135
def ulimit
  return nil unless data? :ulimit

  results = info.ulimit.map do |entry|
    {
      value: entry.split[-1],
      details: entry.split('  ').first
    }
  end

  results.sort_by { |x| x[:details].downcase }
end
uptime() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 176
def uptime
  info.uptime.join.split(',', 4).map(&:lstrip) if info.key? :uptime
end
vmstat_format() click to toggle source
# File lib/greenhat/thing/info_format.rb, line 170
def vmstat_format
  return nil unless data? :vmstat

  info.vmstat[2..].map(&:split)
end