Class: Zabby::Runner

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/zabby/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Runner

Returns a new instance of Runner.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/zabby/runner.rb', line 75

def initialize &block
  @config = Zabby::Config.new &block
  @connection = Zabby::Connection.new
  @pure_binding = instance_eval "binding"
  @zobject = {} # List of Zabbix Object

  if Object.const_defined?(:Readline)
    @readline = true
    Readline.basic_word_break_characters = ""
 Readline.completion_append_character = nil
 #Readline.completion_proc = completion_proc

 $last_res = nil
  else
    @readline = false
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/zabby/runner.rb', line 18

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



18
19
20
# File 'lib/zabby/runner.rb', line 18

def connection
  @connection
end

Class Method Details

.create_zobject(name, zmethods) ⇒ Object

Create a method mapping a Zabbix object.

Parameters:

  • name (Symbol)

    Zabbix Object name (:item, :host, etc.)

  • zmethods (Array)

    The list of supported verbs



43
44
45
46
47
# File 'lib/zabby/runner.rb', line 43

def self.create_zobject(name, zmethods)
  define_method(name) do
    @zobject[name] ||= Zabby::ZObject.new(name, zmethods)
  end
end

Instance Method Details

#logged_in?Boolean Also known as: loggedin?

Returns:

  • (Boolean)


117
118
119
# File 'lib/zabby/runner.rb', line 117

def logged_in?
  @connection.logged_in?
end

#loginObject



109
110
111
# File 'lib/zabby/runner.rb', line 109

def 
  @connection.(@config)
end

#logoutObject



113
114
115
# File 'lib/zabby/runner.rb', line 113

def logout
  @connection.logout
end

#run(command_file = nil, &block) ⇒ Object

Parameters:

  • command_file (String) (defaults to: nil)

    Filename containing commands to execute.

  • block (Proc)

    A block containing commands to execute.



128
129
130
131
132
133
134
# File 'lib/zabby/runner.rb', line 128

def run(command_file = nil, &block)
  unless command_file.nil?
    commands = File.read(command_file)
    instance_eval(commands, command_file, 1)
  end
  instance_eval(&block) if block_given?
end

#set(key_value = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/zabby/runner.rb', line 97

def set(key_value = nil)
  if key_value.nil?
    @config.list
  elsif [ String, Symbol ].include?(key_value.class)
    puts "#{key_value} = #{@config.send(key_value)}"
  elsif key_value.instance_of? Hash
    key = key_value.keys.first
    value = key_value[key]
    @config.send(key, value)
  end
end

#setup(&block) ⇒ Object



93
94
95
# File 'lib/zabby/runner.rb', line 93

def setup &block
  @config.setup &block
end

#shellObject

Execute an irb like shell in which we can type Zabby commands.

Raises:

  • (RuntimeError)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/zabby/runner.rb', line 137

def shell
  raise RuntimeError.new("Shell cannot run because 'readline' is missing.") if !@readline

  puts "Zabby Shell #{Zabby::VERSION}"
  puts
  puts "** This is a simple irb like Zabbix Shell. Multiline commands do not work for e.g. **"
  loop do
    cmd = Readline.readline('zabby> ')

    exit(0) if cmd.nil? or cmd == 'exit'
    next if cmd == ""
    Readline::HISTORY.push(cmd)

    execute(cmd)
  end
end

#versionObject



122
123
124
# File 'lib/zabby/runner.rb', line 122

def version
  Zabby::VERSION
end