logicbomb 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 92f1a9a4f607073f429f0d4e0508e44b058d945e973c698cbff89d10c29820d9
4
+ data.tar.gz: 79a743dda695344e06ecf05a1e76dd52a4b26434bbfe8de1f657c266d799ac1d
5
+ SHA512:
6
+ metadata.gz: 41eb424231a155121777a4542a5e641b3ac0aa011ae181dd1b3c30d06acecda884459320c7a57ce11d6e49b461a743f875f3d8f2c9dc70f61f07dbfcbf675935
7
+ data.tar.gz: 833fbda1cb5b70bb85e6a2fba0393d987554897a6ef3532ac6afe16db8e71cc253c76ec6e27a27c193338db20bf5ffc9be107e725edee2d7eafaa5728973c1b4
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in LogicBomb.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ logicbomb (0.1.0)
5
+ thor
6
+ viewpoint
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.3)
12
+ httpclient (2.8.3)
13
+ little-plugger (1.1.4)
14
+ logging (2.2.2)
15
+ little-plugger (~> 1.1)
16
+ multi_json (~> 1.10)
17
+ mini_portile2 (2.4.0)
18
+ multi_json (1.13.1)
19
+ nokogiri (1.10.1)
20
+ mini_portile2 (~> 2.4.0)
21
+ rake (12.3.2)
22
+ rspec (3.8.0)
23
+ rspec-core (~> 3.8.0)
24
+ rspec-expectations (~> 3.8.0)
25
+ rspec-mocks (~> 3.8.0)
26
+ rspec-core (3.8.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-expectations (3.8.2)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-mocks (3.8.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-support (3.8.0)
35
+ rubyntlm (0.6.2)
36
+ thor (0.20.3)
37
+ viewpoint (1.1.0)
38
+ httpclient
39
+ logging
40
+ nokogiri
41
+ rubyntlm
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler
48
+ logicbomb!
49
+ rake
50
+ rspec
51
+
52
+ BUNDLED WITH
53
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 ahewins
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # LogicBomb
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/LogicBomb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'LogicBomb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install LogicBomb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/LogicBomb.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "logicbomb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/logicbomb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ require 'logicbomb'
4
+
5
+ LogicBomb::Cli.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,64 @@
1
+ require 'thor'
2
+
3
+ module LogicBomb
4
+ class Cli < Thor
5
+ include Thor::Actions
6
+
7
+ def self.exit_on_failure?
8
+ true
9
+ end
10
+
11
+ desc "config-gen", "Generate the YAML config file to fill out"
12
+ def config_gen
13
+ require 'logicbomb/generators/config_gen'
14
+ LogicBomb::Generators::ConfigGen.start
15
+ end
16
+
17
+ desc "install CONFIG", "Installs this as a systemd Linux service to run the daemon with CONFIG at startup"
18
+ def install(config)
19
+ require 'logicbomb/generators/installer'
20
+ LogicBomb::Generators::Installer.start([config])
21
+ end
22
+
23
+ desc "daemon CONFIG", "Run the daemon with CONFIG"
24
+ def daemon(config)
25
+ yaml = load_yaml config
26
+ mailer = create_mailer yaml
27
+ create_ping(yaml, mailer).daemon
28
+ end
29
+
30
+ desc "test-email CONFIG", "Send a test email to everyone in the CONFIG's mail-list"
31
+ def test_email(config)
32
+ yaml = load_yaml config
33
+ puts "YAML loaded. Sending email..."
34
+ create_mailer(yaml).test_email
35
+ puts "Sent."
36
+ end
37
+
38
+ private
39
+ def load_yaml(config)
40
+ require 'yaml'
41
+ YAML::load File.open(config)
42
+ end
43
+
44
+ def create_mailer(yaml)
45
+ require 'logicbomb/mailer'
46
+ Mailer.new(
47
+ endpoint: yaml["mail-endpoint"],
48
+ user: yaml["mail-username"],
49
+ password: yaml["mail-password"],
50
+ mail_send_to_list: yaml["mail-to-list"]
51
+ )
52
+ end
53
+
54
+ def create_ping(yaml, mailer)
55
+ require 'logicbomb/ping'
56
+ Ping.new(
57
+ ping: yaml["ping"],
58
+ tracert: yaml["tracert"],
59
+ interval: yaml["interval"],
60
+ mailer: mailer
61
+ )
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,21 @@
1
+ require 'thor'
2
+
3
+ module LogicBomb
4
+ module Generators
5
+ class ConfigGen < Thor::Group
6
+ include Thor::Actions
7
+
8
+ def self.source_root
9
+ File.expand_path(File.join(File.dirname(__FILE__), '../templates'))
10
+ end
11
+
12
+ def copy_template
13
+ template("config.yml", "./config.yml")
14
+ puts "Created your config."
15
+ puts "It's much smarter to use environment variables for your passwords,"
16
+ puts "so by default the mail password is retrieved from LOGICBOMB_MAIL_PASSWORD."
17
+ puts "Export that var and then set up the rest of your config."
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,56 @@
1
+ require 'thor'
2
+
3
+ module LogicBomb
4
+ module Generators
5
+ class Installer < Thor::Group
6
+ include Thor::Actions
7
+
8
+ argument :config, type: :string
9
+
10
+ def self.source_root
11
+ File.expand_path(File.join(File.dirname(__FILE__), '../templates'))
12
+ end
13
+
14
+ def install
15
+ copy_template
16
+ reload_daemon
17
+ enable_logicbomb
18
+ start_logicbomb
19
+ end
20
+
21
+ private
22
+ def copy_template
23
+ begin
24
+ template("logicbomb.service", "/etc/systemd/system/logicbomb.service")
25
+ rescue Errno::EACCES
26
+ puts "To install the daemon you need sudo access since we hit systemd."
27
+ puts "Try again with rvmsudo if you have it, otherwise sudo."
28
+ exit 1
29
+ end
30
+ end
31
+
32
+ def reload_daemon
33
+ puts "Reloading the daemon..."
34
+ run_command 'sudo systemctl daemon-reload'
35
+ end
36
+
37
+ def enable_logicbomb
38
+ puts "Success. Enabling logicbomb service..."
39
+ run_command('sudo systemctl enable logicbomb')
40
+
41
+ end
42
+
43
+ def start_logicbomb
44
+ puts "Success. Starting logicbomb..."
45
+ run_command('sudo systemctl start logicbomb')
46
+ end
47
+
48
+ def run_command(cmd)
49
+ exit_bool = system(cmd)
50
+ return if exit_bool
51
+ puts "Error running `#{cmd}'. Quitting"
52
+ exit 1
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,42 @@
1
+ require 'viewpoint'
2
+ require 'date'
3
+
4
+ module LogicBomb
5
+ class Mailer
6
+ TEST_EMAIL_SUBJECT = "Test email for logicbomb"
7
+ TEST_EMAIL_BODY = "You are receiving this email because you are subscribed to alerts when " \
8
+ "logicbomb detects an IP failing to ping. You will get an email when the " \
9
+ "pings fail."
10
+
11
+ def initialize(endpoint:, user:, password:, mail_send_to_list:)
12
+ @user, @password = user, password
13
+ @endpoint, @to_list = endpoint, mail_send_to_list
14
+ end
15
+
16
+ def test_email
17
+ send_email(
18
+ subject: TEST_EMAIL_SUBJECT
19
+ body: TEST_EMAIL_BODY
20
+ )
21
+ end
22
+
23
+ def mail(ping, tracert)
24
+ send_email(
25
+ subject: "Internet outage #{DateTime.now}",
26
+ body: compose(ping, tracert)
27
+ )
28
+ end
29
+
30
+ private
31
+ def send_email(opts={})
32
+ opts[:to_recipients] = @to_list unless opts.key?(:to_recipients)
33
+ Viewpoint::EWSClient.new(@endpoint, @user, @password)
34
+ .send_message(opts)
35
+ end
36
+
37
+ def compose(ping, tracert)
38
+ "LogicBomb classified these pings as failures:\n#{ping}" \
39
+ "And ran traceroutes to help diagnose:\n#{tracert}"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ require 'logicbomb/mailer'
2
+
3
+ module LogicBomb
4
+ class Ping
5
+ def initialize(ping:, tracert:, interval:, mailer:)
6
+ @ping = ping
7
+ @tracert = tracert
8
+ @interval = interval
9
+ @mailer = mailer
10
+ end
11
+
12
+ def daemon
13
+ while true
14
+ sleep @interval
15
+ netconns = ping
16
+ next if netconns.nil?
17
+ mailer.mail netconns, tracert
18
+ sleep 3600
19
+ end
20
+ end
21
+
22
+ private
23
+ def ping
24
+ @ping.each do |ip|
25
+ attempt1 = ping ip
26
+ next if successful_ping?(ip)
27
+ sleep 5
28
+ attempt2 = ping ip
29
+ return "#{attempt1}\n#{attempt2}" unless successful_ping?(attempt2)
30
+ end
31
+ end
32
+
33
+ def tracert
34
+ @tracert.map {|ip| "#{ip}:\n" + `traceroute #{ip}`}.join('\n')
35
+ end
36
+
37
+ def ping(ip)
38
+ `ping -c 1 -s 1 #{ip}`
39
+ end
40
+
41
+ def successful_ping?(stdout)
42
+ stdout.include?(" 0% packet loss,")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ ping:
2
+ # List your IPs you wish to ping here, with '-'
3
+ # LogicBomb will go off if these IPs fail to respond.
4
+ - 8.8.8.8
5
+
6
+ tracert:
7
+ # List your IPs you wish to tracert
8
+ # LogicBomb will tracert these IPs and send them in a
9
+ # report to the emails below if the above pings fail
10
+ - 8.8.8.8
11
+
12
+ interval: 1000 # After the pingsweep, sleep(interval)
13
+
14
+ # The email credentials to login with to send the email report
15
+ # If you wish to obfuscate these values you can always use
16
+ # environment variables that hide behind ERB, like the password
17
+ mail-endpoint: https://domain.com/ews/endpoint.asmx
18
+ mail-username: a@a.com
19
+ mail-password: <%%= ENV["LOGICBOMB_MAIL_PASSWORD"] %>
20
+
21
+ # Who to send the report to
22
+ mail-to-list:
23
+ - b@a.com
@@ -0,0 +1,14 @@
1
+ Description=Ruby script that checks if various services are down, and if so, perform some action.
2
+
3
+ Wants=network.target
4
+ After=syslog.target network-online.target
5
+
6
+ [Service]
7
+ Type=simple
8
+ ExecStart=logicbomb daemon <%= config %>
9
+ Restart=on-failure
10
+ RestartSec=10
11
+ KillMode=process
12
+
13
+ [Install]
14
+ WantedBy=multi-user.target
@@ -0,0 +1,3 @@
1
+ module LogicBomb
2
+ VERSION = "0.1.0"
3
+ end
data/lib/logicbomb.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ require "logicbomb/version"
3
+ require 'logicbomb/ping'
4
+ require 'logicbomb/mailer'
5
+ require 'logicbomb/cli'
6
+
7
+ module LogicBomb
8
+ end
data/logicbomb.gemspec ADDED
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "logicbomb/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "logicbomb"
8
+ spec.version = LogicBomb::VERSION
9
+ spec.authors = ["AnthonyHewins"]
10
+ spec.email = ["aahewins@protonmail.com"]
11
+
12
+ spec.summary = %q{Continuously check if the internet is up or not.}
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+
24
+ spec.executables = `git ls-files -- bin/*`.split("\n").map {|f| File.basename(f)}
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "viewpoint"
28
+ spec.add_dependency "thor"
29
+
30
+ spec.add_development_dependency "bundler"
31
+ spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "rspec"
33
+ end
Binary file
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logicbomb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - AnthonyHewins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: viewpoint
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - aahewins@protonmail.com
86
+ executables:
87
+ - console
88
+ - logicbomb
89
+ - setup
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/logicbomb
102
+ - bin/setup
103
+ - lib/logicbomb.rb
104
+ - lib/logicbomb/cli.rb
105
+ - lib/logicbomb/generators/config_gen.rb
106
+ - lib/logicbomb/generators/installer.rb
107
+ - lib/logicbomb/mailer.rb
108
+ - lib/logicbomb/ping.rb
109
+ - lib/logicbomb/templates/config.yml
110
+ - lib/logicbomb/templates/logicbomb.service
111
+ - lib/logicbomb/version.rb
112
+ - logicbomb.gemspec
113
+ - pkg/logicbomb-0.1.0.gem
114
+ homepage:
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubygems_version: 3.0.3
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Continuously check if the internet is up or not.
137
+ test_files: []