Skip to content

Commit

Permalink
Merge pull request #7 from klouddb/piiscanner
Browse files Browse the repository at this point in the history
pii scanner handling and custom template fixes
  • Loading branch information
klouddb authored Aug 13, 2024
2 parents 43201f5 + da62978 commit 8d0c5cf
Show file tree
Hide file tree
Showing 124 changed files with 63,510 additions and 930 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,12 @@ config.toml
.vscode
klouddbshield_report.html
klouddbshield_report.txt
kshield_pii_*.log

cronreport/*

docker_testing/pglog/*

output.csv
detailed_output.csv
all_checks.json
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ linters:
- staticcheck
# - gosec
# - gocritic
- vet
- govet
- errcheck
- gosimple
- govet
Expand Down
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ nfpms:
- src: passwords/*
dst: /etc/klouddbshield/passwords
type: config
- src: python/*
dst: /etc/klouddbshield/python
type: config
114 changes: 114 additions & 0 deletions cmd/ciscollector/caltransactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"context"
"fmt"
"os"
"text/tabwriter"

"github.com/jedib0t/go-pretty/v6/text"
"github.com/klouddb/klouddbshield/htmlreport"
cons "github.com/klouddb/klouddbshield/pkg/const"
"github.com/klouddb/klouddbshield/pkg/postgresdb"
"github.com/klouddb/klouddbshield/postgres/calctransactions"
)

type calTransactionRunner struct {
postgresCnf *postgresdb.Postgres
htmlReportHelper *htmlreport.HtmlReportHelper
printSummary bool
}

func newCalTransactionRunner(postgresCnf *postgresdb.Postgres, htmlReportHelper *htmlreport.HtmlReportHelper,
printSummary bool) *calTransactionRunner {
return &calTransactionRunner{
postgresCnf: postgresCnf,
htmlReportHelper: htmlReportHelper,
printSummary: printSummary,
}
}

func (c *calTransactionRunner) run(_ context.Context) error {
if c.postgresCnf == nil {
return fmt.Errorf(cons.Err_PostgresConfig_Missing)
}

// connect to db
postgresStore, _, err := postgresdb.Open(*c.postgresCnf)
if err != nil {
return fmt.Errorf("failed to connect to postgres: %v", err)
}

defer postgresStore.Close()

// check cluster
clusterStats := calctransactions.RunClusterQuery(calctransactions.Autovacuum_alerts_query, postgresStore)

// check databases
databaseStats := calctransactions.RunPerDatabaseStats(c.postgresCnf, calctransactions.GET_DATABASE_STATS, postgresStore, clusterStats)

// calculate the TxID age details
TxIDAgeDetails, err := calctransactions.GetTxIDAgeDetails(postgresStore)
if err != nil {
return fmt.Errorf("failed to get TxID age details: %v", err)
}
// println("Please wait for 60 seconds, while we are calculating Transactions Per Seconds for your database server...")

// calculate the TPS
TxPerSec, err := calctransactions.GetTxPerSec(postgresStore)
if err != nil {
return fmt.Errorf("failed to get Tx per sec: %v", err)
}

// create reports
data := calctransactions.ReportData{
ClusterStats: clusterStats,
DatabaseStats: databaseStats,
TPS: TxPerSec / 60.00,
TxIDAgeDetails: TxIDAgeDetails,
}

if !c.printSummary {
fmt.Println(text.Bold.Sprint("Transactions Per Second:"))
printReport(data)
}

c.htmlReportHelper.RegisterCalcTranx(data)

return nil
}

func printReport(data calctransactions.ReportData) {
// Initialize a new tab writer
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', tabwriter.Debug)

// Print cluster stats
fmt.Fprintln(w, "Cluster Stats:")
fmt.Fprintf(w, "Oldest Current XID:\t%d\n", data.ClusterStats.OldestCurrentXID)
fmt.Fprintf(w, "Percent Towards Wraparound:\t%.2f%%\n", data.ClusterStats.PercentTowardsWraparound)
fmt.Fprintf(w, "Percent Towards Emergency Autovac:\t%.2f%%\n", data.ClusterStats.PercentTowardsEmergencyAutovac)
fmt.Fprintln(w)

fmt.Println("============================================================")
// Print TPS
fmt.Fprintln(w, "TPS (Transactions Per Second):")
fmt.Fprintf(w, "%d\n", data.TPS)
fmt.Fprintln(w)

fmt.Println("============================================================")

// Print TxID Age Details
fmt.Fprintln(w, "TxID Age Details:")
fmt.Fprintf(w, "Oldest Running Xact Age:\t%.2f\n", data.TxIDAgeDetails.OldestRunningXactAge.Float64)
fmt.Fprintf(w, "Oldest Prepared Xact Age:\t%.2f\n", data.TxIDAgeDetails.OldestPreparedXactAge.Float64)
fmt.Fprintf(w, "Oldest Replication Slot Age:\t%.2f\n", data.TxIDAgeDetails.OldestReplicationSlotAge.Float64)
fmt.Fprintf(w, "Oldest Replica Xact Age:\t%.2f\n", data.TxIDAgeDetails.OldestReplicaXactAge.Float64)
fmt.Fprintf(w, "Oldest Running Xact Left:\t%.2f\n", data.TxIDAgeDetails.OldestRunningXactLeft.Float64)
fmt.Fprintf(w, "Oldest Prepared Xact Left:\t%.2f\n", data.TxIDAgeDetails.OldestPreparedXactLeft.Float64)
fmt.Fprintf(w, "Oldest Replication Slot Left:\t%.2f\n", data.TxIDAgeDetails.OldestReplicationSlotLeft.Float64)
fmt.Fprintf(w, "Oldest Replica Xact Left:\t%.2f\n", data.TxIDAgeDetails.OldestReplicaXactLeft.Float64)
fmt.Fprintln(w)

// Flush the writer
w.Flush()
}
Loading

0 comments on commit 8d0c5cf

Please sign in to comment.