2022-04-18 17:35:16 +08:00
|
|
|
package oracle
|
|
|
|
|
|
|
|
import (
|
2022-04-18 23:03:16 +08:00
|
|
|
"context"
|
2022-04-18 17:35:16 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-04-18 23:03:16 +08:00
|
|
|
"strings"
|
2022-04-18 17:35:16 +08:00
|
|
|
"sync"
|
2022-04-19 12:13:38 +08:00
|
|
|
"sync/atomic"
|
2022-04-18 23:03:16 +08:00
|
|
|
"time"
|
2022-04-18 17:35:16 +08:00
|
|
|
|
|
|
|
"flashcat.cloud/categraf/config"
|
|
|
|
"flashcat.cloud/categraf/inputs"
|
2022-04-18 23:03:16 +08:00
|
|
|
"flashcat.cloud/categraf/pkg/conv"
|
2022-04-18 17:35:16 +08:00
|
|
|
"flashcat.cloud/categraf/types"
|
|
|
|
"github.com/godror/godror"
|
|
|
|
"github.com/godror/godror/dsn"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/toolkits/pkg/container/list"
|
|
|
|
)
|
|
|
|
|
|
|
|
const inputName = "oracle"
|
|
|
|
|
|
|
|
type OrclInstance struct {
|
2022-04-18 22:25:50 +08:00
|
|
|
Address string `toml:"address"`
|
|
|
|
Username string `toml:"username"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
IsSysDBA bool `toml:"is_sys_dba"`
|
|
|
|
IsSysOper bool `toml:"is_sys_oper"`
|
|
|
|
DisableConnectionPool bool `toml:"disable_connection_pool"`
|
|
|
|
MaxOpenConnections int `toml:"max_open_connections"`
|
|
|
|
Labels map[string]string `toml:"labels"`
|
2022-04-19 12:13:38 +08:00
|
|
|
IntervalTimes int64 `toml:"interval_times"`
|
2022-04-18 17:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type MetricConfig struct {
|
2022-04-18 23:47:20 +08:00
|
|
|
Mesurement string `toml:"mesurement"`
|
|
|
|
LabelFields []string `toml:"label_fields"`
|
2022-04-27 17:22:21 +08:00
|
|
|
MetricFields []string `toml:"metric_fields"`
|
2022-04-18 23:47:20 +08:00
|
|
|
FieldToAppend string `toml:"field_to_append"`
|
|
|
|
Timeout config.Duration `toml:"timeout"`
|
|
|
|
Request string `toml:"request"`
|
|
|
|
IgnoreZeroResult bool `toml:"ignore_zero_result"`
|
2022-04-18 17:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Oracle struct {
|
2022-04-29 00:02:20 +08:00
|
|
|
config.Interval
|
|
|
|
Instances []OrclInstance `toml:"instances"`
|
|
|
|
Metrics []MetricConfig `toml:"metrics"`
|
2022-04-18 17:35:16 +08:00
|
|
|
|
|
|
|
dbconnpool map[string]*sqlx.DB // key: instance
|
2022-04-19 12:13:38 +08:00
|
|
|
Counter uint64
|
2022-04-19 17:36:45 +08:00
|
|
|
wg sync.WaitGroup
|
2022-04-18 17:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add(inputName, func() inputs.Input {
|
|
|
|
return &Oracle{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-28 15:24:48 +08:00
|
|
|
func (o *Oracle) Prefix() string {
|
2022-04-18 17:35:16 +08:00
|
|
|
return inputName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) Init() error {
|
|
|
|
if len(o.Instances) == 0 {
|
2022-04-25 12:08:56 +08:00
|
|
|
return types.ErrInstancesEmpty
|
2022-04-18 17:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
o.dbconnpool = make(map[string]*sqlx.DB)
|
|
|
|
for i := 0; i < len(o.Instances); i++ {
|
|
|
|
dbConf := o.Instances[i]
|
2022-04-19 19:05:16 +08:00
|
|
|
if dbConf.Address == "" {
|
|
|
|
return fmt.Errorf("some oracle address is blank")
|
|
|
|
}
|
2022-04-18 17:35:16 +08:00
|
|
|
connString := getConnectionString(dbConf)
|
|
|
|
db, err := sqlx.Open("godror", connString)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open oracle connection: %v", err)
|
|
|
|
}
|
|
|
|
db.SetMaxOpenConns(dbConf.MaxOpenConnections)
|
|
|
|
o.dbconnpool[dbConf.Address] = db
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) Drop() {
|
|
|
|
for address := range o.dbconnpool {
|
|
|
|
if config.Config.DebugMode {
|
|
|
|
log.Println("D! dropping oracle connection:", address)
|
|
|
|
}
|
|
|
|
if err := o.dbconnpool[address].Close(); err != nil {
|
|
|
|
log.Println("E! failed to close oracle connection:", address, "error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:34:15 +08:00
|
|
|
func (o *Oracle) Gather(slist *list.SafeList) {
|
2022-04-19 12:13:38 +08:00
|
|
|
atomic.AddUint64(&o.Counter, 1)
|
2022-04-18 17:35:16 +08:00
|
|
|
for i := range o.Instances {
|
2022-04-18 23:25:07 +08:00
|
|
|
ins := o.Instances[i]
|
2022-04-19 17:36:45 +08:00
|
|
|
o.wg.Add(1)
|
|
|
|
go o.gatherOnce(slist, ins)
|
2022-04-18 17:35:16 +08:00
|
|
|
}
|
2022-04-19 17:36:45 +08:00
|
|
|
o.wg.Wait()
|
2022-04-18 17:35:16 +08:00
|
|
|
}
|
|
|
|
|
2022-04-19 17:36:45 +08:00
|
|
|
func (o *Oracle) gatherOnce(slist *list.SafeList, ins OrclInstance) {
|
|
|
|
defer o.wg.Done()
|
2022-04-18 23:03:16 +08:00
|
|
|
|
2022-04-19 12:13:38 +08:00
|
|
|
if ins.IntervalTimes > 0 {
|
|
|
|
counter := atomic.LoadUint64(&o.Counter)
|
|
|
|
if counter%uint64(ins.IntervalTimes) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 23:03:16 +08:00
|
|
|
tags := map[string]string{"address": ins.Address}
|
|
|
|
for k, v := range ins.Labels {
|
|
|
|
tags[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(begun time.Time) {
|
2022-04-27 16:07:36 +08:00
|
|
|
use := time.Since(begun).Seconds()
|
|
|
|
slist.PushFront(inputs.NewSample("scrape_use_seconds", use, tags))
|
2022-04-18 23:03:16 +08:00
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
db := o.dbconnpool[ins.Address]
|
|
|
|
|
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
slist.PushFront(inputs.NewSample("up", 0, tags))
|
2022-04-18 23:54:10 +08:00
|
|
|
log.Println("E! failed to ping oracle:", ins.Address, "error:", err)
|
2022-04-18 23:03:16 +08:00
|
|
|
} else {
|
|
|
|
slist.PushFront(inputs.NewSample("up", 1, tags))
|
|
|
|
}
|
|
|
|
|
|
|
|
waitMetrics := new(sync.WaitGroup)
|
|
|
|
|
|
|
|
for i := 0; i < len(o.Metrics); i++ {
|
2022-04-18 23:25:07 +08:00
|
|
|
m := o.Metrics[i]
|
2022-04-18 23:03:16 +08:00
|
|
|
waitMetrics.Add(1)
|
2022-04-18 23:25:07 +08:00
|
|
|
go o.scrapeMetric(waitMetrics, slist, db, m, tags)
|
2022-04-18 23:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
waitMetrics.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) scrapeMetric(waitMetrics *sync.WaitGroup, slist *list.SafeList, db *sqlx.DB, metricConf MetricConfig, tags map[string]string) {
|
|
|
|
defer waitMetrics.Done()
|
|
|
|
|
|
|
|
timeout := time.Duration(metricConf.Timeout)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, metricConf.Request)
|
|
|
|
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
log.Println("E! oracle query timeout, request:", metricConf.Request)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("E! failed to query:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
cols, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("E! failed to get columns:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Config.DebugMode {
|
|
|
|
log.Println("D! columns:", cols)
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
columns := make([]interface{}, len(cols))
|
|
|
|
columnPointers := make([]interface{}, len(cols))
|
|
|
|
for i := range columns {
|
|
|
|
columnPointers[i] = &columns[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the result into the column pointers...
|
|
|
|
if err := rows.Scan(columnPointers...); err != nil {
|
|
|
|
log.Println("E! failed to scan:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create our map, and retrieve the value for each column from the pointers slice,
|
|
|
|
// storing it in the map with the name of the column as the key.
|
|
|
|
m := make(map[string]string)
|
|
|
|
for i, colName := range cols {
|
|
|
|
val := columnPointers[i].(*interface{})
|
|
|
|
m[strings.ToLower(colName)] = fmt.Sprint(*val)
|
|
|
|
}
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
if err = o.parseRow(m, metricConf, slist, tags); err != nil {
|
|
|
|
log.Println("E! failed to parse row:", err)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
if !metricConf.IgnoreZeroResult && count == 0 {
|
|
|
|
log.Println("E! no metrics found while parsing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Oracle) parseRow(row map[string]string, metricConf MetricConfig, slist *list.SafeList, tags map[string]string) error {
|
|
|
|
labels := make(map[string]string)
|
|
|
|
for k, v := range tags {
|
|
|
|
labels[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, label := range metricConf.LabelFields {
|
|
|
|
labelValue, has := row[label]
|
|
|
|
if has {
|
|
|
|
labels[label] = strings.Replace(labelValue, " ", "_", -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, column := range metricConf.MetricFields {
|
|
|
|
value, err := conv.ToFloat64(row[column])
|
|
|
|
if err != nil {
|
|
|
|
log.Println("E! failed to convert field:", column, "value:", value, "error:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if metricConf.FieldToAppend == "" {
|
|
|
|
slist.PushFront(inputs.NewSample(metricConf.Mesurement+"_"+column, value, labels))
|
|
|
|
} else {
|
|
|
|
suffix := cleanName(row[metricConf.FieldToAppend])
|
|
|
|
slist.PushFront(inputs.NewSample(metricConf.Mesurement+"_"+suffix+"_"+column, value, labels))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Oracle gives us some ugly names back. This function cleans things up for Prometheus.
|
|
|
|
func cleanName(s string) string {
|
|
|
|
s = strings.Replace(s, " ", "_", -1) // Remove spaces
|
|
|
|
s = strings.Replace(s, "(", "", -1) // Remove open parenthesis
|
|
|
|
s = strings.Replace(s, ")", "", -1) // Remove close parenthesis
|
|
|
|
s = strings.Replace(s, "/", "", -1) // Remove forward slashes
|
|
|
|
s = strings.Replace(s, "*", "", -1) // Remove asterisks
|
|
|
|
s = strings.Replace(s, "%", "percent", -1)
|
|
|
|
s = strings.ToLower(s)
|
|
|
|
return s
|
2022-04-18 17:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getConnectionString(args OrclInstance) string {
|
|
|
|
return godror.ConnectionParams{
|
|
|
|
StandaloneConnection: args.DisableConnectionPool,
|
|
|
|
CommonParams: dsn.CommonParams{
|
|
|
|
Username: args.Username,
|
|
|
|
Password: dsn.NewPassword(args.Password),
|
|
|
|
ConnectString: args.Address,
|
|
|
|
},
|
|
|
|
PoolParams: dsn.PoolParams{
|
|
|
|
MinSessions: 0,
|
|
|
|
MaxSessions: args.MaxOpenConnections,
|
|
|
|
SessionIncrement: 1,
|
|
|
|
},
|
|
|
|
ConnParams: dsn.ConnParams{
|
|
|
|
IsSysDBA: args.IsSysDBA,
|
|
|
|
IsSysOper: args.IsSysOper,
|
|
|
|
},
|
|
|
|
}.StringWithPassword()
|
|
|
|
}
|