Class: Cloudscale::Monitor::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudscale/monitor/agent/agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Agent

Returns a new instance of Agent.



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
# File 'lib/cloudscale/monitor/agent/agent.rb', line 19

def initialize(token)
  @log = Logger.new(STDOUT)
  @server = Socket.gethostname
  client = RestClientWrapper.instance
  registry = Monitor::Registry.instance

  puts " "
  puts "Running preflight verification for cloudscale monitoring agent"

  if (registry.stored_agent == nil)
    raise "You did not provide any valid Agent file under /store/agent/agent.store."
  end

  agent = client.searchOne('agents', 'findByAgentId', { :agentId => registry.stored_agent["agentId"] })

  if (agent == nil)
    raise "No agent registered yet. It is not allowed to connect this agent instance to cloudscale monitoring."
  end

  if (registry.stored_agent_instance != nil)
    agent_instance = client.searchOne('agentInstances',
      'findByAgentInstanceId', { :agentInstanceId => registry.agent_instance_id })
  end

  if (agent_instance == nil)
    puts "    No Instance of this agent was started previously, starting cloudscale monitoring agent for the first, registering..."

    agent_instance_settings = Monitor::Settings.new(15, 0, 1500, 5)
    agent_instance = Monitor::AgentInstance.new(RestClientWrapper.load_entity_ref(agent)["href"],
      registry.agent_instance_id, token, server, agent_instance_settings, Time.now.to_i * 1000)

    client.post('agentInstances', agent_instance)
    Constants::AgentInstance.create(agent_instance.to_h)
  else
    puts "    Instance has been started previously, loading settings and existing data... \n\n"

    agent_instance["lastInitialization"] = Time.now.to_i * 1000

    client.patch('agentInstances', RestClientWrapper.load_entity_id(agent_instance), agent_instance)
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



17
18
19
# File 'lib/cloudscale/monitor/agent/agent.rb', line 17

def client
  @client
end

#logObject

Returns the value of attribute log.



17
18
19
# File 'lib/cloudscale/monitor/agent/agent.rb', line 17

def log
  @log
end

#registryObject

Returns the value of attribute registry.



17
18
19
# File 'lib/cloudscale/monitor/agent/agent.rb', line 17

def registry
  @registry
end

#serverObject

Returns the value of attribute server.



17
18
19
# File 'lib/cloudscale/monitor/agent/agent.rb', line 17

def server
  @server
end

Class Method Details

.alive(agentInstanceId) ⇒ Object



103
104
105
106
107
# File 'lib/cloudscale/monitor/agent/agent.rb', line 103

def self.alive(agentInstanceId)
  rest_client = RestClientWrapper.instance

  rest_client.get_with_path('agentInstances', agentInstanceId, 'alive')
end

.remove(agentInstanceId) ⇒ Object



70
71
72
73
74
# File 'lib/cloudscale/monitor/agent/agent.rb', line 70

def self.remove(agentInstanceId)
  reset(agentInstanceId)

  Constants::AgentInstance.remove()
end

.remove_agenInstance(agentInstanceId) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cloudscale/monitor/agent/agent.rb', line 91

def self.remove_agenInstance(agentInstanceId)
  rest_client = RestClientWrapper.instance
  puts "Removing AgentInstance completely:\n"

  agent_instance = rest_client.searchOne('agentInstances',
          'findByAgentInstanceId', { :agentInstanceId => agentInstanceId })

  rest_client.delete('agentInstances', RestClientWrapper.load_entity_id(agent_instance))

  puts "    Deleting AgentInstance was successful \n\n"
end

.reset(agentInstanceId) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/cloudscale/monitor/agent/agent.rb', line 61

def self.reset(agentInstanceId)
  resetRestEndpoint('coredatas', agentInstanceId, 'Coredata')
  resetRestEndpoint('tables', agentInstanceId, 'Table')
  resetRestEndpoint('charts', agentInstanceId, 'Chart')
  resetRestEndpoint('menus', agentInstanceId, 'Menu')

  remove_agenInstance(agentInstanceId)
end

.resetRestEndpoint(entityRel, agentInstanceId, entityName) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cloudscale/monitor/agent/agent.rb', line 76

def self.resetRestEndpoint(entityRel, agentInstanceId, entityName)
  rest_client = RestClientWrapper.instance
  entities = rest_client.searchAny(entityRel,"findByAgentInstanceId",
                  { :agentInstanceId => agentInstanceId })

  puts "Found #{entityName} elements for Agent (#{agentInstanceId}): #{entities["content"].length.to_s}"
  puts "    Starting to delete all elements..."

  entities["content"].each do  | entity |
    rest_client.delete(entityRel, RestClientWrapper.load_entity_id(entity))
  end

  puts "    Deleting all elements was successful \n\n"
end