forked from plusserver/check_kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_kubernetes.go
370 lines (286 loc) · 10.7 KB
/
check_kubernetes.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main
import (
"flag"
"fmt"
"os"
"github.com/Nexinto/check_kubernetes/nrpe"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func main() {
var kubeconfig, objectType, name, namespace string
flag.StringVar(&kubeconfig, "kubeconfig", "", "kubeconfig location")
flag.StringVar(&objectType, "type", "", "type of object to check")
flag.StringVar(&name, "name", "", "name of object to check")
flag.StringVar(&namespace, "namespace", "default", "namespace of object")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
fmt.Printf("Cannot configure kubernetes client: %s\n", err.Error())
os.Exit(nrpe.UNKNOWN)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("Cannot create kubernetes client: %s\n", err.Error())
os.Exit(nrpe.UNKNOWN)
}
var level nrpe.Result
var message string
switch objectType {
case "pod":
level, message = checkPod(namespace, name, clientset)
case "replicaset":
level, message = checkReplicaSet(namespace, name, clientset)
case "deployment":
level, message = checkDeployment(namespace, name, clientset)
case "daemonset":
level, message = checkDaemonSet(namespace, name, clientset)
case "statefulset":
level, message = checkStatefulSet(namespace, name, clientset)
case "node":
level, message = checkNode(name, clientset)
case "componentstatus":
level, message = checkComponentStatus(name, clientset)
case "service":
level, message = checkService(namespace, name, clientset)
default:
level, message = nrpe.UNKNOWN, fmt.Sprintf("unsupported object type %s", objectType)
}
m := map[nrpe.Result]string{
nrpe.OK: "OK",
nrpe.WARNING: "WARNING",
nrpe.CRITICAL: "CRITICAL",
nrpe.UNKNOWN: "UNKNOWN",
}
fmt.Printf("%s: %s\n", m[level], message)
os.Exit(int(level))
}
func handleLookupError(err error) (nrpe.Result, string) {
if errors.IsNotFound(err) {
return nrpe.UNKNOWN, "object not found"
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
return nrpe.UNKNOWN, fmt.Sprintf("Error getting object: %v", statusError.ErrStatus.Message)
} else if err != nil {
return nrpe.UNKNOWN, fmt.Sprintf("Error getting object: %v", err.Error())
}
return nrpe.OK, ""
}
func checkPod(namespace, name string, kube kubernetes.Interface) (nrpe.Result, string) {
pod, err := kube.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
if result, message := handleLookupError(err); result != nrpe.OK {
return result, message
}
ready, running, max := 0, 0, len(pod.Status.ContainerStatuses)
for _, c := range pod.Status.ContainerStatuses {
if c.Ready {
ready = ready + 1
}
if c.State.Running != nil {
running = running + 1
}
}
containerStatus := fmt.Sprintf("%d/%d containers running, %d/%d containers ready", running, max, ready, max)
switch pod.Status.Phase {
// PodRunning means the pod has been bound to a node and all of the containers have been started.
// At least one container is still running or is in the process of being restarted.
case corev1.PodRunning:
if ready == max && running == max {
return nrpe.OK, fmt.Sprintf("pod running; %s", containerStatus)
}
return nrpe.CRITICAL, fmt.Sprintf("pod running but containers are failing; %s", containerStatus)
// PodPending means the pod has been accepted by the system, but one or more of the containers
// has not been started. This includes time before being bound to a node, as well as time spent
// pulling images onto the host.
case corev1.PodPending:
return nrpe.WARNING, "pod pending"
// PodSucceeded means that all containers in the pod have voluntarily terminated
// with a container exit code of 0, and the system is not going to restart any of these containers.
case corev1.PodSucceeded:
return nrpe.OK, fmt.Sprintf("pod succeeded (terminated without errors); %s", containerStatus)
// PodFailed means that all containers in the pod have terminated, and at least one container has
// terminated in a failure (exited with a non-zero exit code or was stopped by the system).
case corev1.PodFailed:
return nrpe.CRITICAL, fmt.Sprintf("pod failed (at least one container terminated with errors); %s", containerStatus)
// PodUnknown means that for some reason the state of the pod could not be obtained, typically due
// to an error in communicating with the host of the pod.
case corev1.PodUnknown:
return nrpe.UNKNOWN, "pod state could not be determined"
}
return nrpe.OK, containerStatus
}
func checkReplicaSet(namespace, name string, kube kubernetes.Interface) (nrpe.Result, string) {
rs, err := kube.AppsV1beta2().ReplicaSets(namespace).Get(name, metav1.GetOptions{})
if result, message := handleLookupError(err); result != nrpe.OK {
return result, message
}
avail := rs.Status.AvailableReplicas
ready := rs.Status.ReadyReplicas
max := rs.Status.Replicas
rsStatus := fmt.Sprintf("%d/%d pods available, %d/%d pods ready", avail, max, ready, max)
if avail < max || ready < max {
if avail != 0 && ready != 0 {
return nrpe.WARNING, fmt.Sprintf("replicaset degraded: %s", rsStatus)
}
return nrpe.CRITICAL, fmt.Sprintf("replicaset unavailable: %s", rsStatus)
}
return nrpe.OK, rsStatus
}
func checkDeployment(namespace, name string, kube kubernetes.Interface) (nrpe.Result, string) {
dep, err := kube.AppsV1beta2().Deployments(namespace).Get(name, metav1.GetOptions{})
if result, message := handleLookupError(err); result != nrpe.OK {
return result, message
}
avail := dep.Status.AvailableReplicas
updated := dep.Status.UpdatedReplicas
max := dep.Status.Replicas
depStatus := fmt.Sprintf("%d/%d replicas available, %d/%d replicas up to spec and running", avail, max, updated, max)
if avail < max || updated < max {
if avail != 0 && updated != 0 {
return nrpe.WARNING, fmt.Sprintf("deployment degraded: %s", depStatus)
}
return nrpe.CRITICAL, fmt.Sprintf("deployment unavailable: %s", depStatus)
}
return nrpe.OK, depStatus
}
func checkDaemonSet(namespace, name string, kube kubernetes.Interface) (nrpe.Result, string) {
ds, err := kube.AppsV1beta2().DaemonSets(namespace).Get(name, metav1.GetOptions{})
if result, message := handleLookupError(err); result != nrpe.OK {
return result, message
}
current := ds.Status.CurrentNumberScheduled
ready := ds.Status.NumberReady
missched := ds.Status.NumberMisscheduled
max := ds.Status.DesiredNumberScheduled
var misStatus string
if missched == 0 {
misStatus = ""
} else {
misStatus = fmt.Sprintf(" (%d misscheduled)", missched)
}
dsStatus := fmt.Sprintf("%d/%d nodes with pods running, %d/%d pods available%s", current, max, ready, max, misStatus)
if missched > 0 || current < max || ready < max {
return nrpe.CRITICAL, fmt.Sprintf("daemonset incomplete: %s", dsStatus)
}
return nrpe.OK, dsStatus
}
func checkStatefulSet(namespace, name string, kube kubernetes.Interface) (nrpe.Result, string) {
ss, err := kube.AppsV1beta2().StatefulSets(namespace).Get(name, metav1.GetOptions{})
if result, message := handleLookupError(err); result != nrpe.OK {
return result, message
}
current := ss.Status.CurrentReplicas
max := ss.Status.Replicas
ready := ss.Status.ReadyReplicas
ssStatus := fmt.Sprintf("%d/%d replicas created, %d/%d ready", current, max, ready, max)
if current < max || ready < max {
return nrpe.CRITICAL, fmt.Sprintf("statefulset incomplete: %s", ssStatus)
}
return nrpe.OK, ssStatus
}
func checkNode(name string, kube kubernetes.Interface) (nrpe.Result, string) {
node, err := kube.CoreV1().Nodes().Get(name, metav1.GetOptions{})
if result, message := handleLookupError(err); result != nrpe.OK {
return result, message
}
nosched := node.Spec.Unschedulable
ready := false
unknown := true
resourceTroubleMsg := ""
resourceTrouble := false
if len(node.Status.Conditions) < 1 {
return nrpe.UNKNOWN, "node in unknown state"
}
for _, cond := range node.Status.Conditions {
switch cond.Type {
case corev1.NodeReady:
unknown = false
if cond.Status == corev1.ConditionTrue {
ready = true
}
case corev1.NodeOutOfDisk:
if cond.Status == corev1.ConditionTrue {
resourceTrouble = true
resourceTroubleMsg = resourceTroubleMsg + "out of disk "
}
case corev1.NodeDiskPressure:
if cond.Status == corev1.ConditionTrue {
resourceTrouble = true
resourceTroubleMsg = resourceTroubleMsg + "has disk pressure "
}
case corev1.NodeMemoryPressure:
if cond.Status == corev1.ConditionTrue {
resourceTrouble = true
resourceTroubleMsg = resourceTroubleMsg + "has memory pressure "
}
case corev1.NodeNetworkUnavailable:
if cond.Status == corev1.ConditionTrue {
resourceTrouble = true
resourceTroubleMsg = resourceTroubleMsg + "network unavailable "
}
}
}
if unknown {
return nrpe.UNKNOWN, "node in unknown state (invalid condition)"
}
if ready && resourceTrouble {
return nrpe.WARNING, "node ready but hits resource limits ( " + resourceTroubleMsg + ")"
}
if ready && nosched {
return nrpe.WARNING, "node ready but no scheduling allowed"
}
if ready {
return nrpe.OK, "node ready"
}
if !ready && resourceTrouble {
return nrpe.CRITICAL, "node not ready ( " + resourceTroubleMsg + ")"
}
return nrpe.CRITICAL, "node not ready"
}
func checkComponentStatus(name string, kube kubernetes.Interface) (nrpe.Result, string) {
component, err := kube.CoreV1().ComponentStatuses().Get(name, metav1.GetOptions{})
if result, message := handleLookupError(err); result != nrpe.OK {
return result, message
}
healthy := false
var message string
for _, cond := range component.Conditions {
message = cond.Message
if cond.Type == corev1.ComponentHealthy {
switch cond.Status {
case corev1.ConditionTrue:
healthy = true
case corev1.ConditionFalse:
healthy = false
break
case corev1.ConditionUnknown:
return nrpe.UNKNOWN, fmt.Sprintf("component state unknown: %s", message)
}
}
}
if healthy {
return nrpe.OK, fmt.Sprintf("component healthy: %s", message)
}
return nrpe.CRITICAL, fmt.Sprintf("component unhealthy: %s", message)
}
func checkService(namespace, name string, kube kubernetes.Interface) (nrpe.Result, string) {
service, err := kube.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
if result, message := handleLookupError(err); result != nrpe.OK {
return result, message
}
switch service.Spec.Type {
case corev1.ServiceTypeLoadBalancer:
for _, i := range service.Status.LoadBalancer.Ingress {
if len(i.IP) > 0 {
return nrpe.OK, fmt.Sprintf("configured with IP %s", i.IP)
}
if len(i.Hostname) > 0 {
return nrpe.OK, fmt.Sprintf("configured with hostname %s", i.Hostname)
}
}
return nrpe.WARNING, "pending"
}
return nrpe.OK, "service configured"
}