@ -16,6 +16,7 @@ import (
"time"
"github.com/shirou/gopsutil/internal/common"
common "github.com/shirou/gopsutil/internal/common"
"github.com/shirou/gopsutil/process"
)
@ -144,6 +145,26 @@ func Users() ([]UserStat, error) {
}
func getOSRelease ( ) ( platform string , version string , err error ) {
contents , err := common . ReadLines ( common . HostEtc ( "os-release" ) )
if err != nil {
return "" , "" , nil // return empty
}
for _ , line := range contents {
field := strings . Split ( line , "=" )
if len ( field ) < 2 {
continue
}
switch field [ 0 ] {
case "ID" : // use ID for lowercase
platform = field [ 1 ]
case "VERSION" :
version = field [ 1 ]
}
}
return platform , version , nil
}
func getLSB ( ) ( * LSB , error ) {
ret := & LSB { }
if common . PathExists ( common . HostEtc ( "lsb-release" ) ) {
@ -172,7 +193,7 @@ func getLSB() (*LSB, error) {
if err != nil {
return ret , err
}
out , err := exec . Command ( lsb_release ) . Output ( )
out , err := invoke . Command ( lsb_release )
if err != nil {
return ret , err
}
@ -264,6 +285,18 @@ func PlatformInformation() (platform string, family string, version string, err
} else if common . PathExists ( common . HostEtc ( "arch-release" ) ) {
platform = "arch"
version = lsb . Release
} else if common . PathExists ( common . HostEtc ( "alpine-release" ) ) {
platform = "alpine"
contents , err := common . ReadLines ( common . HostEtc ( "alpine-release" ) )
if err == nil && len ( contents ) > 0 {
version = contents [ 0 ]
}
} else if common . PathExists ( common . HostEtc ( "os-release" ) ) {
p , v , err := getOSRelease ( )
if err == nil {
platform = p
version = v
}
} else if lsb . ID == "RedHat" {
platform = "redhat"
version = lsb . Release
@ -298,6 +331,10 @@ func PlatformInformation() (platform string, family string, version string, err
family = "arch"
case "exherbo" :
family = "exherbo"
case "alpine" :
family = "alpine"
case "coreos" :
family = "coreos"
}
return platform , family , version , nil
@ -359,7 +396,7 @@ func Virtualization() (string, string, error) {
if common . PathExists ( filename + "/capabilities" ) {
contents , err := common . ReadLines ( filename + "/capabilities" )
if err == nil {
if common . Strings Ha s( contents , "control_d" ) {
if common . Strings Contain s( contents , "control_d" ) {
role = "host"
}
}
@ -387,9 +424,9 @@ func Virtualization() (string, string, error) {
if common . PathExists ( filename ) {
contents , err := common . ReadLines ( filename )
if err == nil {
if common . Strings Ha s( contents , "QEMU Virtual CPU" ) ||
common . Strings Ha s( contents , "Common KVM processor" ) ||
common . Strings Ha s( contents , "Common 32-bit KVM processor" ) {
if common . Strings Contain s( contents , "QEMU Virtual CPU" ) ||
common . Strings Contain s( contents , "Common KVM processor" ) ||
common . Strings Contain s( contents , "Common 32-bit KVM processor" ) {
system = "kvm"
role = "guest"
}
@ -410,8 +447,8 @@ func Virtualization() (string, string, error) {
contents , err := common . ReadLines ( filename + "/self/status" )
if err == nil {
if common . Strings Ha s( contents , "s_context:" ) ||
common . Strings Ha s( contents , "VxID:" ) {
if common . Strings Contain s( contents , "s_context:" ) ||
common . Strings Contain s( contents , "VxID:" ) {
system = "linux-vserver"
}
// TODO: guest or host
@ -421,16 +458,28 @@ func Virtualization() (string, string, error) {
if common . PathExists ( filename + "/self/cgroup" ) {
contents , err := common . ReadLines ( filename + "/self/cgroup" )
if err == nil {
if common . StringsHas ( contents , "lxc" ) ||
common . StringsHas ( contents , "docker" ) {
if common . StringsContains ( contents , "lxc" ) {
system = "lxc"
role = "guest"
} else if common . PathExists ( "/usr/bin/lxc-version" ) { // TODO: which
} else if common . StringsContains ( contents , "docker" ) {
system = "docker"
role = "guest"
} else if common . StringsContains ( contents , "machine-rkt" ) {
system = "rkt"
role = "guest"
} else if common . PathExists ( "/usr/bin/lxc-version" ) {
system = "lxc"
role = "host"
}
}
}
if common . PathExists ( common . HostEtc ( "os-release" ) ) {
p , _ , err := getOSRelease ( )
if err == nil && p == "coreos" {
system = "rkt" // Is it true?
role = "host"
}
}
return system , role , nil
}