Class: Bosh::Agent::Heartbeat

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_agent/heartbeat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeartbeat

Returns a new instance of Heartbeat.



10
11
12
13
14
15
# File 'lib/bosh_agent/heartbeat.rb', line 10

def initialize
  @logger   = Config.logger
  @nats     = Config.nats
  @agent_id = Config.agent_id
  @state    = Config.state
end

Instance Attribute Details

#agent_idObject

Mostly for tests so we can override these without touching Config



8
9
10
# File 'lib/bosh_agent/heartbeat.rb', line 8

def agent_id
  @agent_id
end

#loggerObject

Mostly for tests so we can override these without touching Config



8
9
10
# File 'lib/bosh_agent/heartbeat.rb', line 8

def logger
  @logger
end

#natsObject

Mostly for tests so we can override these without touching Config



8
9
10
# File 'lib/bosh_agent/heartbeat.rb', line 8

def nats
  @nats
end

#stateObject

Mostly for tests so we can override these without touching Config



8
9
10
# File 'lib/bosh_agent/heartbeat.rb', line 8

def state
  @state
end

Instance Method Details

#heartbeat_payloadObject

Heartbeat payload example:

"job": "cloud_controller",
"index": 3,
"job_state":"running",
"vitals": {
  "load": ["0.09","0.04","0.01"],
  "cpu": {"user":"0.0","sys":"0.0","wait":"0.4",
  "mem": "percent":"3.5","kb":"145996",
  "swap": "percent":"0.0","kb":"0",
  "disk": {
    "system": => "82",
    "ephemeral": => "5",
    "persistent": => "94"
  },
"ntp": {
    "offset": "-0.06423",
    "timestamp": "14 Oct 11:13:19"
}

}



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bosh_agent/heartbeat.rb', line 55

def heartbeat_payload
  job_state = Bosh::Agent::Monit.service_group_state
  monit_vitals = Bosh::Agent::Monit.get_vitals

  disk_usage = Bosh::Agent::DiskUtil.get_usage

  job_name = @state["job"] ? @state["job"]["name"] : nil
  index = @state["index"]

  vitals = monit_vitals.merge("disk" => disk_usage)

  Yajl::Encoder.encode("job" => job_name,
                       "index" => index,
                       "job_state" => job_state,
                       "vitals" => vitals,
                       "ntp" => Bosh::Agent::NTP.offset)
end

#send_via_mbusObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bosh_agent/heartbeat.rb', line 17

def send_via_mbus
  if @state.nil?
    @logger.error("Unable to send heartbeat: agent state unknown")
    return
  end

  if @nats.nil?
    raise Bosh::Agent::HeartbeatError, "NATS should be initialized in order to send heartbeats"
  end

  @nats.publish("hm.agent.heartbeat.#{@agent_id}", heartbeat_payload) do
    yield if block_given?
    @logger.debug("Heartbeat delivered")
  end
  @logger.info("Heartbeat sent")
end