Class: HAProxyStats

Inherits:
HAProxySocket show all
Defined in:
lib/haproxystats/stats.rb

Overview

vim: set ts=2 sw=2 expandtab:

Class for getting statistics

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HAProxySocket

#run, #show_info

Constructor Details

#initialize(location) ⇒ HAProxyStats

Returns a new instance of HAProxyStats.



8
9
10
11
12
# File 'lib/haproxystats/stats.rb', line 8

def initialize(location)
  # Get a socket (via super)
  super location
  @stats = Hash.new
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



6
7
8
# File 'lib/haproxystats/stats.rb', line 6

def stats
  @stats
end

Instance Method Details

#backends(service) ⇒ Object

Return an array of the backend servers for service



44
45
46
47
48
49
50
51
52
53
# File 'lib/haproxystats/stats.rb', line 44

def backends(service)
  out = Array.new
  # iterate the servers removing aggregates for FRONT/BACKEND
  @stats[service].each do |k, v|
    if not (k == 'FRONTEND' or k == 'BACKEND')
      out = out << k
    end
  end
  out
end

#down(service) ⇒ Object

Return DOWN servers as array



66
67
68
# File 'lib/haproxystats/stats.rb', line 66

def down(service)
  find(service, 'DOWN')
end

#maint(service) ⇒ Object

Return MAINT servers as array



61
62
63
# File 'lib/haproxystats/stats.rb', line 61

def maint(service)
  find(service, 'MAINT')
end

#retrieve(all = false) ⇒ Object

Function to extract the data from the stats socket and put into @stats[server/aggregate]



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/haproxystats/stats.rb', line 16

def retrieve(all = false)
  # run 'show stat' on the socket and iterate output
  run('show stat').each do |line|
    if not defined? @headers
      # First row of CSV output is our headers
      @headers = line
    else
      # @stats Hash populating magic
      this = Hash[*@headers.zip(line).flatten]
      if all or (this['pxname'] and not this['pxname'][0,1] == '_')
        if this['pxname']
          # Create hash if one doesn't exist
          unless @stats[this['pxname']]
            @stats[this['pxname']] = Hash.new
          end
          @stats[this['pxname']][this['svname']] = this
        end
      end
    end
  end
end

#servicesObject

Return an array of services



39
40
41
# File 'lib/haproxystats/stats.rb', line 39

def services
    @stats.keys.sort
end

#up(service) ⇒ Object

Return UP servers as array



56
57
58
# File 'lib/haproxystats/stats.rb', line 56

def up(service)
  find(service, 'UP')
end

#upratio(service) ⇒ Object

Return a ratio of the backend servers that are UP (between 0 and 1) E.g if ratio <= 0.5 then at least half of the backend servers are down



72
73
74
# File 'lib/haproxystats/stats.rb', line 72

def upratio(service)
  up(service).length.to_f / backends(service).length
end