malsh 0.1.4 → 0.1.5

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: c78220f9cf431e8bf350ad18ce99eae1fb3ff221
4
- data.tar.gz: 9a0a84bd01564804778fda68bcece6c06c17d854
3
+ metadata.gz: f3f9b890ede7e635dfaadf659beb03d32e373836
4
+ data.tar.gz: 32aa5fa95ccd58e26e16b12d628ec7408416b67b
5
5
  SHA512:
6
- metadata.gz: 8afbff480ce63c1af03370bf5a085a811f923088507e3433af30ef4d5991d239d311b23d2c159ef0bd3924db6ba76d48b780de334df9802e684269ba9375c966
7
- data.tar.gz: fe62b74f8ac35e0b99f44eb7e279e6ca879b88ed3719676b381ed489c18d725d1f37c3a074b42d3d4607557fed78808b9ed59df952766532a7b61a6518d69d60
6
+ metadata.gz: 76a2f05c3b3164c017cb863999a2317041722a2911b480c35c6ee081ec702c7b892653e0138a456c5596ccad0700110013b7fe7cb5098a0ecbbf9ab875f8f83c
7
+ data.tar.gz: ca0d99f7b4d13d13813f20d00aec17c9d69a0b92a40fb87bf4c2bd5f4bcfab9295a032ffed57f0ec66d6f6270113e889a59a8713fb1a9c3e530d1e41082cf3b1
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Malsh
2
2
  *Mackerel Sheperd = malsh*
3
3
 
4
- [![Build Status](https://travis-ci.org/pyama86/sac.svg)](https://travis-ci.org/pyama86/sac)
4
+ [![Build Status](https://travis-ci.org/pyama86/malsh.svg)](https://travis-ci.org/pyama86/malsh)
5
5
 
6
6
  [![Code Climate](https://codeclimate.com/github/pyama86/sac/badges/gpa.svg)](https://codeclimate.com/github/pyama86/sac)
7
7
 
data/lib/malsh/cli.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'malsh'
2
+ require 'parallel'
2
3
  require 'pp'
3
4
  module Malsh
4
5
  class CLI < Thor
@@ -13,11 +14,12 @@ module Malsh
13
14
  def retire
14
15
  Malsh.init options
15
16
 
16
- names = Malsh.metrics('loadavg5').map do|lvg|
17
+ host_names = Parallel.map(Malsh.metrics('loadavg5')) do|lvg|
17
18
  host = Malsh.host_by_id lvg.first
18
19
  host.name if (!lvg.last.loadavg5.respond_to?(:value) || !lvg.last.loadavg5.value)
19
20
  end.flatten.compact
20
- Malsh.notify("退役未了ホスト一覧", names)
21
+
22
+ Malsh.notify("退役未了ホスト一覧", host_names)
21
23
  end
22
24
 
23
25
  desc 'find', 'find by hostname'
@@ -25,22 +27,40 @@ module Malsh
25
27
  def find
26
28
  Malsh.init options
27
29
 
28
- names = Malsh.hosts.map do |h|
30
+ host_names = Parallel.map(Malsh.hosts) do |h|
29
31
  h.name if options[:regexp].find{|r| h.name.match(/#{r}/)}
30
32
  end.flatten.compact
31
33
 
32
- Malsh.notify("不要ホスト候補一覧", names.flatten.compact)
34
+ Malsh.notify("不要ホスト候補一覧", host_names)
33
35
  end
34
36
 
35
37
  desc 'maverick', 'check no role'
36
38
  def maverick
37
39
  Malsh.init options
38
40
 
39
- names = Malsh.hosts.map do |h|
41
+ host_names = Parallel.map(Malsh.hosts) do |h|
40
42
  h.name if h.roles.keys.size < 1
41
43
  end.flatten.compact
42
44
 
43
- Malsh.notify("ロール無所属ホスト一覧", names.flatten.compact)
45
+ Malsh.notify("ロール無所属ホスト一覧", host_names)
46
+ end
47
+
48
+ desc 'obese', 'check obese hosts'
49
+ option :past_date , :type => :numeric, :aliases => :p
50
+ option :cpu_threshold, :type => :numeric, :aliases => :c
51
+ option :memory_threshold, :type => :numeric, :aliases => :m
52
+ def obese
53
+ _host_names = {}
54
+ Malsh.init options
55
+ now = Time.now.to_i
56
+ # 7 = 1week
57
+ from = now - (options[:past_date] || 7) * 86400
58
+
59
+ hosts = Malsh.hosts
60
+ Object.const_get("Malsh::HostMetrics").constants.each do |c|
61
+ hosts = Object.const_get("Malsh::HostMetrics::#{c}").check(hosts, from, now)
62
+ end
63
+ Malsh.notify("余剰リソースホスト一覧", hosts.compact.map {|h| h["name"]})
44
64
  end
45
65
 
46
66
  map %w[--version -v] => :__print_version
@@ -0,0 +1,54 @@
1
+ module Malsh::HostMetrics
2
+ class Base
3
+ class << self
4
+ def check(hosts, from, to)
5
+ if Malsh.options[option_name]
6
+ Parallel.map(hosts) do |h|
7
+ value = get_max_usage(h, from, to) if h
8
+ h if value && lower?(value)
9
+ end || []
10
+ else
11
+ hosts
12
+ end
13
+ end
14
+
15
+ def resources
16
+ []
17
+ end
18
+
19
+ def get_max_value(host_metrics)
20
+ []
21
+ end
22
+
23
+ def normalize_result(max, host)
24
+ []
25
+ end
26
+
27
+ def all_keys?(metrics)
28
+ resources.all? {|k| metrics.keys.include?(k) }
29
+ end
30
+
31
+ def lower?(value)
32
+ value < Malsh.options[option_name]
33
+ end
34
+
35
+ def option_name
36
+ nil
37
+ end
38
+
39
+ def get_max_usage(host, from, to)
40
+ host_metrics = get_host_metrics(host, from, to)
41
+ max = get_max_value(host_metrics) if host_metrics
42
+ normalize_result(max, host) if max
43
+ end
44
+
45
+ def get_host_metrics(host, from, to)
46
+ resources.each_with_object(Hash.new { |h,k| h[k] = {} }) do |name,hash|
47
+ Malsh.host_metrics(host["id"], name, from, to).metrics.each do |v|
48
+ hash[v.time][name] = v.value
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,23 @@
1
+ module Malsh::HostMetrics
2
+ class Cpu < Base
3
+ class << self
4
+ def resources
5
+ %w(cpu.user.percentage cpu.iowait.percentage cpu.system.percentage)
6
+ end
7
+
8
+ def get_max_value(host_metrics)
9
+ host_metrics.max_by do |time,cpu|
10
+ resources.map { |name| cpu[name] }.inject(:+) if all_keys?(cpu)
11
+ end
12
+ end
13
+
14
+ def normalize_result(max, host)
15
+ resources.map { |name| max.last[name] }.inject(:+) / host["meta"]["cpu"].size
16
+ end
17
+
18
+ def option_name
19
+ :cpu_threshold
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module Malsh::HostMetrics
2
+ class Memory < Base
3
+ class << self
4
+ def resources
5
+ %w(memory.total memory.used memory.cached)
6
+ end
7
+
8
+ def get_max_value(host_metrics)
9
+ host_metrics.max_by do |time,memory|
10
+ resources.map { |name| memory[name] }.inject(:+) if all_keys?(memory)
11
+ (memory["memory.used"] + memory["memory.cached"]) / memory["memory.total"] * 100 if all_keys?(memory)
12
+ end
13
+ end
14
+
15
+ def normalize_result(max, host)
16
+ (max.last["memory.used"] + max.last["memory.cached"]) / max.last["memory.total"] * 100
17
+ end
18
+
19
+ def option_name
20
+ :memory_threshold
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module Malsh
2
+ module HostMetrics
3
+ autoload :Base, 'malsh/host_metrics/base'
4
+ autoload :Cpu, 'malsh/host_metrics/cpu'
5
+ autoload :Memory, 'malsh/host_metrics/memory'
6
+ end
7
+ end
data/lib/malsh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Malsh
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/malsh.rb CHANGED
@@ -3,46 +3,56 @@ require 'thor'
3
3
  require 'mackerel-rb'
4
4
  require "malsh/cli"
5
5
  require "malsh/notification"
6
+ require "malsh/host_metrics"
6
7
 
7
8
  module Malsh
9
+ class << self
10
+ def notify(subject, host)
11
+ Malsh::Notification.constants.each do |c|
12
+ Object.const_get("Malsh::Notification::#{c}").notify(options[:subject] || subject, host)
13
+ end
14
+ end
8
15
 
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)
16
+ def options(ops=nil)
17
+ @_options = ops if ops
18
+ @_options
12
19
  end
13
- end
14
20
 
15
- def self.options(ops=nil)
16
- @_options = ops if ops
17
- @_options
18
- end
21
+ def init(options)
22
+ if !ENV['MACKEREL_APIKEY'] && !options[:api_key]
23
+ puts "must set be mackerel api key <--api-key> or ENV['MACKEREL_APIKEY']"
24
+ exit
25
+ end
19
26
 
20
- def self.init(options)
21
- if !ENV['MACKEREL_APIKEY'] && !options[:api_key]
22
- puts "must set be mackerel api key <--api-key> or ENV['MACKEREL_APIKEY']"
23
- exit
27
+ options options
28
+ Mackerel.configure do |config|
29
+ config.api_key = ENV['MACKEREL_APIKEY'] || options[:api_key]
30
+ end
24
31
  end
25
32
 
26
- self.options options
27
- Mackerel.configure do |config|
28
- config.api_key = ENV['MACKEREL_APIKEY'] || options[:api_key]
33
+ def hosts
34
+ @_hosts ||= Mackerel.hosts.reject {|h| Malsh.options[:invert_match] && Malsh.options[:invert_match].find {|v| h.name.match(/#{v}/)}}
29
35
  end
30
- end
31
36
 
32
- def self.hosts
33
- @_hosts ||= Mackerel.hosts.reject {|h| Malsh.options[:invert_match] && Malsh.options[:invert_match].find {|v| h.name.match(/#{v}/)}}
34
- end
37
+ def host_by_id(id)
38
+ Mackerel.host(id)
39
+ end
35
40
 
36
- def self.host_by_id(id)
37
- hosts.find {|h| h.id == id}
38
- end
41
+ def metrics(name)
42
+ hash = {}
43
+ hosts.map(&:id).each_slice(200) do |ids|
44
+ hash.merge!(Mackerel.latest_tsdb({hostId: ids, name: name}))
45
+ end
46
+ hash
47
+ end
39
48
 
40
- def self.metrics(name)
41
- hash = {}
42
- self.hosts.map(&:id).each_slice(200) do |ids|
43
- hash.merge!(Mackerel.latest_tsdb({hostId: ids, name: name}))
49
+ def host_metrics(id, name, from, to)
50
+ begin
51
+ Mackerel.host_metrics(id, name: name, from: from, to: to)
52
+ rescue => e
53
+ puts e
54
+ end
44
55
  end
45
- hash
46
56
  end
47
57
  end
48
58
 
data/malsh.gemspec CHANGED
@@ -23,8 +23,9 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ["lib"]
24
24
 
25
25
  spec.add_dependency 'thor'
26
- spec.add_dependency 'mackerel-rb'
26
+ spec.add_dependency 'mackerel-rb', "~> 0.1.5"
27
27
  spec.add_dependency 'slack-notifier'
28
+ spec.add_dependency 'parallel'
28
29
  spec.add_development_dependency "bundler", "~> 1.9"
29
30
  spec.add_development_dependency "rake", "~> 10.0"
30
31
  spec.add_development_dependency "rspec"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: malsh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuhiko yamahsita
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-23 00:00:00.000000000 Z
11
+ date: 2015-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -26,6 +26,20 @@ dependencies:
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: mackerel-rb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: slack-notifier
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
@@ -39,7 +53,7 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: slack-notifier
56
+ name: parallel
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
@@ -114,6 +128,10 @@ files:
114
128
  - exe/malsh
115
129
  - lib/malsh.rb
116
130
  - lib/malsh/cli.rb
131
+ - lib/malsh/host_metrics.rb
132
+ - lib/malsh/host_metrics/base.rb
133
+ - lib/malsh/host_metrics/cpu.rb
134
+ - lib/malsh/host_metrics/memory.rb
117
135
  - lib/malsh/notification.rb
118
136
  - lib/malsh/notification/base.rb
119
137
  - lib/malsh/notification/slack.rb
@@ -139,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
157
  version: '0'
140
158
  requirements: []
141
159
  rubyforge_project:
142
- rubygems_version: 2.4.6
160
+ rubygems_version: 2.4.8
143
161
  signing_key:
144
162
  specification_version: 4
145
163
  summary: mackerel tools.