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

     1  package collectors
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"bosun.org/metadata"
     8  	"bosun.org/opentsdb"
     9  )
    10  
    11  func init() {
    12  	c_ad := &IntervalCollector{
    13  		F:        c_activedirectory_windows,
    14  		Interval: time.Minute * 5,
    15  	}
    16  	c_ad.init = wmiInitNamespace(c_ad, func() interface{} { return &[]MSAD_ReplNeighbor{} }, "", &adQuery, rootMSAD)
    17  	collectors = append(collectors, c_ad)
    18  }
    19  
    20  var (
    21  	adQuery  string
    22  	rootMSAD = "root\\MicrosoftActiveDirectory"
    23  )
    24  
    25  func c_activedirectory_windows() (opentsdb.MultiDataPoint, error) {
    26  	var dst []MSAD_ReplNeighbor
    27  	err := queryWmiNamespace(adQuery, &dst, rootMSAD)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	var md opentsdb.MultiDataPoint
    32  	for _, v := range dst {
    33  		lastSuccess, err := wmiParseCIMDatetime(v.TimeOfLastSyncSuccess)
    34  		if err != nil {
    35  			return nil, err
    36  		}
    37  		tags := opentsdb.TagSet{"source": strings.ToLower(v.SourceDsaCN), "context": activedirectory_context(v.NamingContextDN)}
    38  		sinceLastSuccess := time.Now().Sub(lastSuccess).Seconds()
    39  		Add(&md, "activedirectory.replication.sync_age", sinceLastSuccess, tags, metadata.Gauge, metadata.Second, descADReplicationSuccess)
    40  		Add(&md, "activedirectory.replication.consecutive_failures", v.NumConsecutiveSyncFailures, tags, metadata.Gauge, metadata.Count, descADReplicationFailures)
    41  	}
    42  	return md, nil
    43  }
    44  
    45  const (
    46  	descADReplicationSuccess  = "The number of seconds since the last successful replication attempt for this context."
    47  	descADReplicationFailures = "The number of consecutive failed replication attempts for this context."
    48  )
    49  
    50  type MSAD_ReplNeighbor struct {
    51  	SourceDsaCN                string
    52  	NamingContextDN            string
    53  	TimeOfLastSyncSuccess      string
    54  	NumConsecutiveSyncFailures uint32
    55  }
    56  
    57  func activedirectory_context(NamingContextDN string) string {
    58  	if strings.HasPrefix(NamingContextDN, "DC=DomainDnsZones,") {
    59  		return "DomainDNSZones"
    60  	}
    61  	if strings.HasPrefix(NamingContextDN, "DC=ForestDnsZones,") {
    62  		return "ForestDnsZones"
    63  	}
    64  	if strings.HasPrefix(NamingContextDN, "CN=Schema,CN=Configuration,") {
    65  		return "Schema"
    66  	}
    67  	if strings.HasPrefix(NamingContextDN, "CN=Configuration,") {
    68  		return "Configuration"
    69  	}
    70  	return "Domain"
    71  }