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
45 changes: 42 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,11 @@ func addClientData(ctx context.Context, zipW *zip.Writer) {

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

err = downloadFile(zipW, fmt.Sprintf("http://%s:%d/logs.zip", agentlocal.Localhost, agentlocal.DefaultPMMAgentListenPort), "pmm-admin")
if err != nil {
logrus.Warnf("%s", err)
}

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

// downloadFile download file and includes into zip file
func downloadFile(zipW *zip.Writer, url, fileName string) error {
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")
}

b, err := ioutil.ReadAll(response.Body)
if err != nil {
return errors.Wrap(err, "cannot read response body")
}
bufR := bytes.NewReader(b)

zipR, err := zip.NewReader(bufR, bufR.Size())
if err != nil {
return errors.Wrap(err, "cannot create Zip reader")
}

for _, rf := range zipR.File {
BupycHuk marked this conversation as resolved.
Show resolved Hide resolved
rc, err := rf.Open()
BupycHuk marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logrus.Errorf("%s", err)
continue
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 🐶
continue with no blank line before (nlreturn)

}
addData(zipW, path.Join(fileName, rf.Name), rf.Modified, rc)
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 🐶
G305: File traversal when extracting zip/tar archive (gosec)


rc.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 rc.Close is not checked (errcheck)

}
return nil
}

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