Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

PMM-5680 store agents logs #197

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
40 changes: 37 additions & 3 deletions commands/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,15 @@ func addClientData(ctx context.Context, zipW *zip.Writer) {
logrus.Errorf("%s", err)
return
}

addVMAgentTargets(ctx, zipW, status.AgentsInfo)

now := time.Now()

b, err := json.MarshalIndent(status, "", " ")
if err != nil {
logrus.Debugf("%s", err)
b = []byte(err.Error())
}
b = append(b, '\n')
now := time.Now()
addData(zipW, "client/status.json", now, bytes.NewReader(b))

// FIXME get it via pmm-agent's API - it is _not_ a good idea to use exec there
Expand All @@ -145,6 +143,14 @@ func addClientData(ctx context.Context, zipW *zip.Writer) {

addData(zipW, "client/pmm-admin-version.txt", now, bytes.NewReader([]byte(version.FullInfo())))

fileName := "pmm-admin-summary.zip"
err = downloadFile(fmt.Sprintf("http://%s:%d/logs.zip", agentlocal.Localhost, agentlocal.DefaultPMMAgentListenPort), fileName)
if err != nil {
logrus.Debugf("%s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's write it in warn level

b = []byte(err.Error())
}
logrus.Info("pmm-admin-summary.zip created")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are on a right track, as a next step let's include files from logs.zip into main zip file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can find example of it in addServerData

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it

if status.ConfigFilepath != "" {
addFile(zipW, "client/pmm-agent-config.yaml", status.ConfigFilepath)
}
Expand Down Expand Up @@ -220,6 +226,34 @@ func getURL(ctx context.Context, url string) ([]byte, error) {
return b, nil
}

// downloadFile download file to local destination
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// downloadFile download file to local destination
// downloadFile download file and includes into zip file

func downloadFile(url, fileName string) error {
//Get the response bytes from the url
response, err := http.Get(url)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
G107: Potential HTTP request made with variable url (gosec)

if err != nil {
return err
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
error returned from external package is unwrapped: sig: func net/http.Get(url string) (resp *net/http.Response, err error) (wrapcheck)

}
defer response.Body.Close()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
Error return value of response.Body.Close is not checked (errcheck)


if response.StatusCode != 200 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
mnd: Magic number: 200, in detected (gomnd)

return errors.New("Received non 200 response code")
}

//Create an empty file
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()

//Write the bytes to the file
_, err = io.Copy(file, response.Body)
if err != nil {
return err
}
return nil
}

type pprofData struct {
name string
data []byte
Expand Down