in internal/sqlservermetrics/sqlservermetrics_windows.go [61:151]
func OSCollection(ctx context.Context, path, logPrefix string, cfg *configpb.Configuration, onetime bool) error {
if !cfg.GetCollectionConfiguration().GetCollectGuestOsMetrics() {
return nil
}
if cfg.GetCredentialConfiguration() == nil || len(cfg.GetCredentialConfiguration()) == 0 {
return fmt.Errorf("empty credentials")
}
wlm, err := initCollection(ctx)
if err != nil {
return err
}
if !onetime {
if err := checkAgentStatus(wlm, path); err != nil {
return err
}
}
sourceInstanceProps := SIP
timeout := time.Duration(cfg.GetCollectionTimeoutSeconds()) * time.Second
interval := time.Duration(cfg.GetRetryIntervalInSeconds()) * time.Second
log.Logger.Info("Guest rules collection starts.")
for _, credentialCfg := range cfg.GetCredentialConfiguration() {
guestCfg := guestConfigFromCredential(credentialCfg)
if err := validateCredCfgGuest(cfg.GetRemoteCollection(), !guestCfg.LinuxRemote, guestCfg, credentialCfg.GetInstanceId(), credentialCfg.GetInstanceName()); err != nil {
log.Logger.Errorw("Invalid credential configuration", "error", err)
UsageMetricsLogger.Error(agentstatus.InvalidConfigurationsError)
if !cfg.GetRemoteCollection() {
break
}
continue
}
targetInstanceProps := sourceInstanceProps
var c guestcollector.GuestCollector
if cfg.GetRemoteCollection() {
// remote collection
targetInstanceProps = InstanceProperties{
InstanceID: credentialCfg.GetInstanceId(),
Instance: credentialCfg.GetInstanceName(),
}
host := guestCfg.ServerName
username := guestCfg.GuestUserName
if !guestCfg.LinuxRemote {
log.Logger.Debug("Starting remote win guest collection for ip " + host)
pswd, err := secretValue(ctx, sourceInstanceProps.ProjectID, guestCfg.GuestSecretName)
if err != nil {
log.Logger.Errorw("Collection failed", "target", guestCfg.ServerName, "error", fmt.Errorf("failed to get secret value: %v", err))
UsageMetricsLogger.Error(agentstatus.SecretValueError)
if !cfg.GetRemoteCollection() {
break
}
continue
}
c = guestcollector.NewWindowsCollector(host, username, pswd, UsageMetricsLogger)
} else {
// on local windows vm collecting on remote linux vm's, we use ssh, otherwise we use wmi
log.Logger.Debug("Starting remote linux guest collection for ip " + host)
// disks only used for local linux collection
c = guestcollector.NewLinuxCollector(nil, host, username, guestCfg.LinuxSSHPrivateKeyPath, true, guestCfg.GuestPortNumber, UsageMetricsLogger)
}
} else {
// local win collection
log.Logger.Debug("Starting local win guest collection")
c = guestcollector.NewWindowsCollector(nil, nil, nil, UsageMetricsLogger)
}
details := runOSCollection(ctx, c, timeout)
updateCollectedData(wlm, sourceInstanceProps, targetInstanceProps, details)
log.Logger.Debug("Finished guest collection")
if onetime {
target := "localhost"
if cfg.GetRemoteCollection() {
target = credentialCfg.GetInstanceName()
}
persistCollectedData(wlm, filepath.Join(filepath.Dir(logPrefix), fmt.Sprintf("%s-%s.json", target, "guest")))
} else {
log.Logger.Debugf("Source vm %s is sending os collected data on target machine, %s, to workload manager.", sourceInstanceProps.Instance, targetInstanceProps.Instance)
sendRequestToWLM(wlm, sourceInstanceProps.Name, cfg.GetMaxRetries(), interval)
}
// Local collection.
// Exit the loop. Only take the first credential in the credentialconfiguration array.
if !cfg.GetRemoteCollection() {
break
}
}
log.Logger.Info("Guest rules collection ends.")
return nil
}