Last active
May 26, 2017 13:41
-
-
Save ecancino/7e1bf0816837531bdcff to your computer and use it in GitHub Desktop.
Create heatmap json for Apache Ambari
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'optparse' | |
| class OptParse | |
| # Return a dictionary describing the options. | |
| # | |
| def self.parse(args) | |
| # The options specified on the command line will be collected in *options*. | |
| # We set default values here. | |
| options = {} | |
| options[:hosts] = 10 | |
| options[:racks] = 3 | |
| opt_parser = OptionParser.new do |opts| | |
| opts.banner = "Usage: example.rb [options]" | |
| opts.separator "" | |
| opts.separator "Specific options:" | |
| opts.on("-h N", OptionParser::DecimalInteger, "Hosts") do |n| | |
| options[:hosts] = n | |
| end | |
| opts.on("-r R", OptionParser::DecimalInteger, "Racks") do |n| | |
| options[:racks] = n | |
| end | |
| end | |
| opt_parser.parse!(args) | |
| options | |
| end # parse() | |
| end # class OptParse | |
| class HostsHeatmap | |
| # Return a hosts document | |
| # | |
| def self.generate(h, r) | |
| oses = ['rhel5', 'rhel6', 'centos5', 'centos6', 'suse11'] | |
| metrics = { | |
| :cpu => 8.0, | |
| :disk => 242.884, | |
| :memory => 8013824 | |
| } | |
| comps = ['APP_TIMELINE_SERVER', 'DATANODE', 'GANGLIA_MONITOR', 'GANGLIA_SERVER','HDFS_CLIENT','HISTORYSERVER','MAPREDUCE2_CLIENT','NAGIOS_SERVER','NAMENODE','NODEMANAGER','RESOURCEMANAGER','SECONDARY_NAMENODE','TEZ_CLIENT','YARN_CLIENT','ZOOKEEPER_CLIENT','ZOOKEEPER_SERVER'] | |
| header = '{"href":"localhost", "items": [' | |
| body = '' | |
| footer = ']}' | |
| host_template = %| | |
| { | |
| "Hosts":{ | |
| "host_name":"%{rack}-%{host}", | |
| "ip":"127.0.1.%{host}", | |
| "os_type":"%{os}", | |
| "public_host_name":"%{rack}-%{host}.org", | |
| "rack_info":"/%{rack}" | |
| }, | |
| "metrics":{ | |
| "cpu":{ | |
| "cpu_system":%{cpu_system}, | |
| "cpu_user":%{cpu_user} | |
| }, | |
| "disk":{ | |
| "disk_free":%{disk_free}, | |
| "disk_total":%{disk_total} | |
| }, | |
| "memory":{ | |
| "mem_free":%{memory_free}, | |
| "mem_total":%{memory_total} | |
| } | |
| }, | |
| "host_components":[ | |
| %{host_components} | |
| ] | |
| },| | |
| component_template = %|{"HostRoles":{"component_name":"%{component}"}},| | |
| ### Generate rack_ids | |
| racks = r.times.map{ (0...8).map { ('a'..'z').to_a[rand(26)] }.join } | |
| ### Generate hosts | |
| (1..h).each do |n| | |
| host = n.to_s | |
| rack = racks.sample | |
| os = oses.sample | |
| cpu_system = rand(metrics[:cpu]) | |
| cpu_user = rand(metrics[:cpu]) | |
| disk_total = rand(metrics[:disk]) | |
| disk_free = rand(disk_total) | |
| memory_total = rand(metrics[:memory]) | |
| memory_free = rand(memory_total) | |
| host_components = '' | |
| components = comps.sample(rand(comps.length)) | |
| components.each do |component| | |
| host_components << component_template % { :component => component } | |
| end | |
| ### Remove last comma | |
| host_components = host_components[0...-1] | |
| ### Fill the document | |
| body << host_template % { | |
| :host => host, | |
| :rack => rack, | |
| :os => os, | |
| :cpu_system => cpu_system, | |
| :cpu_user => cpu_user, | |
| :disk_free => disk_free, | |
| :disk_total => disk_total, | |
| :memory_free => memory_free, | |
| :memory_total => memory_total, | |
| :host_components => host_components | |
| } | |
| end | |
| ### Remove last comma | |
| body = body[0...-1] | |
| ### Get full document | |
| json = header << body << footer | |
| json | |
| end # generate() | |
| end # class HostsHeatmap | |
| options = OptParse.parse(ARGV) | |
| print HostsHeatmap.generate(options[:hosts], options[:racks]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment