Module: GreenHat::InfoFormat

Defined in:
lib/greenhat/thing/info_format.rb

Overview

Info Formatter

Instance Method Summary collapse

Instance Method Details

#cpu_speedObject



122
123
124
125
126
# 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_formatObject



113
114
115
116
117
118
119
120
# 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_free_mObject



42
43
44
# File 'lib/greenhat/thing/info_format.rb', line 42

def format_free_m
  split_white_space raw
end

#format_headers_n_linesObject Also known as: format_df_inodes, format_df_h



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

#format_meminfoObject



46
47
48
# File 'lib/greenhat/thing/info_format.rb', line 46

def format_meminfo
  raw.map { |x| x.split(' ', 2) }
end

#format_mount(data) ⇒ Object



38
39
40
# File 'lib/greenhat/thing/info_format.rb', line 38

def format_mount(data)
  data.select { |x| x.include? ':' }
end

#format_netstatObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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_headersObject



50
51
52
53
54
# 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_iObject



84
85
86
87
88
89
90
91
92
93
# 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_headersObject



56
57
58
59
60
# 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_jsonObject Also known as: format_gitlab_version_manifest_json



187
188
189
# File 'lib/greenhat/thing/info_format.rb', line 187

def format_single_json
  Oj.load raw.join("\n")
end

#info?Boolean

Is this something that can be formatted by info?

Returns:

  • (Boolean)


5
6
7
# File 'lib/greenhat/thing/info_format.rb', line 5

def info?
  methods.include? "format_#{path}".to_sym
end

#info_formatObject

Handle Info Formatting



10
11
12
# File 'lib/greenhat/thing/info_format.rb', line 10

def info_format
  self.result = send("format_#{path}")
end

#manifest_json_formatObject



109
110
111
# File 'lib/greenhat/thing/info_format.rb', line 109

def manifest_json_format
  Oj.load info[:gitlab_version_manifest_json].join
end

#mountObject



180
181
182
183
184
185
# 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_formatObject



99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



95
96
97
# File 'lib/greenhat/thing/info_format.rb', line 95

def split_white_space(data)
  data.map(&:split)
end

#systemctl_color(entry) ⇒ Object

Helper to color the status files



160
161
162
163
164
165
166
167
168
# 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_formatObject



148
149
150
151
152
153
154
155
156
157
# 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_memoryObject



128
129
130
131
132
133
# 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

#ulimitObject



135
136
137
138
139
140
141
142
143
144
145
146
# 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

#uptimeObject



176
177
178
# File 'lib/greenhat/thing/info_format.rb', line 176

def uptime
  info.uptime.join.split(',', 4).map(&:lstrip) if info.key? :uptime
end

#vmstat_formatObject



170
171
172
173
174
# File 'lib/greenhat/thing/info_format.rb', line 170

def vmstat_format
  return nil unless data? :vmstat

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