riemann-tools-dgvz 0.2.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Kyle Kingsbury
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,18 @@
1
+ Riemann Tools
2
+ =============
3
+
4
+ Tiny programs to submit events to Riemann.
5
+
6
+ Riemann-health, for example, submits events about the current CPU, load,
7
+ memory, and disk use. Bench submits randomly distributed metrics for load
8
+ testing. I've got a whole bunch of these internally for monitoring Redis, Riak,
9
+ queues, etc. Most have internal configuration dependencies, so it'll be a while
10
+ before I can extract them for re-use.
11
+
12
+ Get started
13
+ ==========
14
+
15
+ ``` bash
16
+ gem install riemann-tools
17
+ riemann-health --host my.riemann.server
18
+ ```
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Collects Apache metrics and submits them to Riemann
4
+ # More information can be found at http://httpd.apache.org/docs/2.4/mod/mod_status.html
5
+
6
+ # Removes whitespace from 'Total Accesses' and 'Total kBytes' for output to graphite
7
+
8
+ require File.expand_path('../../lib/riemann/tools', __FILE__)
9
+
10
+ class Riemann::Tools::ApacheStatus
11
+ include Riemann::Tools
12
+ require 'net/http'
13
+ require 'uri'
14
+
15
+ opt :uri, 'Apache Server Status URI', :default => 'http://localhost/server-status'
16
+
17
+ def initialize
18
+ @uri = URI.parse(opts[:uri]) + '?auto'
19
+ # Sample Response with ExtendedStatus On
20
+ # Total Accesses: 20643
21
+ # Total kBytes: 36831
22
+ # CPULoad: .0180314
23
+ # Uptime: 43868
24
+ # ReqPerSec: .470571
25
+ # BytesPerSec: 859.737
26
+ # BytesPerReq: 1827.01
27
+ # BusyWorkers: 6
28
+ # IdleWorkers: 94
29
+ # Scoreboard: ___K_____K____________W_
30
+
31
+ @scoreboard_map = { '_' => 'waiting', 'S' => 'starting', 'R' => 'reading', 'W' => 'sending',
32
+ 'K' => 'keepalive', 'D' => 'dns', 'C' => 'closing', 'L' => 'logging', 'G' => 'graceful',
33
+ 'I' => 'idle', '.' => 'open' }
34
+ end
35
+
36
+
37
+ def get_scoreboard_metrics(response)
38
+ results = Hash.new(0)
39
+
40
+ response.slice! 'Scoreboard: '
41
+ response.each_char do |char|
42
+ results[char] += 1
43
+ end
44
+ Hash[results.map { |k, v| [@scoreboard_map[k], v] }]
45
+ end
46
+
47
+ def report_metrics(metrics)
48
+ metrics.each do |k, v|
49
+ report(
50
+ :service => "httpd #{k}",
51
+ :metric => v.to_f,
52
+ :state => 'ok',
53
+ :tags => ['httpd']
54
+ )
55
+ end
56
+ end
57
+
58
+ def get_connection
59
+ response = nil
60
+ begin
61
+ response = Net::HTTP.get(@uri)
62
+ rescue => e
63
+ report(
64
+ :service => 'httpd health',
65
+ :state => 'critical',
66
+ :description => 'Httpd connection error: #{e.class} - #{e.message}',
67
+ :tags => ['httpd']
68
+ )
69
+ else
70
+ report(
71
+ :service => 'httpd health',
72
+ :state => 'ok',
73
+ :description => 'Httpd connection status ok',
74
+ :tags => ['httpd']
75
+ )
76
+ end
77
+ response
78
+ end
79
+
80
+ def tick
81
+ unless (response = get_connection).nil?
82
+ response.each_line do |line|
83
+ metrics = Hash.new
84
+
85
+ if line =~ /Scoreboard/
86
+ metrics = get_scoreboard_metrics(line.strip)
87
+ else
88
+ key, value = line.strip.split(':')
89
+ metrics[key.gsub(/\s/, '')] = value
90
+ end
91
+ report_metrics(metrics)
92
+ end
93
+ end
94
+ end
95
+
96
+ end
97
+
98
+ Riemann::Tools::ApacheStatus.run
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fog'
3
+
4
+
5
+ require File.expand_path('../../lib/riemann/tools', __FILE__)
6
+
7
+ $0 = __FILE__
8
+
9
+ class Riemann::Tools::AWSBilling
10
+ include Riemann::Tools
11
+
12
+ opt :fog_credentials_file, "Fog credentials file", :type => String
13
+ opt :fog_credential, "Fog credentials to use", :type => String
14
+
15
+ opt :access_key, "AWS access key", :type => String
16
+ opt :secret_key, "Secret access key", :type => String
17
+ opt :services, "AWS services: AmazonEC2 AmazonS3 AWSDataTransfer", :type => :strings, :multi => true, :default => ["AmazonEC2", "AmazonS3", "AWSDataTransfer"]
18
+
19
+ opt :time_start, "Start time in seconds of the metrics period (2hrs ago default)", :type => Integer, :default => 7200
20
+ opt :time_end, "End time in seconds of the metrics period ", :type => Integer, :default => 60
21
+
22
+
23
+ def initialize
24
+ if options[:fog_credentials_file]
25
+ Fog.credentials_path = opts[:fog_credentials_file]
26
+ Fog.credential = opts[:fog_credential].to_sym
27
+ @cloudwatch = Fog::AWS::CloudWatch.new
28
+ else
29
+ @cloudwatch = Fog::AWS::CloudWatch.new(:aws_secret_access_key => opts[:secret_key], :aws_access_key_id => opts[:access_key])
30
+ @start_time = (Time.now.utc - opts[:time_start]).iso8601
31
+ @end_time = (Time.now.utc - opts[:time_end]).iso8601
32
+ end
33
+ end
34
+
35
+ def tick
36
+ opts[:services].each do |service|
37
+ data = @cloudwatch.get_metric_statistics({
38
+ 'Statistics' => ["Maximum"],
39
+ 'StartTime' => @start_time,
40
+ 'EndTime' => @end_time,
41
+ 'Period' => 3600,
42
+ 'Unit' => "None",
43
+ 'MetricName' => "EstimatedCharges",
44
+ 'Namespace' => "AWS/Billing",
45
+ 'Dimensions' => [
46
+ {
47
+ 'Name' => "ServiceName",
48
+ 'Value' => service
49
+ },
50
+ {
51
+ 'Name' => "Currency",
52
+ 'Value' => "USD"
53
+ }
54
+ ]
55
+ }).body['GetMetricStatisticsResult']['Datapoints']
56
+
57
+
58
+ data.each do |metrics|
59
+ name = "AWScloudwatch.Billing." + service
60
+ value = metrics["Maximum"]
61
+ timestamp = metrics["Timestamp"].to_i
62
+
63
+ event = {
64
+ host: nil,
65
+ service: name,
66
+ time: timestamp,
67
+ description: "AWS Estimate Charges for #{service}",
68
+ tags: ["aws_billing"],
69
+ state: "ok",
70
+ metric: value
71
+ }
72
+
73
+ report event
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ Riemann::Tools::AWSBilling.run
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'fog'
4
+ require 'date'
5
+
6
+ require File.expand_path('../../lib/riemann/tools', __FILE__)
7
+
8
+ $0 = __FILE__ # Let's not expose our AWS keys in the process list
9
+
10
+ class Riemann::Tools::AWS
11
+ include Riemann::Tools
12
+
13
+ opt :access_key, "AWS access key", :type => String
14
+ opt :secret_key, "Secret access key", :type => String
15
+ opt :region, "AWS region", :type => String, :default => 'eu-west-1'
16
+
17
+ opt :retirement_critical, "Number of days before retirement. Defaults to 2", :default => 2
18
+ opt :event_warning, "Number of days before event. Defaults to nil (i.e. when the event appears)", :default => nil
19
+
20
+ def initialize
21
+ @compute = Fog::Compute.new(:aws_access_key_id => opts[:access_key],
22
+ :aws_secret_access_key => opts[:secret_key],
23
+ :region => opts[:region],
24
+ :provider => 'AWS')
25
+ end
26
+
27
+ def tick
28
+ instance_status = @compute.describe_instance_status.body["instanceStatusSet"]
29
+ status = instance_status.inject({}) do |acc,i|
30
+ acc[i.delete("instanceId")] = i
31
+ acc
32
+ end
33
+
34
+ hosts = @compute.servers.select { |s| s.state == "running" }.
35
+ inject([status, {}]) do |(status, acc), host|
36
+ acc[host.private_dns_name] = status.delete(host.id); [status, acc]
37
+ end[1]
38
+
39
+ hosts.each do |host, status|
40
+ status['eventsSet'].each do |event|
41
+ before, after = ['notBefore', 'notAfter'].map { |k| Date.parse event[k].to_s if event[k] }
42
+
43
+ ev = {:host => host,
44
+ :service => "aws_instance_status",
45
+ :description => "#{event['code']}\n\nstart #{event['notBefore']}\nend #{event['notAfter']}\n\n#{event['description']}",
46
+ :state => "ok",
47
+ :ttl => 300}
48
+
49
+ ev2 = if (event['code'] == 'instance-retirement') and
50
+ Date.today >= before-opts[:retirement_critical]
51
+ {:state => "critical"}
52
+ elsif opts[:event_warning] and Date.today >= before-opts[:event_warning]
53
+ {:state => "warning"}
54
+ else
55
+ {:state => "warning"}
56
+ end
57
+
58
+ report ev.merge(ev2)
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ Riemann::Tools::AWS.run
data/bin/riemann-bench ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Connects to a server (first arg) and populates it with a constant stream of
4
+ # events for testing.
5
+
6
+ require 'rubygems'
7
+ require 'riemann/client'
8
+ require 'pp'
9
+
10
+ class Riemann::Bench
11
+ attr_accessor :client, :hosts, :services, :states
12
+ def initialize
13
+ @hosts = [nil] + (0...10).map { |i| "host#{i}" }
14
+ @hosts = %w(a b c d e f g h i j)
15
+ @services = %w(test1 test2 test3 foo bar baz xyzzy attack cat treat)
16
+ @states = {}
17
+ @client = Riemann::Client.new(:host => (ARGV.first || 'localhost'))
18
+ end
19
+
20
+ def evolve(state)
21
+ m = state[:metric] + (rand - 0.5) * 0.1
22
+ m = [[0,m].max, 1].min
23
+
24
+ s = case m
25
+ when 0...0.75
26
+ 'ok'
27
+ when 0.75...0.9
28
+ 'warning'
29
+ when 0.9..1.0
30
+ 'critical'
31
+ end
32
+
33
+ {
34
+ :metric => m,
35
+ :state => s,
36
+ :host => state[:host],
37
+ :service => state[:service],
38
+ :description => "at #{Time.now}"
39
+ }
40
+ end
41
+
42
+ def tick
43
+ # pp @states
44
+ hosts.product(services).each do |id|
45
+ client << (states[id] = evolve(states[id]))
46
+ end
47
+ end
48
+
49
+ def run
50
+ start
51
+ loop do
52
+ sleep 0.05
53
+ tick
54
+ end
55
+ end
56
+
57
+ def start
58
+ hosts.product(services).each do |host, service|
59
+ states[[host, service]] = {
60
+ :metric => 0.5,
61
+ :state => 'ok',
62
+ :description => "Starting up",
63
+ :host => host,
64
+ :service => service
65
+ }
66
+ end
67
+ end
68
+ end
69
+
70
+ Riemann::Bench.new.run
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Gathers load balancer statistics from Cloudant.com (shared cluster) and submits them to Riemann.
4
+
5
+ require File.expand_path('../../lib/riemann/tools', __FILE__)
6
+
7
+ class Riemann::Tools::Cloudant
8
+ include Riemann::Tools
9
+ require 'net/http'
10
+ require 'json'
11
+
12
+ opt :cloudant_username, "Cloudant username", :type => :string, :required => true
13
+ opt :cloudant_password, "Cloudant pasword", :type => :string, :required => true
14
+
15
+ def tick
16
+ json = JSON.parse(get_json().body)
17
+ json.each do |node|
18
+ return if node['svname'] == 'BACKEND' # this is just a sum of all nodes.
19
+
20
+ ns = "cloudant #{node['pxname']}"
21
+ cluster_name = node['tracked'].split('.')[0] # ie: meritage.cloudant.com
22
+
23
+ # report health of each node.
24
+ report(
25
+ :service => ns,
26
+ :state => (node['status'] == 'UP' ? 'ok' : 'critical'),
27
+ :tags => ['cloudant', cluster_name]
28
+ )
29
+
30
+ # report property->metric of each node.
31
+ node.each do |property, metric|
32
+ unless ['pxname', 'svname', 'status', 'tracked'].include?(property)
33
+ report(
34
+ :host => node['tracked'],
35
+ :service => "#{ns} #{property}",
36
+ :metric => metric.to_f,
37
+ :state => (node['status'] == 'UP' ? 'ok' : 'critical'),
38
+ :tags => ['cloudant', cluster_name]
39
+ )
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ def get_json
47
+ http = Net::HTTP.new('cloudant.com', 443)
48
+ http.use_ssl = true
49
+ http.start do |h|
50
+ get = Net::HTTP::Get.new('/api/load_balancer')
51
+ get.basic_auth opts[:cloudant_username], opts[:cloudant_password]
52
+ h.request get
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ Riemann::Tools::Cloudant.run
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ require 'rubygems'
4
+ require 'riemann/tools'
5
+
6
+ class Riemann::Tools::Diskstats
7
+ include Riemann::Tools
8
+
9
+ opt :devices, "Devices to monitor", :type => :strings, :default => nil
10
+ opt :ignore_devices, "Devices to ignore", :type => :strings, :default =>nil
11
+
12
+ def initialize
13
+ @old_state = nil
14
+ end
15
+
16
+ def state
17
+ f = File.read('/proc/diskstats')
18
+ state = f.split("\n").reject { |d| d =~ /(ram|loop)/ }.inject({}) do |s, line|
19
+ if line =~ /^(?:\s+\d+){2}\s+([\w\d\-]+) (.*)$/
20
+ dev = $1
21
+
22
+ ['reads reqs',
23
+ 'reads merged',
24
+ 'reads sector',
25
+ 'reads time',
26
+ 'writes reqs',
27
+ 'writes merged',
28
+ 'writes sector',
29
+ 'writes time',
30
+ 'io reqs',
31
+ 'io time',
32
+ 'io weighted'
33
+ ].map do |service|
34
+ "#{dev} #{service}"
35
+ end.zip(
36
+ $2.split(/\s+/).map { |str| str.to_i }
37
+ ).each do |service, value|
38
+ s[service] = value
39
+ end
40
+ end
41
+
42
+ s
43
+ end
44
+
45
+ # Filter interfaces
46
+ if is = opts[:devices]
47
+ state = state.select do |service, value|
48
+ is.include? service.split(' ').first
49
+ end
50
+ end
51
+
52
+ if ign = opts[:ignore_devices]
53
+ state = state.reject do |service, value|
54
+ ign.include? service.split(' ').first
55
+ end
56
+ end
57
+
58
+ state
59
+ end
60
+
61
+ def tick
62
+ state = self.state
63
+
64
+ if @old_state
65
+ state.each do |service, metric|
66
+ delta = metric - @old_state[service]
67
+
68
+ report(
69
+ :service => "diskstats " + service,
70
+ :metric => (delta.to_f / opts[:interval]),
71
+ :state => "ok"
72
+ )
73
+
74
+ if service =~ /io time$/
75
+ report(:service => "diskstats " + service.gsub(/time/, 'util'),
76
+ :metric => (delta.to_f / (opts[:interval]*1000)),
77
+ :state => "ok")
78
+ end
79
+ end
80
+ end
81
+
82
+ @old_state = state
83
+ end
84
+ end
85
+
86
+ Riemann::Tools::Diskstats.run
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path('../../lib/riemann/tools', __FILE__)
4
+
5
+ class Riemann::Tools::Elasticsearch
6
+ include Riemann::Tools
7
+
8
+ require 'faraday'
9
+ require 'json'
10
+ require 'uri'
11
+
12
+ opt :read_timeout, 'Faraday read timeout', type: :int, default: 2
13
+ opt :open_timeout, 'Faraday open timeout', type: :int, default: 1
14
+ opt :es_host, 'Elasticsearch host', default: "localhost"
15
+ opt :es_port, 'Elasticsearch port', type: :int, default: 9200
16
+
17
+
18
+ # Handles HTTP connections and GET requests safely
19
+ def safe_get(uri)
20
+ # Handle connection timeouts
21
+ response = nil
22
+ begin
23
+ connection = Faraday.new(uri)
24
+ response = connection.get do |req|
25
+ req.options[:timeout] = options[:read_timeout]
26
+ req.options[:open_timeout] = options[:open_timeout]
27
+ end
28
+ rescue => e
29
+ report(:host => uri.host,
30
+ :service => "elasticsearch health",
31
+ :state => "critical",
32
+ :description => "HTTP connection error: #{e.class} - #{e.message}"
33
+ )
34
+ end
35
+ response
36
+ end
37
+
38
+ def health_url
39
+ "http://#{options[:es_host]}:#{options[:es_port]}/_cluster/health"
40
+ end
41
+
42
+ def tick
43
+ uri = URI(health_url)
44
+ response = safe_get(uri)
45
+
46
+ return if response.nil?
47
+
48
+ if response.status != 200
49
+ report(:host => uri.host,
50
+ :service => "elasticsearch health",
51
+ :state => "critical",
52
+ :description => "HTTP connection error: #{response.status} - #{response.body}"
53
+ )
54
+ else
55
+ # Assuming that a 200 will give json
56
+ json = JSON.parse(response.body)
57
+ cluster_name = json.delete("cluster_name")
58
+ cluster_status = json.delete("status")
59
+ state = case cluster_status
60
+ when "green"
61
+ "ok"
62
+ when "yellow"
63
+ "warning"
64
+ when "red"
65
+ "critical"
66
+ end
67
+
68
+ report(:host => uri.host,
69
+ :service => "elasticsearch health",
70
+ :state => state,
71
+ :description => "Elasticsearch cluster: #{cluster_name} - #{cluster_status}")
72
+
73
+ json.each_pair do |k,v|
74
+ report(:host => uri.host,
75
+ :service => "elasticsearch #{k}",
76
+ :metric => v,
77
+ :description => "Elasticsearch cluster #{k}"
78
+ )
79
+
80
+ end
81
+ end
82
+ end
83
+
84
+
85
+
86
+ end
87
+ Riemann::Tools::Elasticsearch.run