Class: ZabbixRubyClient
- Inherits:
-
Object
show all
- Defined in:
- lib/zabbix-ruby-client.rb,
lib/zabbix-ruby-client/cli.rb,
lib/zabbix-ruby-client/logger.rb,
lib/zabbix-ruby-client/plugins.rb,
lib/zabbix-ruby-client/version.rb,
lib/zabbix-ruby-client/plugins/cpu.rb,
lib/zabbix-ruby-client/plugins/disk.rb,
lib/zabbix-ruby-client/plugins/apache.rb,
lib/zabbix-ruby-client/plugins/memory.rb,
lib/zabbix-ruby-client/plugins/network.rb
Defined Under Namespace
Modules: Logger, Plugins
Classes: Cli
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ZabbixRubyClient.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/zabbix-ruby-client.rb', line 8
def initialize(config_file)
begin
@config ||= YAML::load_file(config_file)
rescue Exception => e
puts "Configuration file cannot be read"
puts e.message
return
end
@logsdir = makedir(@config['logsdir'],'logs')
@datadir = makedir(@config['datadir'],'data')
@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
|
Instance Method Details
#collect ⇒ Object
54
55
56
57
58
|
# File 'lib/zabbix-ruby-client.rb', line 54
def collect
@config['plugins'].each do |plugin|
run_plugin(plugin['name'], plugin['args'])
end
end
|
#data ⇒ Object
26
27
28
|
# File 'lib/zabbix-ruby-client.rb', line 26
def data
@data ||= []
end
|
#datafile ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/zabbix-ruby-client.rb', line 30
def datafile
now = Time.now
@datafile ||= if @config['keepdata']
unless Dir.exists? File.join(@datadir,Time.now.strftime("%Y-%m-%d"))
FileUtils.mkdir File.join(@datadir,Time.now.strftime("%Y-%m-%d"))
end
File.join(@datadir,Time.now.strftime("%Y-%m-%d"),"data_"+Time.now.strftime("%H%M%S"))
else
File.join(@datadir,"data")
end
end
|
#run_plugin(plugin, args = nil) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/zabbix-ruby-client.rb', line 42
def run_plugin(plugin, args = nil)
Plugins.load(plugin) || logger.error( "Plugin #{plugin} not found.")
if Plugins.loaded[plugin]
begin
@data = data + Plugins.loaded[plugin].send(:collect, @config['host'], args)
rescue Exception => e
logger.fatal "Oops"
logger.fatal e.message
end
end
end
|
#show ⇒ Object
60
61
62
63
64
|
# File 'lib/zabbix-ruby-client.rb', line 60
def show
data.each do |line|
puts line
end
end
|
#store ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/zabbix-ruby-client.rb', line 66
def store
File.open(datafile, "w") do |f|
data.each do |d|
f.puts d
end
end
end
|
#upload ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'lib/zabbix-ruby-client.rb', line 74
def upload
store
begin
res = `#{@config['zabbix']['sender']} -z #{@config['zabbix']['host']} -i #{datafile}`
rescue Exception => e
logger.error "Sending failed."
logger.error e.message
end
end
|