mackerel-rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 649b663e0e707d1c964f373805d78d4cc45a20d3
4
+ data.tar.gz: a5ae657699ea0e62ea3b507de860b906ec9233c6
5
+ SHA512:
6
+ metadata.gz: 69eca9890c1c50f16c070ebdffe8a52793ff1eed64595ad6b5e052bc9197f53c4d8dd151621ee1b9fec8e108e9c3dd7681deed8376e85b14cc4dc49d19d66bdd
7
+ data.tar.gz: 093acecf8a73cde69d1b95661949ad05f150a8077dcd376d2024f8cfcc7fcc114c11e536dfcdae0f6a03694869e50a36cf6905760a1e8e11ef540e029b6e9c73
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.vagrant
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /bin/console
7
+ /bin/setup
8
+ /coverage/
9
+ /doc/
10
+ /pkg/
11
+ /spec/reports/
12
+ /tmp/
13
+ **/*.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install:
5
+ - gem update bundler
6
+
7
+ script:
8
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mackerel-rb.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'webmock'
9
+ end
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # mackerel-rb
2
+ [![Build Status](https://travis-ci.org/mizoR/mackerel-rb.svg)](https://travis-ci.org/mizoR/mackerel-rb)
3
+ [![Code Climate](https://codeclimate.com/github/mizoR/mackerel-rb/badges/gpa.svg)](https://codeclimate.com/github/mizoR/mackerel-rb)
4
+ [![Dependency Status](https://gemnasium.com/mizoR/mackerel-rb.svg)](https://gemnasium.com/mizoR/mackerel-rb)
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ $ gem 'mackerel-rb'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mackerel-rb
20
+
21
+ ## Usage
22
+
23
+ ```rb
24
+ Mackerel.configure do |config|
25
+ config.api_key = "<Your API Key>" # Default: ENV['MACKEREL_APIKEY']
26
+ end
27
+
28
+ hosts = Mackerel.hosts
29
+ hosts.each do |host|
30
+ puts host
31
+ #=> #<Hashie::Mash createdAt=1437422856 displayName="www001.mackerel-rb.dev" id="Dummmmmmmmy", ..., status="standby" type="unknown">
32
+ end
33
+ ```
34
+
35
+ ## Feature
36
+
37
+ - [x] `GET /api/v0/hosts.json`
38
+ - [x] `POST /api/v0/hosts`
39
+ - [x] `GET /api/v0/hosts/<hostId>`
40
+ - [x] `PUT /api/v0/hosts/<hostId>`
41
+ - [x] `POST /api/v0/hosts/<hostId>/status`
42
+ - [x] `POST /api/v0/hosts/<hostId>/retire`
43
+ - [ ] `POST /api/v0/tsdb`
44
+ - [x] `GET /api/v0/tsdb/latest`
45
+ - [ ] `POST /api/v0/services/<serviceName>/tsdb`
46
+ - [ ] `POST /api/v0/monitoring/checks/report`
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/mizoR/mackerel-rb/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/Vagrantfile ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ Vagrant.configure(2) do |config|
4
+ config.vm.define 'www001.mackerel-rb.dev' do
5
+ config.vm.box = "puppetlabs/centos-7.0-64-puppet"
6
+ config.vm.hostname = 'www001.mackerel-rb.dev'
7
+ config.vm.network "private_network", ip: '192.168.33.2'
8
+ config.vm.provision :shell, inline: <<-SHELL
9
+ curl -fsSL https://mackerel.io/assets/files/scripts/setup-yum.sh | sh
10
+ sudo yum install -y mackerel-agent
11
+ sudo cat << 'EOF' > /etc/mackerel-agent/mackerel-agent.conf
12
+ apikey="#{ENV['MACKEREL_APIKEY']}"
13
+ EOF
14
+ sudo /etc/init.d/mackerel-agent restart
15
+ SHELL
16
+
17
+ config.vm.provider "virtualbox" do |vb|
18
+ vb.name = 'www001.mackerel-rb.dev'
19
+ vb.memory = 512
20
+ vb.cpus = 1
21
+ end
22
+ end
23
+ end
data/bin/mackerel ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mackerel'
4
+ require 'mackerel/cli'
5
+
6
+ Mackerel::CLI.start(ARGV)
@@ -0,0 +1 @@
1
+ require 'mackerel'
data/lib/mackerel.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "mackerel/version"
2
+ require "mackerel/client"
3
+
4
+ module Mackerel
5
+ class << self
6
+ private
7
+
8
+ def respond_to_missing?(method_name, include_private=false)
9
+ client.respond_to?(method_name, include_private)
10
+ end
11
+
12
+ def method_missing(method_name, *args, &block)
13
+ if client.respond_to?(method_name)
14
+ client.send(method_name, *args, &block)
15
+ else
16
+ super
17
+ end
18
+ end
19
+
20
+ def client
21
+ @client ||= Client.new
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'json'
2
+ require 'thor'
3
+ require 'mackerel/commands/host'
4
+ require 'mackerel/commands/tsdb'
5
+
6
+ module Mackerel
7
+ class CLI < Thor
8
+ desc 'host <subcommand>', 'Commands about hosts control'
9
+ subcommand('hosts', Commands::Host)
10
+
11
+ desc 'tsdb <subcommand>', 'Commands about hosts control'
12
+ subcommand('tsdb', Commands::Tsdb)
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ require 'mackerel/configuration'
2
+ require 'mackerel/connection'
3
+ require 'mackerel/client/host'
4
+ require 'mackerel/client/host_status'
5
+ require 'mackerel/client/tsdb'
6
+ require 'mackerel/response/raise_error'
7
+
8
+ module Mackerel
9
+ class Client
10
+ include Configuration
11
+ include Connection
12
+
13
+ include Client::Host
14
+ include Client::HostStatus
15
+ include Client::Tsdb
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module Mackerel
2
+ class Client
3
+ module Host
4
+ def hosts(options = {})
5
+ response = get 'hosts.json', options
6
+ response.body.hosts
7
+ end
8
+
9
+ def host(id)
10
+ response = get "hosts/#{id}"
11
+ response.body.host
12
+ end
13
+
14
+ def create_host(options)
15
+ response = post "hosts"
16
+ response.body.host
17
+ end
18
+
19
+ def update_host(id, options)
20
+ response = put "hosts/#{id}"
21
+ response.body
22
+ end
23
+
24
+ def retire_host(id)
25
+ response = post "hosts/#{id}/retire"
26
+ response.body
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ module Mackerel
2
+ class Client
3
+ module HostStatus
4
+ def update_host_status(id, options)
5
+ response = post "hosts/#{id}/status", options
6
+ response.body
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Mackerel
2
+ class Client
3
+ module Tsdb
4
+ def latest_tsdb(params)
5
+ response = get 'tsdb/latest', params
6
+ response.body.tsdbLatest
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ require 'mackerel/commands/host_status'
2
+
3
+ module Mackerel
4
+ module Commands
5
+ class Host < Thor
6
+ desc 'list', 'List hosts'
7
+ option "service", type: :string, desc: 'service name'
8
+ option "role", type: :array, desc: 'role names in the service, multiple assignments possible'
9
+ option "name", type: :string, desc: 'host name'
10
+ option "status", type: :array, desc: 'extract host status, multiple assignments possible'
11
+ def list
12
+ puts JSON.pretty_generate Mackerel.hosts(options)
13
+ end
14
+
15
+ desc 'show <hostId>', 'Show host'
16
+ def show(id)
17
+ puts JSON.pretty_generate Mackerel.host(id)
18
+ end
19
+
20
+ desc 'status <subcommand>', 'Commands about host status control'
21
+ subcommand('status', Commands::HostStatus)
22
+
23
+ desc 'retire ID', 'Retire host'
24
+ def retire(id)
25
+ puts JSON.pretty_generate Mackerel.retire_host(id)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module Mackerel
2
+ module Commands
3
+ class HostStatus < Thor
4
+ namespace 'host status'
5
+
6
+ desc 'update <hostId> --status <hostStatus>', 'Update host status'
7
+ option 'status', type: :string, required: true, desc: 'host status'
8
+ def update(id)
9
+ puts JSON.pretty_generate Mackerel.update_host_status(id, options)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Mackerel
2
+ module Commands
3
+ class Tsdb < Thor
4
+ desc 'latest [option]', 'Show latest TSDB'
5
+ option "hostId", type: :array, desc: 'host ID, multiple assignments possible'
6
+ option "name", type: :array, desc: 'metric name, multiple assignments possible'
7
+ def latest
8
+ puts JSON.pretty_generate Mackerel.latest_tsdb(options)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,38 @@
1
+ module Mackerel
2
+ module Configuration
3
+ def config
4
+ @config ||= Config.new
5
+ end
6
+
7
+ def configure
8
+ yield config
9
+ end
10
+
11
+ class Config
12
+ attr_accessor :api_endpoint, :api_key, :http_accept, :params_encoder, :request, :user_agent
13
+
14
+ def initialize
15
+ @api_endpoint = 'https://mackerel.io/api/v0'
16
+ @api_key = ENV['MACKEREL_APIKEY']
17
+ @http_accept = 'application/json'
18
+ @user_agent = "Mackerel.rb Ruby Gem #{Mackerel::VERSION}"
19
+ @params_encoder = Faraday::FlatParamsEncoder
20
+ end
21
+
22
+ def request
23
+ {
24
+ params_encoder: params_encoder
25
+ }
26
+ end
27
+
28
+ def http_request_headers
29
+ headers = {
30
+ 'Accept' => http_accept,
31
+ 'User-Agent' => user_agent,
32
+ 'X-Api-Key' => api_key,
33
+ }
34
+ headers.select {|_, v| !!v}
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ require 'hashie'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+
5
+ module Mackerel
6
+ module Connection
7
+ def get(url, params = {})
8
+ connection.get(url, params)
9
+ end
10
+
11
+ def post(url, params = {})
12
+ connection.post(url) do |req|
13
+ req.headers['Content-Type'] = 'application/json'
14
+ req.body = params.to_json
15
+ end
16
+ end
17
+
18
+ def request(method, url, params = {})
19
+ connection.send(method, url, params)
20
+ end
21
+
22
+ def connection
23
+ @connection ||= Faraday.new(url: config.api_endpoint, headers: config.http_request_headers, request: config.request) do |faraday|
24
+ faraday.use Mackerel::Response::RaiseError
25
+ faraday.use Faraday::Response::Logger if ENV['DEBUG'] == '1'
26
+ faraday.use FaradayMiddleware::Mashify
27
+ faraday.use FaradayMiddleware::ParseJson
28
+ faraday.adapter Faraday.default_adapter
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,52 @@
1
+ module Mackerel
2
+ class Error < StandardError
3
+ attr_reader :response
4
+
5
+ def self.from_response(response)
6
+ status = response.status.to_i
7
+
8
+ klass = case status
9
+ when 400 then BadRequest
10
+ when 400..499 then ClientError
11
+ when 500 then InternalServerError
12
+ when 500..599 then ServerError
13
+ end
14
+
15
+ klass.new(response) if klass
16
+ end
17
+
18
+ def initialize(response)
19
+ @response = response
20
+ super(build_error_message)
21
+ end
22
+
23
+ private
24
+
25
+ def build_error_message
26
+ return nil if @response.nil?
27
+
28
+ message = "#{@response.method.to_s.upcase} "
29
+ message << "#{@response.url.to_s}: "
30
+ message << "#{@response.status}"
31
+ message << " - #{response_error}>" if response_error
32
+ message
33
+ end
34
+
35
+ def response_error
36
+ data = @response.body
37
+ data.error if data.respond_to?(:error) && data.error
38
+ end
39
+ end
40
+
41
+ class ClientError < Error
42
+ end
43
+
44
+ class BadRequest < ClientError
45
+ end
46
+
47
+ class ServerError < Error
48
+ end
49
+
50
+ class InternalServerError < ServerError
51
+ end
52
+ end
@@ -0,0 +1,15 @@
1
+ require 'faraday'
2
+ require 'mackerel/error'
3
+
4
+ module Mackerel
5
+ module Response
6
+ class RaiseError < Faraday::Response::Middleware
7
+ private
8
+
9
+ def on_complete(response)
10
+ error = Mackerel::Error.from_response(response)
11
+ raise error if error
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Mackerel
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mackerel/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mackerel-rb"
8
+ spec.version = Mackerel::VERSION
9
+ spec.authors = ["mizokami"]
10
+ spec.email = ["suzunatsu@yahoo.com"]
11
+
12
+ spec.summary = %q{Yet another mackerel client for Ruby.}
13
+ spec.description = %q{Yet another mackerel client for Ruby.}
14
+ spec.homepage = "https://github.com/mizoR/mackerel-rb"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "hashie"
23
+ spec.add_dependency "faraday"
24
+ spec.add_dependency "faraday_middleware"
25
+ spec.add_development_dependency "bundler", "~> 1.9"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mackerel-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mizokami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
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: hashie
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: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: Yet another mackerel client for Ruby.
98
+ email:
99
+ - suzunatsu@yahoo.com
100
+ executables:
101
+ - mackerel
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - README.md
110
+ - Rakefile
111
+ - Vagrantfile
112
+ - bin/mackerel
113
+ - lib/mackerel-rb.rb
114
+ - lib/mackerel.rb
115
+ - lib/mackerel/cli.rb
116
+ - lib/mackerel/client.rb
117
+ - lib/mackerel/client/host.rb
118
+ - lib/mackerel/client/host_status.rb
119
+ - lib/mackerel/client/tsdb.rb
120
+ - lib/mackerel/commands/host.rb
121
+ - lib/mackerel/commands/host_status.rb
122
+ - lib/mackerel/commands/tsdb.rb
123
+ - lib/mackerel/configuration.rb
124
+ - lib/mackerel/connection.rb
125
+ - lib/mackerel/error.rb
126
+ - lib/mackerel/response/raise_error.rb
127
+ - lib/mackerel/version.rb
128
+ - mackerel-rb.gemspec
129
+ homepage: https://github.com/mizoR/mackerel-rb
130
+ licenses: []
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.4.5
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Yet another mackerel client for Ruby.
152
+ test_files: []