Sha256: ee1c312cc6e220105ce07086f2112267885faed8ec96c017b84e3b2d3db9694f

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require "zabbix-ruby-client/version"
require "zabbix-ruby-client/logger"
require "zabbix-ruby-client/plugins"
require "zabbix-ruby-client/store"
require "zabbix-ruby-client/data"
require "yaml"

class ZabbixRubyClient

  def initialize(config_file, task_file)
    begin
      @config ||= YAML::load_file(config_file)
      if File.exists? task_file
        @tasks ||= YAML::load_file(task_file)
      else
        @tasks = @config["plugins"]
      end
    rescue Exception => e
      puts "Configuration file cannot be read"
      puts e.message
      return
    end

    @store = Store.new(
      @config['datadir'],
      @config['zabbix']['host'],
      File.basename(task_file,'.yml'),
      @config['keepdata']
    )

    @data = Data.new(@config['host'])

    @config["server"] = File.basename(config_file,'.yml')
    @logsdir = makedir(@config['logsdir'],'logs')
    @plugindirs = [ File.expand_path("../zabbix-ruby-client/plugins", __FILE__) ]
    if @config["plugindirs"]
      @plugindirs = @plugindirs + @config["plugindirs"]
    end
    Plugins.load_dirs @plugindirs
    logger.debug @config.inspect
  end

  def collect
    @tasks.each do |plugin|
      @data.run_plugin(plugin['name'], plugin['args'])
    end
  end

  def show
    @data.merge.each do |line|
      puts line
    end
  end

  def upload
    file = @store.record(@data.merge)
    begin
      res = `#{@config['zabbix']['sender']} -z #{@config['zabbix']['host']} -p #{@config['zabbix']['port']} -T -i #{file}`
      if $?.to_i != 0
        @store.keepdata(file)
      end
    rescue Exception => e
      @store.keepdata(file)
      logger.error "Sending failed."
      logger.error e.message
    end
  end

  def logger
    @logger ||= Logger.get_logger(@logsdir, @config["loglevel"])
  end

  private

  def makedir(configdir, defaultdir)
    dir = configdir || defaultdir
    FileUtils.mkdir dir unless Dir.exists? dir
    dir
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zabbix-ruby-client-0.0.15 lib/zabbix-ruby-client.rb