metrix 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/metrix/base.rb CHANGED
@@ -38,5 +38,9 @@ module Metrix
38
38
  def unfiltered_metrics
39
39
  extract(attributes)
40
40
  end
41
+
42
+ def cast_int(value)
43
+ value.to_i if value
44
+ end
41
45
  end
42
46
  end
data/lib/metrix/cli.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "metrix"
2
2
  require "metrix/elastic_search"
3
3
  require "metrix/mongodb"
4
+ require "metrix/nginx"
4
5
  require "metrix/system"
5
6
  require "metrix/load"
6
7
  require "logger"
@@ -32,6 +33,7 @@ module Metrix
32
33
  now = Time.now.utc
33
34
  reporter << Metrix::ElasticSearch.new(elastic_search_status) if elastic_search?
34
35
  reporter << Metrix::Mongodb.new(mongodb_status) if mongodb?
36
+ reporter << Metrix::Nginx.new(nginx_status) if nginx?
35
37
  if system?
36
38
  reporter << Metrix::System.new(File.read("/proc/stat"))
37
39
  reporter << Metrix::Load.new(File.read("/proc/loadavg"))
@@ -50,8 +52,12 @@ module Metrix
50
52
  Metrix.logger.error "#{err.backtrace.inspect}"
51
53
  ensure
52
54
  sleep_for = @interval - (Time.now - started - cnt * interval)
53
- Metrix.logger.info "finished run in %.06f, sleeping for %.06f" % [Time.now - now, sleep_for]
54
- sleep sleep_for
55
+ if sleep_for > 0
56
+ Metrix.logger.info "finished run in %.06f, sleeping for %.06f" % [Time.now - now, sleep_for]
57
+ sleep sleep_for
58
+ else
59
+ Metrix.logger.info "not sleeping because %.06f is negative" % [sleep_for]
60
+ end
55
61
  end
56
62
  end
57
63
  end
@@ -72,6 +78,10 @@ module Metrix
72
78
  !!@mongodb
73
79
  end
74
80
 
81
+ def nginx?
82
+ !!@nginx
83
+ end
84
+
75
85
  def elastic_search_status
76
86
  Metrix.logger.info "fetching elasticsearch metrix"
77
87
  Net::HTTP.get(URI("http://127.0.0.1:9200/_status"))
@@ -82,6 +92,11 @@ module Metrix
82
92
  Net::HTTP.get(URI("http://127.0.0.1:28017/serverStatus"))
83
93
  end
84
94
 
95
+ def nginx_status
96
+ Metrix.logger.info "fetching mongodb metrix"
97
+ Net::HTTP.get(URI("http://127.0.0.1:8000/"))
98
+ end
99
+
85
100
  def system?
86
101
  !!@system
87
102
  end
@@ -94,6 +109,15 @@ module Metrix
94
109
  def opts
95
110
  require "optparse"
96
111
  @opts ||= OptionParser.new do |o|
112
+ o.on("-v", "--version") do
113
+ puts "metrix #{Metrix::VERSION}"
114
+ exit
115
+ end
116
+
117
+ o.on("--nginx") do
118
+ @nginx = true
119
+ end
120
+
97
121
  o.on("--mongodb") do
98
122
  @mongodb = true
99
123
  end
@@ -0,0 +1,56 @@
1
+ require "metrix/base"
2
+
3
+ module Metrix
4
+ class Nginx < Base
5
+ def initialize(data)
6
+ @data = data
7
+ @time = Time.now
8
+ end
9
+
10
+ def active_connections
11
+ cast_int(@data[/Active connections: (\d+)/, 1])
12
+ end
13
+
14
+ [:accepts, :handled, :requests].each do |name|
15
+ define_method(name) do
16
+ numbers[name]
17
+ end
18
+ end
19
+
20
+ def prefix
21
+ "nginx"
22
+ end
23
+
24
+ def extract(data = nil)
25
+ {
26
+ accepts: accepts,
27
+ handled: handled,
28
+ requests: requests,
29
+ active_connections: active_connections,
30
+ reading: reading,
31
+ writing: writing,
32
+ waiting: waiting,
33
+ }
34
+ end
35
+
36
+ def reading
37
+ cast_int(@data[/Reading: (\d+)/, 1])
38
+ end
39
+
40
+ def writing
41
+ cast_int(@data[/Writing: (\d+)/, 1])
42
+ end
43
+
44
+ def waiting
45
+ cast_int(@data[/Waiting: (\d+)/, 1])
46
+ end
47
+
48
+ def numbers
49
+ @numbers ||= if @data.match(/server.*\n\s*(\d+) (\d+) (\d+)/)
50
+ @numbers = { accepts: cast_int($1), handled: cast_int($2), requests: cast_int($3) }
51
+ else
52
+ {}
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/metrix/system.rb CHANGED
@@ -64,9 +64,5 @@ module Metrix
64
64
  def cpu
65
65
  Cpu.new(@raw[/^cpu (.*)/, 1].split(" ").map(&:to_i))
66
66
  end
67
-
68
- def cast_int(value)
69
- value.to_i if value
70
- end
71
67
  end
72
68
  end
@@ -1,3 +1,3 @@
1
1
  module Metrix
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,4 @@
1
+ Active connections: 1
2
+ server accepts handled requests
3
+ 7 8 11
4
+ Reading: 0 Writing: 1 Waiting: 3
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+ require "metrix/nginx"
3
+
4
+ describe "Metrix::Nginx" do
5
+ subject(:nginx) { Metrix::Nginx.new(FIXTURES_PATH.join("nginx.status.txt").read) }
6
+ it { should_not be_nil }
7
+
8
+ it { subject.active_connections.should eq(1) }
9
+ it { subject.accepts.should eq(7) }
10
+ it { subject.handled.should eq(8) }
11
+ it { subject.requests.should eq(11) }
12
+
13
+ it { subject.reading.should eq(0) }
14
+ it { subject.writing.should eq(1) }
15
+ it { subject.waiting.should eq(3) }
16
+
17
+ it { subject.prefix.should eq("nginx") }
18
+
19
+ describe "#extract" do
20
+ subject(:extracted) { nginx.extract(nil) }
21
+ it { should be_kind_of(Hash) }
22
+ it { subject[:accepts].should eq(7) }
23
+ it { subject[:active_connections].should eq(1) }
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-02 00:00:00.000000000 Z
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: SyslogLogger
@@ -98,6 +98,7 @@ files:
98
98
  - lib/metrix/load.rb
99
99
  - lib/metrix/metric.rb
100
100
  - lib/metrix/mongodb.rb
101
+ - lib/metrix/nginx.rb
101
102
  - lib/metrix/opentsdb.rb
102
103
  - lib/metrix/process.rb
103
104
  - lib/metrix/reporter/stdout.rb
@@ -108,6 +109,7 @@ files:
108
109
  - spec/fixtures/es_status.json
109
110
  - spec/fixtures/loadavg.txt
110
111
  - spec/fixtures/mongo_server_status.json
112
+ - spec/fixtures/nginx.status.txt
111
113
  - spec/fixtures/proc.26928.txt
112
114
  - spec/fixtures/proc.stat.txt
113
115
  - spec/lib/metrix/elastic_search_spec.rb
@@ -115,6 +117,7 @@ files:
115
117
  - spec/lib/metrix/load_spec.rb
116
118
  - spec/lib/metrix/metric_spec.rb
117
119
  - spec/lib/metrix/mongodb_spec.rb
120
+ - spec/lib/metrix/nginx_spec.rb
118
121
  - spec/lib/metrix/opentsdb_spec.rb
119
122
  - spec/lib/metrix/process_spec.rb
120
123
  - spec/lib/metrix/system_spec.rb
@@ -148,6 +151,7 @@ test_files:
148
151
  - spec/fixtures/es_status.json
149
152
  - spec/fixtures/loadavg.txt
150
153
  - spec/fixtures/mongo_server_status.json
154
+ - spec/fixtures/nginx.status.txt
151
155
  - spec/fixtures/proc.26928.txt
152
156
  - spec/fixtures/proc.stat.txt
153
157
  - spec/lib/metrix/elastic_search_spec.rb
@@ -155,6 +159,7 @@ test_files:
155
159
  - spec/lib/metrix/load_spec.rb
156
160
  - spec/lib/metrix/metric_spec.rb
157
161
  - spec/lib/metrix/mongodb_spec.rb
162
+ - spec/lib/metrix/nginx_spec.rb
158
163
  - spec/lib/metrix/opentsdb_spec.rb
159
164
  - spec/lib/metrix/process_spec.rb
160
165
  - spec/lib/metrix/system_spec.rb