forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
158 lines (133 loc) · 3.98 KB
/
check.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
157
158
// 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 2018 Datadog, Inc.
package app
import (
"encoding/json"
"fmt"
"time"
"github.com/DataDog/datadog-agent/cmd/agent/common"
"github.com/DataDog/datadog-agent/pkg/aggregator"
"github.com/DataDog/datadog-agent/pkg/collector/check"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/serializer"
"github.com/DataDog/datadog-agent/pkg/status"
"github.com/DataDog/datadog-agent/pkg/util"
"github.com/spf13/cobra"
)
var (
checkRate bool
checkName string
checkDelay int
logLevel string
)
// Make the check cmd aggregator never flush by setting a very high interval
const checkCmdFlushInterval = time.Hour
func init() {
AgentCmd.AddCommand(checkCmd)
checkCmd.Flags().BoolVarP(&checkRate, "check-rate", "r", false, "check rates by running the check twice")
checkCmd.Flags().StringVarP(&logLevel, "log-level", "l", "", "set the log level (default 'off')")
checkCmd.Flags().IntVarP(&checkDelay, "delay", "d", 100, "delay between running the check and grabbing the metrics in miliseconds")
checkCmd.SetArgs([]string{"checkName"})
}
var checkCmd = &cobra.Command{
Use: "check <check_name>",
Short: "Run the specified check",
Long: `Use this to run a specific check with a specific rate`,
RunE: func(cmd *cobra.Command, args []string) error {
// Global Agent configuration
err := common.SetupConfig(confFilePath)
if err != nil {
fmt.Printf("Cannot setup config, exiting: %v\n", err)
return err
}
if logLevel == "" {
if confFilePath != "" {
logLevel = config.Datadog.GetString("log_level")
} else {
logLevel = "off"
}
}
// Setup logger
err = config.SetupLogger(logLevel, "", "", false, false, "", true)
if err != nil {
fmt.Printf("Cannot setup logger, exiting: %v\n", err)
return err
}
if len(args) != 0 {
checkName = args[0]
} else {
cmd.Help()
return nil
}
hostname, err := util.GetHostname()
if err != nil {
fmt.Printf("Cannot get hostname, exiting: %v\n", err)
return err
}
s := &serializer.Serializer{Forwarder: common.Forwarder}
agg := aggregator.InitAggregatorWithFlushInterval(s, hostname, checkCmdFlushInterval)
common.SetupAutoConfig(config.Datadog.GetString("confd_path"))
cs := common.AC.GetChecksByName(checkName)
if len(cs) == 0 {
fmt.Println("no check found")
return fmt.Errorf("no check found")
}
if len(cs) > 1 {
fmt.Println("Multiple check instances found, running each of them")
}
for _, c := range cs {
s := runCheck(c, agg)
// Sleep for a while to allow the aggregator to finish ingesting all the metrics/events/sc
time.Sleep(time.Duration(checkDelay) * time.Millisecond)
printMetrics(agg)
checkStatus, _ := status.GetCheckStatus(c, s)
fmt.Println(string(checkStatus))
}
return nil
},
}
func runCheck(c check.Check, agg *aggregator.BufferedAggregator) *check.Stats {
s := check.NewStats(c)
i := 0
times := 1
if checkRate {
times = 2
}
for i < times {
t0 := time.Now()
err := c.Run()
warnings := c.GetWarnings()
mStats, _ := c.GetMetricStats()
s.Add(time.Since(t0), err, warnings, mStats)
i++
}
return s
}
func printMetrics(agg *aggregator.BufferedAggregator) {
series := agg.GetSeries()
if len(series) != 0 {
fmt.Println("Series: ")
j, _ := json.MarshalIndent(series, "", " ")
fmt.Println(string(j))
}
sketches := agg.GetSketches()
if len(sketches) != 0 {
fmt.Println("Sketches: ")
j, _ := json.MarshalIndent(sketches, "", " ")
fmt.Println(string(j))
}
serviceChecks := agg.GetServiceChecks()
if len(serviceChecks) != 0 {
fmt.Println("Service Checks: ")
j, _ := json.MarshalIndent(serviceChecks, "", " ")
fmt.Println(string(j))
}
events := agg.GetEvents()
if len(events) != 0 {
fmt.Println("Events: ")
j, _ := json.MarshalIndent(events, "", " ")
fmt.Println(string(j))
}
}