insta-monitis 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 527c5711dba81f0180c596d72c46bf6b9cee687e
4
+ data.tar.gz: 38a4557180826ba706c2c0bdb44b22fe82e4135c
5
+ SHA512:
6
+ metadata.gz: a4d6954d0071380f7dca1e7442a65e7e0038a3aa3fb1f064bf25c29c569340cfe53c566a4b1f924a60ec1ee416a505f87999f7aee36a63eb42639dfc13c20954
7
+ data.tar.gz: db4f9e3e2061f09c24238bf5a004cab4f33563722a7857325cfbf7ac0ebb529214a4eb29727563cc5f07f7d41b2ac139ee2c26fad48050da8e55008149d34fea
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in catchpoint-api.gemspec
4
+ gemspec
5
+ gem 'thor'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
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.
@@ -0,0 +1,166 @@
1
+ # Insta-Monitis
2
+
3
+ A simple ruby CLI which enables the quick creation or listing of tests in a Monitis account.
4
+
5
+ Leverages v3 of the Monitis API.
6
+
7
+ TODO:
8
+ * Flesh out creating .monitis via API call with username/password
9
+ * Add functionality to manage Pages/Organization in Monitis dashboard UI
10
+ * Build out Testing via Rake/etc.
11
+ * Comments, Comments, Comments
12
+ * Error handling
13
+
14
+ ## Requirements
15
+
16
+ Requires rubygems thor and bundler.
17
+
18
+ Install with:
19
+
20
+ ~> gem install thor
21
+
22
+ ~> gem install bundler
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'insta-monitis'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ ~> bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ ~> gem install insta-monitis
39
+
40
+ ## Setup / Credentials
41
+
42
+ Create a file called .monitis in your $HOME.
43
+
44
+ It should contain your API key and secret, like so:
45
+
46
+ ~> cat ~/.monitis
47
+ key: RANDOMSTRINGOFRANDOMNESS
48
+ secret: THISISSUPERSECRET
49
+ ~>
50
+
51
+ ## Usage
52
+
53
+ Help menu:
54
+ ```ruby
55
+ ~> insta-monitis
56
+ Commands:
57
+ insta-monitis add <subcommand> <args> # Perform add operations
58
+ insta-monitis help [COMMAND] # Describe available commands or one specific command
59
+ insta-monitis list <subcommand> <args> # Perform list operations
60
+ insta-monitis search <subcommand> <args> # Perform search operations
61
+
62
+ Options:
63
+ [--verbose], [--no-verbose]
64
+ ```
65
+
66
+ List:
67
+ ```ruby
68
+ ~> insta-monitis list
69
+ Commands:
70
+ insta-monitis list help [COMMAND] # Describe subcommands or one specific subcommand
71
+ insta-monitis list all --style=[STYLE] # List all tests, sorted by id
72
+ insta-monitis list http --style=[STYLE] # List all http tests
73
+ insta-monitis list page --style=[STYLE] # List all full page load tests
74
+
75
+ Options:
76
+ [--style=STYLE] # Style of output, yaml:json:hash
77
+ # Default: yaml
78
+
79
+ ~> insta-monitis list help all
80
+ Usage:
81
+ insta-monitis list all --style=[STYLE]
82
+
83
+ Options:
84
+ [--style=STYLE] # Style of output, yaml:json:hash
85
+ # Default: yaml
86
+
87
+ Description:
88
+ Using the API, list every single monitor of any type. Sorted by id
89
+ ```
90
+
91
+ Add:
92
+ ```ruby
93
+ ~> insta-monitis add
94
+ Commands:
95
+ insta-monitis add bulk --file=[FILE] # Via file, create one or many test(s)
96
+ insta-monitis add help [COMMAND] # Describe subcommands or one specific subcommand
97
+ insta-monitis add http # Interactively create a http test
98
+ insta-monitis add page # Interactively create a full page load test
99
+
100
+ ~> insta-monitis add help bulk
101
+ Usage:
102
+ insta-monitis bulk --file=[FILE]
103
+
104
+ Options:
105
+ -f, [--file=FILE] # Filename to load
106
+
107
+ Description:
108
+ Load in a file full of test(s), use the API to created them in Monitis.
109
+
110
+ ```
111
+ The --file option will only accept a .csv file. The .csv needs to contain the headers like so:
112
+
113
+ type,name,url,interval,timeout,locationIds,tag,checkInterval
114
+ http,this_is_a_http,http.com,,,1 8 10,http!,,,
115
+ fullpage,this_is_a_page,fullpage.com,,,1 8 10,fullpage!,1 1 1,,,
116
+ fullpage,asfd,asdf,asdf,9, ,9, , ,
117
+
118
+ Type is a required header, as is name, url, locationIds, and tag.
119
+
120
+ If type is fullpage, checkInterval is required as well and must match the locationIds.
121
+
122
+ locationIds and checkInterval should be ' ' seperated values.
123
+
124
+
125
+ Delete:
126
+ ```ruby
127
+ ~> insta-monitis del
128
+ Commands:
129
+ insta-monitis del help [COMMAND] # Describe subcommands or one specific subcommand
130
+ insta-monitis del http --id=[ID] # Delete the http test with given Id
131
+ insta-monitis del page --id=[ID] # Delete the full page load test with given Id
132
+
133
+ Options:
134
+ -i, [--id=N] # Id of test
135
+ ```
136
+
137
+ Search:
138
+ ```ruby
139
+ ~> insta-monitis search
140
+ Commands:
141
+ insta-monitis search help [COMMAND] # Describe subcommands or one specific subcommand
142
+ insta-monitis search http --[OPTION]=[VALUE] --style=[STYLE] # Search all http tests
143
+ insta-monitis search page --[OPTION]=[VALUE] --style=[STYLE] # Search all fullpage tests
144
+
145
+ Options:
146
+ -s, [--style=STYLE] # Style of output, yaml:json:hash
147
+ # Default: yaml
148
+ -i, [--id=N] # Id of test
149
+ -n, [--name=NAME] # Name of test
150
+ -u, [--url=URL] # URL of test
151
+ -t, [--tag=TAG] # Tag of test(s)
152
+ ```
153
+
154
+ ## Development
155
+
156
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
157
+
158
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
159
+
160
+ ## Contributing
161
+
162
+ 1. Fork it ( https://github.com/praymann/insta-catchpoint/fork )
163
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
164
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
165
+ 4. Push to the branch (`git push origin my-new-feature`)
166
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "insta-monitis"
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
+ @config = InstaMonitis::Configurator.load()
14
+ @api = InstaMonitis::Api.new(@config)
15
+ @api.set_authtoken(@api.get('authToken')["authToken"])
16
+
17
+ @http = InstaMonitis::HTTPMonitor.new
18
+ @fullpage = InstaMonitis::FullPageMonitor.new
19
+
20
+ require "irb"
21
+
22
+
23
+ IRB.start
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require 'insta-monitis'
5
+
6
+ InstaMonitis::Runner.start(ARGV)
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'insta-monitis/ver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "insta-monitis"
8
+ spec.version = InstaMonitis::VERSION
9
+ spec.authors = ["Dan Pramann"]
10
+ spec.email = ["dpramann@lakana.com"]
11
+
12
+ spec.summary = "CLI interface for Monitis API"
13
+ spec.description = "A CLI which allows you to create and list HTTP and Full Page Load tests in Monitis"
14
+ spec.homepage = "https://github.com/praymann/insta-monitis"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ end
@@ -0,0 +1,21 @@
1
+ # Version
2
+ require "insta-monitis/ver/version"
3
+
4
+ ### Classes
5
+
6
+ # API interface
7
+ require "insta-monitis/api"
8
+
9
+ # Monitior definitions
10
+ require "insta-monitis/monitors.rb"
11
+
12
+ # Main class for insta-monitis ( API <-> Backend <-> Runner )
13
+ require "insta-monitis/backend"
14
+ require "insta-monitis/userinput"
15
+
16
+ # CLI class ( Thor )
17
+ require "insta-monitis/runner"
18
+
19
+ # Class to pull in configurations
20
+ require "insta-monitis/configurator"
21
+
@@ -0,0 +1,108 @@
1
+ require "insta-monitis/ver/version"
2
+ require 'net/http'
3
+ require 'base64'
4
+ require 'json'
5
+ require 'uri'
6
+
7
+ module InstaMonitis
8
+ class Api
9
+ @@host = 'https://api.monitis.com'
10
+ @@prefix = '/api'
11
+ @@version = 'version=3'
12
+
13
+ def initialize opts={}
14
+ @key = opts["key"]
15
+ @secret = opts["secret"]
16
+ end
17
+
18
+ def construct_timestp
19
+ return "timestamp=#{Time.now}"
20
+ end
21
+
22
+ def construct_authstring
23
+ if defined?(@token).nil?
24
+ unless defined?(@key).nil? and defined?(@secret).nil?
25
+ return "apikey=#{@key}&secretkey=#{@secret}"
26
+ else
27
+ puts "Either apikey or secretkey isn't set.. exiting."
28
+ exit
29
+ end
30
+ else
31
+ return "apikey=#{@key}&authToken=#{@token}"
32
+ end
33
+ end
34
+
35
+ def construct_apiuri endpoint
36
+ begin
37
+ auth_string = construct_authstring
38
+ return URI("#{@@host}#{@@prefix}?action=#{endpoint}&#{auth_string}&#{@@version}")
39
+ rescue URI::InvalidURIError
40
+ puts "URI build failed. Is this a valid URI format?: #{@@host}#{@@prefix}?action=#{endpoint}&#{auth_string}&#{@@version}"
41
+ exit
42
+ end
43
+ end
44
+
45
+ def get endpoint
46
+
47
+ # remove preceeding slash
48
+ endpoint.gsub!(/^\//, '')
49
+
50
+ uri = construct_apiuri(endpoint)
51
+
52
+ http = Net::HTTP.new(uri.host, uri.port)
53
+
54
+ http.use_ssl = true
55
+
56
+ begin
57
+ res = http.get(uri.path + '?' + uri.query)
58
+ return JSON.parse(res.body) unless res.code == '403'
59
+ puts "HTTP 403 received: #{res.body}"
60
+ exit
61
+ rescue TypeError
62
+ puts "Empty JSON Response from Monitis? Can't convert nil to String"
63
+ rescue Timeout::Error => e
64
+ puts "Possible HTTP Timeout issue"
65
+ rescue
66
+ raise RunTimeError
67
+ end
68
+ end
69
+
70
+ def put endpoint
71
+ # remove preceeding slash
72
+ endpoint.gsub!(/^\//, '')
73
+
74
+ uri = construct_apiuri(endpoint)
75
+
76
+ postdata = uri.query + '&' + construct_timestp
77
+
78
+ http = Net::HTTP.new(uri.host, uri.port)
79
+
80
+ http.use_ssl = true
81
+
82
+ res = http.post(uri.path , postdata)
83
+ puts res.body
84
+ end
85
+
86
+
87
+ def put_test endpoint, test
88
+
89
+ # remove preceeding slash
90
+ endpoint.gsub!(/^\//, '')
91
+
92
+ uri = construct_apiuri(endpoint)
93
+
94
+ postdata = uri.query + '&' + construct_timestp + '&' + test.to_post
95
+
96
+ http = Net::HTTP.new(uri.host, uri.port)
97
+
98
+ http.use_ssl = true
99
+
100
+ res = http.post(uri.path , postdata)
101
+ puts res.body
102
+ end
103
+
104
+ def set_authtoken string
105
+ @token = string
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,145 @@
1
+ require 'uri'
2
+ require 'insta-monitis/userinput'
3
+
4
+ module InstaMonitis
5
+ class Backend
6
+ include UserInput
7
+ # Init method
8
+ # Load all configurators then init a API object
9
+ # Set the authToken in that API object
10
+ def initialize
11
+ conf = Configurator.load()
12
+
13
+ @monitis = Api.new(conf)
14
+
15
+ @monitis.set_authtoken(@monitis.get('authToken')["authToken"])
16
+ end
17
+
18
+ def dump_http style
19
+ puts "External Monitors"
20
+ print(dump('tests')['testList'], style)
21
+ end
22
+
23
+ def dump_fullpage style
24
+ puts "Full Page Monitors"
25
+ print(dump('fullPageLoadTests'), style)
26
+ end
27
+
28
+ def dump_all style
29
+ puts "All Monitors"
30
+ storage = Array.new
31
+ dump('fullPageLoadTests').each do |test|
32
+ storage << test
33
+ end
34
+ dump('tests')['testList'].each do |test|
35
+ storage << test
36
+ end
37
+ storage.sort_by! { |h| h['id'] }
38
+ print(storage, style)
39
+ end
40
+
41
+ def search_http param, style
42
+ unless param['id'].nil?
43
+ print(dump("testinfo&testId=#{param['id']}"), style)
44
+ else
45
+ unless param['tag'].nil?
46
+ print(dump("tagtests&tag=#{param['tag']}"), style)
47
+ else
48
+ print(search(dump('tests')['testList'], param), style)
49
+ end
50
+ end
51
+ end
52
+
53
+ def search_fullpage param, style
54
+ unless param['id'].nil?
55
+ print(dump("fullPageLoadTestInfo&monitorId=#{param['id']}"), style)
56
+ else
57
+ print(search(dump('fullPageLoadTests'), param), style)
58
+ end
59
+ end
60
+
61
+ def create_http
62
+ this = HTTPMonitor.new
63
+ userinput_http this
64
+ push_test('addExternalMonitor', this)
65
+ end
66
+
67
+ def create_fullpage
68
+ this = FullPageMonitor.new
69
+ userinput_fullpage this
70
+ push_test('addFullPageLoadMonitor', this)
71
+ end
72
+
73
+ def create_bulk file
74
+ this = userinput_import file
75
+ this.each do |test|
76
+ case test.to_hash['type']
77
+ when 'http'
78
+ puts "Creating: #{test.to_post}"
79
+ push_test('addExternalMonitor', test)
80
+ when 'fullpage'
81
+ puts "Creating: #{test.to_post}"
82
+ push_test('addFullPageLoadMonitor', test)
83
+ else
84
+ puts "\nMissing type of test, or bad value. Must be http or fullpage. #{test.to_hash}"
85
+ end
86
+ end
87
+ end
88
+
89
+ def delete_http id
90
+ push("deleteExternalMonitor&testIds=#{id}")
91
+ end
92
+
93
+ def delete_fullpage id
94
+ push("deleteFullPageLoadMonitor&monitorId=#{id}")
95
+ end
96
+
97
+ private
98
+
99
+ def dump action
100
+ begin
101
+ return @monitis.get(action.to_s)
102
+ rescue
103
+ puts "API failure? You're not supposed to be here. Exiting."
104
+ exit
105
+ end
106
+ end
107
+
108
+ def search tests, param
109
+ storage = Array.new
110
+ tests.each do |test|
111
+ if test["#{param.keys.first}"].eql? param.values.first or test["#{param.keys.first}"].include? param.values.first
112
+ storage << test
113
+ end
114
+ end
115
+ return storage
116
+ end
117
+
118
+ def push_test action, object
119
+ @monitis.put_test(action.to_s, object)
120
+ end
121
+
122
+ def push action
123
+ @monitis.put(action.to_s)
124
+ end
125
+
126
+ def print object, style
127
+ object.each do |thing|
128
+ send "print_#{style}", thing
129
+ end
130
+ end
131
+
132
+ def print_yaml test
133
+ puts test.to_yaml
134
+ end
135
+
136
+ def print_json test
137
+ puts test.to_json
138
+ end
139
+
140
+ def print_hash test
141
+ puts test.to_h
142
+ end
143
+ end
144
+ end
145
+
@@ -0,0 +1,21 @@
1
+ require 'yaml'
2
+
3
+ module InstaMonitis
4
+ class Configurator
5
+
6
+ @@file = ENV['HOME'] + '/.monitis'
7
+
8
+ def self.load
9
+ opts = {}
10
+ if File.exist? @@file
11
+ file = YAML.load_file(@@file)
12
+
13
+ opts.each do |key, val|
14
+ file[key] = val
15
+ end
16
+
17
+ return file
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,110 @@
1
+ module InstaMonitis
2
+ module MonitorsHelper
3
+ def to_json
4
+ hash = {}
5
+ self.instance_variables.each do |k|
6
+ hash[k] = self.instance_variable_get k
7
+ end
8
+ hash.to_json
9
+ end
10
+
11
+ def from_json! string
12
+ JSON.load(string).each do |k, v|
13
+ self.instance_variable_set "@#{k}", v
14
+ end
15
+ end
16
+
17
+ def to_hash
18
+ hash = {}
19
+ self.instance_variables.each do |k|
20
+ hash[k.to_s.delete('@')] = self.instance_variable_get k
21
+ end
22
+ return hash
23
+ end
24
+
25
+ def from_hash! hash
26
+ hash.each do |k,v|
27
+ self.instance_variables.each do |var|
28
+ if var.to_s.delete('@') == k
29
+ self.instance_variable_set "@#{k}", v
30
+ break
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def to_post
37
+ string = ""
38
+ self.instance_variables.each do |k|
39
+ string << "&#{k.to_s.delete('@')}=#{(self.instance_variable_get k).to_s.delete(' ')}"
40
+ end
41
+ return string
42
+ end
43
+ end
44
+
45
+ class HTTPMonitor
46
+ include MonitorsHelper
47
+
48
+ def initialize
49
+ @type = 'http'
50
+ @name = nil
51
+ @url = nil
52
+ @interval = 1
53
+ @timeout = 30
54
+ @locationIds = nil
55
+ @tag = nil
56
+ end
57
+
58
+ def set_name string
59
+ @name = string
60
+ end
61
+
62
+ def set_url string
63
+ @url = string
64
+ end
65
+
66
+ def set_loc array
67
+ @locationIds = array.join(", ")
68
+ end
69
+
70
+ def set_tag string
71
+ @tag = string
72
+ end
73
+
74
+ end
75
+
76
+ class FullPageMonitor
77
+
78
+ include MonitorsHelper
79
+ def initialize
80
+ @type = 'fullpage'
81
+ @name = nil
82
+ @url = nil
83
+ @timeout = 59000
84
+ @locationIds = nil
85
+ @checkInterval = nil
86
+ @tag = nil
87
+ end
88
+
89
+ def set_name string
90
+ @name = string
91
+ end
92
+
93
+ def set_url string
94
+ @url = string
95
+ end
96
+
97
+ def set_loc array
98
+ @locationIds = array.join(", ")
99
+ end
100
+
101
+ def set_chk array
102
+ @checkInterval = array.join(", ")
103
+ end
104
+
105
+ def set_tag string
106
+ @tag = string
107
+ end
108
+
109
+ end
110
+ end
@@ -0,0 +1,208 @@
1
+ require 'rubygems'
2
+ require 'thor'
3
+
4
+ module InstaMonitis
5
+
6
+ module RunnerHelper
7
+ def check_style string
8
+ case string
9
+ when 'yaml'
10
+ return string
11
+ when 'json'
12
+ return string
13
+ when 'hash'
14
+ return string
15
+ else
16
+ puts "Valid options are json, hash, yaml. Default style is YAML"
17
+ exit
18
+ end
19
+ end
20
+
21
+ def check_options options
22
+ storage = options.reject { |k, v| k == 'verbose' }
23
+ storage.reject! { |k, v| k == 'style' }
24
+ if storage.length < 1 or storage.length > 1
25
+ puts "Too few or too many options, one at a time please."
26
+ exit
27
+ else
28
+ return storage
29
+ end
30
+ end
31
+
32
+ def check_file file
33
+ if File.exist? file
34
+ if file.include? '.csv'
35
+ return file
36
+ else
37
+ puts "File must end with .csv"
38
+ exit
39
+ end
40
+ else
41
+ puts "Is that a valid filename or valid path?"
42
+ exit
43
+ end
44
+ end
45
+ end
46
+
47
+ class List < Thor
48
+ include RunnerHelper
49
+
50
+ class_option :style, :aliases => '-s', :default => 'yaml', :desc => 'Style of output, yaml:json:hash'
51
+
52
+ desc "http --style=[STYLE]", "List all http tests"
53
+ long_desc <<-LONGDESC
54
+ Using the API, list all External Monitors of type http.
55
+ LONGDESC
56
+ def http()
57
+ this = Backend.new
58
+ log("Requesting all http tests in #{options[:style]} format\n\n")
59
+ this.dump_http(check_style(options[:style]))
60
+ end
61
+
62
+ desc "page --style=[STYLE]", "List all full page load tests"
63
+ long_desc <<-LONGDESC
64
+ Using the API, list all Full Page Load Monitors of type fullpageload.
65
+ LONGDESC
66
+ def page()
67
+ this = Backend.new
68
+ log("Requesting all fullpageload tests in #{options[:style]} format\n\n")
69
+ this.dump_fullpage(check_style(options[:style]))
70
+ end
71
+
72
+ desc "all --style=[STYLE]", "List all tests, sorted by id"
73
+ long_desc <<-LONGDESC
74
+ Using the API, list every single monitor of any type. Sorted by id
75
+ LONGDESC
76
+ def all()
77
+ this = Backend.new
78
+ log("Requesting everything single test in #{options[:style]} format\n\n")
79
+ this.dump_all(check_style(options[:style]))
80
+ end
81
+
82
+ no_commands do
83
+ def log(str)
84
+ puts str if options[:verbose]
85
+ end
86
+ end
87
+ end
88
+
89
+
90
+ class Add < Thor
91
+ include RunnerHelper
92
+
93
+ desc "http", "Interactively create a http test"
94
+ long_desc <<-LONGDESC
95
+ After building a local http ExternalMonitor, use the API to created it in Monitis.
96
+ LONGDESC
97
+ def http()
98
+ Backend.new.create_http
99
+ end
100
+
101
+ desc "page", "Interactively create a full page load test"
102
+ long_desc <<-LONGDESC
103
+ After building a local FullPageLoad Monitor, use the API to created it in Monitis.
104
+ LONGDESC
105
+ def page()
106
+ Backend.new.create_fullpage
107
+ end
108
+
109
+ desc "bulk --file=[FILE]", "Via file, create one or many test(s)"
110
+ long_desc <<-LONGDESC
111
+ Load in a file full of test(s), use the API to created them in Monitis.
112
+ LONGDESC
113
+ option :file, :aliases => '-f', :desc => 'Filename to load'
114
+ def bulk()
115
+ Backend.new.create_bulk(check_file(options[:file]))
116
+ end
117
+
118
+
119
+ no_commands do
120
+ def log(str)
121
+ puts str if options[:verbose]
122
+ end
123
+ end
124
+ end
125
+
126
+ class Del < Thor
127
+ class_option :id, :aliases => '-i', :type => :numeric, :desc => 'Id of test'
128
+ desc "http --id=[ID]", "Delete the http test with given Id"
129
+ long_desc <<-LONGDESC
130
+ There is no undo, given an Id go nuclear and nuke it from orbit.
131
+ LONGDESC
132
+ def http()
133
+ Backend.new.delete_http(options[:id])
134
+ end
135
+
136
+ desc "page --id=[ID]", "Delete the full page load test with given Id"
137
+ long_desc <<-LONGDESC
138
+ There is no undo, given an Id go nuclear and nuke it from orbit.
139
+ LONGDESC
140
+ def page()
141
+ Backend.new.delete_fullpage(options[:id])
142
+ end
143
+
144
+ end
145
+
146
+ class Search < Thor
147
+ include RunnerHelper
148
+ class_option :style, :aliases => '-s', :default => 'yaml', :desc => 'Style of output, yaml:json:hash'
149
+ class_option :id, :aliases => '-i', :type => :numeric, :desc => 'Id of test'
150
+ class_option :name, :aliases => '-n', :desc => 'Name of test'
151
+ class_option :url, :aliases => '-u', :desc => 'URL of test'
152
+ class_option :tag, :aliases => '-t', :desc => 'Tag of test(s)'
153
+
154
+ desc "http --[OPTION]=[VALUE] --style=[STYLE]", "Search all http tests"
155
+ long_desc <<-LONGDESC
156
+ Using the API, search through all External Monitors of type http.
157
+
158
+ For a given id ( -i, --id ) find the matching monitor
159
+
160
+ For a given name ( -n, --name ) find the matching monitor
161
+
162
+ For a given string ( -u, --url ) find any monitor which has a URL value that contains string
163
+
164
+ For a given tag ( -t, --tag ) find any montior with a matching tag
165
+ LONGDESC
166
+ def http()
167
+ this = Backend.new
168
+ this.search_http(check_options(options), check_style(options[:style]))
169
+ end
170
+
171
+ desc "page --[OPTION]=[VALUE] --style=[STYLE]", "Search all fullpage tests"
172
+ long_desc <<-LONGDESC
173
+
174
+ LONGDESC
175
+ def page()
176
+ this = Backend.new
177
+ this.search_fullpage(check_options(options), check_style(options[:style]))
178
+ end
179
+
180
+ no_commands do
181
+ def log(str)
182
+ puts str if options[:verbose]
183
+ end
184
+ end
185
+ end
186
+
187
+ class Runner < Thor
188
+ class_option :verbose, :aliases => '-v', :type => :boolean
189
+
190
+ desc "list [COMMAND] [ARGS]", "Perform list operations"
191
+ subcommand "list", List
192
+
193
+ desc "add [COMMAND] [ARGS]", "Perform add operations"
194
+ subcommand "add", Add
195
+
196
+ desc "del [COMMAND] [ARGS]", "Perform delete operations"
197
+ subcommand "del", Del
198
+
199
+ desc "search [COMMAND] [ARGS]", "Perform search operations"
200
+ subcommand "search", Search
201
+
202
+ no_commands do
203
+ def log(str)
204
+ puts str if options[:verbose]
205
+ end
206
+ end
207
+ end
208
+ end
@@ -0,0 +1,105 @@
1
+ require 'csv'
2
+
3
+ module InstaMonitis
4
+ module UserInput
5
+ @@input = [ 'name', 'url', 'tag', 'loc' ]
6
+
7
+ def userinput_http test
8
+ @@input.each do | input |
9
+ test.send "set_#{input}", ( send "get_#{input}" )
10
+ end
11
+ end
12
+
13
+ def userinput_fullpage test
14
+ @@input.each do | input |
15
+ test.send "set_#{input}", ( send "get_#{input}" )
16
+ end
17
+ test.set_chk get_chkint test
18
+ end
19
+
20
+ def userinput_import file
21
+ array = []
22
+ errors = []
23
+ puts "File valid. Importing.\n"
24
+ CSV.foreach(file.to_s, :headers => true, :skip_blanks => true) do |row|
25
+ if row['type'] == 'http'
26
+ begin
27
+ array << cast_to_http(row)
28
+ rescue
29
+ errors << $INPUT_LINE_NUMBER.to_s
30
+ end
31
+ elsif row['type'] == 'fullpage'
32
+ begin
33
+ array << cast_to_fullpage(row)
34
+ rescue => e
35
+ puts e.message
36
+ errors << $INPUT_LINE_NUMBER.to_s
37
+ end
38
+ else
39
+ errors << $INPUT_LINE_NUMBER.to_s
40
+ end
41
+ end
42
+
43
+ if errors.empty?
44
+ return array
45
+ else
46
+ errors.each do |line|
47
+ puts "\tRow ##{line} can't be cast into HTTPMonitor or FullPageMonitor"
48
+ end
49
+ puts "\nFix or remove the above row(s) to proceed\n"
50
+ exit
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def get_name
57
+ puts "Enter name:"
58
+ name = STDIN.gets
59
+ return name.chomp!
60
+ end
61
+
62
+ def get_tag
63
+ puts "Tag:"
64
+ tag = STDIN.gets
65
+ return tag.chomp!
66
+ end
67
+
68
+ def get_url
69
+ puts "URL:"
70
+ url = STDIN.gets
71
+ if url.include?('http:') || url.include?('https:')
72
+ puts "URL can't contain http://\n"
73
+ get_url
74
+ else
75
+ return url.chomp!
76
+ end
77
+ end
78
+
79
+ def get_loc
80
+ puts "1=MID,9=EST,10=WST,26=NY,27=LV"
81
+ puts "Locations:"
82
+ return STDIN.gets.chomp!.split(" ").map(&:to_i)
83
+ end
84
+
85
+ def get_chkint test
86
+ puts "Check interval:"
87
+ return STDIN.gets.chomp!.split(" ").map(&:to_i)
88
+ end
89
+
90
+ def cast_to_http row
91
+ this = HTTPMonitor.new
92
+ this.from_hash! row.to_hash
93
+ this.set_loc row.to_hash["locationIds"].split(' ')
94
+ return this
95
+ end
96
+
97
+ def cast_to_fullpage row
98
+ this = FullPageMonitor.new
99
+ this.from_hash! row.to_hash
100
+ this.set_loc row.to_hash["locationIds"].split(' ')
101
+ this.set_chk row.to_hash["checkInterval"].split(' ')
102
+ return this
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module InstaMonitis
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insta-monitis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Pramann
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A CLI which allows you to create and list HTTP and Full Page Load tests
14
+ in Monitis
15
+ email:
16
+ - dpramann@lakana.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/insta-monitis
30
+ - bin/setup
31
+ - insta-monitis.gemspec
32
+ - lib/insta-monitis.rb
33
+ - lib/insta-monitis/api.rb
34
+ - lib/insta-monitis/backend.rb
35
+ - lib/insta-monitis/configurator.rb
36
+ - lib/insta-monitis/monitors.rb
37
+ - lib/insta-monitis/runner.rb
38
+ - lib/insta-monitis/userinput.rb
39
+ - lib/insta-monitis/ver/version.rb
40
+ homepage: https://github.com/praymann/insta-monitis
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.4.8
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: CLI interface for Monitis API
64
+ test_files: []