-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ping.go
139 lines (116 loc) · 3.72 KB
/
ping.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
package modules
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/config"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
pingcheck "github.com/DataDog/datadog-agent/pkg/networkdevice/pinger"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
"github.com/gorilla/mux"
"go.uber.org/atomic"
)
const (
countParam = "count"
intervalParam = "interval"
timeoutParam = "timeout"
)
type pinger struct{}
// Pinger is a factory for NDMs Ping module
var Pinger = module.Factory{
Name: config.PingModule,
ConfigNamespaces: []string{"ping"},
Fn: func(cfg *sysconfigtypes.Config, _ optional.Option[workloadmeta.Component]) (module.Module, error) {
return &pinger{}, nil
},
NeedsEBPF: func() bool {
return false
},
}
var _ module.Module = &pinger{}
func (p *pinger) GetStats() map[string]interface{} {
return nil
}
func (p *pinger) Register(httpMux *module.Router) error {
var runCounter = atomic.NewUint64(0)
httpMux.HandleFunc("/ping/{host}", func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
vars := mux.Vars(req)
id := getClientID(req)
host := vars["host"]
count, err := getIntParam(countParam, req)
if err != nil {
log.Errorf("unable to run ping invalid count %s: %s", host, err)
w.Write([]byte("invalid count"))
w.WriteHeader(http.StatusBadRequest)
return
}
interval, err := getIntParam(intervalParam, req)
if err != nil {
log.Errorf("unable to run ping invalid interval %s: %s", host, err)
w.Write([]byte("invalid interval"))
w.WriteHeader(http.StatusBadRequest)
return
}
timeout, err := getIntParam(timeoutParam, req)
if err != nil {
log.Errorf("unable to run ping invalid timeout %s: %s", host, err)
w.Write([]byte("invalid timeout"))
w.WriteHeader(http.StatusBadRequest)
return
}
cfg := pingcheck.Config{
UseRawSocket: true,
Interval: time.Duration(interval),
Timeout: time.Duration(timeout),
Count: count,
}
// Run ping using raw socket
result, err := pingcheck.RunPing(&cfg, host)
if err != nil {
log.Errorf("unable to run ping for host %s: %s", host, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
resp, err := json.Marshal(result)
if err != nil {
log.Errorf("unable to marshall ping stats: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = w.Write(resp)
if err != nil {
log.Errorf("unable to write ping response: %s", err)
}
runCount := runCounter.Inc()
logPingRequests(host, id, count, interval, timeout, runCount, start)
})
return nil
}
func (p *pinger) Close() {}
func logPingRequests(host string, client string, count int, interval int, timeout int, runCount uint64, start time.Time) {
args := []interface{}{host, client, count, interval, timeout, runCount, time.Since(start)}
msg := "Got request on /ping/%s?client_id=%s&count=%d&interval=%d&timeout=%d (count: %d): retrieved ping in %s"
switch {
case runCount <= 5, runCount%20 == 0:
log.Infof(msg, args...)
default:
log.Debugf(msg, args...)
}
}
func getIntParam(name string, req *http.Request) (int, error) {
// only return an error if the param is present
if req.URL.Query().Has(name) {
return strconv.Atoi(req.URL.Query().Get(name))
}
return 0, nil
}