Skip to content

Commit

Permalink
Generic SQL input (#8735)
Browse files Browse the repository at this point in the history
(cherry picked from commit 908ad2f)
  • Loading branch information
srebhan authored and reimda committed Jun 16, 2021
1 parent e7b3258 commit 91b6211
Show file tree
Hide file tree
Showing 14 changed files with 1,409 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ For documentation on the latest development code see the [documentation index][d
* [snmp_trap](./plugins/inputs/snmp_trap)
* [socket_listener](./plugins/inputs/socket_listener)
* [solr](./plugins/inputs/solr)
* [sql](./plugins/inputs/sql) (generic SQL query plugin)
* [sql server](./plugins/inputs/sqlserver) (microsoft)
* [stackdriver](./plugins/inputs/stackdriver) (Google Cloud Monitoring)
* [sql](./plugins/outputs/sql) (SQL generic output)
Expand Down
43 changes: 43 additions & 0 deletions docs/SQL_DRIVERS_INPUT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Available SQL drivers for the SQL input plugin

This is a list of available drivers for the SQL input plugin. The data-source-name (DSN) is driver specific and
might change between versions. Please check the driver documentation for available options and the format.

database | driver | aliases | example DSN | comment
---------------------| ------------------------------------------------------| --------------- | -------------------------------------------------------------------------------------- | -------
CockroachDB | [cockroach](https://github.com/jackc/pgx) | postgres<br>pgx | see _postgres_ driver | uses PostgresQL driver
MariaDB | [maria](https://github.com/go-sql-driver/mysql) | mysql | see _mysql_ driver | uses MySQL driver
Microsoft SQL Server | [sqlserver](https://github.com/denisenkom/go-mssqldb) | mssql | `username:password@host/instance?param1=value&param2=value` | uses newer _sqlserver_ driver
MySQL | [mysql](https://github.com/go-sql-driver/mysql) | | `[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]` | see [driver docs](https://github.com/go-sql-driver/mysql) for more information
PostgreSQL | [postgres](https://github.com/jackc/pgx) | pgx | `[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]` | see [postgres docs](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) for more information
SQLite | [sqlite](https://gitlab.com/cznic/sqlite) | | `filename` | see [driver docu](https://pkg.go.dev/modernc.org/sqlite) for more information
TiDB | [tidb](https://github.com/go-sql-driver/mysql) | mysql | see _mysql_ driver | uses MySQL driver

## Comments

### Driver aliases
Some database drivers are supported though another driver (e.g. CockroachDB). For other databases we provide a more
obvious name (e.g. postgres) compared to the driver name. For all of those drivers you might use an _alias_ name
during configuration.

### Example data-source-name DSN
The given examples are just that, so please check the driver documentation for the exact format
and available options and parameters. Please note that the format of a DSN might also change
between driver version.

### Type conversions
Telegraf relies on type conversion of the database driver and/or the golang sql framework. In case you find
any problem, please open an issue!

## Help
If nothing seems to work, you might find help in the telegraf forum or in the chat.

### The documentation is wrong
Please open an issue or even better send a pull-request!

### I found a bug
Please open an issue or even better send a pull-request!

### My database is not supported
We currently cannot support CGO drivers in telegraf! Please check if a **pure Go** driver for the [golang sql framework](https://golang.org/pkg/database/sql/) exists.
If you found such a driver, please let us know by opening an issue or even better by sending a pull-request!
22 changes: 19 additions & 3 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,24 @@ func compileFilterNoGlob(filters []string) Filter {
}

type IncludeExcludeFilter struct {
include Filter
exclude Filter
include Filter
exclude Filter
includeDefault bool
excludeDefault bool
}

func NewIncludeExcludeFilter(
include []string,
exclude []string,
) (Filter, error) {
return NewIncludeExcludeFilterDefaults(include, exclude, true, false)
}

func NewIncludeExcludeFilterDefaults(
include []string,
exclude []string,
includeDefault bool,
excludeDefault bool,
) (Filter, error) {
in, err := Compile(include)
if err != nil {
Expand All @@ -97,20 +108,25 @@ func NewIncludeExcludeFilter(
return nil, err
}

return &IncludeExcludeFilter{in, ex}, nil
return &IncludeExcludeFilter{in, ex, includeDefault, excludeDefault}, nil
}

func (f *IncludeExcludeFilter) Match(s string) bool {
if f.include != nil {
if !f.include.Match(s) {
return false
}
} else if !f.includeDefault {
return false
}

if f.exclude != nil {
if f.exclude.Match(s) {
return false
}
} else if f.excludeDefault {
return false
}

return true
}
Loading

0 comments on commit 91b6211

Please sign in to comment.