Class: Host
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Host
- Defined in:
- app/models/host.rb
Instance Method Summary collapse
- #available_disk_space_in_Gb ⇒ Object
- #available_disk_space_in_percent ⇒ Object
- #available_memory_in_Mb ⇒ Object
- #available_memory_in_percent ⇒ Object
- #available_swap_in_Mb ⇒ Object
- #available_swap_in_percent ⇒ Object
- #disk_status ⇒ Object
-
#distance_to_last_heartbeat_in_seconds ⇒ Object
Seconds to timestamp of last entry in ‘loads’ table for this host.
-
#last_contacted_on ⇒ Object
Timestamp of last entry in ‘loads’ table for this host.
- #latest_load_entry ⇒ Object
- #latest_memory_entry ⇒ Object
- #load_10_min ⇒ Object
- #load_15_min ⇒ Object
- #load_5_min ⇒ Object
-
#loads_15_min_grain(start_time, end_time) ⇒ Object
Returns the load for this host in the following format.
-
#loads_5_min_grain(start_time, end_time) ⇒ Object
Load stats.
- #loads_last ⇒ Object
- #memory_stats_15_min_grain(start_time, end_time) ⇒ Object
- #memory_status ⇒ Object
- #status ⇒ Object
- #swap_status ⇒ Object
-
#total_disk_space_in_Gb ⇒ Object
Disk space.
-
#total_memory_in_Mb ⇒ Object
Memory.
-
#total_swap_in_Mb ⇒ Object
Swap.
- #used_memory_in_Mb ⇒ Object
- #used_swap_in_Mb ⇒ Object
Instance Method Details
#available_disk_space_in_Gb ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'app/models/host.rb', line 57 def available_disk_space_in_Gb @available = 0 self.file_systems.each do |fs| @available += fs.available_in_Gb end @available_disk_space_in_Gb ||= @available end |
#available_disk_space_in_percent ⇒ Object
66 67 68 |
# File 'app/models/host.rb', line 66 def available_disk_space_in_percent sprintf('%.2f', available_disk_space_in_Gb.to_f / total_disk_space_in_Gb * 100) end |
#available_memory_in_Mb ⇒ Object
89 90 91 92 93 94 95 |
# File 'app/models/host.rb', line 89 def available_memory_in_Mb if latest_memory_entry latest_memory_entry.mem_available / 1024 else 0 end end |
#available_memory_in_percent ⇒ Object
136 137 138 |
# File 'app/models/host.rb', line 136 def available_memory_in_percent sprintf('%.2f', available_memory_in_Mb.to_f / total_memory_in_Mb * 100) end |
#available_swap_in_Mb ⇒ Object
158 159 160 161 162 163 164 |
# File 'app/models/host.rb', line 158 def available_swap_in_Mb if latest_memory_entry latest_memory_entry.swap_available / 1024 else 0 end end |
#available_swap_in_percent ⇒ Object
174 175 176 |
# File 'app/models/host.rb', line 174 def available_swap_in_percent sprintf('%.2f', available_swap_in_Mb.to_f / total_swap_in_Mb * 100) end |
#disk_status ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'app/models/host.rb', line 70 def disk_status if available_disk_space_in_percent.to_i > 30 STATUS_GREEN elsif available_disk_space_in_percent.to_i > 10 STATUS_YELLOW else STATUS_RED end end |
#distance_to_last_heartbeat_in_seconds ⇒ Object
Seconds to timestamp of last entry in ‘loads’ table for this host
36 37 38 |
# File 'app/models/host.rb', line 36 def distance_to_last_heartbeat_in_seconds Time.now - last_contacted_on if last_contacted_on end |
#last_contacted_on ⇒ Object
Timestamp of last entry in ‘loads’ table for this host
31 32 33 |
# File 'app/models/host.rb', line 31 def last_contacted_on self.latest_load_entry.created_at if self.latest_load_entry end |
#latest_load_entry ⇒ Object
8 9 10 |
# File 'app/models/host.rb', line 8 def latest_load_entry @latest_load_entry ||= self.loads_dataset.order(:created_at).last end |
#latest_memory_entry ⇒ Object
12 13 14 |
# File 'app/models/host.rb', line 12 def latest_memory_entry @latest_memory_entry ||= self.memories_dataset.order(:created_at).last end |
#load_10_min ⇒ Object
261 262 263 |
# File 'app/models/host.rb', line 261 def load_10_min self.latest_load_entry.load_10_min if self.latest_load_entry end |
#load_15_min ⇒ Object
265 266 267 |
# File 'app/models/host.rb', line 265 def load_15_min self.latest_load_entry.load_15_min if self.latest_load_entry end |
#load_5_min ⇒ Object
257 258 259 |
# File 'app/models/host.rb', line 257 def load_5_min self.latest_load_entry.load_5_min if self.latest_load_entry end |
#loads_15_min_grain(start_time, end_time) ⇒ Object
Returns the load for this host in the following format
series will contain the time series data. This method will return the data in 15 min intervals. series will contain the 5 min load averages series will contain the 10 min load averages series will contain the 15 min load averages
If not data exists for a particular time period, then the value is -1
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'app/models/host.rb', line 229 def loads_15_min_grain(start_time, end_time) load = DB.fetch("SELECT ROUND(AVG(load_5_min),1) AS load_5_min, ROUND(AVG(load_10_min),1) AS load_10_min, ROUND(AVG(load_15_min),1) AS load_15_min, grain_15_min FROM loads WHERE host_id = #{self.id} AND grain_15_min >= '#{start_time.to_fifteen_minute_grain_format.to_sql_format}' AND grain_15_min <= '#{end_time.to_fifteen_minute_grain_format.to_sql_format}' GROUP BY grain_15_min;").all time_range = (start_time..end_time) series = [] series[0] = [] series[1] = [] series[2] = [] series[3] = [] i = 0 time_range.step(900) do |fifteen| series[0] << fifteen.to_minute_format if load.size > 0 and load[0][:grain_15_min].to_minute_format == fifteen.to_minute_format load_for_this_dim = load.shift series[1] << load_for_this_dim[:load_5_min].to_f series[2] << load_for_this_dim[:load_10_min].to_f series[3] << load_for_this_dim[:load_15_min].to_f else series[1] << -1 series[2] << -1 series[3] << -1 end end series end |
#loads_5_min_grain(start_time, end_time) ⇒ Object
Load stats
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'app/models/host.rb', line 192 def loads_5_min_grain(start_time, end_time) load = DB.fetch("SELECT ROUND(AVG(load_5_min),1) AS load_5_min, ROUND(AVG(load_10_min),1) AS load_10_min, ROUND(AVG(load_15_min),1) AS load_15_min, grain_5_min FROM loads WHERE host_id = #{self.id} AND grain_5_min >= '#{start_time.to_five_minute_grain_format.to_sql_format}' AND grain_5_min <= '#{end_time.to_five_minute_grain_format.to_sql_format}' GROUP BY grain_5_min;").all time_range = (start_time..end_time) series = [] series[0] = [] series[1] = [] series[2] = [] series[3] = [] time_range.step(300) do |five| series[0] << five.to_minute_format if load.size > 0 and load[0][:grain_5_min].to_minute_format == five.to_minute_format load_for_this_dim = load.shift series[1] << load_for_this_dim[:load_5_min].to_f series[2] << load_for_this_dim[:load_10_min].to_f series[3] << load_for_this_dim[:load_15_min].to_f else series[1] << -1 series[2] << -1 series[3] << -1 end end series end |
#loads_last ⇒ Object
40 41 42 |
# File 'app/models/host.rb', line 40 def loads_last end |
#memory_stats_15_min_grain(start_time, end_time) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/models/host.rb', line 105 def memory_stats_15_min_grain(start_time, end_time) data = DB.fetch("SELECT ROUND(AVG(mem_available)/1024,0) AS mem_available, ROUND(AVG(mem_used)/1024,0) AS mem_used, ROUND(AVG(swap_available)/1024,0) AS swap_available, ROUND(AVG(swap_used)/1024, 0) AS swap_used, grain_15_min FROM memories WHERE host_id = #{self.id} AND grain_15_min >= '#{start_time.to_fifteen_minute_grain_format.to_sql_format}' AND grain_15_min <= '#{end_time.to_fifteen_minute_grain_format.to_sql_format}' GROUP BY grain_15_min;").all time_range = (start_time..end_time) series = [] series[0] = [] series[1] = [] series[2] = [] series[3] = [] series[4] = [] time_range.step(900) do |fifteen| series[0] << fifteen.to_minute_format if data.size > 0 and data[0][:grain_15_min].to_minute_format == fifteen.to_minute_format fact_for_this_dim = data.shift series[1] << fact_for_this_dim[:mem_available] series[2] << fact_for_this_dim[:mem_used] series[3] << fact_for_this_dim[:swap_available] series[4] << fact_for_this_dim[:swap_used] else series[1] << -1 series[2] << -1 series[3] << -1 series[4] << -1 end end series end |
#memory_status ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'app/models/host.rb', line 140 def memory_status if available_memory_in_percent.to_i > 30 STATUS_GREEN elsif available_memory_in_percent.to_i > 10 STATUS_YELLOW else STATUS_RED end end |
#status ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 |
# File 'app/models/host.rb', line 269 def status if self.last_contacted_on == nil STATUS_RED elsif Time.now - self.last_contacted_on < 75 STATUS_GREEN elsif Time.now - self.last_contacted_on < 120 STATUS_YELLOW else STATUS_RED end end |
#swap_status ⇒ Object
178 179 180 181 182 183 184 185 186 |
# File 'app/models/host.rb', line 178 def swap_status if available_swap_in_percent.to_i > 30 STATUS_GREEN elsif available_swap_in_percent.to_i > 10 STATUS_YELLOW else STATUS_RED end end |
#total_disk_space_in_Gb ⇒ Object
Disk space
48 49 50 51 52 53 54 55 |
# File 'app/models/host.rb', line 48 def total_disk_space_in_Gb @total = 0 self.file_systems.each do |fs| @total += fs.size_in_Gb end @total_disk_space_in_Gb ||= @total end |
#total_memory_in_Mb ⇒ Object
Memory
85 86 87 |
# File 'app/models/host.rb', line 85 def total_memory_in_Mb available_memory_in_Mb + used_memory_in_Mb end |
#total_swap_in_Mb ⇒ Object
Swap
154 155 156 |
# File 'app/models/host.rb', line 154 def total_swap_in_Mb available_swap_in_Mb + used_swap_in_Mb end |
#used_memory_in_Mb ⇒ Object
97 98 99 100 101 102 103 |
# File 'app/models/host.rb', line 97 def used_memory_in_Mb if latest_memory_entry latest_memory_entry.mem_used / 1024 else 0 end end |
#used_swap_in_Mb ⇒ Object
166 167 168 169 170 171 172 |
# File 'app/models/host.rb', line 166 def used_swap_in_Mb if latest_memory_entry latest_memory_entry.swap_used / 1024 else 0 end end |