-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes-pods-pending.rb
159 lines (142 loc) · 4.42 KB
/
kubernetes-pods-pending.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#! /usr/bin/env ruby
#
# kubernetes-pods-pending.eb
# author: Murali Krishna Kanagala
#
# DESCRIPTION:
# Monitors the pod status hitting the kubernetes API.
# Dumps pending pod list to /tmp/sensu and comapres them in the next run.
# Sensu check definition that runs this plugin should use autoresolve: false to
# avoid the check resolving itself if it does not see any change in restart counts
# in subsequesnt runs.
#
# OUTPUT:
# plain text
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: mixlib-shellout
# gem: json
require 'sensu-plugin/check/cli'
require 'mixlib/shellout'
require 'json'
# rubocop:disable ClassLength
# rubocop:disable CyclomaticComplexity
# rubocop:disable RedundantReturn
# rubocop:disable Next
# rubocop:disable UselessAssignment
# rubocop:disable Eval
# rubocop:disable StringLiterals
# rubocop:disable PerceivedComplexity
# rubocop:disable AbcSize
# Inherit Sensu check cli
class PodsPending < Sensu::Plugin::Check::CLI
option :server,
short: '-s server',
description: 'kubernetes api url',
required: true
option :token_file,
short: '-t token_file',
default: '/etc/kubernetes/ssl/auth.token',
description: 'kubernetes authentication token'
option :debug,
short: '-d',
boolean: true,
default: false,
description: 'Run the check in debug mode. For troubleshooting purposes only'
def read_token(token_file, debug)
# rubocop:disable RescueException
if File.exist?(token_file)
begin
puts "Reading token file #{token_file}" if debug
token = File.read(token_file)
rescue Exception => e
puts "Failed to read the file #{token_file} #{e}" if debug
unknown "Failed to read the file #{token_file}\n#{e}"
end
else
puts "File does not exist #{token_file}" if debug
unknown "File does not exist #{token_file}"
end
return token
end
def run_cmd(cmd, debug)
puts cmd if debug
data_cmd = Mixlib::ShellOut.new(cmd)
data_cmd.run_command
puts data_cmd.stdout if debug
if data_cmd.error?
puts "Failed to run the command #{cmd}, #{data_cmd.stderr}" if debug
unknown "Failed to run the command #{cmd.gsub(/\"[^)]+\"\s+/, '')}\n#{data_cmd.stderr}"
end
return data_cmd.stdout
end
def cmd_creator(debug)
token_file = config[:token_file]
server = config[:server]
api_path = '/api/v1/pods'
cmd1 = 'curl -k -H "Accept: application/json" -H "Authorization: Bearer '
cmd2 = read_token(token_file, debug)
cmd3 = "\" #{server}" + api_path
cmd = cmd1 + cmd2 + cmd3
puts cmd if debug
return cmd
end
def pod_status_parser(data, debug)
pod_status = []
json_data = JSON.parse(data)
json_data['items'].each do |item|
cont_count = 0
project = item['metadata']['namespace']
pod_name = item['metadata']['name']
status = item['status']['phase']
if status == 'Pending'
pod_status << { 'pod_name' => pod_name, 'project' => project, 'status' => status }
end
end
puts pod_status if debug
return pod_status
end
def time_diff(file)
a = File.mtime(file).to_i
b = Time.now.to_i
c = b - a
diff = Time.at(c).utc.strftime "%k hours %M minutes %S seconds"
return diff
end
def run
crit_pending = []
debug = config[:debug]
cmd = cmd_creator(debug)
data = run_cmd(cmd, debug)
Dir.mkdir('/tmp/sensu') unless File.directory?('/tmp/sensu')
if File.exist?('/tmp/sensu/pod_status')
previous_stats = File.read('/tmp/sensu/pod_status')
previous_pod_stats = eval(previous_stats)
interval = time_diff('/tmp/sensu/pod_status')
else
previous_pod_stats = {}
end
current_pod_stats = pod_status_parser(data, debug)
previous_pod_stats.each do |prev_stat|
pod_name = prev_stat['pod_name']
proj_name = prev_stat['project']
current_pod_stats.each do |cur_stat|
if (pod_name == cur_stat['pod_name']) && (proj_name == cur_stat['project'])
puts "comparing #{prev_stat} with #{cur_stat}" if debug
if cur_stat['status'] == prev_stat['status']
crit_pending << "#{proj_name}: #{pod_name}"
end
end
end
end
File.open('/tmp/sensu/pod_status', 'w') { |f| f.write(current_pod_stats) }
if crit_pending.length > 0
critical "Pods in pending status from last #{interval}:\n#{crit_pending.join("\n")}"
end
ok "No pods are in pending status"
end
end