haproxystats 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: set ts=2 sw=2 expandtab:
3
+ #
4
+ require 'socket'
5
+ require 'csv'
6
+
7
+ # Container class for the socket
8
+ # Intended to be inherited by othere classes that require use fo the stats
9
+ # socket (e.g. HAProxyStats)
10
+ class HAProxySocket
11
+ # Check we have a socket at location and that we can open it
12
+ def initialize(location)
13
+ @location = location
14
+ raise ArgumentError, "#{location} does not appear to be a socket." unless File.socket?(location)
15
+ raise IOError, 'Cannot read/write to socket at ${location}' unless show_info
16
+ end
17
+
18
+ # Open a socket, run a command, collect output and close
19
+ def run(command)
20
+ sock = UNIXSocket.new @location
21
+ sock.puts command
22
+ out = ''
23
+ first_char = sock.read 2
24
+ if first_char == '# '
25
+ out = CSV.parse(sock.read)
26
+ sock.close
27
+ return out
28
+ end
29
+ out = first_char + sock.read
30
+ sock.close
31
+ out
32
+ end
33
+
34
+ # Show info from haproxy
35
+ def show_info
36
+ run('show info')
37
+ end
38
+ end
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: set ts=2 sw=2 expandtab:
3
+ #
4
+ # Class for getting statistics
5
+ class HAProxyStats < HAProxySocket
6
+ attr_reader :stats
7
+
8
+ def initialize(location)
9
+ # Get a socket (via super)
10
+ super location
11
+ @stats = Hash.new
12
+ end
13
+
14
+ # Function to extract the data from the stats socket
15
+ # and put into @stats[service][server/aggregate]
16
+ def retrieve(all = false)
17
+ # run 'show stat' on the socket and iterate output
18
+ run('show stat').each do |line|
19
+ if not defined? @headers
20
+ # First row of CSV output is our headers
21
+ @headers = line
22
+ else
23
+ # @stats Hash populating magic
24
+ this = Hash[*@headers.zip(line).flatten]
25
+ if all or (this['pxname'] and not this['pxname'][0,1] == '_')
26
+ if this['pxname']
27
+ # Create hash if one doesn't exist
28
+ unless @stats[this['pxname']]
29
+ @stats[this['pxname']] = Hash.new
30
+ end
31
+ @stats[this['pxname']][this['svname']] = this
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ # Return an array of services
39
+ def services
40
+ @stats.keys.sort
41
+ end
42
+
43
+ # Return an array of the backend servers for +service+
44
+ def backends(service)
45
+ out = Array.new
46
+ # iterate the servers removing aggregates for FRONT/BACKEND
47
+ @stats[service].each do |k, v|
48
+ if not (k == 'FRONTEND' or k == 'BACKEND')
49
+ out = out << k
50
+ end
51
+ end
52
+ out
53
+ end
54
+
55
+ # Return number of backend servers for +service+ that are UP
56
+ def up(service)
57
+ up_count = 0
58
+ # iterate servers and count the UPs
59
+ backends(service).each do |this|
60
+ if @stats[service][this]['status'] == 'UP'
61
+ up_count += 1
62
+ end
63
+ end
64
+ # Return the ratio
65
+ up_count
66
+ end
67
+
68
+ # Return a ratio of the backend servers that are UP (between 0 and 1)
69
+ # E.g if ratio <= 0.5 then at least half of the backend servers are down
70
+ def upratio(service)
71
+ up(service).to_f / backends(service).length
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: set ts=2 sw=2 expandtab:
3
+ #
4
+ require 'haproxystats/socket.rb'
5
+ require 'haproxystats/stats.rb'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haproxystats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Taylor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-02-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Reads and parses stats from HAProxy Unix socket.
15
+ email: tom@tommyt.co.uk
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/haproxystats.rb
21
+ - lib/haproxystats/socket.rb
22
+ - lib/haproxystats/stats.rb
23
+ homepage: https://githib.com/t0mmyt/haproxystats-ruby
24
+ licenses:
25
+ - ModifiedBSD
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.23
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Reads and parses stats from HAProxy Unix socket
48
+ test_files: []