malsh 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: a4091cecd7c1b713e374385947630961dd257301
4
+ data.tar.gz: 59fc9bd0b6bf05ee4ee620f7c0420c520215604f
5
+ SHA512:
6
+ metadata.gz: d788c9bbbe4e5b1a25282e09aa47f7ec595b7e2b739782a19ffe97fe6cc82112330d27f2acb22b2bf0904647de69d58033b0047c4be37e079cc9ed9b83548c63
7
+ data.tar.gz: 245496b0d95e204329da656d47e2f66c43414acfcc86af7003692a4608c30202bfbe2f7a10afadd7b1a0e7ee4f39640c0725f6df6547dc672ab82057b4c72767
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in malsh.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 kazuhiko yamahsita
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,60 @@
1
+ # Malsh
2
+ *Mackerel Sheperd = malsh*
3
+
4
+ [![Build Status](https://travis-ci.org/pyama86/sac.svg)](https://travis-ci.org/pyama86/sac)
5
+
6
+ [![Code Climate](https://codeclimate.com/github/pyama86/sac/badges/gpa.svg)](https://codeclimate.com/github/pyama86/sac)
7
+
8
+
9
+ ### mackerelの運用で便利な下記の機能を提供します。
10
+
11
+ * 退役忘れホストの検知
12
+ * developmentっぽいホストの検知
13
+ * ロールに紐付いてないホストの検知
14
+
15
+ ### 通知機能
16
+ * Slackへの通知
17
+
18
+ ## Installation
19
+
20
+ ```ruby
21
+ gem install malsh
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ```sh
27
+ $ export MACKEREL_APIKEY=xxxxxxxxxxxxxxxxxxxxxxxxxx
28
+
29
+ # 過去5分間メトリックの投稿がないホストを検知
30
+ $ malsh retire --subject 退役忘れホスト
31
+
32
+ # 指定された単語がホスト名に含まれるホストを検知
33
+ $ malsh find --regex dev local$ --subject developmentホスト
34
+
35
+ # ロールに所属していないホストを検知
36
+ $ malsh maverick --subject 無所属ホスト
37
+ ```
38
+
39
+ ## options
40
+ * --slack-webhook or ENV["SLACK_WEBHOOK"]
41
+
42
+ slackのwebhookurlを指定するとslackに通知することができます。
43
+ * --slack-channel or ENV["SLACK_CHANNEL"]
44
+
45
+ slackの通知先チャネルを指定します。
46
+ * --slack-user or ENV["SLACK_USER"]
47
+
48
+ slackの通知ユーザーを指定します。
49
+ * --api-key or ENV["MACKEREL_APIKEY"]
50
+
51
+ mackerelのAPIキーを指定します。
52
+ * --subject
53
+
54
+ 通知のタイトルを指定します
55
+ * --invert-match
56
+
57
+ 除外したいホスト名を正規表現で指定します。
58
+
59
+ ## Author
60
+ pyama86
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "rspec/core/rake_task"
2
+ require "bundler/gem_tasks"
3
+ RSpec::Core::RakeTask.new("spec")
4
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "malsh"
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
data/bin/setup ADDED
@@ -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
data/exe/malsh ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'malsh'
3
+ Malsh::CLI.start
data/lib/malsh.rb ADDED
@@ -0,0 +1,44 @@
1
+ require "malsh/version"
2
+ require 'thor'
3
+ require 'mackerel-rb'
4
+ require "malsh/cli"
5
+ require "malsh/notification"
6
+
7
+ module Malsh
8
+
9
+ def self.notify(subject, host)
10
+ Malsh::Notification.constants.each do |c|
11
+ Object.const_get("Malsh::Notification::#{c}").notify(self.options[:subject] || subject, host)
12
+ end
13
+ end
14
+
15
+ def self.options(ops=nil)
16
+ @_options = ops if ops
17
+ @_options
18
+ end
19
+
20
+ def self.init(options)
21
+ self.options options
22
+ Mackerel.configure do |config|
23
+ config.api_key = ENV['MACKEREL_APIKEY'] || options[:api_key]
24
+ end
25
+ end
26
+
27
+ def self.hosts
28
+ @_hosts ||= Mackerel.hosts.reject {|h| Malsh.options[:invert_match] && Malsh.options[:invert_match].find {|v| h.name.match(/#{v}/)}}
29
+ end
30
+
31
+ def self.host_by_id(id)
32
+ hosts.find {|h| h.id == id}
33
+ end
34
+
35
+ def self.metrics(name)
36
+ hash = {}
37
+ self.hosts.map(&:id).each_slice(200) do |ids|
38
+ hash.merge!(Mackerel.latest_tsdb({hostId: ids, name: name}))
39
+ end
40
+ hash
41
+ end
42
+ end
43
+
44
+
data/lib/malsh/cli.rb ADDED
@@ -0,0 +1,56 @@
1
+ require 'malsh'
2
+ require 'pp'
3
+ module Malsh
4
+ class CLI < Thor
5
+ class_option :slack_webhook, :type => :string, :aliases => :sw
6
+ class_option :slack_channel, :type => :string, :aliases => :sc
7
+ class_option :slack_user, :type => :string, :aliases => :su
8
+ class_option :api_key, :type => :string, :aliases => :k
9
+ class_option :subject, :type => :string, :aliases => :s
10
+ class_option :invert_match, :type => :array, :aliases => :v
11
+
12
+ desc 'retire', 'check forget retire host'
13
+ def retire
14
+ Malsh.init options
15
+
16
+ names = Malsh.metrics('loadavg5').map do|lvg|
17
+ host = Malsh.host_by_id lvg.first
18
+ host.name if (!lvg.last.loadavg5.respond_to?(:value) || !lvg.last.loadavg5.value)
19
+ end.flatten.compact
20
+ Malsh.notify("退役未了ホスト一覧", names)
21
+ end
22
+
23
+ desc 'find', 'find by hostname'
24
+ option :regexp, :require => true, :type => :array, :aliases => :e
25
+ def find
26
+ Malsh.init options
27
+
28
+ names = Malsh.hosts.map do |h|
29
+ h.name if options[:regexp].find{|r| h.name.match(/#{r}/)}
30
+ end.flatten.compact
31
+
32
+ Malsh.notify("不要ホスト候補一覧", names.flatten.compact)
33
+ end
34
+
35
+ desc 'maverick', 'check no role'
36
+ def maverick
37
+ Malsh.init options
38
+
39
+ names = Malsh.hosts.map do |h|
40
+ h.name if h.roles.keys.size < 1
41
+ end.flatten.compact
42
+
43
+ Malsh.notify("ロール無所属ホスト一覧", names.flatten.compact)
44
+ end
45
+
46
+ map %w[--version -v] => :__print_version
47
+ desc "--version, -v", "print the version"
48
+ def __print_version
49
+ Malsh.options = options
50
+ puts Pec::VERSION
51
+ end
52
+
53
+ no_commands do
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ module Malsh
2
+ module Notification
3
+ autoload :Base, "malsh/notification/base"
4
+ autoload :Slack, "malsh/notification/slack"
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ module Malsh::Notification
2
+ class Base
3
+ def self.notify(subject, hosts)
4
+ puts "#{subject}: \n#{hosts.join("\n")}" if hosts.size > 0 && doit?
5
+ end
6
+
7
+ def self.doit?
8
+ true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ require 'slack-notifier'
2
+ module Malsh::Notification
3
+ class Slack < Base
4
+ def self.notify(subject, hosts)
5
+ return unless doit?
6
+ note = {
7
+ text: hosts.join("\n"),
8
+ color: "warning"
9
+ }
10
+ notifier.ping "*#{subject}*", attachments: [note]
11
+ end
12
+
13
+ def self.doit?
14
+ %i(slack_webhook slack_channel).all? {|k| Malsh.options.key?(k) || ENV[k.to_s.upcase] }
15
+ end
16
+
17
+ def self.notifier
18
+ ::Slack::Notifier.new(
19
+ ENV["SLACK_WEBHOOK"] || Malsh.options[:slack_webhook],
20
+ channel: ENV["SLACK_CHANNEL"] || Malsh.options[:slack_channel],
21
+ username: ENV["SLACK_USER"] || Malsh.options[:slack_user] || 'Mackerel-Check',
22
+ link_names: 1
23
+ )
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Malsh
2
+ VERSION = "0.1.0"
3
+ end
data/malsh.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'malsh/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "malsh"
8
+ spec.version = Malsh::VERSION
9
+ spec.authors = ["kazuhiko yamahsita"]
10
+ spec.email = ["pyama@pepabo.com"]
11
+
12
+ spec.summary = %q{mackerel tools.}
13
+ spec.description = %q{mackerel tools.}
14
+ spec.homepage = "http://ten-snapon.com."
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
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency 'thor'
26
+ spec.add_dependency 'mackerel-rb'
27
+ spec.add_dependency 'slack-notifier'
28
+ spec.add_development_dependency "bundler", "~> 1.9"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec"
31
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: malsh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kazuhiko yamahsita
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-23 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: mackerel-rb
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: slack-notifier
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: mackerel tools.
98
+ email:
99
+ - pyama@pepabo.com
100
+ executables:
101
+ - malsh
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/malsh
115
+ - lib/malsh.rb
116
+ - lib/malsh/cli.rb
117
+ - lib/malsh/notification.rb
118
+ - lib/malsh/notification/base.rb
119
+ - lib/malsh/notification/slack.rb
120
+ - lib/malsh/version.rb
121
+ - malsh.gemspec
122
+ homepage: http://ten-snapon.com.
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.6
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: mackerel tools.
146
+ test_files: []