Skip to content

Instantly share code, notes, and snippets.

@mooperd
Created February 13, 2015 14:49
Show Gist options
  • Save mooperd/67e18225b5af4dcb45e1 to your computer and use it in GitHub Desktop.
Save mooperd/67e18225b5af4dcb45e1 to your computer and use it in GitHub Desktop.
[root@ip-172-31-13-115 plugins]# vim metrics-curl.rb
-bash: vim: command not found
[root@ip-172-31-13-115 plugins]# vi metrics-curl.rb
[root@ip-172-31-13-115 plugins]# chmod +x metrics-curl.rb
[root@ip-172-31-13-115 plugins]# ./metrics-curl.rb
./metrics-curl.rb:38: syntax error, unexpected ':', expecting kEND
short: '-u URL',
^
./metrics-curl.rb:38: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:39: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:40: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:44: syntax error, unexpected ':', expecting kEND
short: '-a "CURL ARGS"',
^
./metrics-curl.rb:44: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:45: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:46: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:50: syntax error, unexpected ':', expecting kEND
description: 'Metric naming scheme, text t...
^
./metrics-curl.rb:50: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:51: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:52: syntax error, unexpected ',', expecting kEND
./metrics-curl.rb:53: Can't assign to true
./metrics-curl.rb:54: syntax error, unexpected ':', expecting '='
default: "#{Socket.gethostname}.curl_timings"
#! /usr/bin/env ruby
#
# metrics-curl
#
# DESCRIPTION:
# Simple wrapper around curl for getting timing stats from the various phases
# of connecting to an HTTP/HTTPS server.
#
# OUTPUT:
# metric data
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: socket
#
# USAGE:
# #YELLOW
#
# NOTES:
# Based on: http://dev.nuclearrooster.com/2009/12/07/quick-download-benchmarks-with-curl/
# by Nick Stielau.
#
# LICENSE:
# Copyright 2012 Joe Miller
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'socket'
require 'sensu-plugin/metric/cli'
class CurlMetrics < Sensu::Plugin::Metric::CLI::Graphite
option :url,
short: '-u URL',
long: '--url URL',
description: 'valid cUrl url to connect',
default: 'http://127.0.0.1:80/'
option :curl_args,
short: '-a "CURL ARGS"',
long: '--curl_args "CURL ARGS"',
description: 'Additional arguments to pass to curl',
default: ''
option :scheme,
description: 'Metric naming scheme, text to prepend to metric',
short: '-s SCHEME',
long: '--scheme SCHEME',
required: true,
default: "#{Socket.gethostname}.curl_timings"
def run
cmd = "curl --silent --output /dev/null #{config[:curl_args]} "
cmd += '-w "%{time_total},%{time_namelookup},%{time_connect},%{time_pretransfer},%{time_redirect},%{time_starttransfer}" '
cmd += config[:url]
output = `#{cmd}`
(time_total, time_namelookup, time_connect, time_pretransfer, time_redirect, time_starttransfer) = output.split(',')
output "#{config[:scheme]}.time_total", time_total
output "#{config[:scheme]}.time_namelookup", time_namelookup
output "#{config[:scheme]}.time_connect", time_connect
output "#{config[:scheme]}.time_pretransfer", time_pretransfer
output "#{config[:scheme]}.time_redirect", time_redirect
output "#{config[:scheme]}.time_starttransfer", time_starttransfer
ok
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment