-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write active_conn connections counter
- Loading branch information
1 parent
691eb52
commit a76a0cb
Showing
10 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,34 @@ | ||
version: 2.0 | ||
jobs: | ||
build: | ||
docker: | ||
- image: circleci/golang:1.12 | ||
steps: | ||
- checkout | ||
- run: ./build.sh | ||
deploy: | ||
docker: | ||
- image: circleci/golang:1.14 | ||
steps: | ||
- checkout | ||
- run: | | ||
mkdir dist/ | ||
go get github.com/mitchellh/gox | ||
go get github.com/tcnksm/ghr | ||
gox -output="dist/{{.Dir}}_{{.OS}}_{{.Arch}}" -os="linux darwin" -arch="386 amd64" ./paperlog | ||
gox -output="dist/{{.Dir}}_{{.OS}}_{{.Arch}}" -os="linux windows darwin" -arch="386 amd64" ./slack | ||
gox -output="dist/{{.Dir}}_{{.OS}}_{{.Arch}}" -os="linux windows darwin" -arch="386 amd64" ./get_secret | ||
gox -output="dist/{{.Dir}}_{{.OS}}_{{.Arch}}" -os="linux windows darwin" -arch="386 amd64" ./active_conn | ||
ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -delete ${CIRCLE_TAG} ./dist/ | ||
workflows: | ||
version: 2 | ||
release: | ||
jobs: | ||
- build | ||
- deploy: | ||
filters: | ||
tags: | ||
only: /v[0-9]+(\.[0-9]+)*(-.*)*/ | ||
branches: | ||
ignore: /.*/ |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.exe | ||
dist/ | ||
*.log | ||
*.log | ||
.env |
This file contains 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 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,4 @@ | ||
DB_HOST=localhost | ||
DB_USER= | ||
DB_PASS= | ||
DB_NAME= |
This file contains 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 @@ | ||
active_conn |
This file contains 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,8 @@ | ||
# active_conn | ||
|
||
Prints the number of active MySQL connections to a database at a desired interval (default: 5seconds) | ||
|
||
``` | ||
./active_conn -interval 10s | ||
``` | ||
- Print number of active connections every 10 seconds |
This file contains 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,8 @@ | ||
module github.com/peteretelej/tools/active_conn | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/go-sql-driver/mysql v1.5.0 | ||
github.com/joho/godotenv v1.3.0 | ||
) |
This file contains 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,4 @@ | ||
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= | ||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | ||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= | ||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= |
This file contains 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,102 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/joho/godotenv" | ||
) | ||
|
||
var db *sql.DB | ||
|
||
func main() { | ||
var ( | ||
interval = flag.Duration("interval", time.Second*5, "Interval between active connections checks") | ||
) | ||
flag.Parse() | ||
log.SetPrefix("[active_conn] ") | ||
if _, err := os.Stat(".env"); err == nil { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatal("Error loading .env file") | ||
} | ||
} | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt) | ||
go func() { | ||
select { | ||
case _ = <-c: | ||
log.Printf("Shutting down..") | ||
if err := cleanup(); err != nil { | ||
log.Fatalf("cleanup failed: %v", err) | ||
} | ||
os.Exit(0) | ||
} | ||
}() | ||
if err := dbconn(); err != nil { | ||
log.Fatalf("failed to connect to db: %v", err) | ||
} | ||
|
||
if err := checks(*interval); err != nil { | ||
log.Printf("checks failed: %v", err) | ||
if err := cleanup(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
func dbconn() error { | ||
for _, env := range []string{"DB_HOST", "DB_USER", "DB_PASS"} { | ||
if v := os.Getenv(env); v == "" { | ||
return fmt.Errorf("Missing environment variable: %s (please see .env.sample)", env) | ||
} | ||
} | ||
var err error | ||
db, err = sql.Open("mysql", | ||
fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", | ||
os.Getenv("DB_USER"), os.Getenv("DB_PASS"), | ||
os.Getenv("DB_HOST"), os.Getenv("DB_NAME")), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return db.Ping() | ||
} | ||
|
||
func cleanup() error { | ||
if err := db.Close(); err != nil { | ||
log.Printf("Failed to close db conn: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func countConns() (int, error) { | ||
var count int | ||
var a string | ||
err := db.QueryRow("show status where `variable_name` = 'Threads_connected' ").Scan(&a, &count) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return count, nil | ||
|
||
} | ||
|
||
func checks(interval time.Duration) error { | ||
if interval < time.Second { | ||
return fmt.Errorf("interval can't be less than 1 second") | ||
} | ||
for { | ||
c, err := countConns() | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("DB connections: %d", c) | ||
time.Sleep(interval) | ||
} | ||
} |
This file contains 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