Class: Echo_cli::Helper
- Inherits:
-
Object
- Object
- Echo_cli::Helper
- Defined in:
- lib/echo_cli/cli.rb
Instance Method Summary collapse
- #check_for_IP ⇒ Object
- #check_metric_format(metric) ⇒ Object
- #do_post_tcp(statsd_uri, port, request_body) ⇒ Object
- #do_post_udp(statsd_uri, port, request_body) ⇒ Object
- #get_metric_string(metric) ⇒ Object
- #get_metric_string_populate(metric) ⇒ Object
- #query_graphite(graphite_uri) ⇒ Object
- #set_envs ⇒ Object
-
#stfu ⇒ Object
useful silencing function, plz leave here.
- #unset_envs ⇒ Object
Instance Method Details
#check_for_IP ⇒ Object
251 252 253 254 255 256 |
# File 'lib/echo_cli/cli.rb', line 251 def check_for_IP if !ENV["ECHO_CLI_HOST"] || ENV["ECHO_CLI_HOST"] == '' puts "\nFailed to find IP or URL. Please set the environment variable: ".red + "'ECHO_CLI_HOST'".yellow + " to continue.".red exit end end |
#check_metric_format(metric) ⇒ Object
286 287 288 289 290 291 292 293 |
# File 'lib/echo_cli/cli.rb', line 286 def check_metric_format(metric) if /.*(:\d*\.?\d*)\|(ms|c|g|s)/.match(metric).to_s != metric puts "\nThe posted metric was not of the correct form.\nMetrics must be of the form ".red + "<Application_name>.<tag1>.<tag2>...<tag(n)>:<value>|<metric_type>".yellow puts "Where <value> is a number, and <type> is any of the following: 'ms', 'c', 's', 'g'.".red return false end return true end |
#do_post_tcp(statsd_uri, port, request_body) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/echo_cli/cli.rb', line 305 def do_post_tcp(statsd_uri, port, request_body) begin timeout(10) do socket = TCPSocket.new(statsd_uri, port) socket.write(request_body + "\n") socket.close end rescue Timeout::Error puts "\nOperation timed out! Ensure the 'ECHO_CLI_HOST' environment variable is correct.".red exit rescue Errno::ECONNREFUSED end end |
#do_post_udp(statsd_uri, port, request_body) ⇒ Object
295 296 297 298 299 300 301 302 303 |
# File 'lib/echo_cli/cli.rb', line 295 def do_post_udp(statsd_uri, port, request_body) begin udpsocket = UDPSocket.new udpsocket.connect(statsd_uri, port) udpsocket.write(request_body) udpsocket.close rescue Errno::ECONNREFUSED end end |
#get_metric_string(metric) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/echo_cli/cli.rb', line 258 def get_metric_string(metric) type = metric[metric.index('|') + 1 .. metric.length] case type when 'c' return metric[0..metric.index(':')-1] + ".*" when 'ms' return metric[0..metric.index(':')-1] + ".*" when 's' return metric[0..metric.index(':')-1] + ".*" when 'g' return metric[0..metric.index(':')-1] end end |
#get_metric_string_populate(metric) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/echo_cli/cli.rb', line 272 def get_metric_string_populate(metric) type = metric[metric.index('|') + 1 .. metric.length] case type when 'c' return metric[0..metric.index(':')-1] + ".count" when 'ms' return metric[0..metric.index(':')-1] + ".count" when 's' return metric[0..metric.index(':')-1] + ".count" when 'g' return metric[0..metric.index(':')-1] end end |
#query_graphite(graphite_uri) ⇒ Object
319 320 321 322 323 324 325 326 327 |
# File 'lib/echo_cli/cli.rb', line 319 def query_graphite(graphite_uri) uri = URI.parse(graphite_uri) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = false https.verify_mode = OpenSSL::SSL::VERIFY_NONE if https.use_ssl? req = Net::HTTP::Get.new(uri.request_uri, initheader = {'Content-Type' => 'text/plain'}, ) res = https.request(req) return res end |
#set_envs ⇒ Object
241 242 243 244 |
# File 'lib/echo_cli/cli.rb', line 241 def set_envs ENV["http_proxy"] = ENV["HTTP_PROXY"] ENV["https_proxy"] = ENV["HTTPS_PROXY"] end |
#stfu ⇒ Object
useful silencing function, plz leave here
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/echo_cli/cli.rb', line 223 def stfu begin orig_stderr = $stderr.clone orig_stdout = $stdout.clone $stderr.reopen File.new('/dev/null', 'w') $stdout.reopen File.new('/dev/null', 'w') retval = yield rescue Exception => e $stdout.reopen orig_stdout $stderr.reopen orig_stderr raise e ensure $stdout.reopen orig_stdout $stderr.reopen orig_stderr end retval end |
#unset_envs ⇒ Object
246 247 248 249 |
# File 'lib/echo_cli/cli.rb', line 246 def unset_envs ENV["http_proxy"] = ENV[""] ENV["https_proxy"] = ENV[""] end |