Class: HQ::SmsGlobal::BalanceCheck::Script

Inherits:
Tools::CheckScript
  • Object
show all
Includes:
Tools::Escape
Defined in:
lib/hq/sms-global/balance-check/script.rb

Instance Method Summary collapse

Instance Method Details

#get_balance_from_serverObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hq/sms-global/balance-check/script.rb', line 152

def get_balance_from_server

	url =
		URI.parse "#{@server_elem["url"]}/balance-api.php"

	url.query =
		URI.encode_www_form({
			"user" => @account_elem["username"],
			"password" => @account_elem["password"],
		})

	http =
		Net::HTTP.new url.host, url.port

	http.open_timeout = @opts[:timeout]
	http.read_timeout = @opts[:timeout]

	http.start

	begin

		request = Net::HTTP::Get.new "#{url.path}?#{url.query}"

		response = http.request request

		unless response.code == "200"
			unknown "server status #{response.code}"
			return
		end

		unless response.body =~ /^BALANCE: ([^;]+); USER: (.+)$/
			unknown "invalid server response"
			return
		end

		@actual_balance =
			$1.to_f

	rescue Timeout::Error

		unknown "server timed out"
		return

	ensure

		http.finish

	end

end

#load_configObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hq/sms-global/balance-check/script.rb', line 61

def load_config

	config_doc =
		XML::Document.file @opts[:config]

	@config_elem =
		config_doc.root

	@server_elem =
		@config_elem.find_first "server"

	@cache_elem =
		@config_elem.find_first "cache"

	@account_elem =
		@config_elem.find_first "account [
			@name = #{esc_xp @opts[:account]}
		]"

end

#perform_checksObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hq/sms-global/balance-check/script.rb', line 82

def perform_checks

	get_balance_from_server

	if @actual_balance
		used_cache = false
	elsif @cache_elem
		read_cache
		@unknown = false
		used_cache = true
	else
		return
	end

	# report balance

	if @actual_balance < @opts[:critical]

		critical "%s credits (critical is %s)" % [
			@actual_balance.to_i,
			@opts[:critical].to_i,
		]

	elsif @actual_balance < @opts[:warning]

		warning "%s credits (warning is %s)" % [
			@actual_balance.to_i,
			@opts[:warning].to_i,
		]

	else

		message "%s credits" % [
			@actual_balance.to_i,
		]

	end

	# report cache usage

	if used_cache

		if @opts[:cache_critical] \
			&& @cache_age >= @opts[:cache_critical]

			critical "last check %s seconds ago (critical is %s)" % [
				@cache_age,
				@opts[:cache_critical]
			]

		elsif @opts[:cache_warning] \
			&& @cache_age >= @opts[:cache_warning]

			warning "last check %s seconds ago (warning is %s)" % [
				@cache_age,
				@opts[:cache_warning]
			]

		else

			message "last check #{@cache_age} seconds ago"

		end

	end

	write_cache

end

#prepareObject



55
56
57
58
59
# File 'lib/hq/sms-global/balance-check/script.rb', line 55

def prepare

	load_config

end

#process_argsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hq/sms-global/balance-check/script.rb', line 18

def process_args

	@opts, @args =
		Tools::Getopt.process @args, [

			{ :name => :config,
				:required => true },

			{ :name => :account,
				:required => true },

			{ :name => :warning,
				:convert => :to_f,
				:required => true },

			{ :name => :critical,
				:convert => :to_f,
				:required => true },

			{ :name => :timeout,
				:convert => :to_f,
				:required => true },

			{ :name => :cache_warning,
				:convert => :to_i },

			{ :name => :cache_critical,
				:convert => :to_i },

		]

	raise "Extra args" unless @args.empty?

	@name = "SMS Global #{@opts[:account]}"

end

#read_cacheObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/hq/sms-global/balance-check/script.rb', line 203

def read_cache

	cache_path = "%s/%s.yaml" % [
		@cache_elem["dir"],
		@opts[:account],
	]

	cache_data =
		YAML.load_file cache_path

	@actual_balance =
		cache_data["balance"]

	@cache_age =
		(Time.now - cache_data["timestamp"]).to_i

end

#write_cacheObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/hq/sms-global/balance-check/script.rb', line 221

def write_cache

	return unless @cache_elem

	cache_data = {
		"balance" => @actual_balance,
		"timestamp" => Time.now,
	}

	cache_path = "%s/%s.yaml" % [
		@cache_elem["dir"],
		@opts[:account],
	]

	File.open "#{cache_path}.new", "w" do
		|cache_io|

		cache_io.write YAML.dump cache_data

	end

	FileUtils.mv "#{cache_path}.new", cache_path

end