Class: Echo_cli::Echo

Inherits:
Thor
  • Object
show all
Defined in:
lib/echo_cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#convert(timestamp) ⇒ Object



364
365
366
367
368
369
370
371
372
# File 'lib/echo_cli/cli.rb', line 364

def convert(timestamp)
  if timestamp.include? ":"
    time = Time.iso8601(timestamp).to_i.to_s
  else
    time = Time.at(timestamp.to_i).to_datetime.to_s
  end
  puts "\nConverted timstamp: ".green + time.yellow
  return time
end

#cpost(statsd_uri, metric, num = 0) ⇒ Object



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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/echo_cli/cli.rb', line 122

def cpost(statsd_uri, metric, num = 0)

  Helper.new.set_envs()

  uri = URI.parse(statsd_uri)
  request = Net::HTTP::Post.new(uri)
  request["Token"] = "mySecretToken"
  request.body = metric.gsub(/'/,"")

  req_options = {
    use_ssl: uri.scheme == "https",
    verify_mode: OpenSSL::SSL::VERIFY_NONE,
  }

  if(num.to_i > 0)
    s = num.to_i
  else
    s = 10
  end

  if(options[:z])
    pid = Process.fork do
      while true do
        Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
          http.request(request)
        end
        sleep s
      end
    end
    puts "\nProcess ID: ".green + pid.to_s.yellow + "\nUse ".green + "kill -9 ".yellow + pid.to_s.yellow + " to kill the zombie process.".green
  else
    puts "\nPosting: ".green + metric.yellow + " once every ".green + s.to_s.yellow + " seconds".green + " to ".green + statsd_uri.yellow + "\nPress Ctrl-c to kill the process.\n".green
    while true do
      time_start = Time.new.to_i.to_s

      response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
        http.request(request)
      end

      sleep s

      time_end = Time.new.to_i.to_s

      if(options[:v] && response.code == '200')
        puts "\nSuccessfully posted ".green + request.body.yellow + " to ".green + statsd_uri.blue + "\nTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow + "\nPress Ctrl-c to kill the process.\n".green
      end
    end
  end

  Helper.new.unset_envs()

end

#post(statsd_uri, metric, num = 0) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/echo_cli/cli.rb', line 28

def post(statsd_uri, metric, num = 0)

  Helper.new.set_envs()

  uri = URI.parse(statsd_uri)
  request = Net::HTTP::Post.new(uri)
  request["Token"] = "mySecretToken"
  request.body = metric.gsub(/'/,"")

  req_options = {
    use_ssl: uri.scheme == "https",
    verify_mode: OpenSSL::SSL::VERIFY_NONE,
  }

  if(num.to_i > 0)
    begin
      time_start = Time.new.to_i.to_s

      for i in 1..num.to_i
        response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
          http.request(request)
        end
        if response.code != '200'
          puts "Final run at: " + i + " of " + num
          raise
        end
      end

      if options[:q]
        sleep 15 # ensure data reception and replication
      end
      time_end = Time.new.to_i.to_s

      puts "\nSuccessfully posted ".green + request.body.yellow + " to ".green + statsd_uri.blue + " " + num.to_s.green + " time(s)".green + "\nTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow

      if options[:q]
        opentsdb_uri = statsd_uri[0..statsd_uri.index(':8125')-1] + ":4242/api/query"
        query(opentsdb_uri, request.body[0..request.body.index(":")-1], time_start, time_end)
      end

    rescue
      puts "\nFailed to post ".red + request.body.yellow + " to ".red + statsd_uri.blue + " " + num.to_s.red + " time(s)".red
    end

  else
    begin
      time_start = Time.new.to_i.to_s

      response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
        http.request(request)
      end

      if options[:q]
        sleep 15 # ensure data reception and replication
      end

      time_end = Time.new.to_i.to_s

      if response.code == '200'
        puts "\nSuccessfully posted ".green + request.body.yellow + " to ".green + statsd_uri.blue + "\nTime start: ".green + time_start.yellow + ", Time end: ".green + time_end.yellow

        if options[:q]
          opentsdb_uri = statsd_uri[0..statsd_uri.index(':8125')-1] + ":4242/api/query"
          query(opentsdb_uri, request.body[0..request.body.index(":")-1], time_start, time_end)
        end
      end
    rescue
      puts "\nFailed to post ".red + request.body.yellow + " to ".red + statsd_uri.blue
    end
  end
  return response

  Helper.new.unset_envs()
end

#query(opentsdb_uri, metric, time_start, time_end) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/echo_cli/cli.rb', line 201

def query(opentsdb_uri, metric, time_start, time_end)
  metric = Helper.new.get_metric(metric)
  metric_name = Helper.new.get_metric_name(metric)
  tags = Helper.new.check_get_tags(metric)
  opentsdb_query = Helper.new.get_query(time_start, time_end, metric_name, tags)

  Helper.new.set_envs()

  uri = URI.parse(opentsdb_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::Post.new(uri.request_uri, initheader = {'Content-Type' => 'text/plain'}, )
  req.body = opentsdb_query

  if options[:v]
    begin
      res = https.request(req)
      if res.code == '200'

        json = JSON.parse(res.body)[0]

        if !options[:e]
          json["dps"] = {}
          for i in JSON.parse(res.body)[0]["dps"].keys do
            Helper.new.stfu do
              json["dps"][convert(i)] = JSON.parse(res.body)[0]["dps"][i]
            end
          end
        end

        if options[:s]
          sql = "CREATE TABLE " + metric + "(\n  sep VARCHAR(24) NOT NULL PRIMARY KEY\n  ,FIELD2 VARCHAR(21) NOT NULL\n);"
          for i in json["dps"].keys do
            Helper.new.stfu do
              sql = sql + "\nINSERT INTO " + metric + "(sep,FIELD2) VALUES ('" + i.to_s + "','" + json["dps"][i].to_s + "');"
            end
          end

          puts "\nSuccessfully retrieved ".green + metric_name.yellow + " from ".green + opentsdb_uri.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow + "\nSQL-ized data points:\n\n".green + sql.green
        else
          puts "\nSuccessfully retrieved ".green + metric_name.yellow + " from ".green + opentsdb_uri.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow + "\nData points:\n\n".green +  JSON.pretty_generate(json).green
        end
      else
        puts "\nFailed to retrieve ".red + metric_name.yellow + " from ".red + opentsdb_uri.blue + " in timestamp range: ".red + time_start.yellow + " to ".red + time_end.yellow + "\nStatus Code: ".red + res.code.yellow + "\nResponse Body: ".red + JSON.pretty_generate(JSON.parse(res.body)).red
      end
    rescue
      puts "\nYour request returned an error. Are you sure your query was correct?"
    end
  else
    begin
      res = https.request(req)
      if res.code == '200'
        puts "\nSuccessfully retrieved ".green + metric_name.yellow + " from ".green + opentsdb_uri.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow
      else
        puts "\nFailed to retrieve ".red + metric_name.yellow + " from ".red + opentsdb_uri.blue + " in timestamp range: ".red + time_start.yellow + " to ".red + time_end.yellow
      end
    rescue
      puts "\nYour request returned an error. Are you sure your query was correct?"
    end
  end

  Helper.new.unset_envs()

  return res.code
end

#quick_query(opentsdb_uri, metric, range) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/echo_cli/cli.rb', line 291

def quick_query(opentsdb_uri, metric, range)
  time_end = Time.new.to_i
  time_start = time_end - range.to_i
  time_start = time_start.to_s
  time_end = time_end.to_s

  metric = Helper.new.get_metric(metric)
  metric_name = Helper.new.get_metric_name(metric)
  tags = Helper.new.check_get_tags(metric)
  opentsdb_query = Helper.new.get_query(time_start, time_end, metric_name, tags)

  Helper.new.set_envs()

  uri = URI.parse(opentsdb_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::Post.new(uri.request_uri, initheader = {'Content-Type' => 'text/plain'}, )
  req.body = opentsdb_query

  begin
    res = https.request(req)
    if res.code == '200'

      json = JSON.parse(res.body)[0]

      if !options[:e]
        json["dps"] = {}
        for i in JSON.parse(res.body)[0]["dps"].keys do
          Helper.new.stfu do
            json["dps"][convert(i)] = JSON.parse(res.body)[0]["dps"][i]
          end
        end
      end

      if options[:s]
        sql = "CREATE TABLE " + metric + "(\n  sep VARCHAR(24) NOT NULL PRIMARY KEY\n  ,FIELD2 VARCHAR(21) NOT NULL\n);"
        for i in json["dps"].keys do
          Helper.new.stfu do
            sql = sql + "\nINSERT INTO " + metric + "(sep,FIELD2) VALUES ('" + i.to_s + "','" + json["dps"][i].to_s + "');"
          end
        end

        puts "\nSuccessfully retrieved ".green + metric_name.yellow + " from ".green + opentsdb_uri.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow + "\nSQL-ized data points in the last ".green + range.yellow + " seconds:\n\n".green + sql.green
      else
        puts "\nSuccessfully retrieved ".green + metric_name.yellow + " from ".green + opentsdb_uri.blue + " in timestamp range: ".green + time_start.yellow + " to ".green + time_end.yellow + "\nData points in the last ".green + range.yellow + " seconds:\n\n".green +  JSON.pretty_generate(json).green
      end
    else
      puts "\nFailed to retrieve ".red + metric_name.yellow + " from ".red + opentsdb_uri.blue + " in timestamp range: ".red + time_start.yellow + " to ".red + time_end.yellow + "\nStatus Code: ".red + res.code.yellow + "\nResponse Body: ".red + JSON.pretty_generate(JSON.parse(res.body)).red
    end
  rescue
    puts "\nYour request returned an error. Are you sure your query was correct?"
  end

  Helper.new.unset_envs()

  return res.code
end