-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
152 lines (127 loc) · 4.2 KB
/
main.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
// Copyright 2024 SAP SE
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"net/http"
"regexp"
"strings"
"time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/net/context"
"k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/kmsg"
"k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/types"
)
var (
defaultPattern = `^oom-kill.+,task_memcg=\/kubepods(?:\.slice)?\/.+\/(?:kubepods-burstable-)?pod(\w+[-_]\w+[-_]\w+[-_]\w+[-_]\w+)(?:\.slice)?\/(?:cri-containerd-)?([a-f0-9]+)`
kmesgRE = regexp.MustCompile(defaultPattern)
)
var (
kubernetesCounterVec *prometheus.CounterVec
prometheusContainerLabels = map[string]string{
"io.kubernetes.container.name": "container_name",
"io.kubernetes.pod.namespace": "namespace",
"io.kubernetes.pod.uid": "pod_uid",
"io.kubernetes.pod.name": "pod_name",
}
metricsAddr string
)
func init() {
var newPattern string
flag.StringVar(&metricsAddr, "listen-address", ":9102", "The address to listen on for HTTP requests.")
flag.StringVar(&newPattern, "regexp-pattern", defaultPattern, "Overwrites the default regexp pattern to match and extract Pod UID and Container ID.")
if newPattern != "" {
kmesgRE = regexp.MustCompile(newPattern)
}
}
func main() {
flag.Parse()
containerdClient, err := containerd.New("/run/containerd/containerd.sock")
if err != nil {
glog.Fatal(err)
}
defer containerdClient.Close()
var labels []string
for _, label := range prometheusContainerLabels {
labels = append(labels, strings.ReplaceAll(label, ".", "_"))
}
kubernetesCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "klog_pod_oomkill",
Help: "Extract metrics for OOMKilled pods from kernel log",
}, labels)
prometheus.MustRegister(kubernetesCounterVec)
go func() {
glog.Info("Starting prometheus metrics")
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
server := &http.Server{
Addr: metricsAddr,
ReadHeaderTimeout: 3 * time.Second,
Handler: mux,
}
glog.Warning(server.ListenAndServe())
}()
kmsgWatcher := kmsg.NewKmsgWatcher(types.WatcherConfig{Plugin: "kmsg"})
logCh, err := kmsgWatcher.Watch()
if err != nil {
glog.Fatal("Could not create log watcher")
}
for log := range logCh {
podUID, containerID := getContainerIDFromLog(log.Message)
if containerID != "" {
labels, err := getContainerLabels(containerID, containerdClient)
if err != nil || labels == nil {
glog.Warningf("Could not get labels for container id %s, pod %s: %v", containerID, podUID, err)
} else {
prometheusCount(labels)
}
}
}
}
func getContainerIDFromLog(log string) (podUID, containerID string) {
podUID = ""
containerID = ""
if matches := kmesgRE.FindStringSubmatch(log); matches != nil {
podUID = matches[1]
containerID = matches[2]
}
return
}
func getContainerLabels(containerID string, cli *containerd.Client) (map[string]string, error) {
ctx := namespaces.WithNamespace(context.Background(), "k8s.io")
container, err := cli.ContainerService().Get(ctx, containerID)
if err != nil {
return nil, err
}
return container.Labels, nil
}
func prometheusCount(containerLabels map[string]string) {
var counter prometheus.Counter
var err error
labels := make(map[string]string)
for key, label := range prometheusContainerLabels {
labels[label] = containerLabels[key]
}
glog.V(5).Infof("Labels: %v\n", labels)
counter, err = kubernetesCounterVec.GetMetricWith(labels)
if err != nil {
glog.Warning(err)
} else {
counter.Add(1)
}
}