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.



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

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.



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

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#redisObject

Returns the value of attribute redis.



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

def redis
  @redis
end

#settingsObject

Returns the value of attribute settings.



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

def settings
  @settings
end

#timersObject (readonly)

Returns the value of attribute timers.



10
11
12
# File 'lib/sensu/server/tessen.rb', line 10

def timers
  @timers
end

Instance Method Details

#create_dataHash

Create data to be sent to the Tessen service.

Returns:

  • (Hash)


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
103
104
# File 'lib/sensu/server/tessen.rb', line 71

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)


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

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



122
123
124
125
126
# File 'lib/sensu/server/tessen.rb', line 122

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.



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

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



132
133
134
135
136
# File 'lib/sensu/server/tessen.rb', line 132

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.



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

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).



40
41
42
# File 'lib/sensu/server/tessen.rb', line 40

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.



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

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.



62
63
64
65
66
# File 'lib/sensu/server/tessen.rb', line 62

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

#stopObject

Stop Tessen, cancelling and clearing timers.



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

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)


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

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