Skip to content

Commit

Permalink
Merge pull request #256 from martinlindhe/mssql-clean-instance-names
Browse files Browse the repository at this point in the history
Strip special chars from instance names
  • Loading branch information
carlpett authored Aug 31, 2018
2 parents 263ab8c + 7de316a commit cb9da1a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions collector/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down

0 comments on commit cb9da1a

Please sign in to comment.