summaryrefslogtreecommitdiff
path: root/lib/custodian/alerts/graphite.rb
blob: cca5ece179c7176dc55aa6759265987b99fa987d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
require 'socket'
require 'timeout'

#
#  The graphite-alerter.
#
#  This only exists to record timing durations in the local
# graphite/carbon instance.  Updates are sent via UDP
# to localhost:2003.
#
module Custodian

  module Alerter

    class GraphiteAlert < AlertFactory

      #
      # The test this alerter cares about
      #
      attr_reader :test


      #
      # Constructor - save the test-object away.
      #
      def initialize(obj)
        @test = obj
      end



      #
      # NOP.
      #
      def raise
      end



      #
      # NOP.
      #
      def clear
      end



      #
      # Send the test test-duration to graphite/carbon
      #
      def duration(ms)

        #
        # hostname + test-type
        #
        host = @test.target.gsub(/[\/\\.]/, '_')
        test = @test.get_type

        #
        # The payload
        #
        payload = "custodian.#{test}.#{host}.test_duration_ms #{ms} #{Time.now.to_i}"

        #
        #  Send metrics via TCP.
        #
        begin
          Timeout.timeout(10) do
            begin
              socket = TCPSocket.new(@target,2003)
              puts payload
              socket.write(payload)
              socket.flush
              socket.close
            rescue Errno::ENETUNREACH
              puts("Metrics host unreachable: #{e}")
            rescue StandardError => e
              puts("Error submitting metrics: #{e}")
            end
          end
        rescue Timeout::Error
          puts('Timeout submitting metrics')
        end
      end

      register_alert_type 'graphite'


    end
  end
end