Skip to content

Commit

Permalink
improved logging plus minor updates
Browse files Browse the repository at this point in the history
- new DCRCLI_DEBUG_MODE environment variable to enable debug logging
- added debug messages in fscopy, mongocredentials, mongologarchiver
- fixed logpath estimation in mongologarchiver
- renamed mongologarchiver/logpath.go ->
mongologarchiver/logestimator.go
- made minor updates on gstatic suggestions
  • Loading branch information
hasaketa committed Oct 24, 2024
1 parent de37223 commit c8a713e
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 65 deletions.
14 changes: 13 additions & 1 deletion dcrlogger/dcrlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type DCRLogger struct {
FileName string
LogFileHandle *os.File
Logger *slog.Logger
LevelVar *slog.LevelVar
}

func (dl *DCRLogger) CreateLogFileHandle() error {
Expand All @@ -35,11 +36,18 @@ func (dl *DCRLogger) CreateLogFileHandle() error {
}

func (dl *DCRLogger) Create() error {

err := dl.CreateLogFileHandle()
if err != nil {
return err
}
dl.Logger = slog.New(slog.NewJSONHandler(dl.LogFileHandle, nil))

dl.LevelVar = new(slog.LevelVar)

dljsonhandler := slog.NewJSONHandler(dl.LogFileHandle, &slog.HandlerOptions{Level: dl.LevelVar})

dl.Logger = slog.New(dljsonhandler)

return nil
}

Expand All @@ -63,3 +71,7 @@ func (dl *DCRLogger) Warn(msg string) {
func (dl *DCRLogger) Error(msg string) {
dl.Logger.Error(msg)
}

func (dl *DCRLogger) SetLogLevel(sloglvl slog.Level) {
dl.LevelVar.Set(sloglvl)
}
89 changes: 58 additions & 31 deletions fscopy/fscopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package fscopy
import (
"bufio"
"bytes"
"dcrcli/dcrlogger"
"fmt"
"io"
"os"
Expand All @@ -27,23 +28,27 @@ import (
type RemoteCred struct {
Username string
Available bool
Dcrlog *dcrlogger.DCRLogger
}

func (rc *RemoteCred) Get() error {
reader := bufio.NewReader(os.Stdin)
fmt.Println(
"Enter PasswordLess ssh User for remote copy. Leave Blank for cluster without remote nodes): ",
)
fmt.Println("Enter PasswordLess ssh User for remote copy. Leave Blank for cluster without remote nodes): ")
username, err := reader.ReadString('\n')
if err != nil {
return err
}

rc.Username = strings.TrimSuffix(username, "\n")
rc.Dcrlog.Debug(fmt.Sprintf("passwordless ssh username %s:", rc.Username))

if rc.Username == "" {
println("WARNING: PasswordLess SSH Username is empty assuming all nodes local")
rc.Available = false
rc.Dcrlog.Debug("passwordless ssh username left blank assuming all nodes local")
} else {
rc.Available = true
rc.Dcrlog.Debug("passwordless ssh username provided")
}

return nil
Expand All @@ -61,63 +66,60 @@ type DestDir struct {
Path []byte
}

// Copy job
// state:
// N - not started
// P - Progressing
// A - Aborted
// C - Completed successfully
type FSCopyJob struct {
Src SourceDir
Dst DestDir
State string
Output *bytes.Buffer
}

// example rsync -a --include='mongod.log*' --exclude='*' --include='*/' ubuntu@ip-10-0-0-246:/home/ubuntu/testcluster/dbp/ .
type FSCopyJobWithPattern struct {
CopyJobDetails *FSCopyJob
CurrentFileName string
Dcrlog *dcrlogger.DCRLogger
}

func (fcj *FSCopyJobWithPattern) StartCopyWithPattern() error {
if fcj.CopyJobDetails.Src.IsLocal {
return fcj.StartCopyLocalWithPattern()
func (fcjwp *FSCopyJobWithPattern) StartCopyWithPattern() error {
if fcjwp.CopyJobDetails.Src.IsLocal {
return fcjwp.StartCopyLocalWithPattern()
}
return fcj.StartCopyRemoteWithPattern()
return fcjwp.StartCopyRemoteWithPattern()
}

func (fcj *FSCopyJobWithPattern) StartCopyLocalWithPattern() error {
func (fcjwp *FSCopyJobWithPattern) StartCopyLocalWithPattern() error {
return nil
}

func (fcj *FSCopyJobWithPattern) StartCopyRemoteWithPattern() error {
func (fcjwp *FSCopyJobWithPattern) StartCopyRemoteWithPattern() error {
var cmd *exec.Cmd

filepattern := `'` + fcj.CurrentFileName + `*` + `'`
filepattern := `'` + fcjwp.CurrentFileName + `*` + `'`
excludepattern := `'` + `*` + `'`

// we invoke bash shell because the wildcards are interpretted by bash shell not the rsync program
fcjwp.Dcrlog.Debug(fmt.Sprintf("preparing command rsync -az --include=%s --exclude=%s --info=progress2 %s@%s:%s/ %s",
filepattern,
excludepattern,
fcjwp.CopyJobDetails.Src.Username,
fcjwp.CopyJobDetails.Src.Hostname,
fcjwp.CopyJobDetails.Src.Path,
fcjwp.CopyJobDetails.Dst.Path))

cmd = exec.Command(
"bash",
"-c",
fmt.Sprintf(
"rsync -az --include=%s --exclude=%s --info=progress2 %s@%s:%s/ %s",
filepattern,
excludepattern,
fcj.CopyJobDetails.Src.Username,
fcj.CopyJobDetails.Src.Hostname,
fcj.CopyJobDetails.Src.Path,
fcj.CopyJobDetails.Dst.Path,
fcjwp.CopyJobDetails.Src.Username,
fcjwp.CopyJobDetails.Src.Hostname,
fcjwp.CopyJobDetails.Src.Path,
fcjwp.CopyJobDetails.Dst.Path,
))

cmd.Stdout = fcj.CopyJobDetails.Output
cmd.Stdout = fcjwp.CopyJobDetails.Output

stderr, err := cmd.StderrPipe()
if err != nil {
return err
}

fcjwp.Dcrlog.Debug("rsync command start")
err = cmd.Start()
if err != nil {
return err
Expand All @@ -126,21 +128,43 @@ func (fcj *FSCopyJobWithPattern) StartCopyRemoteWithPattern() error {
_, err = io.ReadAll(stderr)
if err != nil {
_ = cmd.Process.Kill()
fcjwp.Dcrlog.Debug(fmt.Sprintf("StartCopyRemoteWithPattern: Error reading from stderr pipe %w", err))
return fmt.Errorf("StartCopyRemoteWithPattern: Error reading from stderr pipe %w", err)
}

err = cmd.Wait()
if err != nil {
fcjwp.Dcrlog.Debug(fmt.Sprintf("StartCopyRemoteWithPattern: error doing remote copy job wait %w", err))
return fmt.Errorf("StartCopyRemoteWithPattern: error doing remote copy job wait %w", err)
}
return nil
}

// Copy job
// state:
// N - not started
// P - Progressing
// A - Aborted
// C - Completed successfully
type FSCopyJob struct {
Src SourceDir
Dst DestDir
State string
Output *bytes.Buffer
Dcrlog *dcrlogger.DCRLogger
}

// currently only run for remote source directories
func (fcj *FSCopyJob) StartCopyRemote() error {
var cmd *exec.Cmd
//var cmd *exec.Cmd

cmd = exec.Command(
fcj.Dcrlog.Debug(fmt.Sprintf("preparing command rsync -az --info=progress2 %s@%s:%s/ %s",
fcj.Src.Username,
fcj.Src.Hostname,
fcj.Src.Path,
fcj.Dst.Path))

cmd := exec.Command(
"rsync",
"-az",
"--info=progress2",
Expand All @@ -159,6 +183,7 @@ func (fcj *FSCopyJob) StartCopyRemote() error {
return err
}

fcj.Dcrlog.Debug("starting rsync command")
err = cmd.Start()
if err != nil {
return err
Expand All @@ -167,11 +192,13 @@ func (fcj *FSCopyJob) StartCopyRemote() error {
_, err = io.ReadAll(stderr)
if err != nil {
_ = cmd.Process.Kill()
return fmt.Errorf("Error reading from stderr pipe %w", err)
fcj.Dcrlog.Debug(fmt.Sprintf("error reading from stderr pipe %w", err))
return fmt.Errorf("error reading from stderr pipe %w", err)
}

err = cmd.Wait()
if err != nil {
fcj.Dcrlog.Debug(fmt.Sprintf("error doing remote copy job wait %w", err))
return fmt.Errorf("error doing remote copy job wait %w", err)
}

Expand Down
34 changes: 27 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"log"
"log/slog"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -48,15 +49,18 @@ func checkEmptyDirectory(OutputPrefix string) string {

func isDirectoryExist(OutputPrefix string) bool {
_, err := os.Stat(OutputPrefix)
if os.IsNotExist(err) {
return false
}
return true
return !os.IsNotExist(err)
}

func main() {
var err error

dcrcliDebugModeEnv, isEnvSet := os.LookupEnv("DCRCLI_DEBUG_MODE")
dcrcliDebugMode := false
if isEnvSet {
dcrcliDebugMode, _ = strconv.ParseBool(dcrcliDebugModeEnv)
}

dcrlog := dcrlogger.DCRLogger{}
dcrlog.OutputPrefix = "./"
dcrlog.FileName = "dcrlogfile"
Expand All @@ -66,16 +70,23 @@ func main() {
log.Fatal("Unable to create log file abormal Exit:", err)
}

if dcrcliDebugMode {
dcrlog.SetLogLevel(slog.LevelDebug)
}

fmt.Println("DCR Log file:", dcrlog.Path())

cred := mongocredentials.Mongocredentials{}
err = cred.Get(&dcrlog)
cred.Dcrlog = &dcrlog

err = cred.Get()
if err != nil {
dcrlog.Error(err.Error())
log.Fatal("Error why getting DB credentials aborting!")
}

remoteCred := fscopy.RemoteCred{}
remoteCred.Dcrlog = &dcrlog
remoteCred.Get()

s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
Expand Down Expand Up @@ -185,17 +196,19 @@ func main() {
logarchive := mongologarchiver.MongoDLogarchive{}
logarchive.Mongo.S = &cred
logarchive.Outputdir = &outputdir
logarchive.Dcrlog = &dcrlog
err = logarchive.Start()
if err != nil {
dcrlog.Error(fmt.Sprintf("Error in LogArchive: %v", err))
//log.Fatal("Error in LogArchive:", err)
}

} else {
if remoteCred.Available == true {
if remoteCred.Available {
dcrlog.Info(fmt.Sprintf("%s is not a local hostname. Proceeding with remote Copier.", hostname))

remotecopyJob := fscopy.FSCopyJob{}
remotecopyJob.Dcrlog = &dcrlog

dcrlog.Info("Running FTDC Archiving")
remoteFTDCArchiver := ftdcarchiver.RemoteFTDCarchive{}
Expand All @@ -221,7 +234,9 @@ func main() {
remoteFTDCArchiver.RemoteCopyJob.Src.IsLocal = false
remoteFTDCArchiver.RemoteCopyJob.Src.Username = []byte(remoteCred.Username)
remoteFTDCArchiver.RemoteCopyJob.Src.Hostname = []byte(cred.Currentmongodhost)
remoteFTDCArchiver.RemoteCopyJob.Output = &bytes.Buffer{}

var buffer bytes.Buffer
remoteFTDCArchiver.RemoteCopyJob.Output = &buffer

remoteFTDCArchiver.RemoteCopyJob.Dst.Path = []byte(
remoteFTDCArchiver.TempOutputdir.Path(),
Expand All @@ -233,9 +248,11 @@ func main() {
//log.Fatal("Error in Remote FTDC Archive: ", err)
}

dcrlog.Debug(fmt.Sprintf("remote copy job output %s:", buffer.String()))
remotecopyJob.Output.Reset()

remotecopyJobWithPattern := fscopy.FSCopyJobWithPattern{}
remotecopyJobWithPattern.Dcrlog = &dcrlog
remotecopyJobWithPattern.CopyJobDetails = &remotecopyJob

dcrlog.Info("Running mongo log Archiving")
Expand All @@ -244,12 +261,15 @@ func main() {
remoteLogArchiver.Mongo.S = &cred
remoteLogArchiver.Outputdir = &outputdir
remoteLogArchiver.TempOutputdir = &tempdir
remoteLogArchiver.Dcrlog = &dcrlog

err = remoteLogArchiver.Start()
if err != nil {
dcrlog.Error(fmt.Sprintf("Error in Remote Log Archive for this node: %v", err))
//log.Fatal("Error in Remote Log Archive: ", err)
}
dcrlog.Debug(fmt.Sprintf("remote copy job output %s:", buffer.String()))
remotecopyJob.Output.Reset()
}

}
Expand Down
Loading

0 comments on commit c8a713e

Please sign in to comment.