-
Notifications
You must be signed in to change notification settings - Fork 11
PostgreSQL Check #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
PostgreSQL Check #294
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cb09d03
Add PostgreSQL check
s-newman 88aad09
Remove reference to MySQL in copied code
s-newman e482805
Add postgresql example check
s-newman 09af7c0
Document postgres check and explain example schema
s-newman bf617ff
Update CHANGELOG
s-newman bd9ba86
Fix typo in struct docstring
s-newman 101f732
Make sure check passes on fallthrough
s-newman 506d411
Fix original issue found from copy/pasting code
s-newman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| PostgreSQL | ||
| ========== | ||
|
|
||
| | Name | Type | Required | Description | | ||
| | ------------ | ------ | ------------ | -------------------------------------------------------------------- | | ||
| | Host | String | Y | IP or FQDN for the PostgreSQL server | | ||
| | Username | String | Y | Username for the database | | ||
| | Password | String | Y | Password for the user | | ||
| | Database | String | Y | Name of the database to access | | ||
| | Table | String | Y | Name of the table to access | | ||
| | Column | String | Y | Name of the column to access | | ||
| | MatchContent | String | N :: "false" | Whether to perform a regex content match on the results of the query | | ||
| | ContentRegex | String | N :: "\.\*" | Regex to match on | | ||
| | Port | String | N :: "3306" | Port for the server | | ||
|
|
||
| Example Check | ||
| ------------- | ||
|
|
||
| Unlike MySQL, PostgreSQL does not ship by default with a queryable database. The example check definition provided in the `examples/` folder uses the following simple schema: | ||
|
|
||
| ```sql | ||
| CREATE TABLE testtable( | ||
| EntryID SERIAL PRIMARY KEY, | ||
| Name TEXT NOT NULL | ||
| ); | ||
| INSERT INTO testtable (Name) VALUES ('scorestack'); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package postgresql | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "regexp" | ||
| "strconv" | ||
|
|
||
| // PostgreSQL driver | ||
| "github.com/jackc/pgx/v4" | ||
| "github.com/scorestack/scorestack/dynamicbeat/checks/schema" | ||
| ) | ||
|
|
||
| // The Definition configures the behavior of the MySQL check | ||
| // it implements the "check" interface | ||
| type Definition struct { | ||
| Config schema.CheckConfig // generic metadata about the check | ||
| Host string `optiontype:"required"` // IP or Hostname for the PostgreSQL server | ||
| Username string `optiontype:"required"` // Username for the database | ||
| Password string `optiontype:"required"` // Password for the user | ||
| Database string `optiontype:"required"` // Name of the database to access | ||
| Table string `optiontype:"required"` // Name of the table to access | ||
| Column string `optiontype:"required"` // Name of the column to access | ||
| MatchContent string `optiontype:"optional"` // Whether to perform a regex content match on the results of the query | ||
| ContentRegex string `optiontype:"optional" optiondefault:".*"` // Regex to match on | ||
| Port string `optiontype:"optional" optiondefault:"5432"` // Port for the server | ||
| } | ||
|
|
||
| // Run a single instance of the check | ||
| func (d *Definition) Run(ctx context.Context) schema.CheckResult { | ||
| // Initialize empty result | ||
| result := schema.CheckResult{} | ||
|
|
||
| // Create DB handle | ||
| db, err := pgx.Connect(ctx, fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", d.Username, d.Password, d.Host, d.Port, d.Database)) | ||
| if err != nil { | ||
| result.Message = fmt.Sprintf("Creating database handle failed : %s", err) | ||
| return result | ||
| } | ||
| defer db.Close(ctx) | ||
|
|
||
| // Check db connection | ||
| err = db.Ping(ctx) | ||
| if err != nil { | ||
| result.Message = fmt.Sprintf("Failed to ping database : %s", err) | ||
| } | ||
|
|
||
| // Query the DB | ||
| // TODO: This is SQL injectable. Figure out Paramerterized queries | ||
| rows, err := db.Query(ctx, fmt.Sprintf("SELECT %s FROM %s;", d.Column, d.Table)) | ||
| if err != nil { | ||
| result.Message = fmt.Sprintf("Could not query database : %s", err) | ||
| return result | ||
| } | ||
| defer rows.Close() | ||
|
|
||
| // Store the value from the column | ||
| var val string | ||
|
|
||
| // Perform regex matching, if necessary | ||
| if matchContent, _ := strconv.ParseBool(d.MatchContent); matchContent { | ||
| // Compile the regex | ||
| regex, err := regexp.Compile(d.ContentRegex) | ||
| if err != nil { | ||
| result.Message = fmt.Sprintf("Error compiling regex string %s : %s", d.ContentRegex, err) | ||
| return result | ||
| } | ||
|
|
||
| // Check the rows | ||
| for rows.Next() { | ||
| // Grab a value | ||
| err := rows.Scan(&val) | ||
| if err != nil { | ||
| result.Message = fmt.Sprintf("Could not scan row values : %s", err) | ||
| return result | ||
| } | ||
| // Check value with regex | ||
| if regex.MatchString(val) { | ||
| // If we reach here the check passes | ||
| result.Passed = true | ||
| return result | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| // Check for error in the rows | ||
| if rows.Err() != nil { | ||
| result.Message = fmt.Sprintf("Something happened to the rows : %s", err) | ||
| return result | ||
| } | ||
|
|
||
| // Check passes if we reach here | ||
| result.Passed = true | ||
| return result | ||
| } | ||
|
|
||
| // GetConfig returns the current CheckConfig struct this check has been | ||
| // configured with | ||
| func (d *Definition) GetConfig() schema.CheckConfig { | ||
| return d.Config | ||
| } | ||
|
|
||
| // SetConfig reconfigures this check with a new CheckConfig struct | ||
| func (d *Definition) SetConfig(config schema.CheckConfig) { | ||
| d.Config = config | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.