kubernetes_health_checker 0.0.0.3 → 0.0.0.4

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: a41c0e00613fdcadb3390cd838e58758d77dc7a4
4
- data.tar.gz: 0e078558a40550481b7e7dc15a75f9a74a0bab24
3
+ metadata.gz: 8a44862baa23546298cbe135442791f9413b092f
4
+ data.tar.gz: 158d714234ec07627bd6e9fce0834aa906506ce8
5
5
  SHA512:
6
- metadata.gz: 25468d36cfaf12e62c897ff6618197989a1c1ae9da83eeb0469129a7c32d35b2721a2a7e5bc8bf98c6ec5a3f8509b70d9b2434c6a146f9c910dd7b36b0e1e207
7
- data.tar.gz: a3b8345de9736ad37dd8ef8bbf857c8f602eea7167dd51e394564e6d28de008267405f90f4a82e5bdb0ac1d86a79cf2b1060ee4fa9dca4b110cec62b8ea8b1c1
6
+ metadata.gz: 55c4048409051f6b23a8760be6640d6441673eb4b90a6df9e88cf7700b836ed5c93e5549df2d08eef30bd19639a1ef1ef564d549a4f08bec5057f52073b1da31
7
+ data.tar.gz: cae075720d0244dadc3e9c3a529afb09d4766f779a9d78d3792083338e01093bab0393da590c2ca70fe1c4a668013d0878b75ae2f4e6fb9ebba94ff557400be4
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'thor'
5
+ require 'byebug'
5
6
  require 'json'
6
7
 
7
8
  module KubernetesHealthChecker
@@ -17,34 +18,19 @@ module KubernetesHealthChecker
17
18
 
18
19
  ALERT_STATUSES = ['CrashLoopBackOff'].freeze
19
20
 
20
- def start
21
- opts = options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
22
- @alert_threshold = opts[:alert_threshold] || 5
23
- @slack_channel = opts[:channel] || '@rohan'
24
- @namespace = opts[:namespace] || 'default'
25
- @test = opts[:test] || false
26
-
27
- raise 'must provide a url to POST alert to' if !opts[:url]
28
- @url = opts[:url]
29
-
30
- if @test
31
- path = File.expand_path('../mock_output.txt', __FILE__)
32
- output = File.read(path)
33
- else
34
- output = `kubectl get pods --namespace #{options[:namespace]}`
35
- end
36
-
37
- message = ''
38
- rows = output.split("\n")
39
- rows.each_with_index do |row, index|
40
- next if index == 0
41
- row = row.split(" ")
42
- response = check_pod_state(pod_name: row[0], status: row[2], restarts: row[3].to_i)
21
+ RUN_INTERVAL = 10
43
22
 
44
- message += response[:message] if response[:should_alert]
23
+ def start
24
+ set_defaults(options)
25
+ run_validations if !@test
26
+ while true
27
+ puts 'starting...'
28
+ output = get_cli_output
29
+ message = construct_message(output)
30
+ send_alert(message) if !message.empty?
31
+ puts 'sleeping...'
32
+ sleep RUN_INTERVAL
45
33
  end
46
-
47
- send_alert(message) if !message.empty?
48
34
  end
49
35
 
50
36
  desc 'version', 'Prints version'
@@ -53,7 +39,36 @@ module KubernetesHealthChecker
53
39
  end
54
40
 
55
41
  no_commands do
42
+ def set_defaults(options)
43
+ opts = options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
44
+ @alert_threshold = opts[:alert_threshold] || 5
45
+ @slack_channel = opts[:channel] || '@rohan'
46
+ @namespace = opts[:namespace] || 'default'
47
+ @test = opts[:test] || false
48
+ @url = opts[:url]
49
+ @pods_data = {}
50
+ end
51
+
52
+ def run_validations
53
+ raise 'must provide a url to POST alert to' if !@url
54
+ end
55
+
56
+ def get_cli_output
57
+ if @test
58
+ # so hacky, but pulls from second mock output if
59
+ # it's the second time running to test state changes
60
+ file_name = @pods_data.empty? ? '../mock_output.txt' : '../mock_output_two.txt'
61
+ path = File.expand_path(file_name, __FILE__)
62
+ output = File.read(path)
63
+ else
64
+ output = `kubectl get pods --namespace #{@namespace}`
65
+ end
66
+ end
67
+
56
68
  def send_alert(message)
69
+ puts "Sending message: #{message}"
70
+ return if @test
71
+
57
72
  payload = {
58
73
  channel: @slack_channel,
59
74
  username: 'kubernetes_health_check_bot',
@@ -64,19 +79,44 @@ module KubernetesHealthChecker
64
79
  `curl -X POST --data-urlencode 'payload=#{JSON.generate(payload)}' #{@url}`
65
80
  end
66
81
 
67
- def check_pod_state(pod_name:, status:, restarts:)
68
- should_alert = false
69
- text = nil
82
+ def get_pod_message(pod_name:, new_status:, new_restarts:)
83
+ old_restarts = @pods_data[pod_name][:restarts]
84
+ old_status = @pods_data[pod_name][:status]
85
+ text = ""
86
+
87
+ if !@pods_data[pod_name].empty? && old_restarts == new_restarts && old_status == new_status
88
+ # we've already alerted from this state, skip
89
+ elsif ALERT_STATUSES.include?(new_status)
90
+ text = "Pod *#{pod_name}* is in status: *#{new_status}*.\n"
91
+ elsif new_restarts > @alert_threshold
92
+ text = "Pod *#{pod_name}* has restarted *#{new_restarts}* times and has status: *#{new_status}*.\n"
93
+ end
94
+
95
+ text
96
+ end
97
+
98
+ def construct_message(output)
99
+ message = ''
100
+ new_pod_store = {}
101
+
102
+ rows = output.split("\n")
103
+ rows.each_with_index do |row, index|
104
+ next if index == 0
105
+
106
+ row = row.split(" ")
107
+ pod_name = row[0]
108
+ status = row[2]
109
+ restarts = row[3].to_i
110
+
111
+ @pods_data[pod_name] ||= {}
112
+ message += get_pod_message(pod_name: pod_name, new_status: status, new_restarts: restarts)
70
113
 
71
- if ALERT_STATUSES.include?(status)
72
- should_alert = true
73
- text = "Pod *#{pod_name}* is in status: *#{status}*.\n"
74
- elsif restarts > @alert_threshold
75
- should_alert = true
76
- text = "Pod *#{pod_name}* has restarted *#{restarts}* times and has status: *#{status}*.\n"
114
+ # update our data store for the pods
115
+ @pods_data[pod_name][:status] = status
116
+ @pods_data[pod_name][:restarts] = restarts
77
117
  end
78
118
 
79
- {should_alert: should_alert, message: text}
119
+ message
80
120
  end
81
121
  end
82
122
  end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'daemons'
4
+
5
+ Daemons.run('./bin/kubernetes_health_checker')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubernetes_health_checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.3
4
+ version: 0.0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rohan Sahai
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: daemons
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'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: byebug
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -43,10 +57,12 @@ description: Simple script to send slack notifications for pods that are crashin
43
57
  email: rohan@sah.ai
44
58
  executables:
45
59
  - kubernetes_health_checker
60
+ - kubernetes_health_checkerd
46
61
  extensions: []
47
62
  extra_rdoc_files: []
48
63
  files:
49
64
  - bin/kubernetes_health_checker
65
+ - bin/kubernetes_health_checkerd
50
66
  - bin/mock_output.txt
51
67
  homepage: http://rubygems.org/gems/kubernetes_health_checker
52
68
  licenses: