kubernetes_health_checker 0.0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/kubernetes_health_checker +86 -0
  3. metadata +74 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d91a2df9bf41a797648a65e6c74775cb34ab6e5
4
+ data.tar.gz: 16e5ea1cf7275cb6137a32998fe8f18f59885980
5
+ SHA512:
6
+ metadata.gz: 9c611e07d3c5cc1df5f645b753f84ac8d8a077009ac9dcd7b447949938459f8ee233f817f22a842ea8d77cac1cd327e22d8fab4e0a688720edb74422ff63c9a9
7
+ data.tar.gz: 5fac09e8356088c55a52039d2a5937833143e30cf9cc7c8f1ae19ae8c7df3674a06b8a2d36e5b5b1603d5f23a8f6081583d645ddf640799a7ecd67519c3d9bd9
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'thor'
5
+ require 'byebug'
6
+ require 'json'
7
+
8
+ module KubernetesHealthChecker
9
+ class Runner < Thor
10
+ default_task :start
11
+
12
+ desc 'start', 'Runs Kubernetes Health Checker'
13
+ method_option :channel, aliases: '-c', type: :string, desc: 'Specify the slack channel for alerting'
14
+ method_option :alert_threshold, aliases: '-a', type: :numeric, desc: 'Specify the threshold of restarts before alerting'
15
+ method_option :namespace, aliases: '-n', type: :string, desc: 'Specify the namespace in which the pods to health check reside'
16
+ method_option :url, aliases: '-u', type: :string, desc: 'Specify the slack url you would like the alert POSTed to'
17
+ method_option :test, aliases: '-t', type: :boolean, desc: 'Specify if running the gem in test mode'
18
+
19
+ ALERT_STATUSES = ['CrashLoopBackOff'].freeze
20
+
21
+ def start
22
+ opts = options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
23
+ @alert_threshold = opts[:alert_threshold] || 5
24
+ @slack_channel = opts[:channel] || '@rohan'
25
+ @namespace = opts[:namespace] || 'default'
26
+ @test = opts[:test] || false
27
+
28
+ raise 'must provide a url to POST alert to' if !opts[:url]
29
+ @url = opts[:url]
30
+
31
+ if @test
32
+ path = File.expand_path('../mock_output.txt', __FILE__)
33
+ output = File.read(path)
34
+ else
35
+ output = `kubectl get pods --namespace #{options[:namespace]}`
36
+ end
37
+
38
+ message = ''
39
+ rows = output.split("\n")
40
+ rows.each_with_index do |row, index|
41
+ next if index == 0
42
+ row = row.split(" ")
43
+ response = check_pod_state(pod_name: row[0], status: row[2], restarts: row[3].to_i)
44
+
45
+ message += response[:message] if response[:should_alert]
46
+ end
47
+
48
+ send_alert(message) if !message.empty?
49
+ end
50
+
51
+ desc 'version', 'Prints version'
52
+ def version
53
+ say "KubernetesHealthChecker 0.0.0"
54
+ end
55
+
56
+ no_commands do
57
+ def send_alert(message)
58
+ payload = {
59
+ channel: @slack_channel,
60
+ username: 'kubernetes_health_check_bot',
61
+ text: message,
62
+ icon_emoji: ":face_with_thermometer:"
63
+ }
64
+
65
+ `curl -X POST --data-urlencode 'payload=#{JSON.generate(payload)}' #{@url}`
66
+ end
67
+
68
+ def check_pod_state(pod_name:, status:, restarts:)
69
+ should_alert = false
70
+ text = nil
71
+
72
+ if ALERT_STATUSES.include?(status)
73
+ should_alert = true
74
+ text = "Pod *#{pod_name}* is in status: *#{status}*.\n"
75
+ elsif restarts > @alert_threshold
76
+ should_alert = true
77
+ text = "Pod *#{pod_name}* has restarted *#{restarts}* times and has status: *#{status}*.\n"
78
+ end
79
+
80
+ {should_alert: should_alert, message: text}
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ KubernetesHealthChecker::Runner.start
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kubernetes_health_checker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rohan Sahai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Simple script to send slack notifications for pods that are crashing/restarting
42
+ too often
43
+ email: rohan@sah.ai
44
+ executables:
45
+ - kubernetes_health_checker
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - bin/kubernetes_health_checker
50
+ homepage: http://rubygems.org/gems/kubernetes_health_checker
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.6.12
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Get notifications for unhealthy kubernetes pods
74
+ test_files: []