Class: Host
- Inherits:
-
Teron
- Object
- Teron
- Host
- Includes:
- ActionView::Helpers::DateHelper, ActionView::Helpers::NumberHelper
- Defined in:
- lib/greenhat/host.rb
Overview
Individual Host Helper
Instance Method Summary collapse
-
#archive_name ⇒ Object
def load(file) @file = file self.files = Archive.load(file) save! end.
- #cpu_speed ⇒ Object
- #cpuinfo ⇒ Object
- #df_h ⇒ Object
- #find_file(file_name) ⇒ Object
- #find_thing(param) ⇒ Object
- #free_m ⇒ Object
-
#gitlab_version ⇒ Object
—————————.
- #icon ⇒ Object
- #netstat ⇒ Object
-
#percent(value, total) ⇒ Object
—————————- Helpers —————————-.
- #processes ⇒ Object
- #systemctl_color(entry) ⇒ Object
- #systemctl_unit_files ⇒ Object
- #things ⇒ Object
- #total_memory ⇒ Object
- #ulimit ⇒ Object
- #uname ⇒ Object
- #uptime ⇒ Object
Instance Method Details
#archive_name ⇒ Object
def load(file)
@file = file
self.files = Archive.load(file)
save!
end
16 17 18 |
# File 'lib/greenhat/host.rb', line 16 def archive_name archive.friendly_name end |
#cpu_speed ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/greenhat/host.rb', line 94 def cpu_speed file = find_thing 'lscpu' return nil unless file details = file.raw.find { |x| x.include? 'MHz' } details.reverse.split(' ', 2).map(&:reverse).reverse end |
#cpuinfo ⇒ Object
87 88 89 90 91 92 |
# File 'lib/greenhat/host.rb', line 87 def cpuinfo file = find_thing 'lscpu' return nil unless file file.data end |
#df_h ⇒ Object
117 118 119 120 121 122 |
# File 'lib/greenhat/host.rb', line 117 def df_h file = find_thing 'df_hT' return nil unless file file.raw end |
#find_file(file_name) ⇒ Object
20 21 22 |
# File 'lib/greenhat/host.rb', line 20 def find_file(file_name) files.find { |x| x.name == file_name } end |
#find_thing(param) ⇒ Object
28 29 30 |
# File 'lib/greenhat/host.rb', line 28 def find_thing(param) things.find { |x| x.name.include? param } end |
#free_m ⇒ Object
110 111 112 113 114 115 |
# File 'lib/greenhat/host.rb', line 110 def free_m file = find_thing 'free_m' return nil unless file file.raw end |
#gitlab_version ⇒ Object
66 67 68 69 70 71 |
# File 'lib/greenhat/host.rb', line 66 def gitlab_version file = find_thing 'gitlab/version-manifest.json' return nil unless file file.data.build_version end |
#icon ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/greenhat/host.rb', line 32 def icon release = find_thing('etc/os-release').data['ID_LIKE'] if release.include? 'ubuntu' 'fa-ubuntu' elsif release.include? 'suse' 'fa-suse' elsif release.include? 'redhat' 'fa-redhat' elsif release.include? 'centos' 'fa-centos' else 'fa-linux' end end |
#netstat ⇒ Object
124 125 126 127 128 129 |
# File 'lib/greenhat/host.rb', line 124 def netstat file = find_thing 'netstat' return nil unless file file.raw end |
#percent(value, total) ⇒ Object
Helpers
51 52 53 |
# File 'lib/greenhat/host.rb', line 51 def percent(value, total) (value.to_i / total.to_f).round(2) * 100 end |
#processes ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/greenhat/host.rb', line 145 def processes file = find_thing 'ps' return nil unless file headers = file.raw.first.split(' ', 11) list = file.raw[1..].each.map do |row| row.split(' ', 11).each_with_index.with_object({}) do |(v, i), obj| obj[headers[i]] = v end end { headers: headers, list: list } end |
#systemctl_color(entry) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/greenhat/host.rb', line 55 def systemctl_color(entry) case entry.status when 'enabled' then :green when 'static' then :orange when 'disabled' then :red else :grey end end |
#systemctl_unit_files ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/greenhat/host.rb', line 158 def systemctl_unit_files file = find_thing 'systemctl_unit_files' return nil unless file all = file.raw[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 |
#things ⇒ Object
24 25 26 |
# File 'lib/greenhat/host.rb', line 24 def things archive.things end |
#total_memory ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/greenhat/host.rb', line 102 def total_memory file = find_thing 'free_m' return nil unless file value = file.raw.dig(1, 1).to_i number_to_human_size(value * 1024 * 1024) end |
#ulimit ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/greenhat/host.rb', line 131 def ulimit file = find_thing 'ulimit' return nil unless file results = file.raw.map do |entry| { value: entry.split[-1], details: entry.split(' ').first } end results.sort_by { |x| x[:details].downcase } end |
#uname ⇒ Object
80 81 82 83 84 85 |
# File 'lib/greenhat/host.rb', line 80 def uname file = find_thing 'uname' return nil unless file file.raw.join end |
#uptime ⇒ Object
73 74 75 76 77 78 |
# File 'lib/greenhat/host.rb', line 73 def uptime file = find_thing 'uptime' return nil unless file file.raw end |