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

     1  package collectors
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"bosun.org/metadata"
     9  	"bosun.org/opentsdb"
    10  	"bosun.org/util"
    11  )
    12  
    13  func init() {
    14  	collectors = append(collectors, &IntervalCollector{F: c_sntp_windows})
    15  }
    16  
    17  func c_sntp_windows() (opentsdb.MultiDataPoint, error) {
    18  	var md opentsdb.MultiDataPoint
    19  	const metric = "sntp."
    20  	var (
    21  		stratum string
    22  		delay   float64
    23  		when    float64
    24  		source  string
    25  		poll    float64
    26  	)
    27  	if err := util.ReadCommand(func(line string) error {
    28  		f := strings.SplitN(line, ":", 2)
    29  		if len(f) != 2 {
    30  			return nil
    31  		}
    32  		f[1] = strings.TrimSpace(f[1])
    33  		switch f[0] {
    34  		case "Stratum":
    35  			sf := strings.Fields(f[1])
    36  			if len(sf) < 1 {
    37  				return fmt.Errorf("Unexpected value for stratum")
    38  			}
    39  			stratum = sf[0]
    40  		case "Root Delay":
    41  			d, err := time.ParseDuration(f[1])
    42  			if err != nil {
    43  				return err
    44  			}
    45  			delay = d.Seconds()
    46  		case "Last Successful Sync Time":
    47  			if f[1] == "unspecified" {
    48  				break
    49  			}
    50  			t, err := time.Parse("1/2/2006 3:04:05 PM", f[1])
    51  			if err != nil {
    52  				return err
    53  			}
    54  			when = time.Since(t).Seconds()
    55  		case "Source":
    56  			source = strings.TrimSpace(f[1])
    57  		case "Poll Interval":
    58  			sf := strings.Fields(f[1])
    59  			if len(sf) != 2 {
    60  				return fmt.Errorf("Unexpected value for Poll Interval")
    61  			}
    62  			s := strings.Trim(sf[1], "()")
    63  			d, err := time.ParseDuration(strings.TrimSpace(s))
    64  			if err != nil {
    65  				return err
    66  			}
    67  			poll = d.Seconds()
    68  		}
    69  		return nil
    70  	}, "w32tm", "/query", "/status"); err != nil {
    71  		return nil, nil
    72  	}
    73  	tags := opentsdb.TagSet{"remote": source}
    74  	Add(&md, metric+"stratum", stratum, tags, metadata.Gauge, "Stratum", "")
    75  	Add(&md, metric+"delay", delay, tags, metadata.Gauge, metadata.Second, "")
    76  	Add(&md, metric+"when", when, tags, metadata.Gauge, metadata.Second, "")
    77  	Add(&md, metric+"poll", poll, tags, metadata.Gauge, metadata.Second, "")
    78  	_ = util.ReadCommand(func(line string) error {
    79  		f := strings.SplitN(line, ",", 2)
    80  		if len(f) != 2 {
    81  			return nil
    82  		}
    83  		d, err := time.ParseDuration(strings.TrimSpace(f[1]))
    84  		if err != nil {
    85  			return nil
    86  		}
    87  		Add(&md, metric+"offset", d.Seconds(), tags, metadata.Gauge, metadata.Second, "")
    88  		return nil
    89  	}, "w32tm", "/stripchart", fmt.Sprintf("/computer:%v", source), "/samples:1", "/dataonly")
    90  	return md, nil
    91  }