github.com/google/cloudprober@v0.11.3/sysvars/runtime_linux.go (about)

     1  // Copyright 2017-2021 The Cloudprober Authors.
     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  //go:build linux
    16  // +build linux
    17  
    18  package sysvars
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/google/cloudprober/logger"
    24  	"github.com/google/cloudprober/metrics"
    25  	"golang.org/x/sys/unix"
    26  )
    27  
    28  func osRuntimeVars(dataChan chan *metrics.EventMetrics, l *logger.Logger) {
    29  	em := metrics.NewEventMetrics(time.Now()).
    30  		AddLabel("ptype", "sysvars").
    31  		AddLabel("probe", "sysvars")
    32  
    33  	// CPU usage
    34  	var timeSpec unix.Timespec
    35  	if err := unix.ClockGettime(unix.CLOCK_PROCESS_CPUTIME_ID, &timeSpec); err != nil {
    36  		l.Warningf("Error while trying to get CPU usage: %v", err)
    37  	} else {
    38  		em.AddMetric("cpu_usage_msec", metrics.NewFloat(float64(timeSpec.Nano())/1e6))
    39  	}
    40  
    41  	dataChan <- em
    42  	l.Debug(em.String())
    43  }