1/*
2** Zabbix
3** Copyright (C) 2001-2021 Zabbix SIA
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18**/
19
20package oracle
21
22import (
23	"context"
24
25	"zabbix.com/pkg/metric"
26	"zabbix.com/pkg/plugin"
27	"zabbix.com/pkg/uri"
28)
29
30const (
31	keyArchive                = "oracle.archive.info"
32	keyArchiveDiscovery       = "oracle.archive.discovery"
33	keyASMDiskGroups          = "oracle.diskgroups.stats"
34	keyASMDiskGroupsDiscovery = "oracle.diskgroups.discovery"
35	keyCDB                    = "oracle.cdb.info"
36	keyCustomQuery            = "oracle.custom.query"
37	keyDatabasesDiscovery     = "oracle.db.discovery"
38	keyDataFiles              = "oracle.datafiles.stats"
39	keyFRA                    = "oracle.fra.stats"
40	keyInstance               = "oracle.instance.info"
41	keyPDB                    = "oracle.pdb.info"
42	keyPDBDiscovery           = "oracle.pdb.discovery"
43	keyPGA                    = "oracle.pga.stats"
44	keyPing                   = "oracle.ping"
45	keyProc                   = "oracle.proc.stats"
46	keyRedoLog                = "oracle.redolog.info"
47	keySessions               = "oracle.sessions.stats"
48	keySGA                    = "oracle.sga.stats"
49	keySysMetrics             = "oracle.sys.metrics"
50	keySysParams              = "oracle.sys.params"
51	keyTablespaces            = "oracle.ts.stats"
52	keyTablespacesDiscovery   = "oracle.ts.discovery"
53	keyUser                   = "oracle.user.info"
54)
55
56// handlerFunc defines an interface must be implemented by handlers.
57type handlerFunc func(ctx context.Context, conn OraClient,
58	params map[string]string, extraParams ...string) (res interface{}, err error)
59
60// getHandlerFunc returns a handlerFunc related to a given key.
61func getHandlerFunc(key string) handlerFunc {
62	switch key {
63	case keyASMDiskGroups:
64		return asmDiskGroupsHandler
65	case keyASMDiskGroupsDiscovery:
66		return asmDiskGroupsDiscovery
67	case keyArchive:
68		return archiveHandler
69	case keyArchiveDiscovery:
70		return archiveDiscoveryHandler
71	case keyCDB:
72		return cdbHandler
73	case keyCustomQuery:
74		return customQueryHandler
75	case keyDataFiles:
76		return dataFileHandler
77	case keyDatabasesDiscovery:
78		return databasesDiscoveryHandler
79	case keyFRA:
80		return fraHandler
81	case keyInstance:
82		return instanceHandler
83	case keyPDB:
84		return pdbHandler
85	case keyPDBDiscovery:
86		return pdbDiscoveryHandler
87	case keyPGA:
88		return pgaHandler
89	case keyPing:
90		return pingHandler
91	case keyProc:
92		return procHandler
93	case keyRedoLog:
94		return redoLogHandler
95	case keySGA:
96		return sgaHandler
97	case keySessions:
98		return sessionsHandler
99	case keySysMetrics:
100		return sysMetricsHandler
101	case keySysParams:
102		return sysParamsHandler
103	case keyTablespaces:
104		return tablespacesHandler
105	case keyTablespacesDiscovery:
106		return tablespacesDiscoveryHandler
107	case keyUser:
108		return userHandler
109
110	default:
111		return nil
112	}
113}
114
115var uriDefaults = &uri.Defaults{Scheme: "tcp", Port: "1521"}
116
117// Common params: [URI|Session][,User][,Password][,Service]
118var (
119	paramURI = metric.NewConnParam("URI", "URI to connect or session name.").
120			WithDefault(uriDefaults.Scheme + "://localhost:" + uriDefaults.Port).WithSession().
121			WithValidator(uri.URIValidator{Defaults: uriDefaults, AllowedSchemes: []string{"tcp"}})
122	paramUsername = metric.NewConnParam("User", "Oracle user.").WithDefault("")
123	paramPassword = metric.NewConnParam("Password", "User's password.").WithDefault("")
124	paramService  = metric.NewConnParam("Service", "Service name to be used for connection.").
125			WithDefault("XE")
126)
127
128var metrics = metric.MetricSet{
129	keyASMDiskGroups: metric.New("Returns ASM disk groups statistics.",
130		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
131
132	keyASMDiskGroupsDiscovery: metric.New("Returns list of ASM disk groups in LLD format.",
133		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
134
135	keyArchive: metric.New("Returns archive logs statistics.",
136		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
137
138	keyArchiveDiscovery: metric.New("Returns list of archive logs in LLD format.",
139		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
140
141	keyCDB: metric.New("Returns CDBs info.",
142		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
143
144	keyCustomQuery: metric.New("Returns result of a custom query.",
145		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService,
146			metric.NewParam("QueryName", "Name of a custom query "+
147				"(must be equal to a name of an SQL file without an extension).").SetRequired(),
148		}, true),
149
150	keyDataFiles: metric.New("Returns data files statistics.",
151		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
152
153	keyDatabasesDiscovery: metric.New("Returns list of databases in LLD format.",
154		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
155
156	keyFRA: metric.New("Returns FRA statistics.",
157		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
158
159	keyInstance: metric.New("Returns instance stats.",
160		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
161
162	keyPDB: metric.New("Returns PDBs info.",
163		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
164
165	keyPDBDiscovery: metric.New("Returns list of PDBs in LLD format.",
166		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
167
168	keyPGA: metric.New("Returns PGA statistics.",
169		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
170
171	keyPing: metric.New("Tests if connection is alive or not.",
172		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
173
174	keyProc: metric.New("Returns processes statistics.",
175		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
176
177	keyRedoLog: metric.New("Returns log file information from the control file.",
178		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
179
180	keySGA: metric.New("Returns SGA statistics.",
181		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
182
183	keySessions: metric.New("Returns sessions statistics.",
184		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService,
185			metric.NewParam("LockMaxTime", "Maximum session lock duration in seconds to count "+
186				"the session as a prolongedly locked.").WithDefault("600").WithValidator(metric.NumberValidator{}),
187		}, false),
188
189	keySysMetrics: metric.New("Returns a set of system metric values.",
190		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService,
191			metric.NewParam("Duration", "Capturing interval in seconds of system metric values.").
192				WithDefault("60").WithValidator(metric.SetValidator{Set: []string{"15", "60"}}),
193		}, false),
194
195	keySysParams: metric.New("Returns a set of system parameter values.",
196		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
197
198	keyTablespaces: metric.New("Returns tablespaces statistics.",
199		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
200
201	keyTablespacesDiscovery: metric.New("Returns list of tablespaces in LLD format.",
202		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService}, false),
203
204	keyUser: metric.New("Returns user information.",
205		[]*metric.Param{paramURI, paramUsername, paramPassword, paramService,
206			metric.NewParam("Username", "Username for which the information is needed."),
207		}, false),
208}
209
210func init() {
211	plugin.RegisterMetrics(&impl, pluginName, metrics.List()...)
212}
213