From 91da633fdbb7595f725ec10a4f773fc0d0264643 Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Mon, 18 Apr 2022 17:35:16 +0800 Subject: [PATCH] add collector oracle framework --- agent/agent.go | 2 + conf/input.oracle/metric.toml | 85 +++++++++++ conf/input.oracle/oracle.toml | 19 +++ go.mod | 7 +- go.sum | 23 ++- inputs/cpu/cpu.go | 3 + inputs/disk/disk.go | 4 +- inputs/diskio/diskio.go | 3 +- inputs/exec/exec.go | 2 + inputs/inputs.go | 1 + inputs/kernel/kernel.go | 2 + .../linux_sysctl_fs/linux_sysctl_fs_linux.go | 2 + inputs/mem/mem.go | 3 +- inputs/net/net.go | 3 +- inputs/netstat/netstat.go | 2 + inputs/oracle/oracle.go | 144 ++++++++++++++++++ inputs/processes/processes_notwindows.go | 2 + inputs/system/system.go | 3 +- 18 files changed, 303 insertions(+), 7 deletions(-) create mode 100644 conf/input.oracle/metric.toml create mode 100644 conf/input.oracle/oracle.toml create mode 100644 inputs/oracle/oracle.go diff --git a/agent/agent.go b/agent/agent.go index 25487d0..c1d0633 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -22,6 +22,7 @@ import ( _ "flashcat.cloud/categraf/inputs/mem" _ "flashcat.cloud/categraf/inputs/net" _ "flashcat.cloud/categraf/inputs/netstat" + _ "flashcat.cloud/categraf/inputs/oracle" _ "flashcat.cloud/categraf/inputs/processes" _ "flashcat.cloud/categraf/inputs/redis" _ "flashcat.cloud/categraf/inputs/system" @@ -49,6 +50,7 @@ func (a *Agent) Stop() { for name := range InputReaders { InputReaders[name].QuitChan <- struct{}{} close(InputReaders[name].Queue) + InputReaders[name].Instance.Drop() } log.Println("I! agent stopped") diff --git a/conf/input.oracle/metric.toml b/conf/input.oracle/metric.toml new file mode 100644 index 0000000..5298d38 --- /dev/null +++ b/conf/input.oracle/metric.toml @@ -0,0 +1,85 @@ +[[metrics]] +mesurement = "sessions" +label_fields = [ "status", "type" ] +metric_fields = { value = "float64" } +timeout = "3s" +request = ''' +SELECT status, type, COUNT(*) as value FROM v$session GROUP BY status, type +''' + +[[metrics]] +mesurement = "resource" +label_fields = [ "resource_name" ] +metric_fields = { current_utilization= "float64", limit_value="float64" } +timeout = "3s" +request = ''' +SELECT resource_name,current_utilization,CASE WHEN TRIM(limit_value) LIKE 'UNLIMITED' THEN '-1' ELSE TRIM(limit_value) END as limit_value FROM v$resource_limit +''' + +[[metrics]] +mesurement = "asm_diskgroup" +label_fields = [ "name" ] +metric_fields = { total = "float64", free = "float64" } +timeout = "3s" +request = ''' +SELECT name,total_mb*1024*1024 as total,free_mb*1024*1024 as free FROM v$asm_diskgroup_stat where exists (select 1 from v$datafile where name like '+%') +''' +IgnoreZeroResult = true + +[[metrics]] +mesurement = "activity" +metric_fields = { value = "float64" } +field_to_append = "name" +timeout = "3s" +request = ''' +SELECT name, value FROM v$sysstat WHERE name IN ('parse count (total)', 'execute count', 'user commits', 'user rollbacks') +''' + +[[metrics]] +mesurement = "process" +metric_fields = { count = "float64" } +timeout = "3s" +request = ''' +SELECT COUNT(*) as count FROM v$process +''' + +[[metrics]] +mesurement = "wait_time" +metric_fields = { value = "float64" } +label_fields = ["wait_class"] +timeout = "3s" +request = ''' +SELECT + n.wait_class as WAIT_CLASS, + round(m.time_waited/m.INTSIZE_CSEC,3) as VALUE +FROM + v$waitclassmetric m, v$system_wait_class n +WHERE + m.wait_class_id=n.wait_class_id AND n.wait_class != 'Idle' +''' + +[[metrics]] +mesurement = "tablespace" +label_fields = [ "tablespace", "type" ] +metric_fields = { bytes = "float64", max_bytes = "float64", free = "float64" } +timeout = "3s" +request = ''' +SELECT + dt.tablespace_name as tablespace, + dt.contents as type, + dt.block_size * dtum.used_space as bytes, + dt.block_size * dtum.tablespace_size as max_bytes, + dt.block_size * (dtum.tablespace_size - dtum.used_space) as free +FROM dba_tablespace_usage_metrics dtum, dba_tablespaces dt +WHERE dtum.tablespace_name = dt.tablespace_name +ORDER by tablespace +''' + +[[metrics]] +mesurement = "sysmetric" +metric_fields = { value = "float64" } +field_to_append = "metric_name" +timeout = "3s" +request = ''' +select METRIC_NAME,VALUE from v$sysmetric where group_id=2 +''' diff --git a/conf/input.oracle/oracle.toml b/conf/input.oracle/oracle.toml new file mode 100644 index 0000000..11487b5 --- /dev/null +++ b/conf/input.oracle/oracle.toml @@ -0,0 +1,19 @@ +[[instances]] +address = "10.1.2.3:1521/orcl" +username = "monitor" +password = "123456" +is_sys_dba = false +is_sys_oper = false +disable_connection_pool = false +max_open_connections = 5 +# labels = {region="cloud"} + +# [[instances]] +# address = "192.168.10.10:1521/orcl" +# username = "monitor" +# password = "123456" +# is_sys_dba = false +# is_sys_oper = false +# disable_connection_pool = false +# max_open_connections = 5 +# # labels = {region="local"} diff --git a/go.mod b/go.mod index 7ca4c49..dfe1a53 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,11 @@ go 1.17 require ( github.com/gobwas/glob v0.2.3 + github.com/godror/godror v0.33.0 github.com/golang/protobuf v1.5.2 github.com/golang/snappy v0.0.4 github.com/influxdata/line-protocol/v2 v2.2.1 + github.com/jmoiron/sqlx v1.3.5 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 github.com/prometheus/client_golang v1.12.1 @@ -20,7 +22,10 @@ require ( github.com/BurntSushi/toml v1.1.0 // indirect github.com/fatih/camelcase v1.0.0 // indirect github.com/fatih/structs v1.1.0 // indirect + github.com/go-logfmt/logfmt v0.5.1 // indirect + github.com/go-logr/logr v1.2.3 // indirect github.com/go-ole/go-ole v1.2.6 // indirect + github.com/godror/knownpb v0.1.0 // indirect github.com/gogo/protobuf v1.1.1 // indirect github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect @@ -34,6 +39,6 @@ require ( golang.org/x/text v0.3.7 // indirect google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect google.golang.org/grpc v1.33.1 // indirect - google.golang.org/protobuf v1.26.0 // indirect + google.golang.org/protobuf v1.27.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 3102bb5..010e69c 100644 --- a/go.sum +++ b/go.sum @@ -81,15 +81,25 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= +github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= +github.com/godror/godror v0.33.0 h1:ZK1W7GohHVDPoLp/37U9QCSHARnYB4vVxNJya+CyWQ4= +github.com/godror/godror v0.33.0/go.mod h1:qHYnDISFm/h0vM+HDwg0LpyoLvxRKFRSwvhYF7ufjZ8= +github.com/godror/knownpb v0.1.0 h1:dJPK8s/I3PQzGGaGcUStL2zIaaICNzKKAK8BzP1uLio= +github.com/godror/knownpb v0.1.0/go.mod h1:4nRFbQo1dDuwKnblRXDxrfCFYeT4hjg3GjMqef58eRE= github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -163,6 +173,8 @@ github.com/influxdata/line-protocol/v2 v2.0.0-20210312151457-c52fdecb625a/go.mod github.com/influxdata/line-protocol/v2 v2.1.0/go.mod h1:QKw43hdUBg3GTk2iC3iyCxksNj7PX9aUSeYOYE/ceHY= github.com/influxdata/line-protocol/v2 v2.2.1 h1:EAPkqJ9Km4uAxtMRgUubJyqAr6zgWM0dznKMLRauQRE= github.com/influxdata/line-protocol/v2 v2.2.1/go.mod h1:DmB3Cnh+3oxmG6LOBIxce4oaL4CPj3OmMPgvauXh+tM= +github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= +github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -189,9 +201,13 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -202,6 +218,9 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/oklog/ulid/v2 v2.0.2 h1:r4fFzBm+bv0wNKNh5eXTwU7i85y5x+uwkxCUTNVQqLc= +github.com/oklog/ulid/v2 v2.0.2/go.mod h1:mtBL0Qe/0HAx6/a4Z30qxVIAL1eQDweXq5lxOEiwQ68= +github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -347,6 +366,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -524,8 +544,9 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/inputs/cpu/cpu.go b/inputs/cpu/cpu.go index 4ed3e29..47f773f 100644 --- a/inputs/cpu/cpu.go +++ b/inputs/cpu/cpu.go @@ -44,6 +44,9 @@ func (c *CPUStats) Init() error { return nil } +func (c *CPUStats) Drop() { +} + func (c *CPUStats) Gather() []*types.Sample { var samples []*types.Sample diff --git a/inputs/disk/disk.go b/inputs/disk/disk.go index 0be533e..85c5ff0 100644 --- a/inputs/disk/disk.go +++ b/inputs/disk/disk.go @@ -38,11 +38,13 @@ func (s *DiskStats) GetInterval() config.Duration { return s.Interval } -// overwrite func func (s *DiskStats) Init() error { return nil } +func (s *DiskStats) Drop() { +} + func (s *DiskStats) Gather() []*types.Sample { var samples []*types.Sample diff --git a/inputs/diskio/diskio.go b/inputs/diskio/diskio.go index c025aeb..998920d 100644 --- a/inputs/diskio/diskio.go +++ b/inputs/diskio/diskio.go @@ -39,7 +39,8 @@ func (d *DiskIO) GetInterval() config.Duration { return d.Interval } -// overwrite func +func (d *DiskIO) Drop() {} + func (d *DiskIO) Init() error { for _, device := range d.Devices { if filter.HasMeta(device) { diff --git a/inputs/exec/exec.go b/inputs/exec/exec.go index 02b5b12..e9bfa7e 100644 --- a/inputs/exec/exec.go +++ b/inputs/exec/exec.go @@ -47,6 +47,8 @@ func (e *Exec) GetInputName() string { return "" } +func (e *Exec) Drop() {} + func (e *Exec) GetInterval() config.Duration { return e.Interval } diff --git a/inputs/inputs.go b/inputs/inputs.go index b6a19e4..d165de1 100644 --- a/inputs/inputs.go +++ b/inputs/inputs.go @@ -8,6 +8,7 @@ import ( type Input interface { Init() error + Drop() GetInputName() string GetInterval() config.Duration Gather() []*types.Sample diff --git a/inputs/kernel/kernel.go b/inputs/kernel/kernel.go index 624c038..e36f5b8 100644 --- a/inputs/kernel/kernel.go +++ b/inputs/kernel/kernel.go @@ -56,6 +56,8 @@ func (s *KernelStats) Init() error { return nil } +func (s *KernelStats) Drop() {} + func (s *KernelStats) Gather() (samples []*types.Sample) { data, err := s.getProcStat() if err != nil { diff --git a/inputs/linux_sysctl_fs/linux_sysctl_fs_linux.go b/inputs/linux_sysctl_fs/linux_sysctl_fs_linux.go index 5381bad..85cb51f 100644 --- a/inputs/linux_sysctl_fs/linux_sysctl_fs_linux.go +++ b/inputs/linux_sysctl_fs/linux_sysctl_fs_linux.go @@ -46,6 +46,8 @@ func (s *SysctlFS) Init() error { return nil } +func (s *SysctlFS) Drop() {} + func (s *SysctlFS) Gather() []*types.Sample { fields := map[string]interface{}{} diff --git a/inputs/mem/mem.go b/inputs/mem/mem.go index 926e62c..b205b68 100644 --- a/inputs/mem/mem.go +++ b/inputs/mem/mem.go @@ -38,7 +38,8 @@ func (s *MemStats) GetInterval() config.Duration { return s.Interval } -// overwrite func +func (s *MemStats) Drop() {} + func (s *MemStats) Init() error { s.platform = runtime.GOOS return nil diff --git a/inputs/net/net.go b/inputs/net/net.go index d51be78..7bd1ad9 100644 --- a/inputs/net/net.go +++ b/inputs/net/net.go @@ -42,7 +42,8 @@ func (s *NetIOStats) GetInterval() config.Duration { return s.Interval } -// overwrite func +func (s *NetIOStats) Drop() {} + func (s *NetIOStats) Init() error { var err error diff --git a/inputs/netstat/netstat.go b/inputs/netstat/netstat.go index dbb97e1..f5aa0ca 100644 --- a/inputs/netstat/netstat.go +++ b/inputs/netstat/netstat.go @@ -36,6 +36,8 @@ func (s *NetStats) GetInterval() config.Duration { return s.Interval } +func (s *NetStats) Drop() {} + func (s *NetStats) Init() error { return nil } diff --git a/inputs/oracle/oracle.go b/inputs/oracle/oracle.go new file mode 100644 index 0000000..5c655c3 --- /dev/null +++ b/inputs/oracle/oracle.go @@ -0,0 +1,144 @@ +package oracle + +import ( + "fmt" + "log" + "sync" + + "flashcat.cloud/categraf/config" + "flashcat.cloud/categraf/inputs" + "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 { + 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"` +} + +type MetricConfig struct { + Mesurement string `toml:"mesurement"` + LabelFields []string `toml:"label_fields"` + MetricFields map[string]string `toml:"metric_fields"` // column_name -> value type(float64, bool, int64) + FieldToAppend string `toml:"field_to_append"` + Timeout config.Duration `toml:"timeout"` + Request string `toml:"request"` + IgnoreZeroResult bool `toml:"ignore_zero_result"` +} + +type Oracle struct { + PrintConfigs bool `toml:"print_configs"` + Interval config.Duration `toml:"interval"` + Instances []OrclInstance `toml:"instances"` + Metrics []MetricConfig `toml:"metrics"` + + dbconnpool map[string]*sqlx.DB // key: instance +} + +func init() { + inputs.Add(inputName, func() inputs.Input { + return &Oracle{} + }) +} + +func (o *Oracle) GetInputName() string { + return inputName +} + +func (o *Oracle) GetInterval() config.Duration { + return o.Interval +} + +func (o *Oracle) Init() error { + if len(o.Instances) == 0 { + return fmt.Errorf("oracle instances empty") + } + + o.dbconnpool = make(map[string]*sqlx.DB) + for i := 0; i < len(o.Instances); i++ { + dbConf := o.Instances[i] + 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) + } + } +} + +func (o *Oracle) Gather() (samples []*types.Sample) { + slist := list.NewSafeList() + + var wg sync.WaitGroup + for i := range o.Instances { + wg.Add(1) + go o.collectOnce(&wg, o.Instances[i], slist) + } + wg.Wait() + + interfaceList := slist.PopBackAll() + for i := 0; i < len(interfaceList); i++ { + samples = append(samples, interfaceList[i].(*types.Sample)) + } + + return +} + +func (o *Oracle) collectOnce(wg *sync.WaitGroup, ins OrclInstance, slist *list.SafeList) { + log.Println("->", ins.Address) + log.Printf("%#v\n", ins) + log.Println("-> metrics count:", len(o.Metrics)) + + log.Println(o.Metrics[0].Mesurement) + log.Println(o.Metrics[0].Request) + log.Println(o.Metrics[0].FieldToAppend) + log.Println(o.Metrics[0].IgnoreZeroResult) + log.Println(o.Metrics[0].LabelFields) + log.Println(o.Metrics[0].MetricFields) + + defer wg.Done() +} + +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() +} diff --git a/inputs/processes/processes_notwindows.go b/inputs/processes/processes_notwindows.go index ea2526e..16738c7 100644 --- a/inputs/processes/processes_notwindows.go +++ b/inputs/processes/processes_notwindows.go @@ -43,6 +43,8 @@ func (p *Processes) GetInterval() config.Duration { return p.Interval } +func (p *Processes) Drop() {} + func (p *Processes) Init() error { return nil } diff --git a/inputs/system/system.go b/inputs/system/system.go index a4e9a57..6d0922d 100644 --- a/inputs/system/system.go +++ b/inputs/system/system.go @@ -35,11 +35,12 @@ func (s *SystemStats) GetInterval() config.Duration { return s.Interval } -// overwrite func func (s *SystemStats) Init() error { return nil } +func (s *SystemStats) Drop() {} + func (s *SystemStats) Gather() []*types.Sample { var samples []*types.Sample