Merge pull request #256 from martinlindhe/mssql-clean-instance-names

Strip special chars from instance names
This commit is contained in:
Calle Pettersson 2018-08-31 18:16:43 +02:00 committed by GitHub
commit cb9da1ae22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 2 deletions

View File

@ -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)