#!/usr/bin/env ruby RUBIX_ROOT = File.expand_path('../../lib', __FILE__) $: << RUBIX_ROOT unless $:.include?(RUBIX_ROOT) require 'rubix' require 'configliere' Settings.use :commandline Settings.use :env_var Settings.define :pretty, :description => "Pretty print output", :required => true, :type => :boolean, :default => false Settings.define :url, :description => "URL for the Zabbix API", :required => true, :default => 'http://localhost/api_jsonrpc.php' Settings.define :username, :description => "Username for the Zabbix API", :required => true, :env_var => "ZABBIX_API_USERNAME", :default => 'admin' Settings.define :password, :description => "Password for the Zabbix API", :required => true, :env_var => "ZABBIX_API_PASSWORD", :default => 'zabbix' Settings.define :server_error_sleep, :description => "Seconds to sleep after a 50x error from the server", :required => true, :type => Integer, :default => 1 def Settings.usage %Q{usage: #{raw_script_name} [...--param=val...] [--shell || METHOD PARAMS]} end def bail msg=nil $stderr.puts msg if msg exit(1) end def connect Rubix.connect(Settings[:url], Settings[:username], Settings[:password]) end def perform_query begin method = Settings.rest[0] params = JSON.parse(Settings.rest[1]) response = Rubix.connection.request(method, params) if Settings[:pretty] puts JSON.pretty_generate(response.parsed) else puts response.parsed.to_json end exit(0) rescue JSON::ParserError => e bail("Invalid JSON -- #{e.message}") rescue Rubix::Error => e bail($e.message) end end def drop_into_a_shell require 'irb' ARGV.clear include Rubix IRB.start end begin Settings.resolve! rescue RuntimeError => e Settings.dump_help bail(e.message) end begin connect rescue Rubix::Error => e bail("Could not connect to Zabbix API: #{e.message}") end if Settings.rest.size < 2 drop_into_a_shell else perform_query end