-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprometheus.go
156 lines (133 loc) · 4.7 KB
/
prometheus.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
package main
import (
"strconv"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/pkg/api/v1"
beta "k8s.io/client-go/pkg/apis/apps/v1beta1"
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
)
func (e *Exporter) resetGaugeVecs() {
for _, m := range e.gaugeVecs {
m.Reset()
}
}
// Describe describes all the metrics ever exported by the Rancher exporter
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.gaugeVecs {
m.Describe(ch)
}
}
// Collect function, called on by Prometheus Client library
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.resetGaugeVecs() // Clean starting point
gathData, err := e.gatherData(ch)
if err == nil {
if gathData != nil {
for _, pod := range gathData.pods.Items {
var state float64 = 1
containerSize := len(pod.Status.ContainerStatuses)
if containerSize == 0 {
state = 0
}
for _, item := range pod.Status.ContainerStatuses {
if !item.Ready {
state = 0
}
}
e.gaugeVecs["pods"].With(prometheus.Labels{"name": pod.Name, "namespace": pod.Namespace, "podPhase": string(pod.Status.Phase), "hostIP": pod.Status.HostIP, "podIP": pod.Status.PodIP, "reason": pod.Status.Reason, "message": pod.Status.Message, "containerCount": strconv.Itoa(containerSize)}).Set(state)
}
for _, node := range gathData.nodes.Items {
var state float64 = 1
var nodeState = "Ready"
var hostname, externalIP, internalIP string
for _, address := range node.Status.Addresses {
if address.Type == v1.NodeHostName {
hostname = address.Address
} else if address.Type == v1.NodeExternalIP {
externalIP = address.Address
} else if address.Type == v1.NodeInternalIP {
internalIP = address.Address
}
}
for _, item := range node.Status.Conditions {
if item.Type != v1.NodeReady && item.Status == v1.ConditionTrue {
state = 0
nodeState = string(item.Type)
}
}
e.gaugeVecs["nodes"].With(
prometheus.Labels{
"name": node.Name,
"nodeState": nodeState,
"osImage": node.Status.NodeInfo.OSImage,
"containerRuntimeVersion": node.Status.NodeInfo.ContainerRuntimeVersion,
"kubeletVersion": node.Status.NodeInfo.KubeletVersion,
"operatingSystem": node.Status.NodeInfo.OperatingSystem,
"architecture": node.Status.NodeInfo.Architecture,
"hostname": hostname,
"externalIp": externalIP,
"internalIp": internalIP,
}).Set(state)
}
for _, deployment := range gathData.deployments.Items {
var state = getDeploymentState(deployment)
e.gaugeVecs["controller"].With(prometheus.Labels{"name": deployment.Name, "namespace": deployment.Namespace, "type": "deployment"}).Set(state)
}
for _, daemonset := range gathData.daemonsets.Items {
var state = getDaemonSetState(daemonset)
e.gaugeVecs["controller"].With(prometheus.Labels{"name": daemonset.Name, "namespace": daemonset.Namespace, "type": "daemonset"}).Set(state)
}
for _, statefulset := range gathData.statefulsets.Items {
var state = getStatefulSetState(statefulset)
e.gaugeVecs["controller"].With(prometheus.Labels{"name": statefulset.Name, "namespace": statefulset.Namespace, "type": "statefulset"}).Set(state)
}
for stack, deployments := range gathData.stacks {
var state float64 = 1
for _, deployment := range *deployments {
if getDeploymentState(deployment) == 0 {
state = 0
break
}
}
e.gaugeVecs["stacks"].With(prometheus.Labels{"name": stack.Name, "namespace": stack.Namespace}).Set(state)
}
for component, pods := range gathData.components {
var state float64 = 1
for _, pod := range *pods {
for _, item := range pod.Status.ContainerStatuses {
if !item.Ready {
state = 0
}
}
}
e.gaugeVecs["components"].With(prometheus.Labels{"name": component.Name, "namespace": component.Namespace}).Set(state)
}
}
}
for _, m := range e.gaugeVecs {
m.Collect(ch)
}
}
func getDaemonSetState(daemonset v1beta1.DaemonSet) float64 {
var state float64 = 1
if daemonset.Status.NumberReady != daemonset.Status.DesiredNumberScheduled {
state = 0
}
return state
}
func getDeploymentState(deployment v1beta1.Deployment) float64 {
var state float64 = 1
if deployment.Status.Replicas != deployment.Status.AvailableReplicas {
state = 0
}
return state
}
func getStatefulSetState(stateful beta.StatefulSet) float64 {
var state float64 = 1
if *stateful.Spec.Replicas != stateful.Status.Replicas {
state = 0
}
return state
}