Class: OneHostHelper
- Inherits:
-
OpenNebulaHelper::OneHelper
- Object
- OpenNebulaHelper::OneHelper
- OneHostHelper
- Defined in:
- lib/one_helper/onehost_helper.rb
Overview
implements onehost command
Constant Summary collapse
- TEMPLATE_XPATH =
'//HOST/TEMPLATE'- VERSION_XPATH =
"#{TEMPLATE_XPATH}/VERSION"- MONITORING =
{ 'FREE_CPU' => 'CAPACITY', 'FREE_CPU_FORECAST' => 'CAPACITY', 'FREE_CPU_FORECAST_FAR' => 'CAPACITY', 'FREE_MEMORY' => 'CAPACITY', 'FREE_MEMORY_FORECAST' => 'CAPACITY', 'FREE_MEMORY_FORECAST_FAR' => 'CAPACITY', 'USED_CPU' => 'CAPACITY', 'USED_CPU_FORECAST' => 'CAPACITY', 'USED_CPU_FORECAST_FAR' => 'CAPACITY', 'USED_MEMORY' => 'CAPACITY', 'USED_MEMORY_FORECAST' => 'CAPACITY', 'USED_MEMORY_FORECAST_FAR' => 'CAPACITY', 'NETRX' => 'SYSTEM', 'NETRX_FORECAST' => 'SYSTEM', 'NETRX_FORECAST_FAR' => 'SYSTEM', 'NETTX' => 'SYSTEM', 'NETTX_FORECAST' => 'SYSTEM', 'NETTX_FORECAST_FARR' => 'SYSTEM' }
- NUM_THREADS =
15
Instance Attribute Summary
Attributes inherited from OpenNebulaHelper::OneHelper
Class Method Summary collapse
Instance Method Summary collapse
- #forceupdate(host_ids, options) ⇒ Object
- #format_pool(options) ⇒ Object
- #monitoring(host, attr, options) ⇒ Object
- #sync(host_ids, options) ⇒ Object
Methods inherited from OpenNebulaHelper::OneHelper
#backup_mode_valid?, #check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, list_layout_help, #list_pool, #list_pool_format, #list_pool_table, #list_pool_top, #list_pool_xml, #list_to_id, list_to_id_desc, name_to_id, #perform_action, #perform_actions, #print_page, #retrieve_resource, #set_client, set_endpoint, set_password, set_user, #show_resource, #start_pager, #stop_pager, table_conf, template_input_help, #to_id, to_id_desc, #user_name
Constructor Details
This class inherits a constructor from OpenNebulaHelper::OneHelper
Class Method Details
.conf_file ⇒ Object
57 58 59 |
# File 'lib/one_helper/onehost_helper.rb', line 57 def self.conf_file 'onehost.yaml' end |
.rname ⇒ Object
53 54 55 |
# File 'lib/one_helper/onehost_helper.rb', line 53 def self.rname 'HOST' end |
.state_to_str(id) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/one_helper/onehost_helper.rb', line 61 def self.state_to_str(id) id = id.to_i state_str = Host::HOST_STATES[id] Host::SHORT_HOST_STATES[state_str] end |
Instance Method Details
#forceupdate(host_ids, options) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/one_helper/onehost_helper.rb', line 355 def forceupdate(host_ids, ) if Process.uid.zero? || Process.gid.zero? STDERR.puts("Cannot run 'onehost forceupdate' as root") exit(-1) end cluster_id = [:cluster] # Get the Host pool filter_flag ||= OpenNebula::Pool::INFO_ALL pool = factory_pool(filter_flag) rc = pool.info return -1, rc. if OpenNebula.is_error?(rc) host_errors = [] pool.each do |host| if host_ids next unless host_ids.include?(host['ID'].to_i) elsif cluster_id next if host['CLUSTER_ID'].to_i != cluster_id end state = host['STATE'] # Skip this host from remote syncing if it's OFFLINE next if Host::HOST_STATES[state.to_i] == 'OFFLINE' rc = host.forceupdate host_errors << host['NAME'] if OpenNebula.is_error?(rc) end if host_errors.empty? puts 'All hosts updated successfully.' 0 else STDERR.puts 'Failed to update the following hosts:' host_errors.each {|h| STDERR.puts "* #{h}" } -1 end end |
#format_pool(options) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/one_helper/onehost_helper.rb', line 68 def format_pool() config_file = self.class.table_conf CLIHelper::ShowTable.new(config_file, self) do column :ID, 'ONE identifier for Host', :size => 4 do |d| d['ID'] end column :NAME, 'Name of the Host', :left, :size => 15 do |d| d['NAME'] end column :CLUSTER, 'Name of the Cluster', :left, :size => 9 do |d| OpenNebulaHelper.cluster_str(d['CLUSTER']) end column :TVM, 'Total Virtual Machines allocated to the Host', :size => 3 do |d| d['HOST_SHARE']['RUNNING_VMS'] || 0 end column :ZVM, 'Number of Virtual Machine zombies', :size => 3 do |d| d['TEMPLATE']['TOTAL_ZOMBIES'] || 0 end column :TCPU, 'Total CPU percentage', :size => 4 do |d| d['HOST_SHARE']['MAX_CPU'] || 0 end column :FCPU, 'Free CPU percentage', :size => 4 do |d| d['HOST_SHARE']['MAX_CPU'].to_i - d['HOST_SHARE']['USED_CPU'].to_i rescue '-' end column :ACPU, 'Available cpu percentage (not reserved by VMs)', :size => 4 do |d| max_cpu = d['HOST_SHARE']['MAX_CPU'].to_i max_cpu = 100 if max_cpu.zero? max_cpu - d['HOST_SHARE']['CPU_USAGE'].to_i end column :TMEM, 'Total Memory', :size => 7 do |d| OpenNebulaHelper.unit_to_str( d['HOST_SHARE']['MAX_MEM'].to_i, ) rescue '-' end column :FMEM, 'Free Memory', :size => 7 do |d| OpenNebulaHelper.unit_to_str( d['HOST_SHARE']['FREE_MEM'].to_i, ) rescue '-' end column :AMEM, 'Available Memory (not reserved by VMs)', :size => 7 do |d| acpu = d['HOST_SHARE']['MAX_MEM'].to_i - d['HOST_SHARE']['MEM_USAGE'].to_i OpenNebulaHelper.unit_to_str(acpu, ) end column :REAL_CPU, 'Real CPU', :size => 18 do |d| max_cpu = d['HOST_SHARE']['MAX_CPU'].to_i if max_cpu != 0 used_cpu = d['HOST_SHARE']['USED_CPU'].to_i ratio = (used_cpu * 100) / max_cpu "#{used_cpu} / #{max_cpu} (#{ratio}%)" else '-' end end column :ALLOCATED_CPU, 'Allocated CPU)', :size => 18 do |d| max_cpu = d['HOST_SHARE']['MAX_CPU'].to_i cpu_usage = d['HOST_SHARE']['CPU_USAGE'].to_i if max_cpu.zero? && cpu_usage.zero? '-' else cpu_usage = d['HOST_SHARE']['CPU_USAGE'].to_i if max_cpu != 0 ratio = (cpu_usage * 100) / max_cpu "#{cpu_usage} / #{max_cpu} (#{ratio}%)" else "#{cpu_usage} / -" end end end column :REAL_MEM, 'Real MEM', :size => 18 do |d| max_mem = d['HOST_SHARE']['MAX_MEM'].to_i if max_mem != 0 used_mem = d['HOST_SHARE']['USED_MEM'].to_i ratio = (used_mem * 100) / max_mem "#{OpenNebulaHelper.unit_to_str(used_mem, )} / "\ "#{OpenNebulaHelper.unit_to_str(max_mem, )} "\ "(#{ratio}%)" else '-' end end column :ALLOCATED_MEM, 'Allocated MEM', :size => 18 do |d| max_mem = d['HOST_SHARE']['MAX_MEM'].to_i mem_usage = d['HOST_SHARE']['MEM_USAGE'].to_i if max_mem.zero? && mem_usage.zero? '-' elsif max_mem != 0 ratio = (mem_usage * 100) / max_mem "#{OpenNebulaHelper.unit_to_str(mem_usage, )} / "\ "#{OpenNebulaHelper.unit_to_str(max_mem, )} "\ "(#{ratio}%)" else "#{OpenNebulaHelper.unit_to_str(mem_usage, )} / -" end end column :STAT, 'Host status', :left, :size => 6 do |d| OneHostHelper.state_to_str(d['STATE']) end default :ID, :NAME, :CLUSTER, :TVM, :ALLOCATED_CPU, :ALLOCATED_MEM, :STAT end end |
#monitoring(host, attr, options) ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/one_helper/onehost_helper.rb', line 400 def monitoring(host, attr, ) unit = [:unit] || 'G' start_d = [:start] end_d = [:end] n_elems = [:n_elems] || 8 # Different available size units units = ['K', 'M', 'G', 'T'] if unit && !units.include?(unit) STDERR.puts "Invalid unit `#{unit}`" exit(-1) end attr = attr.upcase start_d = Time.parse(start_d) if start_d end_d = Time.parse(end_d) if end_d # Get monitoring data from user path # # 0 -> timestamp # 1 -> data retrieved monitoring_data = host.monitoring(["#{MONITORING[attr]}/#{attr}"]) monitoring_data = monitoring_data["#{MONITORING[attr]}/#{attr}"] if monitoring_data.empty? STDERR.puts 'No monitoring data found' return end # Get data max and min date start_d ||= Time.at(monitoring_data.min {|v| v[0].to_i }[0].to_i) end_d ||= Time.at(monitoring_data.max {|v| v[0].to_i }[0].to_i) # Filter data betwen dates monitoring_data.reject! do |v| v[0].to_i < start_d.to_i || v[0].to_i > end_d.to_i end if monitoring_data.empty? STDERR.puts "No monitoring data found between #{start_d} " \ "and #{end_d}" return end start_d = start_d.strftime('%d/%m/%Y %H:%M') end_d = end_d.strftime('%d/%m/%Y %H:%M') # Parse dcollected data x = monitoring_data.collect {|v| Time.at(v[0].to_i).strftime('%H:%M') } y = monitoring_data.collect do |v| if attr.match(/_MEMORY/) # GB is the default unit v = OpenNebulaHelper.bytes_to_unit(v[1].to_i, unit).round(2) "#{v} #{unit}B" else v[1] end end title = '' title << "Host #{host.id} #{attr} " title << "in #{unit}B " if unit && attr.match(/_MEMORY/) title << "from #{start_d} to #{end_d}" x = x.last(n_elems) y = y.last(n_elems) if [:table] print_monitoring_table(x, y, title) elsif [:csv] csv = '' csv << "TIME#{[:csv]}VALUE\n" x.zip(y) {|x_v, y_v| csv << "#{x_v}#{[:csv]}#{y_v}\n" } puts csv else puts OpenNebulaHelper.get_plot(x, y, attr, title) end end |
#sync(host_ids, options) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/one_helper/onehost_helper.rb', line 200 def sync(host_ids, ) if Process.uid.zero? || Process.gid.zero? STDERR.puts("Cannot run 'onehost sync' as root") exit(-1) end begin current_version = File.read(REMOTES_LOCATION + '/VERSION').strip rescue StandardError STDERR.puts("Could not read #{REMOTES_LOCATION}/VERSION") exit(-1) end if current_version.empty? STDERR.puts 'Remotes version can not be empty' exit(-1) end begin current_version = Gem::Version.new(current_version) rescue StandardError STDERR.puts 'VERSION file is malformed, use semantic versioning.' end cluster_id = [:cluster] rc = OpenNebula::System.new(@client).get_configuration return -1, rc. if OpenNebula.is_error?(rc) conf = rc sync_manager = HostSyncManager.new(conf) # Verify the existence of REMOTES_LOCATION if !File.directory? REMOTES_LOCATION error_msg = "'#{REMOTES_LOCATION}' does not exist. " \ 'This command must be run in the frontend.' return -1, error_msg end # Touch the update file FileUtils.touch(File.join(REMOTES_LOCATION, '.update')) # Get the Host pool filter_flag ||= OpenNebula::Pool::INFO_ALL pool = factory_pool(filter_flag) rc = pool.info return -1, rc. if OpenNebula.is_error?(rc) # Assign hosts to threads queue = [] pool.each do |host| if host_ids next unless host_ids.include?(host['ID'].to_i) elsif cluster_id next if host['CLUSTER_ID'].to_i != cluster_id end state = host['STATE'] # Skip this host from remote syncing if it's OFFLINE next if Host::HOST_STATES[state.to_i] == 'OFFLINE' host_version = host['TEMPLATE/VERSION'] begin host_version = Gem::Version.new(host_version) rescue StandardError nil end if ![:force] begin next if host_version && host_version >= current_version rescue StandardError STDERR.puts 'Error comparing versions '\ " for host #{host['NAME']}." end end puts "* Adding #{host['NAME']} to upgrade" queue << host end # Run the jobs in threads host_errors = [] queue_lock = Mutex.new error_lock = Mutex.new total = queue.length if total.zero? puts 'No hosts are going to be updated.' exit(0) end ts = (1..NUM_THREADS).map do |_t| Thread.new do loop do host = nil size = 0 queue_lock.synchronize do host = queue.shift size = queue.length end break unless host print_update_info(total - size, total, host['NAME']) retries = 3 begin copy_method = [:ssh] ? :ssh : :rsync rc = sync_manager.update_remotes(host['NAME'], nil, copy_method) rescue IOError # Workaround for broken Ruby 2.5 # https://github.com/OpenNebula/one/issues/3229 if (retries -= 1) > 0 sleep 1 retry end end if rc != 0 error_lock.synchronize do host_errors << host['NAME'] end else update_version(host, current_version) end end end end # Wait for threads to finish ts.each {|t| t.join } puts if host_errors.empty? puts 'All hosts updated successfully.' 0 else STDERR.puts 'Failed to update the following hosts:' host_errors.each {|h| STDERR.puts "* #{h}" } -1 end end |