-
Notifications
You must be signed in to change notification settings - Fork 77
/
main.go
1103 lines (1029 loc) · 32 KB
/
main.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"database/sql"
"flag"
"net"
"net/http"
"strconv"
"time"
_ "github.com/mattn/go-oci8"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
)
// Metric name parts.
const (
namespace = "oracledb"
exporter = "exporter"
)
// Exporter collects Oracle DB metrics. It implements prometheus.Collector.
type Exporter struct {
duration, error prometheus.Gauge
totalScrapes prometheus.Counter
scrapeErrors *prometheus.CounterVec
session *prometheus.GaugeVec
sysstat *prometheus.GaugeVec
waitclass *prometheus.GaugeVec
sysmetric *prometheus.GaugeVec
interconnect *prometheus.GaugeVec
uptime *prometheus.GaugeVec
up *prometheus.GaugeVec
tablespace *prometheus.GaugeVec
recovery *prometheus.GaugeVec
redo *prometheus.GaugeVec
cache *prometheus.GaugeVec
alertlog *prometheus.GaugeVec
alertdate *prometheus.GaugeVec
services *prometheus.GaugeVec
parameter *prometheus.GaugeVec
//query *prometheus.GaugeVec
asmspace *prometheus.GaugeVec
tablerows *prometheus.GaugeVec
tablebytes *prometheus.GaugeVec
indexbytes *prometheus.GaugeVec
lobbytes *prometheus.GaugeVec
lastIp string
vTabRows bool
vTabBytes bool
vIndBytes bool
vLobBytes bool
vRecovery bool
custom map[string]*prometheus.GaugeVec
}
var (
// Version will be set at build time.
Version = "1.1.5"
listenAddress = flag.String("web.listen-address", ":9161", "Address to listen on for web interface and telemetry.")
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
pMetrics = flag.Bool("defaultmetrics", true, "Expose standard metrics")
pTabRows = flag.Bool("tablerows", false, "Expose Table rows (CAN TAKE VERY LONG)")
pTabBytes = flag.Bool("tablebytes", false, "Expose Table size (CAN TAKE VERY LONG)")
pIndBytes = flag.Bool("indexbytes", false, "Expose Index size for any Table (CAN TAKE VERY LONG)")
pLobBytes = flag.Bool("lobbytes", false, "Expose Lobs size for any Table (CAN TAKE VERY LONG)")
pRecovery = flag.Bool("recovery", false, "Expose Recovery percentage usage of FRA (CAN TAKE VERY LONG)")
configFile = flag.String("configfile", "oracle.conf", "ConfigurationFile in YAML format.")
logFile = flag.String("logfile", "exporter.log", "Logfile for parsed Oracle Alerts.")
accessFile = flag.String("accessfile", "access.conf", "Last access for parsed Oracle Alerts.")
landingPage = []byte(`<html>
<head><title>Prometheus Oracle exporter</title></head>
<body>
<h1>Prometheus Oracle exporter</h1><p>
<a href='` + *metricPath + `'>Metrics</a></p>
<a href='` + *metricPath + `?tablerows=true'>Metrics with tablerows</a></p>
<a href='` + *metricPath + `?tablebytes=true'>Metrics with tablebytes</a></p>
<a href='` + *metricPath + `?indexbytes=true'>Metrics with indexbytes</a></p>
<a href='` + *metricPath + `?lobbytes=true'>Metrics with lobbytes</a></p>
<a href='` + *metricPath + `?recovery=true'>Metrics with recovery</a></p>
</body>
</html>`)
)
// NewExporter returns a new Oracle DB exporter for the provided DSN.
func NewExporter() *Exporter {
e := Exporter{
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape of metrics from Oracle DB.",
}),
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrapes_total",
Help: "Total number of times Oracle DB was scraped for metrics.",
}),
scrapeErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrape_errors_total",
Help: "Total number of times an error occured scraping a Oracle database.",
}, []string{"collector"}),
error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from Oracle DB resulted in an error (1 for error, 0 for success).",
}),
sysmetric: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "sysmetric",
Help: "Gauge metric with read/write pysical IOPs/bytes (v$sysmetric).",
}, []string{"database", "dbinstance", "type"}),
waitclass: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "waitclass",
Help: "Gauge metric with Waitevents (v$waitclassmetric).",
}, []string{"database", "dbinstance", "type"}),
sysstat: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "sysstat",
Help: "Gauge metric with commits/rollbacks/parses (v$sysstat).",
}, []string{"database", "dbinstance", "type"}),
session: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "session",
Help: "Gauge metric user/system active/passive sessions (v$session).",
}, []string{"database", "dbinstance", "type", "state"}),
uptime: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "uptime",
Help: "Gauge metric with uptime in days of the Instance.",
}, []string{"database", "dbinstance"}),
tablespace: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "tablespace",
Help: "Gauge metric with total/free size of the Tablespaces.",
}, []string{"database", "dbinstance", "type", "name", "contents", "autoextend"}),
interconnect: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "interconnect",
Help: "Gauge metric with interconnect block transfers (v$sysstat).",
}, []string{"database", "dbinstance", "type"}),
recovery: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "recovery",
Help: "Gauge metric with percentage usage of FRA (v$recovery_file_dest).",
}, []string{"database", "dbinstance", "type"}),
redo: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "redo",
Help: "Gauge metric with Redo log switches over last 5 min (v$log_history).",
}, []string{"database", "dbinstance"}),
cache: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "cachehitratio",
Help: "Gauge metric witch Cache hit ratios (v$sysmetric).",
}, []string{"database", "dbinstance", "type"}),
up: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Whether the Oracle server is up.",
}, []string{"database", "dbinstance"}),
alertlog: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "error",
Help: "Oracle Errors occured during configured interval.",
}, []string{"database", "dbinstance", "code", "description", "ignore"}),
alertdate: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "error_unix_seconds",
Help: "Unixtime of Alertlog modified Date.",
}, []string{"database", "dbinstance"}),
services: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "services",
Help: "Active Oracle Services (v$active_services).",
}, []string{"database", "dbinstance", "name"}),
parameter: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "parameter",
Help: "oracle Configuration Parameters (v$parameter).",
}, []string{"database", "dbinstance", "name"}),
// query: prometheus.NewGaugeVec(prometheus.GaugeOpts{
// Namespace: namespace,
// Name: "query",
// Help: "Self defined Queries from Configuration File.",
// }, []string{"database", "dbinstance", "name", "column", "row"}),
asmspace: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "asmspace",
Help: "Gauge metric with total/free size of the ASM Diskgroups.",
}, []string{"database", "dbinstance", "type", "name"}),
tablerows: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "tablerows",
Help: "Gauge metric with rows of all Tables.",
}, []string{"database", "dbinstance", "owner", "table_name", "tablespace"}),
tablebytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "tablebytes",
Help: "Gauge metric with bytes of all Tables.",
}, []string{"database", "dbinstance", "owner", "table_name"}),
indexbytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "indexbytes",
Help: "Gauge metric with bytes of all Indexes per Table.",
}, []string{"database", "dbinstance", "owner", "table_name"}),
lobbytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "lobbytes",
Help: "Gauge metric with bytes of all Lobs per Table.",
}, []string{"database", "dbinstance", "owner", "table_name"}),
custom: make(map[string]*prometheus.GaugeVec),
}
// add custom metrics
for _, conn := range config.Cfgs {
for _, query := range conn.Queries {
labels := []string{}
for _, label := range query.Labels {
labels = append(labels, cleanName(label))
}
e.custom[query.Name] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "custom_" + cleanName(query.Name),
Help: query.Help,
}, append(labels, "metric", "database", "dbinstance", "rownum"))
}
}
return &e
}
// ScrapeCustomQueries collects metrics from self defined queries from configuration file.
func (e *Exporter) ScrapeCustomQueries() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
for _, query := range conn.Queries {
rows, err = conn.db.Query(query.Sql)
if err != nil {
continue
}
cols, _ := rows.Columns()
vals := make([]interface{}, len(cols))
defer rows.Close()
var rownum int = 1
for rows.Next() {
for i := range cols {
vals[i] = &vals[i]
}
err = rows.Scan(vals...)
if err != nil {
break
}
MetricLoop:
for _, metric := range query.Metrics {
metricColumnIndex := -1
for i, col := range cols {
if cleanName(metric) == cleanName(col) {
metricColumnIndex = i
break
}
}
if metricColumnIndex == -1 {
//log.Infoln("Metric column '" + metric + "' not found")
continue MetricLoop
}
if metricValue, ok := vals[metricColumnIndex].(float64); ok {
promLabels := prometheus.Labels{}
promLabels["database"] = conn.Database
promLabels["dbinstance"] = conn.Instance
promLabels["metric"] = metric
promLabels["rownum"] = strconv.Itoa(rownum)
LebelLoop:
for _, label := range query.Labels {
labelColumnIndex := -1
for i, col := range cols {
if cleanName(label) == cleanName(col) {
labelColumnIndex = i
break
}
}
if labelColumnIndex == -1 {
//log.Infoln("Label column not found")
break LebelLoop
}
if a, ok := vals[labelColumnIndex].(string); ok {
promLabels[cleanName(label)] = a
} else if b, ok := vals[labelColumnIndex].(float64); ok {
// if value is integer
if b == float64(int64(b)) {
promLabels[cleanName(label)] = strconv.Itoa(int(b))
} else {
promLabels[cleanName(label)] = strconv.FormatFloat(b, 'e', -1, 64)
}
}
}
e.custom[query.Name].With(promLabels).Set(metricValue)
}
}
rownum++
}
}
}
}
}
// ScrapeQuery collects metrics from self defined queries from configuration file.
// func (e *Exporter) ScrapeQuery() {
// var (
// rows *sql.Rows
// err error
// )
// for _, conn := range config.Cfgs {
// if conn.db != nil {
// for _, query := range conn.Queries {
// rows, err = conn.db.Query(query.Sql)
// if err != nil {
// continue
// }
// cols, _ := rows.Columns()
// vals := make([]interface{}, len(cols))
// var rownum int = 1
// defer rows.Close()
// for rows.Next() {
// for i := range cols {
// vals[i] = &vals[i]
// }
// err = rows.Scan(vals...)
// if err != nil {
// break
// }
// for i := range cols {
// if value, ok := vals[i].(float64); ok {
// e.query.WithLabelValues(conn.Database, conn.Instance, query.Name, cols[i], strconv.Itoa(rownum)).Set(value)
// }
// }
// rownum++
// }
// }
// }
// }
// }
// ScrapeParameters collects metrics from the v$parameters view.
func (e *Exporter) ScrapeParameter() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
//num metric_name
//43 sessions
if conn.db != nil {
rows, err = conn.db.Query(`select name,value from v$parameter WHERE num=43`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
var value float64
if err := rows.Scan(&name, &value); err != nil {
break
}
name = cleanName(name)
e.parameter.WithLabelValues(conn.Database, conn.Instance, name).Set(value)
}
}
}
}
// ScrapeServices collects metrics from the v$active_services view.
func (e *Exporter) ScrapeServices() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`select name from v$active_services`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
break
}
name = cleanName(name)
e.services.WithLabelValues(conn.Database, conn.Instance, name).Set(1)
}
}
}
}
// ScrapeCache collects session metrics from the v$sysmetrics view.
func (e *Exporter) ScrapeCache() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
//metric_id metric_name
//2000 Buffer Cache Hit Ratio
//2050 Cursor Cache Hit Ratio
//2112 Library Cache Hit Ratio
//2110 Row Cache Hit Ratio
if conn.db != nil {
rows, err = conn.db.Query(`select metric_name,value
from v$sysmetric
where group_id=2 and metric_id in (2000,2050,2112,2110)`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
var value float64
if err := rows.Scan(&name, &value); err != nil {
break
}
name = cleanName(name)
e.cache.WithLabelValues(conn.Database, conn.Instance, name).Set(value)
}
}
}
}
// ScrapeRecovery collects tablespace metrics
func (e *Exporter) ScrapeRedo() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`select count(*) from v$log_history where first_time > sysdate - 1/24/12`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var value float64
if err := rows.Scan(&value); err != nil {
break
}
e.redo.WithLabelValues(conn.Database, conn.Instance).Set(value)
}
}
}
}
// ScrapeRecovery collects tablespace metrics
func (e *Exporter) ScrapeRecovery() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`SELECT sum(percent_space_used) , sum(percent_space_reclaimable)
from V$FLASH_RECOVERY_AREA_USAGE`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var used float64
var recl float64
if err := rows.Scan(&used, &recl); err != nil {
break
}
e.recovery.WithLabelValues(conn.Database, conn.Instance, "percent_space_used").Set(used)
e.recovery.WithLabelValues(conn.Database, conn.Instance, "percent_space_reclaimable").Set(recl)
}
}
}
}
// ScrapeTablespaces collects tablespace metrics
func (e *Exporter) ScrapeInterconnect() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`SELECT name, value
FROM V$SYSSTAT
WHERE name in ('gc cr blocks served','gc cr blocks flushed','gc cr blocks received')`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
var value float64
if err := rows.Scan(&name, &value); err != nil {
break
}
name = cleanName(name)
e.interconnect.WithLabelValues(conn.Database, conn.Instance, name).Set(value)
}
}
}
}
// ScrapeAsmspace collects ASM metrics
func (e *Exporter) ScrapeAsmspace() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`SELECT g.name, sum(d.total_mb), sum(d.free_mb)
FROM v$asm_disk_stat d, v$asm_diskgroup_stat g
WHERE d.group_number = g.group_number
AND d.header_status = 'MEMBER'
GROUP by g.name, g.group_number`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
var tsize float64
var tfree float64
if err := rows.Scan(&name, &tsize, &tfree); err != nil {
break
}
e.asmspace.WithLabelValues(conn.Database, conn.Instance, "total", name).Set(tsize)
e.asmspace.WithLabelValues(conn.Database, conn.Instance, "free", name).Set(tfree)
e.asmspace.WithLabelValues(conn.Database, conn.Instance, "used", name).Set(tsize - tfree)
}
}
}
}
// ScrapeTablespaces collects tablespace metrics
func (e *Exporter) ScrapeTablespace() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`WITH
getsize AS (SELECT tablespace_name, max(autoextensible) autoextensible, SUM(bytes) tsize, sum(maxbytes) maxbytes
FROM dba_data_files GROUP BY tablespace_name),
getfree as (SELECT tablespace_name, contents, SUM(blocks*block_size) tfree
FROM DBA_LMT_FREE_SPACE a, v$tablespace b, dba_tablespaces c
WHERE a.TABLESPACE_ID= b.ts# and b.name=c.tablespace_name
GROUP BY tablespace_name,contents)
SELECT a.tablespace_name, b.contents, a.tsize, a.maxbytes, b.tfree, a.autoextensible autoextend
FROM GETSIZE a, GETFREE b
WHERE a.tablespace_name = b.tablespace_name
UNION
SELECT tablespace_name, 'TEMPORARY', sum(tablespace_size), sum(tablespace_size), sum(free_space), 'NO'
FROM dba_temp_free_space
GROUP BY tablespace_name`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
var contents string
var tsize float64
var maxsize float64
var tfree float64
var auto string
if err := rows.Scan(&name, &contents, &tsize, &maxsize, &tfree, &auto); err != nil {
break
}
e.tablespace.WithLabelValues(conn.Database, conn.Instance, "total", name, contents, auto).Set(tsize)
e.tablespace.WithLabelValues(conn.Database, conn.Instance, "max", name, contents, auto).Set(maxsize)
e.tablespace.WithLabelValues(conn.Database, conn.Instance, "free", name, contents, auto).Set(tfree)
e.tablespace.WithLabelValues(conn.Database, conn.Instance, "used", name, contents, auto).Set(tsize - tfree)
}
}
}
}
// ScrapeSessions collects session metrics from the v$session view.
func (e *Exporter) ScrapeSession() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`SELECT decode(username,NULL,'SYSTEM','SYS','SYSTEM','USER'), status,count(*)
FROM v$session
GROUP BY decode(username,NULL,'SYSTEM','SYS','SYSTEM','USER'),status`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var user string
var status string
var value float64
if err := rows.Scan(&user, &status, &value); err != nil {
break
}
e.session.WithLabelValues(conn.Database, conn.Instance, user, status).Set(value)
}
}
}
}
// ScrapeUptime Instance uptime
func (e *Exporter) ScrapeUptime() {
var uptime float64
for _, conn := range config.Cfgs {
if conn.db != nil {
err := conn.db.QueryRow("select sysdate-startup_time from v$instance").Scan(&uptime)
if err != nil {
return
}
e.uptime.WithLabelValues(conn.Database, conn.Instance).Set(uptime)
}
}
}
// ScrapeSysstat collects activity metrics from the v$sysstat view.
func (e *Exporter) ScrapeSysstat() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`SELECT name, value FROM v$sysstat
WHERE statistic# in (6,7,1084,1089)`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
var value float64
if err := rows.Scan(&name, &value); err != nil {
break
}
name = cleanName(name)
e.sysstat.WithLabelValues(conn.Database, conn.Instance, name).Set(value)
}
}
}
}
// ScrapeWaitTime collects wait time metrics from the v$waitclassmetric view.
func (e *Exporter) ScrapeWaitclass() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`SELECT n.wait_class, round(m.time_waited/m.INTSIZE_CSEC,3)
FROM v$waitclassmetric m, v$system_wait_class n
WHERE m.wait_class_id=n.wait_class_id and n.wait_class != 'Idle'`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
var value float64
if err := rows.Scan(&name, &value); err != nil {
break
}
name = cleanName(name)
e.waitclass.WithLabelValues(conn.Database, conn.Instance, name).Set(value)
}
}
}
}
// ScrapeSysmetrics collects session metrics from the v$sysmetrics view.
func (e *Exporter) ScrapeSysmetric() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
//metric_id metric_name
//2092 Physical Read Total IO Requests Per Sec
//2093 Physical Read Total Bytes Per Sec
//2100 Physical Write Total IO Requests Per Sec
//2124 Physical Write Total Bytes Per Sec
if conn.db != nil {
rows, err = conn.db.Query("select metric_name,value from v$sysmetric where metric_id in (2092,2093,2124,2100)")
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var name string
var value float64
if err := rows.Scan(&name, &value); err != nil {
break
}
name = cleanName(name)
e.sysmetric.WithLabelValues(conn.Database, conn.Instance, name).Set(value)
}
}
}
}
// ScrapeTablerows collects bytes from dba_tables view.
func (e *Exporter) ScrapeTablerows() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`select owner,table_name, tablespace_name, num_rows
from dba_tables
where owner not like '%SYS%' and num_rows is not null`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var owner string
var name string
var space string
var value float64
if err := rows.Scan(&owner, &name, &space, &value); err != nil {
break
}
name = cleanName(name)
e.tablerows.WithLabelValues(conn.Database, conn.Instance, owner, name, space).Set(value)
}
}
}
}
func (e *Exporter) ScrapeTablebytes() {
// ScrapeTablebytes collects bytes from dba_tables/dba_segments view.
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`SELECT tab.owner, tab.table_name, stab.bytes
FROM dba_tables tab, dba_segments stab
WHERE stab.owner = tab.owner AND stab.segment_name = tab.table_name
AND tab.owner NOT LIKE '%SYS%'`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var owner string
var name string
var value float64
if err = rows.Scan(&owner, &name, &value); err != nil {
break
}
name = cleanName(name)
e.tablebytes.WithLabelValues(conn.Database, conn.Instance, owner, name).Set(value)
}
}
}
}
// ScrapeTablebytes collects bytes from dba_indexes/dba_segments view.
func (e *Exporter) ScrapeIndexbytes() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`select table_owner,table_name, sum(bytes)
from dba_indexes ind, dba_segments seg
WHERE ind.owner=seg.owner and ind.index_name=seg.segment_name
and table_owner NOT LIKE '%SYS%'
group by table_owner,table_name`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var owner string
var name string
var value float64
if err = rows.Scan(&owner, &name, &value); err != nil {
break
}
name = cleanName(name)
e.indexbytes.WithLabelValues(conn.Database, conn.Instance, owner, name).Set(value)
}
}
}
}
// ScrapeLobbytes collects bytes from dba_lobs/dba_segments view.
func (e *Exporter) ScrapeLobbytes() {
var (
rows *sql.Rows
err error
)
for _, conn := range config.Cfgs {
if conn.db != nil {
rows, err = conn.db.Query(`select l.owner, l.table_name, sum(bytes)
from dba_lobs l, dba_segments seg
WHERE l.owner=seg.owner and l.table_name=seg.segment_name
and l.owner NOT LIKE '%SYS%'
group by l.owner,l.table_name`)
if err != nil {
continue
}
defer rows.Close()
for rows.Next() {
var owner string
var name string
var value float64
if err = rows.Scan(&owner, &name, &value); err != nil {
break
}
name = cleanName(name)
e.lobbytes.WithLabelValues(conn.Database, conn.Instance, owner, name).Set(value)
}
}
}
}
// Describe describes all the metrics exported by the Oracle exporter.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.duration.Describe(ch)
e.totalScrapes.Describe(ch)
e.scrapeErrors.Describe(ch)
e.session.Describe(ch)
e.sysstat.Describe(ch)
e.waitclass.Describe(ch)
e.sysmetric.Describe(ch)
e.interconnect.Describe(ch)
e.tablespace.Describe(ch)
e.recovery.Describe(ch)
e.redo.Describe(ch)
e.cache.Describe(ch)
e.uptime.Describe(ch)
e.up.Describe(ch)
e.alertlog.Describe(ch)
e.alertdate.Describe(ch)
e.services.Describe(ch)
e.parameter.Describe(ch)
//e.query.Describe(ch)
e.asmspace.Describe(ch)
e.tablerows.Describe(ch)
e.tablebytes.Describe(ch)
e.indexbytes.Describe(ch)
e.lobbytes.Describe(ch)
for _, metric := range e.custom {
metric.Describe(ch)
}
}
// Connect the DBs and gather Databasename and Instancename
func (e *Exporter) Connect() {
var dbname string
var inname string
var err error
for i, conf := range config.Cfgs {
// Close Connect from former scrape that not closed properly
if config.Cfgs[i].db != nil {
config.Cfgs[i].db.Close()
config.Cfgs[i].db = nil
}
if len(conf.Connection) > 0 {
config.Cfgs[i].db, err = sql.Open("oci8", conf.Connection)
if err == nil {
err = config.Cfgs[i].db.QueryRow("select db_unique_name,instance_name from v$database,v$instance").Scan(&dbname, &inname)
if err == nil {
if (len(conf.Database) == 0) || (len(conf.Instance) == 0) {
config.Cfgs[i].Database = dbname
config.Cfgs[i].Instance = inname
}
e.up.WithLabelValues(conf.Database, conf.Instance).Set(1)
} else {
config.Cfgs[i].db.Close()
e.up.WithLabelValues(conf.Database, conf.Instance).Set(0)
log.Errorln("Error connecting to database:", err)
//log.Infoln("Connect OK, Inital query failed: ", conf.Connection)
}
}
} else {
//log.Infoln("Dummy Connection: ", conf.Database)
e.up.WithLabelValues(conf.Database, conf.Instance).Set(0)
}
}
e.session.Reset()
e.sysstat.Reset()
e.waitclass.Reset()
e.sysmetric.Reset()
e.interconnect.Reset()
e.tablespace.Reset()
e.recovery.Reset()
e.redo.Reset()
e.cache.Reset()
e.uptime.Reset()
e.alertlog.Reset()
e.alertdate.Reset()
e.services.Reset()
e.parameter.Reset()
//e.query.Reset()
e.asmspace.Reset()
e.tablerows.Reset()
e.tablebytes.Reset()
e.indexbytes.Reset()
e.lobbytes.Reset()
for _, metric := range e.custom {
metric.Reset()
}
}
// Close Connections
func (e *Exporter) Close() {
for _, conn := range config.Cfgs {
if conn.db != nil {
conn.db.Close()
conn.db = nil
}
}
}
// Collect implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
var err error
e.totalScrapes.Inc()
defer func(begun time.Time) {
e.duration.Set(time.Since(begun).Seconds())
if err == nil {
e.error.Set(0)
} else {
e.error.Set(1)
}
}(time.Now())
e.Connect()
e.up.Collect(ch)
if e.vRecovery || *pRecovery {
e.ScrapeRecovery()
e.recovery.Collect(ch)
}
if *pMetrics {
e.ScrapeUptime()
e.uptime.Collect(ch)
e.ScrapeSession()
e.session.Collect(ch)
e.ScrapeSysstat()
e.sysstat.Collect(ch)
e.ScrapeWaitclass()
e.waitclass.Collect(ch)
e.ScrapeSysmetric()
e.sysmetric.Collect(ch)
e.ScrapeTablespace()
e.tablespace.Collect(ch)