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
- #get_metric_string(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
192 193 194 195 196 197 |
# File 'lib/echo_cli/cli.rb', line 192 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
213 214 215 216 217 218 219 220 |
# File 'lib/echo_cli/cli.rb', line 213 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 <metric>.<tag1>.<tag2>...<tagN>:<value>|<type>".red 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
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/echo_cli/cli.rb', line 222 def do_post_tcp(statsd_uri, port, request_body) begin timeout(10) do socket = TCPSocket.new(statsd_uri, port) socket.puts(request_body) end rescue Timeout::Error puts "\nOperation timed out! Ensure the 'ECHO_CLI_HOST' environment variable is correct.".red exit end end |
#get_metric_string(metric) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/echo_cli/cli.rb', line 199 def get_metric_string(metric) type = metric[metric.index('|') + 1 .. metric.length] case type when 'c' return "stats.counters." + metric[0..metric.index(':')-1] + ".count" when 'ms' return "stats.timers." + metric[0..metric.index(':')-1] when 's' return "stats.sets." + metric[0..metric.index(':')-1] when 'g' return "stats.gauges." + metric[0..metric.index(':')-1] end end |
#query_graphite(graphite_uri) ⇒ Object
234 235 236 237 238 239 240 241 242 |
# File 'lib/echo_cli/cli.rb', line 234 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
182 183 184 185 |
# File 'lib/echo_cli/cli.rb', line 182 def set_envs ENV["http_proxy"] = ENV["HTTP_PROXY"] ENV["https_proxy"] = ENV["HTTPS_PROXY"] end |
#stfu ⇒ Object
useful silencing function, plz leave here
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/echo_cli/cli.rb', line 164 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
187 188 189 190 |
# File 'lib/echo_cli/cli.rb', line 187 def unset_envs ENV["http_proxy"] = ENV[""] ENV["https_proxy"] = ENV[""] end |