Permalink
Browse files

fixed haproxy for Ruby 1.9 and added tests

  • Loading branch information...
Andre Lewis
Andre Lewis committed Apr 6, 2010
1 parent 085298b commit f6cf4454208766029b0185d202c123e9a4c440ab
Showing with 52 additions and 3 deletions.
  1. +8 −3 haproxy_scout/haproxy_scout.rb
  2. +43 −0 haproxy_scout/test.rb
  3. +1 −0 test_helper.rb
@@ -1,6 +1,11 @@
class HaproxyStats < Scout::Plugin
-
- needs 'fastercsv', 'open-uri'
+
+ if RUBY_VERSION < "1.9"
+ needs 'fastercsv'
+ else
+ needs 'csv'
+ end
+ needs 'open-uri'
OPTIONS=<<-EOS
uri:
@@ -9,7 +14,7 @@ class HaproxyStats < Scout::Plugin
EOS
def build_report
- FasterCSV.parse(open(option(:uri)), :headers => true) do |row|
+ (RUBY_VERSION < "1.9" ? FasterCSV : CSV).parse(open(option(:uri)), :headers => true) do |row|
if row["svname"] == 'FRONTEND' || row["svname"] == 'BACKEND'
name = row["# pxname"] + ' ' + row["svname"].downcase
report "#{name} Current Sessions" => row["scur"]
View
@@ -0,0 +1,43 @@
+require File.dirname(__FILE__)+"/../test_helper"
+require File.dirname(__FILE__)+"/haproxy_scout"
+require 'open-uri'
+class HaProxyTest < Test::Unit::TestCase
+
+ def test_normal_run
+ # Stub the plugin instance where necessary and run
+ # @plugin=PluginName.new(last_run, memory, options)
+ # date hash hash
+ uri='http://fake' # output comes from http://demo.1wt.eu/;csv
+ FakeWeb.register_uri(:get, uri, :body => FIXTURES[:csv])
+ @plugin=HaproxyStats.new(nil,{},{:uri=>uri})
+ res= @plugin.run()
+ assert_equal [{"http-in frontend Current Sessions"=>"1"},
+ {"http-in frontend Requests / second"=>"1"},
+ {"www backend Current Sessions"=>"0"},
+ {"www backend Current Queue"=>"0"},
+ {"www backend Requests / second"=>"0"},
+ {"git backend Current Sessions"=>"0"},
+ {"git backend Current Queue"=>"0"},
+ {"git backend Requests / second"=>"0"},
+ {"demo backend Current Sessions"=>"1"},
+ {"demo backend Current Queue"=>"0"},
+ {"demo backend Requests / second"=>"1"}], res[:reports]
+ end
+
+ FIXTURES=YAML.load(<<-EOS)
+ :csv: |
+ # pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,
+ http-in,FRONTEND,,,1,34,100,329625,104912702,9082767400,0,0,436,,,,,OPEN,,,,,,,,,1,1,0,,,,0,1,0,26,,,,0,320967,20890,9724,300,0,,1,32,351882,,,
+ http-in,IPv4-direct,,,1,27,100,41837,21004375,739334057,0,0,423,,,,,OPEN,,,,,,,,,1,1,1,,,,3,,,,,,,,,,,,,,,,,,,
+ http-in,IPv4-cached,,,0,32,100,286965,83724634,8336734700,0,0,3,,,,,OPEN,,,,,,,,,1,1,2,,,,3,,,,,,,,,,,,,,,,,,,
+ http-in,IPv6-direct,,,0,4,100,823,183693,6698643,0,0,10,,,,,OPEN,,,,,,,,,1,1,3,,,,3,,,,,,,,,,,,,,,,,,,
+ http-in,local,,,0,0,100,0,0,0,0,0,0,,,,,OPEN,,,,,,,,,1,1,4,,,,3,,,,,,,,,,,,,,,,,,,
+ www,www,0,0,0,10,10,226818,88575426,8853553673,,0,,0,6,0,0,UP,1,1,0,0,0,1729584,0,,1,2,1,,226782,,2,0,,32,L7OK,200,3,0,198059,20422,8328,0,0,0,,,,4295,5,
+ www,bck,0,0,0,0,10,0,0,0,,0,,0,0,0,0,UP,1,0,1,0,0,1729584,0,,1,2,2,,0,,2,0,,0,L7OK,200,2,0,0,0,0,0,0,0,,,,0,0,
+ www,BACKEND,0,7,0,18,100,226837,88592903,8853557341,0,0,,4,6,0,0,UP,1,1,1,,0,1729584,0,,1,2,0,,226782,,1,0,,32,,,,0,198059,20422,8343,13,0,,,,,4296,5,
+ git,www,0,0,0,2,2,4400,1368928,130726732,,0,,0,0,0,0,UP,1,1,0,0,0,1729584,0,,1,3,1,,3050,,2,0,,2,L7OK,200,3,0,3875,468,57,0,0,0,,,,545,0,
+ git,bck,0,0,0,0,2,0,0,0,,0,,0,0,0,0,UP,1,0,1,0,0,1729584,0,,1,3,2,,0,,2,0,,0,L7OK,200,2,0,0,0,0,0,0,0,,,,0,0,
+ git,BACKEND,0,30,0,32,2,4664,1784466,130782700,0,0,,264,0,0,0,UP,1,1,1,,0,1729584,0,,1,3,0,,3050,,1,0,,11,,,,0,3875,468,57,264,0,,,,,621,0,
+ demo,BACKEND,0,0,1,7,0,5188,1885693,80518954,0,0,,0,0,0,0,UP,0,0,0,,0,1729584,0,,1,15,0,,0,,1,1,,5,,,,0,5187,0,0,0,0,,,,,787,0,
+ EOS
+end
View
@@ -1,6 +1,7 @@
require 'test/unit'
require 'rubygems'
require 'mocha' # gem install mocha
+require 'fakeweb' # gem install fakeweb
require 'timecop' # gem install timecop
require 'scout'

0 comments on commit f6cf445

Please sign in to comment.