Skip to content

Commit dbdd3dc

Browse files
author
Ruijian Zhang
committed
rewrite check_fd plugin by golang
1 parent f998961 commit dbdd3dc

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

Dockerfile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ RUN test -h /etc/localtime && rm -f /etc/localtime && cp /usr/share/zoneinfo/UTC
2828

2929
ADD ./bin/node-problem-detector /node-problem-detector
3030
ADD ./bin/log-counter /home/kubernetes/bin/log-counter
31+
ADD ./bin/check-fd /home/kubernetes/bin/check-fd
3132
ADD config /config
3233
RUN chmod +x /config/plugin/*.sh
3334
ENTRYPOINT ["/node-problem-detector", "--system-log-monitors=/config/kernel-monitor.json"]

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ version:
102102
$(BUILD_TAGS) \
103103
./cmd/nodeproblemdetector
104104

105+
./bin/check-fd: $(PKG_SOURCES)
106+
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GO111MODULE=on go build \
107+
-mod vendor \
108+
-o bin/check-fd \
109+
-ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \
110+
$(BUILD_TAGS) \
111+
./cmd/checkfd
112+
105113
Dockerfile: Dockerfile.in
106114
sed -e 's|@BASEIMAGE@|$(BASEIMAGE)|g' $< >$@
107115

@@ -118,12 +126,12 @@ e2e-test: vet fmt build-tar
118126
-boskos-project-type=$(BOSKOS_PROJECT_TYPE) -job-name=$(JOB_NAME) \
119127
-artifacts-dir=$(ARTIFACTS)
120128

121-
build-binaries: ./bin/node-problem-detector ./bin/log-counter
129+
build-binaries: ./bin/node-problem-detector ./bin/log-counter ./bin/check-fd
122130

123131
build-container: build-binaries Dockerfile
124132
docker build -t $(IMAGE) .
125133

126-
build-tar: ./bin/node-problem-detector ./bin/log-counter
134+
build-tar: ./bin/node-problem-detector ./bin/log-counter ./bin/check-fd
127135
tar -zcvf $(TARBALL) bin/ config/ test/e2e-install.sh
128136
sha1sum $(TARBALL)
129137
md5sum $(TARBALL)
@@ -150,4 +158,5 @@ push: push-container push-tar
150158
clean:
151159
rm -f bin/log-counter
152160
rm -f bin/node-problem-detector
161+
rm -f bin/check-fd
153162
rm -f node-problem-detector-*.tar.gz

cmd/checkfd/check_fd.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"regexp"
9+
"strconv"
10+
"strings"
11+
"sync"
12+
)
13+
14+
const (
15+
OK = 0
16+
NONOK = 1
17+
UNKNOWN = 2
18+
)
19+
20+
var (
21+
procPath string
22+
printCount bool
23+
percentThreshold int
24+
)
25+
26+
func init() {
27+
flag.StringVar(&procPath, "p", "/proc", "actual path of /proc")
28+
flag.BoolVar(&printCount,"c", false, "print numbers of used df and max")
29+
flag.IntVar(&percentThreshold, "t", 80, "Warning threshold of percentage of fd usage to max limitation")
30+
}
31+
32+
func main() {
33+
flag.Parse()
34+
35+
if percentThreshold >= 100 || percentThreshold <=0 {
36+
fmt.Fprintf(os.Stderr, "value of -t must between 0 and 100")
37+
os.Exit(UNKNOWN)
38+
}
39+
40+
maxPath := procPath + "/sys/fs/file-max"
41+
f, err := os.Open(maxPath)
42+
if err != nil {
43+
fmt.Fprintf(os.Stderr, "%v", err)
44+
os.Exit(UNKNOWN)
45+
}
46+
defer f.Close()
47+
maxBytes, err := ioutil.ReadAll(f)
48+
if err != nil {
49+
fmt.Fprintf(os.Stderr, "%v", err)
50+
os.Exit(UNKNOWN)
51+
}
52+
fdMax, err := strconv.Atoi(strings.TrimSpace(string(maxBytes)))
53+
if err != nil {
54+
fmt.Fprintf(os.Stderr, "%v", err)
55+
os.Exit(UNKNOWN)
56+
}
57+
58+
files, err := ioutil.ReadDir(procPath)
59+
if err != nil {
60+
fmt.Fprintf(os.Stderr, "%v", err)
61+
os.Exit(UNKNOWN)
62+
}
63+
64+
ch := make(chan int)
65+
wg := sync.WaitGroup{}
66+
re := regexp.MustCompile(`[0-9][0-9]*`)
67+
for _, f := range files {
68+
if f.IsDir() {
69+
if re.MatchString(f.Name()) {
70+
wg.Add(1)
71+
go countFD(procPath+"/"+f.Name()+"/fd", &wg, ch)
72+
}
73+
}
74+
}
75+
76+
go func() {
77+
wg.Wait()
78+
close(ch)
79+
}()
80+
81+
fdTotal := 0
82+
for {
83+
c, ok := <-ch
84+
if !ok {
85+
break
86+
}
87+
fdTotal += c
88+
}
89+
90+
if printCount {
91+
fmt.Fprintf(os.Stdout, "current fd usage is %d and limitaion is %d\n", fdTotal, fdMax)
92+
}
93+
if fdTotal > fdMax / 100 * percentThreshold {
94+
fmt.Fprintf(os.Stdout, "current fd usage is %d and is over %d of limition %d, \n",
95+
fdTotal, percentThreshold, fdMax)
96+
os.Exit(NONOK)
97+
} else {
98+
fmt.Fprintf(os.Stdout, "node has no fd pressure\n")
99+
os.Exit(OK)
100+
}
101+
}
102+
103+
func countFD(path string, wg *sync.WaitGroup, fNum chan<- int) {
104+
defer wg.Done()
105+
files, err := ioutil.ReadDir(path)
106+
if err != nil {
107+
fmt.Fprintf(os.Stderr, "%v", err)
108+
return
109+
}
110+
fNum <- len(files)
111+
}

0 commit comments

Comments
 (0)