forked from SumoLogic/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ras.go
341 lines (286 loc) · 9.26 KB
/
ras.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// +build linux
// +build 386 amd64 arm arm64
package ras
import (
"database/sql"
"fmt"
"os"
"strconv"
"strings"
"time"
_ "modernc.org/sqlite" //to register SQLite driver
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
// Ras plugin gathers and counts errors provided by RASDaemon
type Ras struct {
DBPath string `toml:"db_path"`
Log telegraf.Logger `toml:"-"`
db *sql.DB `toml:"-"`
latestTimestamp time.Time `toml:"-"`
cpuSocketCounters map[int]metricCounters `toml:"-"`
serverCounters metricCounters `toml:"-"`
}
type machineCheckError struct {
ID int
Timestamp string
SocketID int
ErrorMsg string
MciStatusMsg string
}
type metricCounters map[string]int64
const (
mceQuery = `
SELECT
id, timestamp, error_msg, mcistatus_msg, socketid
FROM mce_record
WHERE timestamp > ?
`
defaultDbPath = "/var/lib/rasdaemon/ras-mc_event.db"
dateLayout = "2006-01-02 15:04:05 -0700"
memoryReadCorrected = "memory_read_corrected_errors"
memoryReadUncorrected = "memory_read_uncorrectable_errors"
memoryWriteCorrected = "memory_write_corrected_errors"
memoryWriteUncorrected = "memory_write_uncorrectable_errors"
instructionCache = "cache_l0_l1_errors"
instructionTLB = "tlb_instruction_errors"
levelTwoCache = "cache_l2_errors"
upi = "upi_errors"
processorBase = "processor_base_errors"
processorBus = "processor_bus_errors"
internalTimer = "internal_timer_errors"
smmHandlerCode = "smm_handler_code_access_violation_errors"
internalParity = "internal_parity_errors"
frc = "frc_errors"
externalMCEBase = "external_mce_errors"
microcodeROMParity = "microcode_rom_parity_errors"
unclassifiedMCEBase = "unclassified_mce_errors"
)
// SampleConfig returns sample configuration for this plugin.
func (r *Ras) SampleConfig() string {
return `
## Optional path to RASDaemon sqlite3 database.
## Default: /var/lib/rasdaemon/ras-mc_event.db
# db_path = ""
`
}
// Description returns the plugin description.
func (r *Ras) Description() string {
return "RAS plugin exposes counter metrics for Machine Check Errors provided by RASDaemon (sqlite3 output is required)."
}
// Start initializes connection to DB, metrics are gathered in Gather
func (r *Ras) Start(telegraf.Accumulator) error {
err := validateDbPath(r.DBPath)
if err != nil {
return err
}
r.db, err = connectToDB(r.DBPath)
if err != nil {
return err
}
return nil
}
// Stop closes any existing DB connection
func (r *Ras) Stop() {
if r.db != nil {
err := r.db.Close()
if err != nil {
r.Log.Errorf("Error appeared during closing DB (%s): %v", r.DBPath, err)
}
}
}
// Gather reads the stats provided by RASDaemon and writes it to the Accumulator.
func (r *Ras) Gather(acc telegraf.Accumulator) error {
rows, err := r.db.Query(mceQuery, r.latestTimestamp)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
mcError, err := fetchMachineCheckError(rows)
if err != nil {
return err
}
tsErr := r.updateLatestTimestamp(mcError.Timestamp)
if tsErr != nil {
return err
}
r.updateCounters(mcError)
}
addCPUSocketMetrics(acc, r.cpuSocketCounters)
addServerMetrics(acc, r.serverCounters)
return nil
}
func (r *Ras) updateLatestTimestamp(timestamp string) error {
ts, err := parseDate(timestamp)
if err != nil {
return err
}
if ts.After(r.latestTimestamp) {
r.latestTimestamp = ts
}
return nil
}
func (r *Ras) updateCounters(mcError *machineCheckError) {
if strings.Contains(mcError.ErrorMsg, "No Error") {
return
}
r.initializeCPUMetricDataIfRequired(mcError.SocketID)
r.updateSocketCounters(mcError)
r.updateServerCounters(mcError)
}
func newMetricCounters() *metricCounters {
return &metricCounters{
memoryReadCorrected: 0,
memoryReadUncorrected: 0,
memoryWriteCorrected: 0,
memoryWriteUncorrected: 0,
instructionCache: 0,
instructionTLB: 0,
processorBase: 0,
processorBus: 0,
internalTimer: 0,
smmHandlerCode: 0,
internalParity: 0,
frc: 0,
externalMCEBase: 0,
microcodeROMParity: 0,
unclassifiedMCEBase: 0,
}
}
func (r *Ras) updateServerCounters(mcError *machineCheckError) {
if strings.Contains(mcError.ErrorMsg, "CACHE Level-2") && strings.Contains(mcError.ErrorMsg, "Error") {
r.serverCounters[levelTwoCache]++
}
if strings.Contains(mcError.ErrorMsg, "UPI:") {
r.serverCounters[upi]++
}
}
func validateDbPath(dbPath string) error {
pathInfo, err := os.Stat(dbPath)
if os.IsNotExist(err) {
return fmt.Errorf("provided db_path does not exist: [%s]", dbPath)
}
if err != nil {
return fmt.Errorf("cannot get system information for db_path file: [%s] - %v", dbPath, err)
}
if mode := pathInfo.Mode(); !mode.IsRegular() {
return fmt.Errorf("provided db_path does not point to a regular file: [%s]", dbPath)
}
return nil
}
func connectToDB(dbPath string) (*sql.DB, error) {
return sql.Open("sqlite", dbPath)
}
func (r *Ras) initializeCPUMetricDataIfRequired(socketID int) {
if _, ok := r.cpuSocketCounters[socketID]; !ok {
r.cpuSocketCounters[socketID] = *newMetricCounters()
}
}
func (r *Ras) updateSocketCounters(mcError *machineCheckError) {
r.updateMemoryCounters(mcError)
r.updateProcessorBaseCounters(mcError)
if strings.Contains(mcError.ErrorMsg, "Instruction TLB") && strings.Contains(mcError.ErrorMsg, "Error") {
r.cpuSocketCounters[mcError.SocketID][instructionTLB]++
}
if strings.Contains(mcError.ErrorMsg, "BUS") && strings.Contains(mcError.ErrorMsg, "Error") {
r.cpuSocketCounters[mcError.SocketID][processorBus]++
}
if (strings.Contains(mcError.ErrorMsg, "CACHE Level-0") ||
strings.Contains(mcError.ErrorMsg, "CACHE Level-1")) &&
strings.Contains(mcError.ErrorMsg, "Error") {
r.cpuSocketCounters[mcError.SocketID][instructionCache]++
}
}
func (r *Ras) updateProcessorBaseCounters(mcError *machineCheckError) {
if strings.Contains(mcError.ErrorMsg, "Internal Timer error") {
r.cpuSocketCounters[mcError.SocketID][internalTimer]++
r.cpuSocketCounters[mcError.SocketID][processorBase]++
}
if strings.Contains(mcError.ErrorMsg, "SMM Handler Code Access Violation") {
r.cpuSocketCounters[mcError.SocketID][smmHandlerCode]++
r.cpuSocketCounters[mcError.SocketID][processorBase]++
}
if strings.Contains(mcError.ErrorMsg, "Internal parity error") {
r.cpuSocketCounters[mcError.SocketID][internalParity]++
r.cpuSocketCounters[mcError.SocketID][processorBase]++
}
if strings.Contains(mcError.ErrorMsg, "FRC error") {
r.cpuSocketCounters[mcError.SocketID][frc]++
r.cpuSocketCounters[mcError.SocketID][processorBase]++
}
if strings.Contains(mcError.ErrorMsg, "External error") {
r.cpuSocketCounters[mcError.SocketID][externalMCEBase]++
r.cpuSocketCounters[mcError.SocketID][processorBase]++
}
if strings.Contains(mcError.ErrorMsg, "Microcode ROM parity error") {
r.cpuSocketCounters[mcError.SocketID][microcodeROMParity]++
r.cpuSocketCounters[mcError.SocketID][processorBase]++
}
if strings.Contains(mcError.ErrorMsg, "Unclassified") || strings.Contains(mcError.ErrorMsg, "Internal unclassified") {
r.cpuSocketCounters[mcError.SocketID][unclassifiedMCEBase]++
r.cpuSocketCounters[mcError.SocketID][processorBase]++
}
}
func (r *Ras) updateMemoryCounters(mcError *machineCheckError) {
if strings.Contains(mcError.ErrorMsg, "Memory read error") {
if strings.Contains(mcError.MciStatusMsg, "Corrected_error") {
r.cpuSocketCounters[mcError.SocketID][memoryReadCorrected]++
} else {
r.cpuSocketCounters[mcError.SocketID][memoryReadUncorrected]++
}
}
if strings.Contains(mcError.ErrorMsg, "Memory write error") {
if strings.Contains(mcError.MciStatusMsg, "Corrected_error") {
r.cpuSocketCounters[mcError.SocketID][memoryWriteCorrected]++
} else {
r.cpuSocketCounters[mcError.SocketID][memoryWriteUncorrected]++
}
}
}
func addCPUSocketMetrics(acc telegraf.Accumulator, cpuSocketCounters map[int]metricCounters) {
for socketID, data := range cpuSocketCounters {
tags := map[string]string{
"socket_id": strconv.Itoa(socketID),
}
fields := make(map[string]interface{})
for errorName, count := range data {
fields[errorName] = count
}
acc.AddCounter("ras", fields, tags)
}
}
func addServerMetrics(acc telegraf.Accumulator, counters map[string]int64) {
fields := make(map[string]interface{})
for errorName, count := range counters {
fields[errorName] = count
}
acc.AddCounter("ras", fields, map[string]string{})
}
func fetchMachineCheckError(rows *sql.Rows) (*machineCheckError, error) {
mcError := &machineCheckError{}
err := rows.Scan(&mcError.ID, &mcError.Timestamp, &mcError.ErrorMsg, &mcError.MciStatusMsg, &mcError.SocketID)
if err != nil {
return nil, err
}
return mcError, nil
}
func parseDate(date string) (time.Time, error) {
return time.Parse(dateLayout, date)
}
func init() {
inputs.Add("ras", func() telegraf.Input {
defaultTimestamp, _ := parseDate("1970-01-01 00:00:01 -0700")
return &Ras{
DBPath: defaultDbPath,
latestTimestamp: defaultTimestamp,
cpuSocketCounters: map[int]metricCounters{
0: *newMetricCounters(),
},
serverCounters: map[string]int64{
levelTwoCache: 0,
upi: 0,
},
}
})
}