module DiplomaticBag
Usefull usage of Diplomat lib.
Usefull usage of Diplomat lib: Datacenter functions
Usefull usage of Diplomat lib: Info functions
Usefull usage of Diplomat lib: Nodes functions
Usefull usage of Diplomat lib: Service functions
Usefull usage of Diplomat lib: Services functions
Public Class Methods
Get sevice name or empty string @param s service information @return [string] the name of service
# File lib/diplomatic_bag/services.rb, line 8 def self.compute_service_name(s) return '/node::health/' if s.nil? || s == '' s end
rubocop:disable Metrics/AbcSize
# File lib/diplomatic_bag/info.rb, line 6 def self.consul_info(options = {}) consul_self = Diplomat::Agent.self(options) puts 'Server: ' + consul_self['Config']['NodeName'] puts 'Datacenter: ' + consul_self['Config']['Datacenter'] puts 'Consul Version: ' + consul_self['Config']['Version'] if consul_self['Stats']['consul']['leader_addr'] puts 'Leader Address: ' + consul_self['Stats']['consul']['leader_addr'] puts 'applied_index: ' + consul_self['Stats']['raft']['applied_index'] puts 'commit_index: ' + consul_self['Stats']['raft']['commit_index'] end members = Diplomat::Members.get(options) servers = members.select { |member| member['Tags']['role'] == 'consul' }.sort_by { |n| n['Name'] } nodes = members.select { |member| member['Tags']['role'] == 'node' } leader = Diplomat::Status.leader(options).split(':')[0] puts 'Servers Count: ' + servers.count.to_s puts 'Nodes Count: ' + nodes.count.to_s puts 'Servers:' servers.map do |s| if s['Tags']['role'] == 'consul' if s['Addr'] == leader puts ' ' + s['Name'] + ' ' + s['Addr'] + ' *Leader*' else puts ' ' + s['Name'] + ' ' + s['Addr'] end end end end
Get the full list of services with their status @param options to query list @return #[Hash] A hash of statuses (passing|warning|critical) per service
# File lib/diplomatic_bag/services.rb, line 17 def self.get_all_services_status(options = {}) result = {} services = Diplomat::Health.state('any', options) grouped_by_service = services.group_by { |h| compute_service_name(h['ServiceName']) }.values grouped_by_service.each do |s| grouped_by_status = s.group_by { |h| h['Status'] }.values status = {} grouped_by_status.each do |state| status[state[0]['Status']] = state.count end service_name = compute_service_name(s[0]['ServiceName']) result[service_name] = status end result end
Return the list of datacenters matching given regexp @return [Array] an array of DC matching given regexp
# File lib/diplomatic_bag/datacenters.rb, line 7 def self.get_datacenters_list(dc, options = {}) dcs = [] datacenters = Diplomat::Datacenter.get(nil, options) dc.each do |c| dcs.concat(datacenters.select { |d| d[/#{c}/] }) end dcs.uniq end
Get the list of nodes having duplicate node ids @param options options to query with @return [Array] an array of objects with {name: node_name, status: status}
# File lib/diplomatic_bag/nodes.rb, line 8 def self.get_duplicate_node_id(options = {}) status = { 1 => 'Alive', 2 => '?', 3 => 'Left', 4 => 'Failed' } result = [] members = Diplomat::Members.get(options) grouped = members.group_by do |row| [row['Tags']['id']] end filtered = grouped.values.select { |a| a.size > 1 } filtered.each do |dup| instance = {} instance[:node_id] = dup[0]['Tags']['id'] nodes = [] dup.each do |inst| nodes << { name: inst['Name'], status: status[inst['Status']] } end instance[:nodes] = nodes result << instance end result end
# File lib/diplomatic_bag/service.rb, line 5 def self.get_service_info(service, options = {}) result = {} health = Diplomat::Health.service(service, options) result[service] = {} health.each do |h| result[service][h['Node']['Node']] = { 'Address': h['Node']['Address'], 'Port': h['Service']['Port'] } checks = {} h['Checks'].each do |c| checks[c['Name']] = { 'status': c['Status'], 'output': c['Output'] } end result[service][h['Node']['Node']]['Checks'] = checks end result end
# File lib/diplomatic_bag/services.rb, line 38 def self.get_services_info(service, options = {}) result = [] services = get_services_list(service, options) services.each do |s| result << get_service_info(s.to_s, options) end result end
# File lib/diplomatic_bag/services.rb, line 33 def self.get_services_list(service, options = {}) services = Diplomat::Service.get_all(options) services.to_h.keys.grep(/#{service}/) end