aws_site_monitor 0.1.1 → 0.1.2

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: c82ef18a627acea47eba9c717821d2d521481325
4
- data.tar.gz: 327a17f119ec3e359b190901c4633833293f634d
3
+ metadata.gz: 1b4efdad1bd774afcda2cdfb73079a02d503ee84
4
+ data.tar.gz: 7f076bb696837c26bf682705dba6cf1723eedc1d
5
5
  SHA512:
6
- metadata.gz: fe1d718eb5d70e8599e16a05ae1cae06d3236aedcc8f5490fb0af744fc7bdc60671ca900f163b545be9156ea55c0cdba5ad38e9619aa97abf69decb7e37b90bf
7
- data.tar.gz: be476950872ad2fcbd4605dacb1dc2e7690189a90e5e1276e8d2db8feeb6a38757a6b2d72e1e45a704af19c289400858eb354f550eb33ee39a3fcc1f738e2678
6
+ metadata.gz: ce4dbcc315b7cc39fce9afb36596774752162f5dbb9f543367941b9e5c14ceca25cccd1dd19342efa5ef7eeb4a8cd8140cd68e681739ad91b6878a892bd4eb64
7
+ data.tar.gz: 4b768c6daa6ed908d4d6582b4a090da4d61b115664d4902f27aeb2af1bd70b7524083ef87721771d62b75441c84caa964843430f775c322b6cc4f060fc86e47f
data/.gitignore CHANGED
@@ -7,6 +7,7 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  .pstore
10
+ *.pstore
10
11
 
11
12
  # rspec failure tracking
12
13
  .rspec_status
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- aws_site_monitor (0.1.1)
4
+ aws_site_monitor (0.1.2)
5
5
  activesupport
6
6
  aws-sdk
7
7
  concurrent-ruby
data/README.md CHANGED
@@ -14,18 +14,36 @@ gem install aws_site_monitor
14
14
  ## Usage
15
15
 
16
16
  ### Add a site to watch list
17
- `bin/monitor add --url=https://www.google.com --instance-ids=12345 123456`
17
+ `aws_site_monitor add --url=https://www.google.com --instance-ids=12345 123456`
18
18
 
19
19
  ### Remove a site from watch list
20
- `bin/monitor remove --url=https://www.google.com`
20
+ `aws_site_monitor remove --url=https://www.google.com`
21
+
22
+ ### List sites on watchlist
23
+ `aws_site_monitor ls`
21
24
 
22
25
  ### Start watching
23
- `bin/monitor start --check_every_seconds=30 --aws_region='us-east-1'`
26
+ `aws_site_monitor start --check_every_seconds=60 --request_timeout_seconds=15 --aws_region='us-east-1'`
27
+
28
+ ### Watch sites with killswitch url
29
+ Basically this feature is if you are running the script on a local server, i.e. raspberry pi,
30
+ and you want to stop the process without physical access to it. You can pass in a killswitch_url,
31
+ which can be for instance a dropbox url. Then if you need to kill the script,
32
+ you can just delete the file.
33
+
34
+ `aws_site_monitor start --killswitch_url=https://www.dropbox.com/s/gtcocntwdm6ae16/site_monitor_killswitch.txt?dl=0`
35
+
36
+ ### List events (only tracked when non 200 response occurs)
37
+ `aws_site_monitor list_events`
38
+
39
+ ### Clear events
40
+ `aws_site_monitor clear_events`
41
+
24
42
 
25
43
  Also make sure you have ENV variables set up for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
26
44
 
27
45
  ## Contributing
28
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/aws-site-monitor.
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jasonayre/aws-site-monitor.
29
47
 
30
48
  ## License
31
49
 
@@ -7,6 +7,8 @@ require "active_support/core_ext"
7
7
  require 'aws-sdk'
8
8
  require 'pstore'
9
9
  require 'aws/site_monitor/pstore_record'
10
+ require 'aws/site_monitor/site'
11
+ require 'aws/site_monitor/event'
10
12
  require 'aws/site_monitor/cli'
11
13
 
12
14
  ENV['AWS_REGION'] ||= 'us-east-1'
@@ -19,51 +21,8 @@ module Aws
19
21
  credentials: ::Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
20
22
  })
21
23
 
22
- ::Aws::EC2::Client.new(region: AWS_REGION)
24
+ ::Aws::EC2::Client.new(region: ENV['AWS_REGION'])
23
25
  end
24
26
  end
25
-
26
- def self.register(url:, instance_ids:)
27
- ::Aws::SiteMonitor::Site.create(url: url, instance_ids: instance_ids)
28
- end
29
-
30
- def self.unregister(url:)
31
- ::Aws::SiteMonitor::Site.find_by(:url => url)
32
- end
33
-
34
- class Event
35
- include ::Aws::SiteMonitor::PstoreRecord
36
-
37
- def initialize(occured_at: ::Time.now, status_code:, **attributes)
38
- super(occured_at: occured_at, status_code: status_code, **attributes)
39
- end
40
- end
41
-
42
- class Site
43
- include ::Aws::SiteMonitor::PstoreRecord
44
- end
45
-
46
- class RestartTask
47
- def initialize(site)
48
- @site = site
49
- end
50
-
51
- def run
52
- ::Aws::SiteMonitor.ec2_client.reboot_instances({
53
- instance_ids: @site[:instance_ids]
54
- })
55
- end
56
-
57
- # todo: maybe support hard shutdown / start
58
- # def stop
59
- # begin
60
- # Aws::SiteMonitor.ec2_client.stop_instances
61
- # ec2.wait_until(:instance_stopped, instance_ids:[@id])
62
- # puts "instance stopped"
63
- # rescue Aws::Waiters::Errors::WaiterFailed => error
64
- # puts "failed waiting for instance running: #{error.message}"
65
- # end
66
- # end
67
- end
68
27
  end
69
28
  end
@@ -1,8 +1,10 @@
1
1
  module Aws
2
2
  module SiteMonitor
3
3
  class CLI < ::Thor
4
- option :check_every_seconds, :type => :numeric, :default => 5, :desc => 'Check every x seconds'
4
+ option :check_every_seconds, :type => :numeric, :default => 60, :desc => 'Check every x seconds'
5
5
  option :aws_region, :type => :string, :default => 'us-east-1', :desc => 'AWS region'
6
+ option :killswitch_url, :type => :string, :desc => 'If a file no longer exists at this url, kill script'
7
+ option :request_timeout_seconds, :type => :numeric, :default => 15, :desc => 'How long to wait for response before request times out which will trigger a reboot'
6
8
 
7
9
  desc "start", "Start Watching"
8
10
  def start
@@ -10,7 +12,7 @@ module Aws
10
12
  start_monitoring!
11
13
  sleep
12
14
  rescue => e
13
- puts e.inspect
15
+ puts "ERROR #{e.message}"
14
16
  start_monitoring!
15
17
  end
16
18
 
@@ -32,6 +34,24 @@ module Aws
32
34
  puts "removed #{site[:url]} from watchlist"
33
35
  end
34
36
 
37
+
38
+ desc "ls", "List sites being monitored"
39
+ def ls
40
+ sites = ::Aws::SiteMonitor::Site.all.map(&:attributes).join("\n")
41
+ puts sites.inspect
42
+ end
43
+
44
+ desc "list_events", "List events"
45
+ def list_events
46
+ events = ::Aws::SiteMonitor::Event.all.map(&:attributes).join("\n")
47
+ puts events.inspect
48
+ end
49
+
50
+ desc "clear_events", "Clear events"
51
+ def clear_events
52
+ ::Aws::SiteMonitor::Event.all.map(&:destroy)
53
+ end
54
+
35
55
  no_tasks do
36
56
  def configure!
37
57
  configure_traps
@@ -55,24 +75,38 @@ module Aws
55
75
  execution_interval: options.check_every_seconds,
56
76
  timeout_interval: options.check_every_seconds
57
77
  ) do
58
- tasks = ::Aws::SiteMonitor::Site.all.map do |site|
78
+ check_killswitch if check_killswitch?
79
+
80
+ ::Aws::SiteMonitor::Site.all.each do |site|
59
81
  puts "MAKING REQUEST TO #{site[:url]}"
60
- result = `curl -s -o /dev/null -I -w "%{http_code}" #{site[:url]}`
61
- puts result.inspect
82
+ result = `curl -s -o /dev/null -I -w "%{http_code}" --max-time #{options.request_timeout_seconds} #{site[:url]}`
62
83
 
63
- if result[0] === "2"
84
+ if result[0] == "2"
64
85
  puts "GOT 200 EVERYTHING OK"
65
- nil
66
86
  else
67
87
  ::Aws::SiteMonitor::Event.create(:status_code => result)
68
- ::Aws::SiteMonitor::RestartTask.new(site)
88
+ site.reboot_instances!
69
89
  end
70
90
  end
71
-
72
- tasks.flatten.compact.map(&:run)
73
91
  end
74
92
  end
75
93
 
94
+ def check_killswitch
95
+ puts "CHECKING KILLSWITCH #{options.killswitch_url}"
96
+ result = `curl -s -o /dev/null -I -w "%{http_code}" -L #{options.killswitch_url}`
97
+ puts result
98
+ kill_process! if result[0] != "2"
99
+ end
100
+
101
+ def check_killswitch?
102
+ !!options.killswitch_url
103
+ end
104
+
105
+ #because we are in a new thread with the timer task, exit/abort wont work
106
+ def kill_process!
107
+ ::Process.kill 9, ::Process.pid
108
+ end
109
+
76
110
  def start_monitoring!
77
111
  monitor_task.shutdown if @monitor_task
78
112
  @monitor_task = nil
@@ -0,0 +1,11 @@
1
+ module Aws
2
+ module SiteMonitor
3
+ class Event
4
+ include ::Aws::SiteMonitor::PstoreRecord
5
+
6
+ def initialize(occured_at: ::Time.now, status_code:, **attributes)
7
+ super(occured_at: occured_at, status_code: status_code, **attributes)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ module Aws
2
+ module SiteMonitor
3
+ class Site
4
+ include ::Aws::SiteMonitor::PstoreRecord
5
+
6
+ def reboot_instances!
7
+ puts "RESTARTING SITE #{self.attributes}"
8
+ ::Aws::SiteMonitor.ec2_client.reboot_instances(
9
+ instance_ids: self[:instance_ids]
10
+ )
11
+ rescue ::Aws::EC2::Errors::IncorrectState => e
12
+ puts e.message
13
+ start_instances!
14
+ end
15
+
16
+ def start_instances!
17
+ puts "STARTING STOPPED INSTANCES"
18
+ ::Aws::SiteMonitor.ec2_client.start_instances(
19
+ instance_ids: self[:instance_ids]
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module Aws
2
2
  module SiteMonitor
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_site_monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Ayre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-28 00:00:00.000000000 Z
11
+ date: 2018-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -146,7 +146,9 @@ files:
146
146
  - bin/setup
147
147
  - lib/aws/site_monitor.rb
148
148
  - lib/aws/site_monitor/cli.rb
149
+ - lib/aws/site_monitor/event.rb
149
150
  - lib/aws/site_monitor/pstore_record.rb
151
+ - lib/aws/site_monitor/site.rb
150
152
  - lib/aws/site_monitor/version.rb
151
153
  homepage: https://github.com/jasonayre/aws_site_monitor
152
154
  licenses: