master
go 226 lines 5.49 KB
Raw
1 //go:build cgo
2
3 package db2
4
5 // SPDX-License-Identifier: GPL-3.0-or-later
6
7 import (
8 "context"
9 "errors"
10 "fmt"
11 "time"
12
13 "github.com/netdata/netdata/go/plugins/pkg/confopt"
14 "github.com/netdata/netdata/go/plugins/pkg/matcher"
15 "github.com/netdata/netdata/go/plugins/plugin/ibm.d/framework"
16 "github.com/netdata/netdata/go/plugins/plugin/ibm.d/modules/db2/contexts"
17 "github.com/netdata/netdata/go/plugins/plugin/ibm.d/pkg/dbdriver"
18 db2proto "github.com/netdata/netdata/go/plugins/plugin/ibm.d/protocols/db2"
19 )
20
21 func defaultConfig() Config {
22 return Config{
23 Config: framework.Config{
24 UpdateEvery: 5,
25 },
26 Vnode: "",
27 DSN: "",
28 Timeout: confopt.Duration(2 * time.Second),
29 MaxDbConns: 1,
30 MaxDbLifeTime: confopt.Duration(10 * time.Minute),
31
32 CollectDatabaseMetrics: confopt.AutoBoolAuto,
33 CollectBufferpoolMetrics: confopt.AutoBoolAuto,
34 CollectTablespaceMetrics: confopt.AutoBoolAuto,
35 CollectConnectionMetrics: confopt.AutoBoolAuto,
36 CollectLockMetrics: confopt.AutoBoolAuto,
37 CollectTableMetrics: confopt.AutoBoolAuto,
38 CollectIndexMetrics: confopt.AutoBoolAuto,
39
40 CollectMemoryMetrics: true,
41 CollectWaitMetrics: true,
42 CollectTableIOMetrics: true,
43
44 MaxDatabases: 10,
45 MaxBufferpools: 20,
46 MaxTablespaces: 50,
47 MaxConnections: 50,
48 MaxTables: 25,
49 MaxIndexes: 50,
50
51 BackupHistoryDays: 30,
52
53 CollectDatabasesMatching: "",
54
55 IncludeConnections: []string{
56 "db2sysc*",
57 "db2agent*",
58 "db2hadr*",
59 "db2acd*",
60 "db2bmgr*",
61 },
62 ExcludeConnections: []string{
63 "*TEMP*",
64 },
65
66 IncludeBufferpools: []string{
67 "IBMDEFAULTBP",
68 "IBMSYSTEMBP*",
69 "IBMHADRBP*",
70 },
71 ExcludeBufferpools: nil,
72
73 IncludeTablespaces: []string{
74 "SYSCATSPACE",
75 "TEMPSPACE*",
76 "SYSTOOLSPACE",
77 },
78 ExcludeTablespaces: []string{
79 "TEMPSPACE2",
80 },
81
82 IncludeTables: nil,
83 ExcludeTables: nil,
84
85 IncludeIndexes: nil,
86 ExcludeIndexes: nil,
87 }
88 }
89
90 func (c *Collector) Init(ctx context.Context) error {
91 c.initOnce()
92
93 if c.Config.ObsoletionIterations != 0 {
94 c.Collector.Config.ObsoletionIterations = c.Config.ObsoletionIterations
95 }
96 if c.Config.UpdateEvery != 0 {
97 c.Collector.Config.UpdateEvery = c.Config.UpdateEvery
98 }
99 if c.Config.CollectionGroups != nil {
100 c.Collector.Config.CollectionGroups = c.Config.CollectionGroups
101 }
102
103 c.RegisterContexts(contexts.GetAllContexts()...)
104
105 if err := c.Collector.Init(ctx); err != nil {
106 return err
107 }
108
109 c.SetImpl(c)
110
111 if err := c.verifyConfig(); err != nil {
112 return err
113 }
114
115 originalDSN := c.DSN
116 c.DSN = dbdriver.EnsureDriver(c.DSN, "IBM DB2 ODBC DRIVER")
117 if c.DSN != originalDSN {
118 c.Debugf("DSN missing driver keyword; prepended default driver mapping")
119 }
120
121 clientCfg := db2proto.Config{
122 DSN: c.DSN,
123 Timeout: time.Duration(c.Timeout),
124 MaxOpenConns: c.MaxDbConns,
125 ConnMaxLife: time.Duration(c.MaxDbLifeTime),
126 }
127 c.client = db2proto.NewClient(clientCfg)
128
129 if c.CollectDatabasesMatching != "" {
130 m, err := matcher.NewSimplePatternsMatcher(c.CollectDatabasesMatching)
131 if err != nil {
132 return fmt.Errorf("invalid database selector pattern '%s': %v", c.CollectDatabasesMatching, err)
133 }
134 c.databaseSelector = m
135 }
136
137 connInclude, err := compileMatcher(c.IncludeConnections)
138 if err != nil {
139 return fmt.Errorf("invalid include_connections patterns: %w", err)
140 }
141 connExclude, err := compileMatcher(c.ExcludeConnections)
142 if err != nil {
143 return fmt.Errorf("invalid exclude_connections patterns: %w", err)
144 }
145 c.connectionInclude = connInclude
146 c.connectionExclude = connExclude
147
148 bpInclude, err := compileMatcher(c.IncludeBufferpools)
149 if err != nil {
150 return fmt.Errorf("invalid include_bufferpools patterns: %w", err)
151 }
152 bpExclude, err := compileMatcher(c.ExcludeBufferpools)
153 if err != nil {
154 return fmt.Errorf("invalid exclude_bufferpools patterns: %w", err)
155 }
156 c.bufferpoolInclude = bpInclude
157 c.bufferpoolExclude = bpExclude
158
159 tspInclude, err := compileMatcher(c.IncludeTablespaces)
160 if err != nil {
161 return fmt.Errorf("invalid include_tablespaces patterns: %w", err)
162 }
163 tspExclude, err := compileMatcher(c.ExcludeTablespaces)
164 if err != nil {
165 return fmt.Errorf("invalid exclude_tablespaces patterns: %w", err)
166 }
167 c.tablespaceInclude = tspInclude
168 c.tablespaceExclude = tspExclude
169
170 tblInclude, err := compileMatcher(c.IncludeTables)
171 if err != nil {
172 return fmt.Errorf("invalid include_tables patterns: %w", err)
173 }
174 tblExclude, err := compileMatcher(c.ExcludeTables)
175 if err != nil {
176 return fmt.Errorf("invalid exclude_tables patterns: %w", err)
177 }
178 c.tableInclude = tblInclude
179 c.tableExclude = tblExclude
180
181 idxInclude, err := compileMatcher(c.IncludeIndexes)
182 if err != nil {
183 return fmt.Errorf("invalid include_indexes patterns: %w", err)
184 }
185 idxExclude, err := compileMatcher(c.ExcludeIndexes)
186 if err != nil {
187 return fmt.Errorf("invalid exclude_indexes patterns: %w", err)
188 }
189 c.indexInclude = idxInclude
190 c.indexExclude = idxExclude
191
192 if err := c.ensureConnected(ctx); err != nil {
193 return err
194 }
195
196 if err := c.detectDB2Edition(ctx); err != nil {
197 c.Warningf("failed to detect DB2 edition: %v", err)
198 }
199 c.logVersionInformation()
200 c.setConfigurationDefaults()
201 c.detectColumnOrganizedSupport(ctx)
202 c.applyGlobalLabels()
203
204 return nil
205 }
206
207 func (c *Collector) Check(ctx context.Context) error {
208 if err := c.ensureConnected(ctx); err != nil {
209 return err
210 }
211 return nil
212 }
213
214 func (c *Collector) Cleanup(ctx context.Context) {
215 if c.client != nil {
216 _ = c.client.Close()
217 }
218 c.db = nil
219 }
220
221 func (c *Collector) verifyConfig() error {
222 if c.DSN == "" {
223 return errors.New("DSN is required but not set")
224 }
225 return nil
226 }