mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
33858d7301
# Checklist for submitter If some of the following don't apply, delete the relevant line. - [ ] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [ ] Documented any API changes (docs/Using-Fleet/REST-API.md or docs/Contributing/API-for-contributors.md) - [ ] Documented any permissions changes (docs/Using Fleet/manage-access.md) - [ ] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for new osquery data ingestion features. - [ ] Added/updated tests - [ ] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [ ] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - [ ] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)). Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
)
|
|
|
|
func (c *Client) getRawBody(endpoint string) ([]byte, error) {
|
|
response, err := c.AuthenticatedDo("GET", endpoint, "", nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("GET %s: %w", endpoint, err)
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("get %s received status %d", endpoint, response.StatusCode)
|
|
}
|
|
|
|
body, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read %s response body: %w", endpoint, err)
|
|
}
|
|
|
|
return body, nil
|
|
}
|
|
|
|
// DebugPprof calls the /debug/pprof/ endpoints.
|
|
func (c *Client) DebugPprof(name string) ([]byte, error) {
|
|
return c.getRawBody("/debug/pprof/" + name)
|
|
}
|
|
|
|
func (c *Client) DebugMigrations() (*fleet.MigrationStatus, error) {
|
|
var migrationStatus fleet.MigrationStatus
|
|
err := c.authenticatedRequest(nil, "GET", "/debug/migrations", &migrationStatus)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &migrationStatus, nil
|
|
}
|
|
|
|
// DebugErrors calls the /debug/errors endpoint and on success writes its
|
|
// (potentially large) response body to w.
|
|
func (c *Client) DebugErrors(w io.Writer, flush bool) error {
|
|
endpoint := "/debug/errors"
|
|
rawQuery := ""
|
|
if flush {
|
|
rawQuery = "flush=true"
|
|
}
|
|
response, err := c.AuthenticatedDo("GET", endpoint, rawQuery, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("GET %s: %w", endpoint, err)
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("get errors received status %d", response.StatusCode)
|
|
}
|
|
|
|
if _, err := io.Copy(w, response.Body); err != nil {
|
|
return fmt.Errorf("read errors response body: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DebugDBLocks calls the /debug/db/locks endpoint and on success returns its
|
|
// response body data.
|
|
func (c *Client) DebugDBLocks() ([]byte, error) {
|
|
return c.getRawBody("/debug/db/locks")
|
|
}
|
|
|
|
func (c *Client) DebugInnoDBStatus() ([]byte, error) {
|
|
return c.getRawBody("/debug/db/innodb-status")
|
|
}
|
|
|
|
func (c *Client) DebugProcessList() ([]byte, error) {
|
|
return c.getRawBody("/debug/db/process-list")
|
|
}
|