metrix 0.0.4 → 0.0.5

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.
data/lib/metrix/cli.rb CHANGED
@@ -4,7 +4,9 @@ require "metrix/mongodb"
4
4
  require "metrix/nginx"
5
5
  require "metrix/system"
6
6
  require "metrix/load"
7
+ require "metrix/fpm"
7
8
  require "logger"
9
+ require "fileutils"
8
10
 
9
11
  module Metrix
10
12
  class CLI
@@ -19,15 +21,58 @@ module Metrix
19
21
  end
20
22
 
21
23
  def run
22
- opts.parse(@args)
23
24
  Metrix.logger.level = log_level
25
+ action = opts.parse(@args).first
26
+ case action
27
+ when "start"
28
+ if running?
29
+ logger.warn "refuse to run. seems that #{pid_path} exists!"
30
+ abort "not allowed to run" if running?
31
+ end
32
+ pid = Process.fork do
33
+ start
34
+ end
35
+ sleep 1
36
+ Process.detach(pid)
37
+ when "status"
38
+ if File.exists?(pid_path)
39
+ logger.debug "#{pid_path} exists"
40
+ puts "STATUS: running with pid #{File.read(pid_path).strip}"
41
+ else
42
+ logger.debug "#{pid_path} does not exist"
43
+ puts "STATUS: not running"
44
+ end
45
+ when "stop"
46
+ abort "not running!" if !running?
47
+ pid = File.read(pid_path).strip
48
+ logger.info "killing pid #{pid}"
49
+ system "kill #{pid}"
50
+ puts "killed #{pid}"
51
+ else
52
+ logger.warn "action #{action} unknown!"
53
+ abort "action #{action} unknown!"
54
+ end
55
+ end
56
+
57
+ def delete_pidfile!
58
+ logger.info "deleteing pidfile #{pid_path}"
59
+ FileUtils.rm_f(pid_path)
60
+ end
61
+
62
+ def start
24
63
  if self.reporter.nil?
25
64
  puts "ERROR: at least one reporter must be specified"
26
65
  abort opts.to_s
27
66
  end
67
+ Signal.trap("TERM") do
68
+ logger.info "terminating..."
69
+ $running = false
70
+ end
71
+ $running = true
28
72
  cnt = -1
29
73
  started = Time.now
30
- while true
74
+ write_pidfile!(Process.pid)
75
+ while $running
31
76
  begin
32
77
  cnt += 1
33
78
  now = Time.now.utc
@@ -49,6 +94,12 @@ module Metrix
49
94
  end
50
95
  end
51
96
 
97
+ if fpm?
98
+ fetch_metrix :fpm do
99
+ reporter << Metrix::FPM.new(fpm_status)
100
+ end
101
+ end
102
+
52
103
  if system?
53
104
  fetch_metrix :system do
54
105
  reporter << Metrix::System.new(File.read("/proc/stat"))
@@ -67,7 +118,7 @@ module Metrix
67
118
  end
68
119
  reporter.flush
69
120
  rescue SystemExit
70
- exit
121
+ $running = false
71
122
  rescue => err
72
123
  Metrix.logger.error "#{err.message}"
73
124
  Metrix.logger.error "#{err.backtrace.inspect}"
@@ -81,6 +132,24 @@ module Metrix
81
132
  end
82
133
  end
83
134
  end
135
+ delete_pidfile!
136
+ end
137
+
138
+ def write_pidfile!(pid)
139
+ logger.info "writing #{pid} to #{pid_path}"
140
+ File.open(pid_path, "w") { |f| f.print(pid) }
141
+ end
142
+
143
+ def allowed_to_run?
144
+ !running?
145
+ end
146
+
147
+ def pid_path
148
+ "/var/run/metrix.pid"
149
+ end
150
+
151
+ def running?
152
+ File.exists?(pid_path)
84
153
  end
85
154
 
86
155
  def log_level
@@ -99,6 +168,10 @@ module Metrix
99
168
  !!@mongodb
100
169
  end
101
170
 
171
+ def fpm?
172
+ !!@fpm
173
+ end
174
+
102
175
  def nginx?
103
176
  !!@nginx
104
177
  end
@@ -111,8 +184,11 @@ module Metrix
111
184
  get_url "http://127.0.0.1:28017/serverStatus"
112
185
  end
113
186
 
187
+ def fpm_status
188
+ get_url "http://127.0.0.1:9001/fpm-status"
189
+ end
190
+
114
191
  def nginx_status
115
- Metrix.logger.info "fetching mongodb metrix"
116
192
  get_url "http://127.0.0.1:8000/"
117
193
  end
118
194
 
@@ -154,6 +230,10 @@ module Metrix
154
230
  exit
155
231
  end
156
232
 
233
+ o.on("--fpm") do
234
+ @fpm = true
235
+ end
236
+
157
237
  o.on("--nginx") do
158
238
  @nginx = true
159
239
  end
@@ -179,7 +259,6 @@ module Metrix
179
259
  o.on("--stdout") do
180
260
  require "metrix/reporter/stdout"
181
261
  @reporter = Metrix::Reporter::Stdout.new
182
- log_to_stdout
183
262
  end
184
263
 
185
264
  o.on("--debug") do
data/lib/metrix/fpm.rb ADDED
@@ -0,0 +1,50 @@
1
+ require "metrix/base"
2
+
3
+ module Metrix
4
+ class FPM < Base
5
+ def initialize(data)
6
+ @data = data
7
+ @time = Time.now
8
+ end
9
+
10
+ def prefix
11
+ "fpm"
12
+ end
13
+
14
+ def extract(data)
15
+ {
16
+ accepted_conn: accepted_conn,
17
+ start_since: start_since,
18
+ accepted_conn: accepted_conn,
19
+ listen_queue: listen_queue,
20
+ max_listen_queue: max_listen_queue,
21
+ listen_queue_len: listen_queue_len,
22
+ idle_processes: idle_processes,
23
+ active_processes: active_processes,
24
+ total_processes: total_processes,
25
+ max_active_processes: max_active_processes,
26
+ max_children_reached: max_children_reached,
27
+ slow_requests: slow_requests,
28
+ }
29
+ end
30
+
31
+ {
32
+ "accepted conn" => :accepted_conn,
33
+ "start since" => :start_since,
34
+ "accepted conn" => :accepted_conn,
35
+ "listen queue" => :listen_queue,
36
+ "max listen queue" => :max_listen_queue,
37
+ "listen queue len" => :listen_queue_len,
38
+ "idle processes" => :idle_processes,
39
+ "active processes" => :active_processes,
40
+ "total processes" => :total_processes,
41
+ "max active processes" => :max_active_processes,
42
+ "max children reached" => :max_children_reached,
43
+ "slow requests" => :slow_requests,
44
+ }.each do |from, to|
45
+ define_method(to) do
46
+ cast_int(@data[/^#{from}:\s*(\d+)/, 1])
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module Metrix
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,14 @@
1
+ pool: www
2
+ process manager: dynamic
3
+ start time: 03/Jun/2013:16:34:04 +0000
4
+ start since: 29
5
+ accepted conn: 4
6
+ listen queue: 1
7
+ max listen queue: 2
8
+ listen queue len: 128
9
+ idle processes: 1
10
+ active processes: 2
11
+ total processes: 3
12
+ max active processes: 4
13
+ max children reached: 1
14
+ slow requests: 2
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+ require "metrix/cli"
3
+
4
+ describe "Metrix::CLI", :wip do
5
+ subject(:cli) { Metrix::CLI.new([]) }
6
+ it { should_not be_nil }
7
+
8
+ it { cli.pid_path.should eq("/var/run/metrix.pid") }
9
+
10
+ describe "#running?" do
11
+ before do
12
+ File.stub(:exists?).with("/var/run/metrix.pid") { false }
13
+ end
14
+ it { cli.should_not be_running }
15
+
16
+ describe "being running" do
17
+ before do
18
+ File.stub(:exists?).with("/var/run/metrix.pid") { true }
19
+ end
20
+ it { cli.should be_running }
21
+ end
22
+ end
23
+
24
+ describe "#allowed_to_run?" do
25
+ before do
26
+ cli.stub(:running?) { true }
27
+ end
28
+
29
+ it { should_not be_allowed_to_run }
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+ require "metrix/fpm"
3
+
4
+ describe "Metrix::FPM", :wip do
5
+ let(:data) { FIXTURES_PATH.join("php.fpm.status.txt").read }
6
+ subject(:fpm) { Metrix::FPM.new(data) }
7
+ it { should_not be_nil }
8
+ it { subject.start_since.should eq(29) }
9
+ it { subject.accepted_conn.should eq(4) }
10
+ it { subject.listen_queue.should eq(1) }
11
+ it { subject.idle_processes.should eq(1) }
12
+
13
+ it { subject.prefix.should eq("fpm") }
14
+
15
+ describe "#extract" do
16
+ subject(:extract) { fpm.extract(1) }
17
+ it { should be_kind_of(Hash) }
18
+ it { subject[:accepted_conn].should eq(4) }
19
+ it { subject[:active_processes].should eq(2) }
20
+ end
21
+ end
22
+
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.4
4
+ version: 0.0.5
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-03 00:00:00.000000000 Z
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: SyslogLogger
@@ -93,6 +93,7 @@ files:
93
93
  - lib/metrix/base.rb
94
94
  - lib/metrix/cli.rb
95
95
  - lib/metrix/elastic_search.rb
96
+ - lib/metrix/fpm.rb
96
97
  - lib/metrix/graphite.rb
97
98
  - lib/metrix/json.rb
98
99
  - lib/metrix/load.rb
@@ -110,9 +111,12 @@ files:
110
111
  - spec/fixtures/loadavg.txt
111
112
  - spec/fixtures/mongo_server_status.json
112
113
  - spec/fixtures/nginx.status.txt
114
+ - spec/fixtures/php.fpm.status.txt
113
115
  - spec/fixtures/proc.26928.txt
114
116
  - spec/fixtures/proc.stat.txt
117
+ - spec/lib/metrix/cli_spec.rb
115
118
  - spec/lib/metrix/elastic_search_spec.rb
119
+ - spec/lib/metrix/fpm_spec.rb
116
120
  - spec/lib/metrix/graphite_spec.rb
117
121
  - spec/lib/metrix/load_spec.rb
118
122
  - spec/lib/metrix/metric_spec.rb
@@ -152,9 +156,12 @@ test_files:
152
156
  - spec/fixtures/loadavg.txt
153
157
  - spec/fixtures/mongo_server_status.json
154
158
  - spec/fixtures/nginx.status.txt
159
+ - spec/fixtures/php.fpm.status.txt
155
160
  - spec/fixtures/proc.26928.txt
156
161
  - spec/fixtures/proc.stat.txt
162
+ - spec/lib/metrix/cli_spec.rb
157
163
  - spec/lib/metrix/elastic_search_spec.rb
164
+ - spec/lib/metrix/fpm_spec.rb
158
165
  - spec/lib/metrix/graphite_spec.rb
159
166
  - spec/lib/metrix/load_spec.rb
160
167
  - spec/lib/metrix/metric_spec.rb