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

     1  package collectors
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/StackExchange/wmi"
     9  )
    10  
    11  func queryWmi(query string, dst interface{}) error {
    12  	return queryWmiNamespace(query, dst, "")
    13  }
    14  
    15  func queryWmiNamespace(query string, dst interface{}, namespace string) error {
    16  	return wmi.QueryNamespace(query, dst, namespace)
    17  }
    18  
    19  func wmiInit(c *IntervalCollector, dst func() interface{}, where string, query *string) func() {
    20  	return func() {
    21  		*query = wmi.CreateQuery(dst(), where)
    22  		c.Enable = func() bool {
    23  			return queryWmi(*query, dst()) == nil
    24  		}
    25  	}
    26  }
    27  
    28  func wmiInitNamespace(c *IntervalCollector, dst func() interface{}, where string, query *string, namespace string) func() {
    29  	return func() {
    30  		*query = wmi.CreateQuery(dst(), where)
    31  		c.Enable = func() bool {
    32  			return queryWmiNamespace(*query, dst(), namespace) == nil
    33  		}
    34  	}
    35  }
    36  
    37  // wmiParseDatetime converts a string from the CIM_DATETIME format into UTC time.
    38  // Example: "20150616101948.494497-360" = 2015-06-16 04:19:48.494497 +0000 UTC.
    39  func wmiParseCIMDatetime(cimdatetime string) (time.Time, error) {
    40  	i := strings.IndexAny(cimdatetime, "+-")
    41  	if i < 0 {
    42  		return time.Time{}, fmt.Errorf("Invalid CIM_DATETIME format, cannot find UTC offset.")
    43  	}
    44  	t, err := time.Parse("20060102150405", cimdatetime[:i])
    45  	if err != nil {
    46  		return time.Time{}, err
    47  	}
    48  	offset, err := time.ParseDuration(fmt.Sprintf("%vm", cimdatetime[i:]))
    49  	if err != nil {
    50  		return time.Time{}, err
    51  	}
    52  	return t.Add(offset), nil
    53  }