forked from newrelic/infrastructure-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added hostinfo plugin for darwin (newrelic#629)
- Loading branch information
1 parent
0671792
commit debe71c
Showing
8 changed files
with
646 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2020 New Relic Corporation. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// +build darwin | ||
|
||
package distro | ||
|
||
import ( | ||
"fmt" | ||
"github.com/newrelic/infrastructure-agent/pkg/helpers" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
productNameRegex = regexp.MustCompile("ProductName:\\s*(.*)") | ||
productVersionRegex = regexp.MustCompile("ProductVersion:\\s*(.*)") | ||
|
||
defaultProductName = "macOS" | ||
) | ||
|
||
type distroFetcherFn func() (string, error) | ||
|
||
func fetchDistro() (string, error) { | ||
return helpers.RunCommand("/usr/bin/sw_vers", "") | ||
} | ||
|
||
// GetDistro will parse the output for '/usr/bin/sw_vers' command to detect | ||
// the ProductName and ProductVersion fields | ||
func GetDistro() string { | ||
return parseDistro(fetchDistro) | ||
} | ||
|
||
func parseDistro(distro distroFetcherFn) string { | ||
var productName, productVersion string | ||
output, err := distro() | ||
if err != nil { | ||
return defaultProductName | ||
} | ||
|
||
if productNameLine := productNameRegex.FindStringSubmatch(output); len(productNameLine) > 1 { | ||
productName = productNameLine[1] | ||
} else { | ||
productName = defaultProductName | ||
} | ||
|
||
if productVersionLine := productVersionRegex.FindStringSubmatch(output); len(productVersionLine) > 1 { | ||
productVersion = productVersionLine[1] | ||
} else { | ||
productVersion = "(Unknown)" | ||
} | ||
|
||
return fmt.Sprintf("%s %s", productName, productVersion) | ||
} | ||
|
||
func IsCentos5() bool { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2020 New Relic Corporation. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// +build darwin | ||
|
||
package distro | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestParseDistro_Success(t *testing.T) { | ||
actual := parseDistro(func() (string, error) { | ||
return `ProductName: macOS2 | ||
ProductVersion: 11.2.3 | ||
BuildVersion: 20D91`, nil | ||
}) | ||
|
||
assert.Equal(t, "macOS2 11.2.3", actual) | ||
} | ||
|
||
func TestParseDistro_MissingProductName(t *testing.T) { | ||
actual := parseDistro(func() (string, error) { | ||
return `ProductVersion: 11.2.3 | ||
BuildVersion: 20D91`, nil | ||
}) | ||
|
||
assert.Equal(t, "macOS 11.2.3", actual) | ||
} | ||
|
||
func TestParseDistro_MissingProductVersion(t *testing.T) { | ||
actual := parseDistro(func() (string, error) { | ||
return `BuildVersion: 20D91`, nil | ||
}) | ||
|
||
assert.Equal(t, "macOS (Unknown)", actual) | ||
} | ||
|
||
func TestParseDistro_OnError(t *testing.T) { | ||
actual := parseDistro(func() (string, error) { | ||
return ``, fmt.Errorf("error") | ||
}) | ||
|
||
assert.Equal(t, defaultProductName, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.