forked from cloudfoundry/bosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_collector.rb
187 lines (149 loc) · 6.31 KB
/
metrics_collector.rb
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require 'db_migrator'
require 'rufus-scheduler'
require 'prometheus/client'
require 'bosh/director/dns/canonicalizer'
module Bosh
module Director
class MetricsCollector
def initialize(config)
@config = config
@logger = config.metrics_server_logger
@deploy_config_enabled = Prometheus::Client.registry.gauge(
:bosh_deploy_config_enabled,
docstring: 'Is a config of type deploy uploaded? 0 for no, 1 for yes',
)
@resurrection_enabled = Prometheus::Client.registry.gauge(
:bosh_resurrection_enabled,
docstring: 'Is resurrection enabled? 0 for disabled, 1 for enabled',
)
@tasks = Prometheus::Client.registry.gauge(
:bosh_tasks_total,
labels: %i[state type],
docstring: 'Number of BOSH tasks',
)
@network_available_ips = Prometheus::Client.registry.gauge(
:bosh_networks_dynamic_ips_total,
labels: %i[name],
docstring: 'Size of network pool for all dynamically allocated IPs',
)
@network_free_ips = Prometheus::Client.registry.gauge(
:bosh_networks_dynamic_free_ips_total,
labels: %i[name],
docstring: 'Number of dynamical free IPs left per network',
)
@unresponsive_agents = Prometheus::Client.registry.gauge(
:bosh_unresponsive_agents,
labels: %i[name],
docstring: 'Number of unresponsive agents per deployment',
)
@scheduler = Rufus::Scheduler.new
end
def prep
ensure_migrations
Bosh::Director::App.new(@config)
end
def start
@logger.info('starting metrics collector')
populate_metrics
@scheduler.interval '30s' do
populate_metrics
end
end
def stop
@logger.info('stopping metrics collector')
@scheduler.shutdown
end
private
def ensure_migrations
if defined?(Bosh::Director::Models)
raise 'Bosh::Director::Models were loaded before ensuring migrations are current. ' \
'Cowardly refusing to start metrics collector.'
end
migrator = DBMigrator.new(@config.db, :director)
unless migrator.finished?
@logger.error(
"Migrations not current during metrics collector start after #{DBMigrator::MAX_MIGRATION_ATTEMPTS} attempts.",
)
raise "Migrations not current after #{DBMigrator::MAX_MIGRATION_ATTEMPTS} retries"
end
require 'bosh/director'
end
def populate_metrics
@logger.info('populating metrics')
@deploy_config_enabled.set(Api::ConfigManager.deploy_config_enabled? ? 1 : 0)
@resurrection_enabled.set(Api::ResurrectorManager.new.pause_for_all? ? 0 : 1)
metrics = { 'processing' => {}, 'queued' => {} }
Models::Task.group_and_count(:type).distinct.each do |task|
metrics['processing'][task.type] = 0
metrics['queued'][task.type] = 0
end
Models::Task.where(state: %w[processing queued]).group_and_count(:type, :state).each do |task|
state, type, count = task.values[:state], task.values[:type], task.values[:count]
metrics[state][type] = count
end
metrics.each do |state, types|
types.each do |type, count|
@tasks.set(count, labels: { state: state, type: type })
end
end
populate_network_metrics
populate_vm_metrics
@logger.info('populated metrics')
end
def populate_vm_metrics
response = Net::HTTP.get_response('127.0.0.1', '/unresponsive_agents', @config.health_monitor_port)
return unless response.is_a?(Net::HTTPSuccess)
unresponsive_agent_counts = JSON.parse(response.body)
return unless unresponsive_agent_counts.is_a?(Hash)
existing_deployment_names = @unresponsive_agents.values.map do |key, _|
# The keys within the Prometheus::Client::Metric#values method are actually hashes. So the
# data returned from values looks like:
# { { name: "deployment_a"} => 10, { name: "deployment_b "} => 0, ... }
key[:name]
end
unresponsive_agent_counts.each do |deployment, count|
@unresponsive_agents.set(count, labels: { name: deployment })
end
removed_deployments = existing_deployment_names - unresponsive_agent_counts.keys
removed_deployments.each do |deployment|
@unresponsive_agents.set(0, labels: { name: deployment })
end
end
def populate_network_metrics
configs = Models::Config.latest_set('cloud')
@network_available_ips.set(0, labels: { name: canonicalize_to_prometheus('no-networks') })
@network_free_ips.set(0, labels: { name: canonicalize_to_prometheus('no-networks') })
return if configs.empty?
consolidated_configs = Bosh::Director::CloudConfig::CloudConfigsConsolidator.new(configs)
networks = DeploymentPlan::CloudManifestParser.new(@logger).parse(consolidated_configs.raw_manifest).networks
networks.flatten!
return if networks.empty?
networks.each do |network|
next unless network.manual?
total, free = calculate_network_metrics(network)
@network_available_ips.set(total, labels: { name: canonicalize_to_prometheus(network.name) })
@network_free_ips.set(free, labels: { name: canonicalize_to_prometheus(network.name) })
end
end
def canonicalize_to_prometheus(label)
# https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
# prometheus supports underscores and not dashes, but dns is reversed
Canonicalizer.canonicalize(label).gsub('-', '_')
end
def calculate_network_metrics(network)
total_available = 0
total_static = 0
total_restricted = 0
total_used = Models::IpAddress.where(network_name: network.name, static: false).count
network.subnets.each do |subnet|
total_static += subnet.static_ips.size
total_restricted += subnet.restricted_ips.size
total_available += subnet.range.size
end
total_available -= total_static
total_available -= total_restricted
[total_available, total_available - total_used]
end
end
end
end