class CdnManagerPcacheProbe::Cli

Public Class Methods

exit_on_failure?() click to toggle source

exit with return code 1 in case of an error

# File lib/cdn_manager_pcache_probe/cli.rb, line 18
def self.exit_on_failure?
  true
end

Public Instance Methods

collect() click to toggle source
# 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
version() click to toggle source
# File lib/cdn_manager_pcache_probe/cli.rb, line 23
def version
  say "cdn_manager_pcache_probe v#{VERSION}"
end