malsh 0.1.8 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +2 -0
- data/lib/malsh.rb +32 -2
- data/lib/malsh/cli.rb +22 -10
- data/lib/malsh/notification/base.rb +16 -2
- data/lib/malsh/notification/slack.rb +55 -2
- data/lib/malsh/version.rb +1 -1
- data/malsh.gemspec +3 -3
- metadata +18 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '04893527ed6c71386b40145b68b31ae4bacce444e5d3edce147327789b18349b'
|
4
|
+
data.tar.gz: 1ef47a2b0df230ca310900b2036c3ea63ca8ce4f4058e572f9c3154069142563
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea062a8de9ff0d9b425b042b7aff1bb1bbaf43bc4aad3c9f020a936496f07cece395ccb1d0fd4c15fb90789437c95933d0d5287a34f897e43c4339978d394d1c
|
7
|
+
data.tar.gz: 7fbc8dc08cee0e942531086f53640653ca4cf27b7a66b0e836fa19396ae6a53cb20d1466f50ca5c6cb2725031b7541f430b2b2c00d5efd1b08d7acb4e551d021
|
data/README.md
CHANGED
data/lib/malsh.rb
CHANGED
@@ -7,9 +7,15 @@ require "malsh/host_metrics"
|
|
7
7
|
|
8
8
|
module Malsh
|
9
9
|
class << self
|
10
|
-
def
|
10
|
+
def notify_host(subject, host)
|
11
11
|
Malsh::Notification.constants.each do |c|
|
12
|
-
Object.const_get("Malsh::Notification::#{c}").
|
12
|
+
Object.const_get("Malsh::Notification::#{c}").notify_host(options[:subject] || subject, host)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def notify_alert(subject, alerts)
|
17
|
+
Malsh::Notification.constants.each do |c|
|
18
|
+
Object.const_get("Malsh::Notification::#{c}").notify_alert(options[:subject] || subject, alerts)
|
13
19
|
end
|
14
20
|
end
|
15
21
|
|
@@ -35,6 +41,22 @@ module Malsh
|
|
35
41
|
Malsh.options[:invert_match] && Malsh.options[:invert_match].find {|v| host_name(h).match(/#{v}/) }
|
36
42
|
end.reject do |h|
|
37
43
|
Malsh.options[:regexp] && Malsh.options[:regexp].all? {|r| !host_name(h).match(/#{r}/)}
|
44
|
+
end.reject do |h|
|
45
|
+
Malsh.options[:invert_role] && Malsh.options[:invert_role].find do |r|
|
46
|
+
service, role = r.split(/:/)
|
47
|
+
h.roles[service] && h.roles[service].include?(role)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def alerts()
|
53
|
+
@_alerts ||= Mackerel.alerts.map do |alert|
|
54
|
+
if alert.type == 'external'
|
55
|
+
alert['monitor'] = Mackerel.monitor(alert.monitorId)
|
56
|
+
else
|
57
|
+
alert['host'] = Malsh.host_by_id(alert.hostId)
|
58
|
+
end
|
59
|
+
alert
|
38
60
|
end
|
39
61
|
end
|
40
62
|
|
@@ -61,5 +83,13 @@ module Malsh
|
|
61
83
|
puts e
|
62
84
|
end
|
63
85
|
end
|
86
|
+
|
87
|
+
def host_metric_names(id)
|
88
|
+
begin
|
89
|
+
Mackerel.host_metric_names(id)
|
90
|
+
rescue => e
|
91
|
+
puts e
|
92
|
+
end
|
93
|
+
end
|
64
94
|
end
|
65
95
|
end
|
data/lib/malsh/cli.rb
CHANGED
@@ -8,30 +8,36 @@ module Malsh
|
|
8
8
|
class_option :slack_user, :type => :string, :aliases => :su
|
9
9
|
class_option :api_key, :type => :string, :aliases => :k
|
10
10
|
class_option :subject, :type => :string, :aliases => :s
|
11
|
+
class_option :org, :type => :string, :aliases => :o
|
11
12
|
class_option :invert_match, :type => :array, :aliases => :v
|
13
|
+
class_option :invert_role, :type => :array, :aliases => :r
|
12
14
|
class_option :regexp,:type => :array, :aliases => :e
|
13
15
|
|
14
16
|
desc 'retire', 'check forget retire host'
|
15
17
|
def retire
|
16
18
|
Malsh.init options
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
hosts = Parallel.map(Malsh.hosts) do |h|
|
21
|
+
m = Malsh.host_metrics(
|
22
|
+
h.id,
|
23
|
+
Malsh.host_metric_names(h.id).first,
|
24
|
+
Time.now.to_i - 86400,
|
25
|
+
Time.now.to_i
|
26
|
+
)
|
27
|
+
h if !m || m["metrics"].size == 0
|
21
28
|
end.flatten.compact
|
22
|
-
|
23
|
-
Malsh.notify("退役未了ホスト一覧", host_names)
|
29
|
+
Malsh.notify_host("退役未了ホスト一覧", hosts)
|
24
30
|
end
|
25
31
|
|
26
32
|
desc 'maverick', 'check no role'
|
27
33
|
def maverick
|
28
34
|
Malsh.init options
|
29
35
|
|
30
|
-
|
31
|
-
h
|
36
|
+
hosts = Parallel.map(Malsh.hosts) do |h|
|
37
|
+
h if h.roles.keys.size < 1
|
32
38
|
end.flatten.compact
|
33
39
|
|
34
|
-
Malsh.
|
40
|
+
Malsh.notify_host("ロール無所属ホスト一覧", hosts)
|
35
41
|
end
|
36
42
|
|
37
43
|
desc 'search', 'search hosts'
|
@@ -40,14 +46,20 @@ module Malsh
|
|
40
46
|
option :memory_threshold, :type => :numeric, :aliases => :m
|
41
47
|
option :status, :type => :string, :aliases => :st
|
42
48
|
def search
|
43
|
-
_host_names = {}
|
44
49
|
Malsh.init options
|
45
50
|
o = options[:status] ? { status: options[:status] } : {}
|
46
51
|
hosts = Malsh.hosts(o)
|
47
52
|
Object.const_get("Malsh::HostMetrics").constants.each do |c|
|
48
53
|
hosts = Object.const_get("Malsh::HostMetrics::#{c}").check(hosts)
|
49
54
|
end
|
50
|
-
Malsh.
|
55
|
+
Malsh.notify_host("ホスト一覧", hosts.compact)
|
56
|
+
end
|
57
|
+
|
58
|
+
desc 'alert', 'list alerts'
|
59
|
+
def alert
|
60
|
+
Malsh.init options
|
61
|
+
alerts = Malsh.alerts
|
62
|
+
Malsh.notify_alert('アラート一覧', alerts)
|
51
63
|
end
|
52
64
|
|
53
65
|
map %w[--version -v] => :__print_version
|
@@ -1,7 +1,21 @@
|
|
1
1
|
module Malsh::Notification
|
2
2
|
class Base
|
3
|
-
def self.
|
4
|
-
|
3
|
+
def self.notify_host(subject, hosts)
|
4
|
+
names = hosts.map(&:name)
|
5
|
+
puts "#{subject}: \n#{names.join("\n")}" if names.size > 0 && doit?
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.notify_alert(subject, alerts)
|
9
|
+
puts "#{subject}: "
|
10
|
+
alerts.each do |alert|
|
11
|
+
title = case alert.type
|
12
|
+
when 'external'
|
13
|
+
alert.monitor.name
|
14
|
+
else
|
15
|
+
alert.host.name
|
16
|
+
end
|
17
|
+
puts "#{title}: #{alert.message}"
|
18
|
+
end
|
5
19
|
end
|
6
20
|
|
7
21
|
def self.doit?
|
@@ -1,15 +1,68 @@
|
|
1
1
|
require 'slack-notifier'
|
2
2
|
module Malsh::Notification
|
3
3
|
class Slack < Base
|
4
|
-
def self.
|
4
|
+
def self.notify_host(subject, hosts)
|
5
5
|
return unless doit?
|
6
|
+
lists = if Malsh.options[:org]
|
7
|
+
hosts.map do |h|
|
8
|
+
"<https://mackerel.io/orgs/#{Malsh.options[:org]}/hosts/#{h.id}/-/setting|#{h.name}>"
|
9
|
+
end
|
10
|
+
else
|
11
|
+
hosts.map(&:name)
|
12
|
+
end
|
6
13
|
note = {
|
7
|
-
text:
|
14
|
+
text: lists.join("\n"),
|
8
15
|
color: "warning"
|
9
16
|
}
|
10
17
|
notifier.ping "*#{subject}*", attachments: [note]
|
11
18
|
end
|
12
19
|
|
20
|
+
def self.notify_alert(subject, alerts)
|
21
|
+
org = Mackerel.org.name
|
22
|
+
attachments = []
|
23
|
+
alerts.each do |alert|
|
24
|
+
color = case alert.status
|
25
|
+
when 'CRITICAL'
|
26
|
+
'danger'
|
27
|
+
when 'WARNING'
|
28
|
+
'warning'
|
29
|
+
else
|
30
|
+
''
|
31
|
+
end
|
32
|
+
|
33
|
+
title = if alert.type == 'external'
|
34
|
+
alert.monitor.name
|
35
|
+
else
|
36
|
+
alert.host.name
|
37
|
+
end
|
38
|
+
|
39
|
+
author_name = if alert.type == 'external'
|
40
|
+
''
|
41
|
+
else
|
42
|
+
alert.host.roles.map{|k, v| v.map{|r| "#{k}: #{r}"}}.flatten.join(" ")
|
43
|
+
end
|
44
|
+
|
45
|
+
attachments << {
|
46
|
+
author_name: author_name,
|
47
|
+
title: title,
|
48
|
+
title_link: "https://mackerel.io/orgs/#{org}/alerts/#{alert.id}",
|
49
|
+
text: alert.message,
|
50
|
+
color: color,
|
51
|
+
fields: [
|
52
|
+
{
|
53
|
+
title: 'Type',
|
54
|
+
value: alert.type
|
55
|
+
},
|
56
|
+
{
|
57
|
+
title: 'OpenedAt',
|
58
|
+
value: Time.at(alert.openedAt).strftime("%Y/%m/%d %H:%M:%S")
|
59
|
+
}
|
60
|
+
]
|
61
|
+
}
|
62
|
+
end
|
63
|
+
notifier.ping "*#{subject}*", attachments: attachments
|
64
|
+
end
|
65
|
+
|
13
66
|
def self.doit?
|
14
67
|
%i(slack_webhook slack_channel).all? {|k| Malsh.options.key?(k) || ENV[k.to_s.upcase] }
|
15
68
|
end
|
data/lib/malsh/version.rb
CHANGED
data/malsh.gemspec
CHANGED
@@ -23,10 +23,10 @@ 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'
|
27
27
|
spec.add_dependency 'slack-notifier'
|
28
28
|
spec.add_dependency 'parallel'
|
29
|
-
spec.add_development_dependency "bundler"
|
30
|
-
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "bundler"
|
30
|
+
spec.add_development_dependency "rake"
|
31
31
|
spec.add_development_dependency "rspec"
|
32
32
|
end
|
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.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kazuhiko yamahsita
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: mackerel-rb
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0
|
33
|
+
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: slack-notifier
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,30 +70,30 @@ dependencies:
|
|
70
70
|
name: bundler
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rake
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rspec
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -141,7 +141,7 @@ homepage: http://ten-snapon.com.
|
|
141
141
|
licenses:
|
142
142
|
- MIT
|
143
143
|
metadata: {}
|
144
|
-
post_install_message:
|
144
|
+
post_install_message:
|
145
145
|
rdoc_options: []
|
146
146
|
require_paths:
|
147
147
|
- lib
|
@@ -156,9 +156,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
156
|
- !ruby/object:Gem::Version
|
157
157
|
version: '0'
|
158
158
|
requirements: []
|
159
|
-
|
160
|
-
|
161
|
-
signing_key:
|
159
|
+
rubygems_version: 3.0.6
|
160
|
+
signing_key:
|
162
161
|
specification_version: 4
|
163
162
|
summary: mackerel tools.
|
164
163
|
test_files: []
|