malsh 0.1.4 → 0.1.5
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 +4 -4
- data/README.md +1 -1
- data/lib/malsh/cli.rb +26 -6
- data/lib/malsh/host_metrics/base.rb +54 -0
- data/lib/malsh/host_metrics/cpu.rb +23 -0
- data/lib/malsh/host_metrics/memory.rb +24 -0
- data/lib/malsh/host_metrics.rb +7 -0
- data/lib/malsh/version.rb +1 -1
- data/lib/malsh.rb +37 -27
- data/malsh.gemspec +2 -1
- metadata +22 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3f9b890ede7e635dfaadf659beb03d32e373836
|
4
|
+
data.tar.gz: 32aa5fa95ccd58e26e16b12d628ec7408416b67b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
[](https://travis-ci.org/pyama86/malsh)
|
5
5
|
|
6
6
|
[](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
|
-
|
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
|
-
|
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
|
-
|
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("不要ホスト候補一覧",
|
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
|
-
|
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("ロール無所属ホスト一覧",
|
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
|
data/lib/malsh/version.rb
CHANGED
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
|
-
|
10
|
-
|
11
|
-
|
16
|
+
def options(ops=nil)
|
17
|
+
@_options = ops if ops
|
18
|
+
@_options
|
12
19
|
end
|
13
|
-
end
|
14
20
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
27
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
37
|
+
def host_by_id(id)
|
38
|
+
Mackerel.host(id)
|
39
|
+
end
|
35
40
|
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
+
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
|
+
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:
|
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.
|
160
|
+
rubygems_version: 2.4.8
|
143
161
|
signing_key:
|
144
162
|
specification_version: 4
|
145
163
|
summary: mackerel tools.
|