From 7de316af9f6b98110ba764e2a63f08d7718a8400 Mon Sep 17 00:00:00 2001 From: Calle Pettersson Date: Fri, 31 Aug 2018 17:02:07 +0200 Subject: [PATCH] Strip special chars from instance names --- collector/mssql.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/collector/mssql.go b/collector/mssql.go index 66092f00..fadd3ea6 100644 --- a/collector/mssql.go +++ b/collector/mssql.go @@ -66,7 +66,7 @@ func getMSSQLInstances() mssqlInstancesType { instanceNames, err := k.ReadValueNames(0) if err != nil { - log.Warn("Can't ReadSubKeyNames %#v", err) + log.Warnf("Can't ReadSubKeyNames %#v", err) return sqlDefaultInstance } @@ -87,7 +87,15 @@ func getMSSQLInstances() mssqlInstancesType { func mssqlBuildWMIInstanceClass(suffix string, instance string) string { instancePart := "MSSQLSERVER_SQLServer" if instance != "MSSQLSERVER" { - instancePart = fmt.Sprintf("MSSQL%s_MSSQL%s", instance, instance) + // Instance names can contain some special characters, which are not supported in the WMI class name. + // We strip those out. + cleanedName := strings.Map(func(r rune) rune { + if r == '_' || r == '$' || r == '#' { + return -1 + } + return r + }, instance) + instancePart = fmt.Sprintf("MSSQL%s_MSSQL%s", cleanedName, cleanedName) } return fmt.Sprintf("Win32_PerfRawData_%s%s", instancePart, suffix)