Class: CdnManagerPcacheProbe::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/cdn_manager_pcache_probe/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

exit with return code 1 in case of an error

Returns:

  • (Boolean)


18
19
20
# File 'lib/cdn_manager_pcache_probe/cli.rb', line 18

def self.exit_on_failure?
  true
end

Instance Method Details

#collectObject



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
# File 'lib/cdn_manager_pcache_probe/cli.rb', line 57

def collect
  bytes_sent = CdnManagerPcacheProbe::Connector.new(
    options[:url]
  ).get_probe(options)
  usage = (bytes_sent / (options[:interval] * 60) * 8 / 1024**2).round(3)

  # post result to the cdn-manager-api
  if options[:api_base_url]
    RestClient.log = STDOUT if options[:debug]
    begin
      RestClient.post(
        File.join(
          options[:api_base_url], "api/v1",
          "services", options[:service_name],
          "metrics", options[:metric_name],
          usage.round(0).to_s
        ),
        { key: options[:api_key] }
      )
    rescue => e
      say "Error posting metric: #{e.message}", :red
    end
  end

  if options[:quiet]
    puts usage.round(0)
  else
    if options[:key]
      say "Usage for #{options[:key]} ", :yellow
    else
      say "Overall usage ", :yellow
    end
    say "over #{options[:interval]} min (#{options[:delay]} min delay):", :yellow
    puts "Total volume sent: #{(bytes_sent / 1024**3).round(3)} GB"
    print "Average bandwidth: #{usage} Mbit/s"
    puts usage >= 1000 ? " (#{(usage / 1024.0).round(2)} Gbit/s)" : nil
  end
end

#versionObject



23
24
25
# File 'lib/cdn_manager_pcache_probe/cli.rb', line 23

def version
  say "cdn_manager_pcache_probe v#{VERSION}"
end