Class: SnapmonSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/snapmon_setup.rb

Constant Summary collapse

SERVER =
'www.snapmon.com'

Instance Method Summary collapse

Constructor Details

#initializeSnapmonSetup

Returns a new instance of SnapmonSetup.



7
8
9
# File 'lib/snapmon_setup.rb', line 7

def initialize
	load_config
end

Instance Method Details

#disableObject



54
55
56
57
58
59
60
61
# File 'lib/snapmon_setup.rb', line 54

def disable
	upload_config
	resp = http_post("http://#{SERVER}/host_monitors/disable", {:data => @config.to_json, :api => @config['api-key']})

	if resp.strip != 'ok'
		puts resp
	end		
end

#enableObject



45
46
47
48
49
50
51
52
# File 'lib/snapmon_setup.rb', line 45

def enable
	upload_config
	resp = http_post("http://#{SERVER}/host_monitors/enable", {:data => @config.to_json, :api => @config['api-key']})

	if resp.strip != 'ok'
		puts resp
	end
end

#http_post(url, data) ⇒ Object



40
41
42
43
# File 'lib/snapmon_setup.rb', line 40

def http_post(url, data)
	res = Net::HTTP.post_form(URI.parse(url), data)
	return res.body
end

#load_configObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/snapmon_setup.rb', line 11

def load_config
	if defined?(RAILS_ROOT)
		file_name = "#{RAILS_ROOT}/config/snapmon.yml"
	else
		file_name = File.join(File.dirname(__FILE__), '../../../../config/snapmon.yml')
	end
	if File.exists?(file_name)
		begin
			@config = YAML::load(File.open(file_name).read)
		rescue Exception => e
			puts "Snapmon.yml config file is invalid"
		end
	end
end

#upload_configObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/snapmon_setup.rb', line 26

def upload_config
	begin
		puts "Updating monitoring setup on Snapmon.com"
		resp = http_post("http://#{SERVER}/host_monitors/create_from_config", {:data => @config.to_json, :api => @config['api-key']})
	
		if resp.strip != 'ok'
			puts resp
		end
	rescue Exception => e
		puts "Unable to update SnapMon, invalid config file"
	end
end