lib/cloudstack-nagios/commands/nagios_config.rb in cloudstack-nagios-0.7.8 vs lib/cloudstack-nagios/commands/nagios_config.rb in cloudstack-nagios-0.8.0

- old
+ new

@@ -1,117 +1,87 @@ class NagiosConfig < CloudstackNagios::Base + TEMPLATE_DIR = File.join(File.dirname(__FILE__), '..', 'templates') + class_option :bin_path, desc: "absolute path to the nagios-cloudstack binary" - desc "generate_all", "generate all nagios configs" - def generate_all - commands = %w(router_hosts router_services api_hosts storage_pool_services capacity_services asyncjobs_services) - commands.each do |command| - invoke "nagios_config:#{command}" - puts - end - end - - desc "router_hosts", "generate nagios hosts configuration for virtual routers" - option :template, - desc: "path of ERB template to use", - default: File.join(File.dirname(__FILE__), '..', 'templates', 'cloudstack_router_hosts.cfg.erb'), + class_option :template, + desc: "Path of ERB template to use. Only valid when generating a single configuration", aliases: '-t' - def router_hosts - host_template = load_template(options[:template]) - puts host_template.result( - routers: cs_routers, - date: date_string - ) - end - desc "router_services", "generate nagios services configuration for system vms" - option :template, - desc: "path of ERB template to use", - default: File.join(File.dirname(__FILE__), '..', 'templates', 'cloudstack_router_services.cfg.erb'), - aliases: '-t' - option :if_speed, + class_option :if_speed, desc: 'network interface speed in bits per second', type: :numeric, default: 1000000, aliases: '-s' - def router_services - service_template = load_template(options[:template]) - puts service_template.result( - routers: cs_routers, - bin_path: bin_path, - if_speed: options[:if_speed], - config_file: options[:config], - date: date_string - ) - end - desc "api_hosts", "generate nagios api host configuration" - option :template, - desc: "path of ERB template to use", - default: File.join(File.dirname(__FILE__), '..', 'templates', 'cloudstack_api_hosts.cfg.erb'), - aliases: '-t' - def api_hosts - service_template = load_template(options[:template]) - puts service_template.result( - zones: client.list_zones, - bin_path: bin_path, - config_file: options[:config], + desc "generate [type]", "generate all nagios configs" + def generate(*configs) + configs = get_configs(configs) + if configs.size == 0 + say "Please specify a valid configuration.", :green + say "Possible values are..." + invoke "nagios_config:list", [] + exit + end + + routers = configs.include?("router_hosts") ? cs_routers : nil + pools = configs.include?("storage_pools") ? storage_pools : nil + zones = client.list_zones + config_name = configs.size == 1 ? + "#{configs[0]} configuration" : + "all configurations" + + header = load_template(File.join(TEMPLATE_DIR, "header.cfg.erb")) + output = header.result( + config_name: config_name, date: date_string ) - end - - desc "storage_pool_services", "generate nagios storage pool services configuration" - option :template, - desc: "path of ERB template to use", - default: File.join(File.dirname(__FILE__), '..', 'templates', 'cloudstack_storage_pool_services.cfg.erb'), - aliases: '-t' - option :over_provisioning, type: :numeric, default: 1.0 - def storage_pool_services - service_template = load_template(options[:template]) - storage_pools = client.list_storage_pools.select do |pool| - pool['state'].downcase == 'up' + configs.each do |config| + if configs.size == 1 && options[:template] + tmpl_file = options[:template] + else + tmpl_file = File.join(TEMPLATE_DIR, "cloudstack_#{config}.cfg.erb") + end + template = load_template(tmpl_file) + output += template.result( + routers: routers, + bin_path: bin_path, + if_speed: options[:if_speed], + config_file: options[:config], + zones: zones, + capacity_types: Capacity::CAPACITY_TYPES, + storage_pools: pools, + over_provisioning: options[:over_provisioning], + ) end - puts service_template.result( - storage_pools: storage_pools, - over_provisioning: options[:over_provisioning], - bin_path: bin_path, - config_file: options[:config], - date: date_string + footer = load_template(File.join(TEMPLATE_DIR, "footer.cfg.erb")) + output += footer.result( + config_name: config_name ) + puts output end - desc "capacity_services", "generate nagios capacity services configuration" - option :template, - desc: "path of ERB template to use", - default: File.join(File.dirname(__FILE__), '..', 'templates', 'cloudstack_capacity_services.cfg.erb'), - aliases: '-t' - def capacity_services - service_template = load_template(options[:template]) - puts service_template.result( - zones: client.list_zones, - capacity_types: Capacity::CAPACITY_TYPES, - bin_path: bin_path, - config_file: options[:config], - date: date_string - ) + desc "list", "show a list of possible configurations which can be generated." + def list + configs = get_configs + puts ["all"] << configs end - desc "asyncjobs_services", "generate nagios capacity services configuration" - option :template, - desc: "path of ERB template to use", - default: File.join(File.dirname(__FILE__), '..', 'templates', 'cloudstack_asyncjobs_services.cfg.erb'), - aliases: '-t' - def asyncjobs_services - service_template = load_template(options[:template]) - puts service_template.result( - bin_path: bin_path, - config_file: options[:config], - date: date_string - ) - end - no_commands do + + def get_configs(configs = []) + all_configs = %w(hostgroup zone_hosts router_hosts router_services capacities async_jobs storage_pools) + if configs.size == 0 + return all_configs + else + if configs.size == 1 && configs[0].downcase == "all" + return all_configs + end + return all_configs.select { |config| configs.include? config } + end + end + def date_string Time.new.strftime("%d.%m.%Y - %H:%M:%S") end def bin_path \ No newline at end of file