perus 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f56e61b3219419f836bbae92b49cfc35330f89d
4
- data.tar.gz: e6728c4313c0068683998ae1f0afd3a5ac93af21
3
+ metadata.gz: cb71157c8e99e2d438be1de8effd475492a88e20
4
+ data.tar.gz: 937cae814206ed5fc94d1ef613d76881b1139247
5
5
  SHA512:
6
- metadata.gz: cb8f44cea18719b51eb9887bb02d07836e5742f57c0bf5ca2987fffce41665aea8ae483b18860f1c7cf97a6accd269ed940521cc4b655117f5a77c22975bed78
7
- data.tar.gz: 0aab536725d3c43c4404fec26ecae26afc34cc3577d4bd430a0b8ee92e048ae5817fc9aabba11a9fb534cd327fb32f7339b1b6537952dccad0d633cbaca20b63
6
+ metadata.gz: b29144c8a6f3b45bd0038dbfbeea93d52adde137368557dddf049c9a1faacfe2235c1a9c1e208e07634bf146024dfb3f4a06a672955df683122813af399a4942
7
+ data.tar.gz: 9cd259d515b06c7c25da09cfec9f7f133b337688afbcb883ee30263bb040bd42d75cf5602ca037a12d7b6a9cea0e2c4dff2c06619163cb71980618df9320d8ee
data/lib/perus/options.rb CHANGED
@@ -2,7 +2,7 @@ require 'iniparse'
2
2
 
3
3
  module Perus
4
4
  class Options
5
- def initialize()
5
+ def initialize
6
6
  @defaults = {}
7
7
  end
8
8
 
@@ -12,7 +12,12 @@ module Perus
12
12
  else
13
13
  user_options = {}
14
14
  end
15
- @options = defaults.merge(user_options)
15
+
16
+ # options are only one level deep, so resolve conflicts
17
+ # by just merging the two conflicting hashes again
18
+ @options = defaults.merge(user_options) do |key, default, user|
19
+ default.merge(user)
20
+ end
16
21
  end
17
22
 
18
23
  def method_missing(name, *params, &block)
@@ -126,8 +126,15 @@ module Perus::Pinger
126
126
  class ShellCommandError < StandardError; end
127
127
  def shell(command)
128
128
  out, err, status = Open3.capture3(command)
129
- raise ShellCommandError.new(err.strip) unless err.empty?
130
- raise ShellCommandError.new(out.strip) if status.exitstatus > 0
129
+
130
+ unless err.empty?
131
+ raise ShellCommandError.new(err.strip)
132
+ end
133
+
134
+ if status.exitstatus > 0
135
+ raise ShellCommandError.new("#{out.strip}; exit: #{status.exitstatus}")
136
+ end
137
+
131
138
  out
132
139
  end
133
140
 
@@ -0,0 +1,12 @@
1
+ module Perus::Pinger
2
+ class UpstartStart < Command
3
+ description 'Start the upstart job specified with "job". Valid values
4
+ for "job" are contained in the pinger config file.'
5
+ option :job, restricted: true
6
+
7
+ def run
8
+ result = shell("sudo start #{option.job}")
9
+ true # shell will capture any errors
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Perus::Pinger
2
+ class UpstartStop < Command
3
+ description 'Stop the upstart job specified with "job". Valid values
4
+ for "job" are contained in the pinger config file.'
5
+ option :job, restricted: true
6
+
7
+ def run
8
+ result = shell("sudo stop #{option.job}")
9
+ true # shell will capture any errors
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ module Perus::Pinger
2
+ class Running < Command
3
+ description 'Reports "yes" if "process_path" is running, "no"
4
+ otherwise. Valid values for "process_path" are contained
5
+ in the pinger config file.'
6
+ option :process_path, restricted: true
7
+ metric!
8
+
9
+ def run
10
+ begin
11
+ ps_result = shell("ps aux | grep -v grep | grep #{options.process_path}")
12
+ rescue ShellCommandError
13
+ ps_result = ''
14
+ end
15
+
16
+ metric_name = "#{File.basename(options.process_path)}_running"
17
+ {metric_name => ps_result.empty? ? 'no' : 'yes'}
18
+ end
19
+ end
20
+ end
@@ -69,8 +69,8 @@ module Perus::Pinger
69
69
 
70
70
  def run
71
71
  load_config
72
- run_metrics
73
72
  run_actions
73
+ run_metrics
74
74
  send_response
75
75
  cleanup
76
76
  end
data/lib/perus/pinger.rb CHANGED
@@ -15,6 +15,8 @@ module Perus
15
15
  require './pinger/commands/upgrade'
16
16
  require './pinger/commands/script'
17
17
  require './pinger/commands/sleep'
18
+ require './pinger/commands/upstart_start'
19
+ require './pinger/commands/upstart_stop'
18
20
 
19
21
  # metrics
20
22
  require './pinger/metrics/chrome'
@@ -26,6 +28,7 @@ module Perus
26
28
  require './pinger/metrics/temp'
27
29
  require './pinger/metrics/value'
28
30
  require './pinger/metrics/uptime'
31
+ require './pinger/metrics/running'
29
32
 
30
33
  # pinger
31
34
  require './pinger/pinger'
@@ -147,7 +147,12 @@ module Perus::Server
147
147
  # update the system with its last known ip and update time
148
148
  system = System.with_pk!(params['id'])
149
149
  system.last_updated = timestamp
150
- system.ip = request.ip
150
+
151
+ if request.ip == '127.0.0.1'
152
+ system.ip = request.env['HTTP_X_FORWARDED_FOR']
153
+ else
154
+ system.ip = request.ip
155
+ end
151
156
 
152
157
  # errors is either nil or a hash of the format - module: [err, ...]
153
158
  system.save_metric_errors(params, timestamp)
@@ -62,7 +62,7 @@ module Perus::Server
62
62
  keep_hours = Server.options.keep_hours
63
63
 
64
64
  # remove old values
65
- min_timestamp = Time.now.to_i - (keep_hours * 60)
65
+ min_timestamp = Time.now.to_i - (keep_hours * 60 * 60)
66
66
  values = Value.where("timestamp < #{min_timestamp}")
67
67
  puts "Deleting #{values.count} values"
68
68
  values.each(&:destroy)
@@ -32,6 +32,17 @@ module Perus::Server
32
32
  system.values_dataset.where(metric: name)
33
33
  end
34
34
 
35
+ def values_over_period(period)
36
+ raise 'invalid period' unless period.keys.include?(:hours)
37
+ min_timeout = Time.now.to_i - (period[:hours] * 60 * 60)
38
+ values_dataset.where("timestamp >= #{min_timeout}")
39
+ end
40
+
41
+ def num_values_over_period(period)
42
+ values = values_over_period(period)
43
+ values.map(&:num_value)
44
+ end
45
+
35
46
  def after_destroy
36
47
  super
37
48
  File.unlink(path) if file && File.exists?(path)
@@ -94,7 +94,11 @@ module Perus::Server
94
94
  end
95
95
 
96
96
  def latest(name)
97
- values_dataset.where(metric: name).order_by('timestamp desc').first
97
+ values_dataset.where(metric: name.to_s).order_by(:timestamp).last
98
+ end
99
+
100
+ def metric(name)
101
+ metrics_dataset.where(name: name.to_s).first
98
102
  end
99
103
 
100
104
 
data/lib/perus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Perus
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Cannings
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-11 00:00:00.000000000 Z
11
+ date: 2015-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -214,11 +214,14 @@ files:
214
214
  - lib/perus/pinger/commands/sleep.rb
215
215
  - lib/perus/pinger/commands/upgrade.rb
216
216
  - lib/perus/pinger/commands/upload.rb
217
+ - lib/perus/pinger/commands/upstart_start.rb
218
+ - lib/perus/pinger/commands/upstart_stop.rb
217
219
  - lib/perus/pinger/metrics/chrome.rb
218
220
  - lib/perus/pinger/metrics/cpu.rb
219
221
  - lib/perus/pinger/metrics/hd.rb
220
222
  - lib/perus/pinger/metrics/mem.rb
221
223
  - lib/perus/pinger/metrics/process.rb
224
+ - lib/perus/pinger/metrics/running.rb
222
225
  - lib/perus/pinger/metrics/screenshot.rb
223
226
  - lib/perus/pinger/metrics/temp.rb
224
227
  - lib/perus/pinger/metrics/uptime.rb