github.com/zhyoulun/cilium@v1.6.12/daemon/debuginfo.go (about)

     1  // Copyright 2017-2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  
    22  	"github.com/cilium/cilium/api/v1/models"
    23  	restapi "github.com/cilium/cilium/api/v1/server/restapi/daemon"
    24  	"github.com/cilium/cilium/api/v1/server/restapi/endpoint"
    25  	"github.com/cilium/cilium/pkg/debug"
    26  	"github.com/cilium/cilium/pkg/version"
    27  
    28  	"github.com/go-openapi/runtime/middleware"
    29  	"github.com/spf13/viper"
    30  )
    31  
    32  type getDebugInfo struct {
    33  	daemon *Daemon
    34  }
    35  
    36  // NewGetDebugInfoHandler returns the debug info endpoint handler for the agent
    37  func NewGetDebugInfoHandler(d *Daemon) restapi.GetDebuginfoHandler {
    38  	return &getDebugInfo{daemon: d}
    39  }
    40  
    41  func (h *getDebugInfo) Handle(params restapi.GetDebuginfoParams) middleware.Responder {
    42  	dr := models.DebugInfo{}
    43  	d := h.daemon
    44  
    45  	dr.CiliumVersion = version.Version
    46  	if kver, err := getKernelVersion(); err != nil {
    47  		dr.KernelVersion = fmt.Sprintf("Error: %s\n", err)
    48  	} else {
    49  		dr.KernelVersion = fmt.Sprintf("%s", kver)
    50  	}
    51  
    52  	status := d.getStatus(false)
    53  	dr.CiliumStatus = &status
    54  
    55  	var p endpoint.GetEndpointParams
    56  
    57  	dr.EndpointList = getEndpointList(p)
    58  	dr.Policy = d.policy.GetRulesList()
    59  	dr.Subsystem = debug.CollectSubsystemStatus()
    60  	dr.CiliumMemoryMap = memoryMap(os.Getpid())
    61  
    62  	dr.EnvironmentVariables = []string{}
    63  	for _, k := range viper.AllKeys() {
    64  		// Assuming we are only getting strings
    65  		v := fmt.Sprintf("%s:%s", k, viper.GetString(k))
    66  		dr.EnvironmentVariables = append(dr.EnvironmentVariables, v)
    67  	}
    68  
    69  	dr.ServiceList = d.GetServiceList()
    70  
    71  	return restapi.NewGetDebuginfoOK().WithPayload(&dr)
    72  }
    73  
    74  func memoryMap(pid int) string {
    75  	m, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/maps", pid))
    76  	if err != nil {
    77  		return ""
    78  	}
    79  	return string(m)
    80  }