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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0fa06d731f4c92e454a03334fa7c3964ca767371
4
- data.tar.gz: b44f30a0dbaac3f827206cb03065b87b176aca18
2
+ SHA256:
3
+ metadata.gz: '04893527ed6c71386b40145b68b31ae4bacce444e5d3edce147327789b18349b'
4
+ data.tar.gz: 1ef47a2b0df230ca310900b2036c3ea63ca8ce4f4058e572f9c3154069142563
5
5
  SHA512:
6
- metadata.gz: e82b0c6a6924df2d1b3905ae08d01c50814cf8f031560571aac52e67786ce7cd5724a74d094f356cab1a76de75ecdfe4d7ae897eac6fe1725a430d1201698a3a
7
- data.tar.gz: 9cf7bb1593c2edf7489e44ce0d20e476225b71eb48fa490faed22fb69f4a8fe2b69bdca7976439c2ead7f28186fcaad120b8cfdd0502a81b501a2590f2c054f0
6
+ metadata.gz: ea062a8de9ff0d9b425b042b7aff1bb1bbaf43bc4aad3c9f020a936496f07cece395ccb1d0fd4c15fb90789437c95933d0d5287a34f897e43c4339978d394d1c
7
+ data.tar.gz: 7fbc8dc08cee0e942531086f53640653ca4cf27b7a66b0e836fa19396ae6a53cb20d1466f50ca5c6cb2725031b7541f430b2b2c00d5efd1b08d7acb4e551d021
data/README.md CHANGED
@@ -54,6 +54,8 @@ $ malsh search --past_date 7 --cpu 40 --memory 40 --subject 過去7日間のCPU
54
54
  * 除外したいホスト名を正規表現で指定します。
55
55
  * --regex
56
56
  * 特定したいホスト名を正規表現で指定します。
57
+ * --invert-role
58
+ * 除外したいロール名を`service:role`で指定します。
57
59
 
58
60
  ## Author
59
61
  pyama86
@@ -7,9 +7,15 @@ require "malsh/host_metrics"
7
7
 
8
8
  module Malsh
9
9
  class << self
10
- def notify(subject, host)
10
+ def notify_host(subject, host)
11
11
  Malsh::Notification.constants.each do |c|
12
- Object.const_get("Malsh::Notification::#{c}").notify(options[:subject] || subject, host)
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
@@ -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
- host_names = Parallel.map(Malsh.metrics('loadavg5')) do|lvg|
19
- host = Malsh.host_by_id lvg.first
20
- host.name if (!lvg.last.loadavg5.respond_to?(:value) || !lvg.last.loadavg5.value)
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
- host_names = Parallel.map(Malsh.hosts) do |h|
31
- h.name if h.roles.keys.size < 1
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.notify("ロール無所属ホスト一覧", host_names)
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.notify("ホスト一覧", hosts.compact.map {|h| h["name"]})
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.notify(subject, hosts)
4
- puts "#{subject}: \n#{hosts.join("\n")}" if hosts.size > 0 && doit?
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.notify(subject, hosts)
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: hosts.join("\n"),
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
@@ -1,3 +1,3 @@
1
1
  module Malsh
2
- VERSION = "0.1.8"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -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', "~> 0.1.5"
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", "~> 1.9"
30
- spec.add_development_dependency "rake", "~> 10.0"
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.8
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: 2016-03-02 00:00:00.000000000 Z
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.1.5
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.1.5
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: '1.9'
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: '1.9'
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: '10.0'
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: '10.0'
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
- rubyforge_project:
160
- rubygems_version: 2.4.8
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: []