github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/cmd/tendermint/commands/debug/util.go (about)

     1  package debug
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  
    11  	cfg "github.com/tendermint/tendermint/config"
    12  	rpchttp "github.com/tendermint/tendermint/rpc/client/http"
    13  )
    14  
    15  // dumpStatus gets node status state dump from the Tendermint RPC and writes it
    16  // to file. It returns an error upon failure.
    17  func dumpStatus(rpc *rpchttp.HTTP, dir, filename string) error {
    18  	status, err := rpc.Status()
    19  	if err != nil {
    20  		return fmt.Errorf("failed to get node status: %w", err)
    21  	}
    22  
    23  	return writeStateJSONToFile(status, dir, filename)
    24  }
    25  
    26  // dumpNetInfo gets network information state dump from the Tendermint RPC and
    27  // writes it to file. It returns an error upon failure.
    28  func dumpNetInfo(rpc *rpchttp.HTTP, dir, filename string) error {
    29  	netInfo, err := rpc.NetInfo()
    30  	if err != nil {
    31  		return fmt.Errorf("failed to get node network information: %w", err)
    32  	}
    33  
    34  	return writeStateJSONToFile(netInfo, dir, filename)
    35  }
    36  
    37  // dumpConsensusState gets consensus state dump from the Tendermint RPC and
    38  // writes it to file. It returns an error upon failure.
    39  func dumpConsensusState(rpc *rpchttp.HTTP, dir, filename string) error {
    40  	consDump, err := rpc.DumpConsensusState()
    41  	if err != nil {
    42  		return fmt.Errorf("failed to get node consensus dump: %w", err)
    43  	}
    44  
    45  	return writeStateJSONToFile(consDump, dir, filename)
    46  }
    47  
    48  // copyWAL copies the Tendermint node's WAL file. It returns an error if the
    49  // WAL file cannot be read or copied.
    50  func copyWAL(conf *cfg.Config, dir string) error {
    51  	walPath := conf.Consensus.WalFile()
    52  	walFile := filepath.Base(walPath)
    53  
    54  	return copyFile(walPath, filepath.Join(dir, walFile))
    55  }
    56  
    57  // copyConfig copies the Tendermint node's config file. It returns an error if
    58  // the config file cannot be read or copied.
    59  func copyConfig(home, dir string) error {
    60  	configFile := "config.toml"
    61  	configPath := filepath.Join(home, "config", configFile)
    62  
    63  	return copyFile(configPath, filepath.Join(dir, configFile))
    64  }
    65  
    66  func dumpProfile(dir, addr, profile string, debug int) error {
    67  	endpoint := fmt.Sprintf("%s/debug/pprof/%s?debug=%d", addr, profile, debug)
    68  
    69  	resp, err := http.Get(endpoint) // nolint: gosec
    70  	if err != nil {
    71  		return fmt.Errorf("failed to query for %s profile: %w", profile, err)
    72  	}
    73  	defer resp.Body.Close()
    74  
    75  	body, err := ioutil.ReadAll(resp.Body)
    76  	if err != nil {
    77  		return fmt.Errorf("failed to read %s profile response body: %w", profile, err)
    78  	}
    79  
    80  	return ioutil.WriteFile(path.Join(dir, fmt.Sprintf("%s.out", profile)), body, os.ModePerm)
    81  }