Skip to content
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

Pt stalk go #908

Draft
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ require (
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sql-driver/mysql v1.8.1
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand All @@ -52,6 +60,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sevlyar/go-daemon v0.1.6
github.com/spf13/cobra v1.8.1
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/AlekSi/pointer v1.2.0 h1:glcy/gc4h8HnG2Z3ZECSzZ1IX1x2JxRVuDzaJwQE0+w=
github.com/AlekSi/pointer v1.2.0/go.mod h1:gZGfd3dpW4vEc/UlyfKKi1roIqcCgwOIvb0tSNSBle0=
github.com/Ladicle/tabwriter v1.0.0 h1:DZQqPvMumBDwVNElso13afjYLNp0Z7pHqHnu0r4t9Dg=
Expand All @@ -17,6 +19,7 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -30,6 +33,8 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
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-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand All @@ -51,8 +56,12 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef h1:A9HsByNhogrvm9cWb28sjiS3i7tcKCkflWFEkHfuAgM=
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY=
Expand Down Expand Up @@ -91,10 +100,15 @@ github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sevlyar/go-daemon v0.1.6 h1:EUh1MDjEM4BI109Jign0EaknA2izkOyi0LV3ro3QQGs=
github.com/sevlyar/go-daemon v0.1.6/go.mod h1:6dJpPatBT9eUwM5VCw9Bt6CdX9Tk6UWvhW3MebLDRKE=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
138 changes: 138 additions & 0 deletions src/go/pt-stalk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# pt-stalk (Go Version)

A Go implementation of the Percona pt-stalk tool for collecting MySQL and system metrics.

## Installation

Install using go:

go install github.com/percona/pt-stalk@latest

## Usage Examples

### Basic MySQL Monitoring

Collect only MySQL metrics:

pt-stalk --collectors=mysql \
--mysql-host=localhost \
--mysql-user=root \
--mysql-password=secret \
--dest=/var/log/mysql/samples \
--interval=1

### MySQL and System Monitoring

Collect both MySQL and system metrics:

pt-stalk --collectors=mysql,system \
--mysql-host=localhost \
--mysql-user=root \
--mysql-password=secret \
--collect-gdb=true \
--collect-tcpdump=true \
--dest=/var/log/mysql/samples \
--interval=1

### Running as a Daemon

Run pt-stalk in the background:

pt-stalk --collectors=mysql,system \
--mysql-host=localhost \
--mysql-user=root \
--mysql-password=secret \
--daemonize=true \
--pid=/var/run/pt-stalk.pid \
--log=/var/log/pt-stalk.log \
--dest=/var/log/mysql/samples

### Using a Custom Plugin

Run with a custom plugin script:

pt-stalk --collectors=mysql \
--mysql-host=localhost \
--mysql-user=root \
--mysql-password=secret \
--plugin=/path/to/custom/plugin.sh \
--dest=/var/log/mysql/samples

Example plugin script (plugin.sh):

#!/bin/bash
# Environment variables available:
# PT_DEST - destination directory
# PT_PREFIX - file prefix
# PT_INTERVAL - check interval
# PT_RUNTIME - collection duration

echo "Custom collection started" > "$PT_DEST/${PT_PREFIX}_custom.txt"
# Add your custom collection logic here

### Basic MongoDB Monitoring

Collect MongoDB metrics:

pt-stalk --collectors=mongodb \
--mongodb-host=localhost \
--mongodb-user=myuser \
--mongodb-password=secret \
--dest=/var/log/mongodb/samples \
--interval=1

## Configuration Options

### Common Options
- --collectors: Comma-separated list of collectors to enable (mysql,system)
- --interval: Check interval in seconds (default: 1)
- --run-time: How long to collect data in seconds (default: 30)
- --sleep: Sleep time between collections in seconds (default: 1)
- --dest: Destination directory for collected data (default: /var/lib/pt-stalk)
- --prefix: Filename prefix for samples
- --daemonize: Run as daemon (default: false)

### MySQL Collector Options
- --mysql-host: MySQL host (default: localhost)
- --mysql-port: MySQL port (default: 3306)
- --mysql-user: MySQL user
- --mysql-password: MySQL password
- --mysql-socket: MySQL socket file
- --mysql-defaults-file: MySQL configuration file

### System Collector Options
- --collect-gdb: Collect GDB stacktraces (default: false)
- --collect-oprofile: Collect OProfile data (default: false)
- --collect-strace: Collect strace data (default: false)
- --collect-tcpdump: Collect tcpdump data (default: false)

### Retention Options
- --retention-time: Days to retain samples (default: 30)
- --retention-count: Number of samples to retain (default: 0)
- --retention-size: Maximum size in MB to retain (default: 0)
- --disk-bytes-free: Minimum bytes free (default: 100MB)
- --disk-pct-free: Minimum percent free (default: 5)

### Notification Options
- --notify-by-email: Email address for notifications
- --verbose: Verbosity level (0-3) (default: 2)

### MongoDB Collector Options
- --mongodb-host: MongoDB host (default: localhost)
- --mongodb-port: MongoDB port (default: 27017)
- --mongodb-user: MongoDB user
- --mongodb-password: MongoDB password

## Output Files

Each collection creates files with the specified prefix and timestamps:
- {prefix}_status.txt: MySQL status variables
- {prefix}_variables.txt: MySQL system variables
- {prefix}_processlist.txt: MySQL process list
- {prefix}_diskstats.txt: System disk statistics
- {prefix}_meminfo.txt: System memory information
- {prefix}_loadavg.txt: System load average
- {prefix}_plugin.txt: Custom plugin output (if configured)
- {prefix}_server_status.txt: MongoDB server status metrics
- {prefix}_current_op.txt: MongoDB currently running operations
- {prefix}_db_stats.txt: MongoDB database statistics
24 changes: 24 additions & 0 deletions src/go/pt-stalk/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"context"

"github.com/spf13/cobra"
)

type CollectorRegistration struct {
Name string
AddFlags func(*cobra.Command, map[string]interface{})
NewCollector func(*Config) Collector
}

var registeredCollectors = make(map[string]CollectorRegistration)

func RegisterCollector(reg CollectorRegistration) {
registeredCollectors[reg.Name] = reg
}

// Base interface that all collectors must implement
type Collector interface {
Collect(ctx context.Context) error
}
140 changes: 140 additions & 0 deletions src/go/pt-stalk/collect_mongodb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package main

import (
"context"
"fmt"
"os"
"path/filepath"
"sync"

"github.com/spf13/cobra"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

type MongoDBCollector struct {
stalker *Stalker
client *mongo.Client
outDir string
prefix string
wg sync.WaitGroup
mongoCfg *MongoDBConfig
}

func NewMongoDBCollector(config *Config) Collector {
mongoCfg := config.CollectorConfigs["mongodb"].(*MongoDBConfig)
return &MongoDBCollector{
stalker: nil,
client: nil,
outDir: config.Dest,
prefix: config.Prefix,
mongoCfg: mongoCfg,
}
}

func (c *MongoDBCollector) Collect(ctx context.Context) error {
if c.client == nil {
uri := fmt.Sprintf("mongodb://%s:%s@%s:%d",
c.mongoCfg.User,
c.mongoCfg.Password,
c.mongoCfg.Host,
c.mongoCfg.Port,
)

client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
return fmt.Errorf("failed to connect to MongoDB: %v", err)
}
c.client = client
defer client.Disconnect(ctx)
}

c.wg.Add(1)
go func() {
defer c.wg.Done()
c.collectServerStatus(ctx)
c.collectCurrentOp(ctx)
c.collectDatabaseStats(ctx)
}()

c.wg.Wait()
return nil
}

func (c *MongoDBCollector) collectServerStatus(ctx context.Context) error {
result := bson.M{}
err := c.client.Database("admin").RunCommand(ctx, bson.D{{Key: "serverStatus", Value: 1}}).Decode(&result)
if err != nil {
return err
}
return c.writeResults(result, c.prefix+"_server_status.txt")
}

func (c *MongoDBCollector) collectCurrentOp(ctx context.Context) error {
result := bson.M{}
err := c.client.Database("admin").RunCommand(ctx, bson.D{{Key: "currentOp", Value: 1}}).Decode(&result)
if err != nil {
return err
}
return c.writeResults(result, c.prefix+"_current_op.txt")
}

func (c *MongoDBCollector) collectDatabaseStats(ctx context.Context) error {
dbs, err := c.client.ListDatabaseNames(ctx, bson.D{})
if err != nil {
return err
}

stats := make(map[string]bson.M)
for _, dbName := range dbs {
result := bson.M{}
err := c.client.Database(dbName).RunCommand(ctx, bson.D{{Key: "dbStats", Value: 1}}).Decode(&result)
if err != nil {
return err
}
stats[dbName] = result
}
return c.writeResults(stats, c.prefix+"_db_stats.txt")
}

func (c *MongoDBCollector) writeResults(data interface{}, filename string) error {
f, err := os.Create(filepath.Join(c.outDir, filename))
if err != nil {
return err
}
defer f.Close()

formatted, err := bson.MarshalExtJSON(data, true, false)
if err != nil {
return err
}

_, err = f.Write(formatted)
return err
}

type MongoDBConfig struct {
Host string
Port int
User string
Password string
}

func addMongoDBFlags(cmd *cobra.Command, cfg map[string]interface{}) {
mongoCfg := &MongoDBConfig{}
cfg["mongodb"] = mongoCfg

cmd.PersistentFlags().StringVar(&mongoCfg.Host, "mongodb-host", "localhost", "MongoDB host")
cmd.PersistentFlags().IntVar(&mongoCfg.Port, "mongodb-port", 27017, "MongoDB port")
cmd.PersistentFlags().StringVar(&mongoCfg.User, "mongodb-user", "", "MongoDB user")
cmd.PersistentFlags().StringVar(&mongoCfg.Password, "mongodb-password", "", "MongoDB password")
}

func init() {
RegisterCollector(CollectorRegistration{
Name: "mongodb",
AddFlags: addMongoDBFlags,
NewCollector: NewMongoDBCollector,
})
}
Loading