Class: Sensu::Server::Tessen

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu/server/tessen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tessen

Create a new instance of Tessen. The instance variable ‘@timers` is use to track EventMachine timers for stopping/shutdown.

Parameters:

  • options (Hash) (defaults to: {})

    containing the Sensu server Settings, Logger, and Redis connection.



16
17
18
19
20
21
22
# File 'lib/sensu/server/tessen.rb', line 16

def initialize(options={})
  @timers = []
  @settings = options[:settings]
  @logger = options[:logger]
  @redis = options[:redis]
  @options = @settings.to_hash.fetch(:tessen, {})
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/sensu/server/tessen.rb', line 7

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/sensu/server/tessen.rb', line 7

def options
  @options
end

#redisObject

Returns the value of attribute redis.



7
8
9
# File 'lib/sensu/server/tessen.rb', line 7

def redis
  @redis
end

#settingsObject

Returns the value of attribute settings.



7
8
9
# File 'lib/sensu/server/tessen.rb', line 7

def settings
  @settings
end

#timersObject (readonly)

Returns the value of attribute timers.



8
9
10
# File 'lib/sensu/server/tessen.rb', line 8

def timers
  @timers
end

Instance Method Details

#create_dataHash

Create data to be sent to the Tessen service.

Returns:

  • (Hash)


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
95
96
97
98
99
100
101
102
# File 'lib/sensu/server/tessen.rb', line 69

def create_data
  get_install_id do |install_id|
    get_client_count do |client_count|
      get_server_count do |server_count|
        identity_key = @options.fetch(:identity_key, "")
        flavour, version = get_version_info
        timestamp = Time.now.to_i
        data = {
          :tessen_identity_key => identity_key,
          :install => {
            :id => install_id,
            :sensu_flavour => flavour,
            :sensu_version => version
          },
          :metrics => {
            :points => [
              {
                :name => "client_count",
                :value => client_count,
                :timestamp => timestamp
              },
              {
                :name => "server_count",
                :value => server_count,
                :timestamp => timestamp
              }
            ]
          }
        }
        yield data
      end
    end
  end
end

#enabled?TrueClass, FalseClass

Determine if Tessen is enabled (opt-in).

Returns:

  • (TrueClass, FalseClass)


27
28
29
30
31
32
33
34
35
# File 'lib/sensu/server/tessen.rb', line 27

def enabled?
  enabled = @options[:enabled] == true
  unless enabled
    note = "tessen collects anonymized data to help inform the sensu team about installations"
    note << " - you can opt-in via configuration: {\"tessen\": {\"enabled\": true}}"
    @logger.info("the tessen call-home mechanism is not enabled", :note => note)
  end
  enabled
end

#get_client_count {|count| ... } ⇒ Object

Get the Sensu client count for the installation. This count currently includes proxy clients.

Yields:

  • (count)

Yield Parameters:

  • client (Integer)

    count



120
121
122
123
124
# File 'lib/sensu/server/tessen.rb', line 120

def get_client_count
  @redis.scard("clients") do |count|
    yield count.to_i
  end
end

#get_install_idObject

Get the Sensu installation ID. The ID is randomly generated and stored in Redis. This ID provides context and allows multiple Sensu servers to report data for the same installation.



107
108
109
110
111
112
113
# File 'lib/sensu/server/tessen.rb', line 107

def get_install_id
  @redis.setnx("tessen:install_id", rand(36**12).to_s(36)) do |created|
    @redis.get("tessen:install_id") do |install_id|
      yield install_id
    end
  end
end

#get_server_count {|count| ... } ⇒ Object

Get the Sensu server count for the installation.

Yields:

  • (count)

Yield Parameters:

  • server (Integer)

    count



130
131
132
133
134
# File 'lib/sensu/server/tessen.rb', line 130

def get_server_count
  @redis.scard("servers") do |count|
    yield count.to_i
  end
end

#get_version_infoObject

Get the Sensu version info for the local Sensu service.



137
138
139
140
141
142
143
# File 'lib/sensu/server/tessen.rb', line 137

def get_version_info
  if defined?(Sensu::Enterprise::VERSION)
    ["enterprise", Sensu::Enterprise::VERSION]
  else
    ["core", Sensu::VERSION]
  end
end

#runObject

Run Tessen, scheduling data reports (every 6h).



38
39
40
# File 'lib/sensu/server/tessen.rb', line 38

def run
  schedule_data_reports
end

#schedule_data_reportsObject

Schedule data reports, sending data to the Tessen service immediately and then every 6 hours after that.



52
53
54
55
56
57
# File 'lib/sensu/server/tessen.rb', line 52

def schedule_data_reports
  send_data
  @timers << EM::PeriodicTimer.new(21600) do
    send_data
  end
end

#send_data(&block) ⇒ Object

Send data to the Tessen service.



60
61
62
63
64
# File 'lib/sensu/server/tessen.rb', line 60

def send_data(&block)
  create_data do |data|
    tessen_api_request(data, &block)
  end
end

#stopObject

Stop Tessen, cancelling and clearing timers.



43
44
45
46
47
48
# File 'lib/sensu/server/tessen.rb', line 43

def stop
  @timers.each do |timer|
    timer.cancel
  end
  @timers.clear
end

#tessen_api_request(data) ⇒ Object

Make a Tessen service API request.

Parameters:

  • data (Hash)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sensu/server/tessen.rb', line 148

def tessen_api_request(data)
  @logger.debug("sending data to the tessen call-home service", {
    :data => data,
    :options => @options
  })
  connection = {}
  connection[:proxy] = @options[:proxy] if @options[:proxy]
  post_options = {:body => Sensu::JSON.dump(data)}
  http = EM::HttpRequest.new("https://tessen.sensu.io/v1/data", connection).post(post_options)
  http.callback do
    @logger.debug("tessen call-home service response", :status => http.response_header.status)
    yield if block_given?
  end
  http.errback do
    @logger.debug("tessen call-home service error", :error => http.error)
    yield if block_given?
  end
end