2020-11-18 01:12:37 +00:00
package service
import (
2021-11-22 14:13:26 +00:00
"fmt"
2021-12-06 14:26:01 +00:00
"io"
2020-11-18 01:12:37 +00:00
"io/ioutil"
"net/http"
2021-11-22 17:06:12 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
2020-11-18 01:12:37 +00:00
)
// DebugPprof calls the /debug/pprof/ endpoints.
func ( c * Client ) DebugPprof ( name string ) ( [ ] byte , error ) {
endpoint := "/debug/pprof/" + name
response , err := c . AuthenticatedDo ( "GET" , endpoint , "" , nil )
if err != nil {
2021-11-22 14:13:26 +00:00
return nil , fmt . Errorf ( "GET %s: %w" , endpoint , err )
2020-11-18 01:12:37 +00:00
}
defer response . Body . Close ( )
if response . StatusCode != http . StatusOK {
2021-11-22 14:13:26 +00:00
return nil , fmt . Errorf (
2020-11-18 01:12:37 +00:00
"get pprof received status %d" ,
response . StatusCode ,
)
}
body , err := ioutil . ReadAll ( response . Body )
if err != nil {
2021-11-22 14:13:26 +00:00
return nil , fmt . Errorf ( "read pprof response body: %w" , err )
2020-11-18 01:12:37 +00:00
}
return body , nil
}
2021-11-22 17:06:12 +00:00
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
}
2021-12-06 14:26:01 +00:00
// DebugErrors calls the /debug/errors endpoint and on success writes its
// (potentially large) response body to w.
func ( c * Client ) DebugErrors ( w io . Writer ) error {
endpoint := "/debug/errors"
response , err := c . AuthenticatedDo ( "GET" , endpoint , "" , 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/dblocks endpoint and on success returns its
// response body data.
func ( c * Client ) DebugDBLocks ( ) ( [ ] byte , error ) {
endpoint := "/debug/dblocks"
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 {
if response . StatusCode == http . StatusInternalServerError {
return nil , fmt . Errorf ( "get dblocks received status %d; note that this is currently only supported for mysql 5.7 and the database user must have PROCESS privilege, see the fleet logs for error details" , response . StatusCode )
}
return nil , fmt . Errorf ( "get dblocks received status %d" , response . StatusCode )
}
body , err := ioutil . ReadAll ( response . Body )
if err != nil {
return nil , fmt . Errorf ( "read dblocks response body: %w" , err )
}
return body , nil
}