github.com/elfadel/cilium@v1.6.12/pkg/health/server/health.go (about)

     1  // Copyright 2017-2019 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 server
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  
    21  	healthModels "github.com/cilium/cilium/api/v1/health/models"
    22  	. "github.com/cilium/cilium/api/v1/health/server/restapi"
    23  	ciliumModels "github.com/cilium/cilium/api/v1/models"
    24  	"github.com/cilium/cilium/pkg/api"
    25  	"github.com/cilium/cilium/pkg/client"
    26  
    27  	"github.com/c9s/goprocinfo/linux"
    28  	"github.com/go-openapi/runtime/middleware"
    29  )
    30  
    31  func dumpLoad() (*healthModels.LoadResponse, error) {
    32  	load, err := linux.ReadLoadAvg("/proc/loadavg")
    33  	if err != nil {
    34  		return nil, fmt.Errorf("Failure getting /stat: %s", err.Error())
    35  	}
    36  	return &healthModels.LoadResponse{
    37  		Last1min:  strconv.FormatFloat(load.Last1Min, 'f', 2, 64),
    38  		Last5min:  strconv.FormatFloat(load.Last5Min, 'f', 2, 64),
    39  		Last15min: strconv.FormatFloat(load.Last15Min, 'f', 2, 64),
    40  	}, nil
    41  }
    42  
    43  type getHealthz struct {
    44  	*Server
    45  }
    46  
    47  // NewGetHealthzHandler handles health requests.
    48  func NewGetHealthzHandler(s *Server) GetHealthzHandler {
    49  	return &getHealthz{Server: s}
    50  }
    51  
    52  func (h *getHealthz) getCiliumStatus() (*ciliumModels.StatusResponse, error) {
    53  	resp, err := h.Daemon.GetHealthz(nil)
    54  	if err != nil {
    55  		return nil, client.Hint(err)
    56  	}
    57  	return resp.Payload, nil
    58  }
    59  
    60  // Handle handles GET requests for /healthz .
    61  func (h *getHealthz) Handle(params GetHealthzParams) middleware.Responder {
    62  	log.Debug("Handling request for /healthz")
    63  
    64  	ciliumStatus, err := h.getCiliumStatus()
    65  	if err != nil {
    66  		return api.Error(GetHealthzFailedCode, err)
    67  	}
    68  	load, err := dumpLoad()
    69  	if err != nil {
    70  		return api.Error(GetHealthzFailedCode, err)
    71  	}
    72  
    73  	sr := healthModels.HealthResponse{}
    74  	sr.Cilium = ciliumStatus
    75  	sr.Uptime = h.Server.DumpUptime()
    76  	sr.SystemLoad = load
    77  	return NewGetHealthzOK().WithPayload(&sr)
    78  }