Class: ZabbixNudge::Nudge
- Inherits:
-
Object
- Object
- ZabbixNudge::Nudge
- Defined in:
- lib/zabbix_nudge.rb
Instance Method Summary collapse
-
#initialize(templates, options = {}) ⇒ Nudge
constructor
A new instance of Nudge.
- #send(items = :all) ⇒ Object
- #template_files(templates) ⇒ Object
- #template_items(templates) ⇒ Object
Constructor Details
#initialize(templates, options = {}) ⇒ Nudge
Returns a new instance of Nudge.
19 20 21 22 23 24 25 26 27 |
# File 'lib/zabbix_nudge.rb', line 19 def initialize(templates, = {} ) [:zabbix_server_name] = 'localhost' unless [:zabbix_server_name] [:zabbix_server_port] = '10051' unless [:zabbix_server_port] [:sender_hostname] = Socket.gethostname unless [:sender_hostname] @options = @templates = template_files(templates) @items = template_items(@templates) end |
Instance Method Details
#send(items = :all) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/zabbix_nudge.rb', line 57 def send(items = :all) return if @items.nil? nudgers = ( items == :all) ? ( @items.keys.map{ |key| "ZabbixNudge::#{key.to_s.camelize}".constantize }) : ["ZabbixNudge::#{items.camelize}".constantize] processed_items = Hash.new nudgers.map do |nudger| nudger_key = nudger.to_s.demodulize.underscore.to_sym processed_items.update nudger.new(@items[nudger_key],@options[nudger_key]).send(:processed_items) end zbx = Zabbix::Sender::Buffer.new :host => @options[:zabbix_server_name], :port => @options[:zabbix_server_port] processed_items.each do |key,value| zbx.append key, value, :host => @options[:sender_hostname] end zbx.flush processed_items end |
#template_files(templates) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/zabbix_nudge.rb', line 29 def template_files(templates) template_files = [] template_files = Dir.glob(File.join(templates,'*.xml')) if !templates.is_a?(Array) && File.directory?(templates) template_files = templates.map{ |template| template if File.exist?(template) }.compact if templates.is_a?(Array) template_files = [templates] if !templates.is_a?(Array) && File.exist?(templates) && !File.directory?(templates) template_files end |
#template_items(templates) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/zabbix_nudge.rb', line 37 def template_items(templates) parsed_items = Hash.new templates.each do |template| template_items = Nokogiri::XML(File.open(template)) items = template_items.xpath('//item').map {|item| item.attributes['key'].text}.compact items.each do |item| parts = item.match(/([^\[]+)\[([^\]]+)/) unless parts.nil? key = parts[1].underscore.to_sym attributes = parts[2] (parsed_items[key]) ? parsed_items[key] << attributes : parsed_items[key] = [attributes] else puts "not evaluating: "+item end end end parsed_items end |