bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/yum_update_linux.go (about)

     1  package collectors
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"bosun.org/metadata"
     8  	"bosun.org/opentsdb"
     9  	"bosun.org/util"
    10  )
    11  
    12  func init() {
    13  	collectors = append(collectors, &IntervalCollector{F: yum_update_stats_linux, Interval: time.Minute * 30})
    14  }
    15  
    16  func yum_update_stats_linux() (opentsdb.MultiDataPoint, error) {
    17  	var md opentsdb.MultiDataPoint
    18  	regular_c := 0
    19  	kernel_c := 0
    20  	// This is a silly long timeout, but until we implement sigint this will
    21  	// Prevent a currupt yum db https://github.com/bosun-monitor/scollector/issues/56
    22  	err := util.ReadCommandTimeout(time.Minute*5, func(line string) error {
    23  		fields := strings.Fields(line)
    24  		if len(fields) > 1 && !strings.HasPrefix(fields[0], "Updated Packages") {
    25  			if strings.HasPrefix(fields[0], "kern") {
    26  				kernel_c++
    27  			} else {
    28  				regular_c++
    29  			}
    30  		}
    31  		return nil
    32  
    33  	}, nil, "yum", "list", "updates", "-q")
    34  	if err == util.ErrPath {
    35  		return nil, nil
    36  	} else if err != nil {
    37  		return nil, err
    38  	}
    39  	Add(&md, "linux.updates.count", regular_c, opentsdb.TagSet{"type": "non-kernel"}, metadata.Gauge, metadata.Count, "")
    40  	Add(&md, "linux.updates.count", kernel_c, opentsdb.TagSet{"type": "kernel"}, metadata.Gauge, metadata.Count, "")
    41  	return md, nil
    42  }