-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
traceroute.go
141 lines (118 loc) · 3.74 KB
/
traceroute.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
// 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 || windows
package modules
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
tracerouteutil "github.com/DataDog/datadog-agent/pkg/networkpath/traceroute"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
"github.com/gorilla/mux"
"go.uber.org/atomic"
"google.golang.org/grpc"
)
type traceroute struct {
runner *tracerouteutil.Runner
}
var (
_ module.Module = &traceroute{}
tracerouteConfigNamespaces = []string{"traceroute"}
)
func createTracerouteModule(_ *sysconfigtypes.Config, _ optional.Option[workloadmeta.Component]) (module.Module, error) {
runner, err := tracerouteutil.NewRunner()
if err != nil {
return &traceroute{}, err
}
return &traceroute{
runner: runner,
}, nil
}
func (t *traceroute) GetStats() map[string]interface{} {
return nil
}
func (t *traceroute) Register(httpMux *module.Router) error {
var runCounter = atomic.NewUint64(0)
// TODO: what other config should be passed as part of this request?
httpMux.HandleFunc("/traceroute/{host}", func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
id := getClientID(req)
cfg, err := parseParams(req)
if err != nil {
log.Errorf("invalid params for host: %s: %s", cfg.DestHostname, err)
w.WriteHeader(http.StatusBadRequest)
return
}
// Run traceroute
path, err := t.runner.RunTraceroute(context.Background(), cfg)
if err != nil {
log.Errorf("unable to run traceroute for host: %s: %s", cfg.DestHostname, err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
resp, err := json.Marshal(path)
if err != nil {
log.Errorf("unable to marshall traceroute response: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = w.Write(resp)
if err != nil {
log.Errorf("unable to write traceroute response: %s", err)
}
runCount := runCounter.Inc()
logTracerouteRequests(cfg.DestHostname, id, runCount, start)
})
return nil
}
func (t *traceroute) RegisterGRPC(_ grpc.ServiceRegistrar) error {
return nil
}
func (t *traceroute) Close() {}
func logTracerouteRequests(host string, client string, runCount uint64, start time.Time) {
args := []interface{}{host, client, runCount, time.Since(start)}
msg := "Got request on /traceroute/%s?client_id=%s (count: %d): retrieved traceroute in %s"
switch {
case runCount <= 5, runCount%20 == 0:
log.Infof(msg, args...)
default:
log.Debugf(msg, args...)
}
}
func parseParams(req *http.Request) (tracerouteutil.Config, error) {
vars := mux.Vars(req)
host := vars["host"]
port, err := parseUint(req, "port", 16)
if err != nil {
return tracerouteutil.Config{}, fmt.Errorf("invalid port: %s", err)
}
maxTTL, err := parseUint(req, "max_ttl", 8)
if err != nil {
return tracerouteutil.Config{}, fmt.Errorf("invalid max_ttl: %s", err)
}
timeout, err := parseUint(req, "timeout", 32)
if err != nil {
return tracerouteutil.Config{}, fmt.Errorf("invalid timeout: %s", err)
}
return tracerouteutil.Config{
DestHostname: host,
DestPort: uint16(port),
MaxTTL: uint8(maxTTL),
TimeoutMs: uint(timeout),
}, nil
}
func parseUint(req *http.Request, field string, bitSize int) (uint64, error) {
if req.URL.Query().Has(field) {
return strconv.ParseUint(req.URL.Query().Get(field), 10, bitSize)
}
return 0, nil
}