-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcheck.go
202 lines (157 loc) · 4.59 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// 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.
package checks
import (
"time"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
"github.com/DataDog/datadog-agent/pkg/collector/check"
"github.com/DataDog/datadog-agent/pkg/compliance"
"github.com/DataDog/datadog-agent/pkg/compliance/checks/env"
"github.com/DataDog/datadog-agent/pkg/compliance/event"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/version"
)
// eventNotify is a callback invoked when a compliance check reported an event
type eventNotify func(ruleID string, event *event.Event)
type resourceReporter func(*compliance.Report) compliance.ReportResource
// complianceCheck implements a compliance check
type complianceCheck struct {
env.Env
ruleID string
description string
interval time.Duration
suiteMeta *compliance.SuiteMeta
scope compliance.RuleScope
resourceHandler resourceReporter
checkable checkable
eventNotify eventNotify
}
func (c *complianceCheck) Stop() {
}
func (c *complianceCheck) Cancel() {
}
func (c *complianceCheck) String() string {
return compliance.CheckName(c.ruleID, c.description)
}
func (c *complianceCheck) Configure(config, initConfig integration.Data, source string) error {
return nil
}
func (c *complianceCheck) Interval() time.Duration {
return c.interval
}
func (c *complianceCheck) ID() check.ID {
return check.ID(c.ruleID)
}
func (c *complianceCheck) GetWarnings() []error {
return nil
}
func (c *complianceCheck) GetSenderStats() (check.SenderStats, error) {
return check.NewSenderStats(), nil
}
func (c *complianceCheck) Version() string {
return c.suiteMeta.Version
}
func (c *complianceCheck) ConfigSource() string {
return c.suiteMeta.Source
}
func (c *complianceCheck) IsTelemetryEnabled() bool {
return false
}
func (c *complianceCheck) reportToResource(report *compliance.Report) compliance.ReportResource {
if c.resourceHandler != nil {
return c.resourceHandler(report)
}
return compliance.ReportResource{
Type: string(c.scope),
ID: c.Hostname(),
}
}
type resourceQuadID struct {
AgentRuleID string
AgentFrameworkID string
ResourceID string
ResourceType string
}
func (c *complianceCheck) Run() error {
if !c.IsLeader() {
return nil
}
var err error
reports := c.checkable.check(c)
resourceQuadIDs := make(map[resourceQuadID]bool)
for _, report := range reports {
if report.Error != nil {
log.Debugf("%s: check run failed: %v", c.ruleID, report.Error)
err = report.Error
}
data, result := reportToEventData(report)
resource := c.reportToResource(report)
quadID := resourceQuadID{
AgentRuleID: c.ruleID,
AgentFrameworkID: c.suiteMeta.Framework,
ResourceID: resource.ID,
ResourceType: resource.Type,
}
// skip if we already sent an event with this quad ID
if _, present := resourceQuadIDs[quadID]; present {
continue
}
resourceQuadIDs[quadID] = true
evaluator := report.Evaluator
if evaluator == "" {
evaluator = "legacy"
}
e := &event.Event{
AgentRuleID: quadID.AgentRuleID,
AgentFrameworkID: quadID.AgentFrameworkID,
AgentVersion: version.AgentVersion,
ResourceID: quadID.ResourceID,
ResourceType: quadID.ResourceType,
Result: result,
Data: data,
Evaluator: evaluator,
ExpireAt: c.computeExpireAt(),
}
log.Debugf("%s: reporting [%s] [%s] [%s]", c.ruleID, e.Result, e.ResourceID, e.ResourceType)
c.Reporter().Report(e)
if c.eventNotify != nil {
c.eventNotify(c.ruleID, e)
}
}
return err
}
// ExpireAtIntervalFactor represents the amount of intervals between a check and its expiration
const ExpireAtIntervalFactor = 3
func (c *complianceCheck) computeExpireAt() time.Time {
base := time.Now().Add(c.interval * ExpireAtIntervalFactor).UTC()
// remove sub-second precision
truncated := base.Truncate(1 * time.Second)
return truncated
}
func reportToEventData(report *compliance.Report) (event.Data, string) {
data := report.Data
passed := report.Passed
if report.Error != nil {
data = event.Data{
"error": report.Error.Error(),
}
}
if report.Aggregated {
if data == nil {
data = event.Data{}
}
data["aggregated"] = true
}
return data, eventResult(passed, report.Error)
}
func eventResult(passed bool, err error) string {
if err != nil {
return event.Error
}
if passed {
return event.Passed
}
return event.Failed
}