pull/4/head
Shirou WAKAYAMA 11 years ago
parent 4b7a2b298d
commit 955c9d23c0

@ -9,10 +9,10 @@ import (
type HostInfoStat struct { type HostInfoStat struct {
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
Uptime int64 `json:"uptime"` Uptime int64 `json:"uptime"`
Procs uint64 `json:"procs"` // number of processes Procs uint64 `json:"procs"` // number of processes
OS string `json:"os"` // ex: freebsd, linux OS string `json:"os"` // ex: freebsd, linux
Platform string `json:"platform"` // ex: ubuntu, linuxmint Platform string `json:"platform"` // ex: ubuntu, linuxmint
PlatformFamily string `json:"platformFamily"` // ex: debian, rhel PlatformFamily string `json:"platformFamily"` // ex: debian, rhel
PlatformVersion string `json:"platformVersion"` PlatformVersion string `json:"platformVersion"`
} }

@ -9,9 +9,9 @@ import (
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"strings"
"syscall" "syscall"
"unsafe" "unsafe"
"strings"
) )
type LSB struct { type LSB struct {
@ -33,13 +33,12 @@ func HostInfo() (*HostInfoStat, error) {
} }
platform, family, version, err := getPlatformInformation() platform, family, version, err := getPlatformInformation()
if err == nil{ if err == nil {
ret.Platform = platform ret.Platform = platform
ret.PlatformFamily = family ret.PlatformFamily = family
ret.PlatformVersion = version ret.PlatformVersion = version
} }
return ret, nil return ret, nil
} }
@ -162,36 +161,36 @@ func getPlatformInformation() (string, string, string, error) {
version, _ = getRedhatishVersion(contents) version, _ = getRedhatishVersion(contents)
} }
} else if pathExists("/etc/debian_version") { } else if pathExists("/etc/debian_version") {
if lsb.ID == "Ubuntu"{ if lsb.ID == "Ubuntu" {
platform = "ubuntu" platform = "ubuntu"
version = lsb.Release version = lsb.Release
}else if lsb.ID == "LinuxMint"{ } else if lsb.ID == "LinuxMint" {
platform = "linuxmint" platform = "linuxmint"
version = lsb.Release version = lsb.Release
}else{ } else {
if pathExists("/usr/bin/raspi-config"){ if pathExists("/usr/bin/raspi-config") {
platform = "raspbian" platform = "raspbian"
}else{ } else {
platform = "debian" platform = "debian"
} }
contents, err := readLines("/etc/debian_version") contents, err := readLines("/etc/debian_version")
if err == nil{ if err == nil {
version = contents[0] version = contents[0]
} }
} }
} else if pathExists("/etc/redhat-release"){ } else if pathExists("/etc/redhat-release") {
contents, err := readLines("/etc/redhat-release") contents, err := readLines("/etc/redhat-release")
if err == nil { if err == nil {
version, _ = getRedhatishVersion(contents) version, _ = getRedhatishVersion(contents)
platform, _ = getRedhatishPlatform(contents) platform, _ = getRedhatishPlatform(contents)
} }
} else if pathExists("/etc/system-release"){ } else if pathExists("/etc/system-release") {
contents, err := readLines("/etc/system-release") contents, err := readLines("/etc/system-release")
if err == nil { if err == nil {
version, _ = getRedhatishVersion(contents) version, _ = getRedhatishVersion(contents)
platform, _ = getRedhatishPlatform(contents) platform, _ = getRedhatishPlatform(contents)
} }
} else if pathExists("/etc/gentoo-release"){ } else if pathExists("/etc/gentoo-release") {
platform = "gentoo" platform = "gentoo"
contents, err := readLines("/etc/gentoo-release") contents, err := readLines("/etc/gentoo-release")
if err == nil { if err == nil {
@ -199,27 +198,26 @@ func getPlatformInformation() (string, string, string, error) {
} }
// TODO: suse detection // TODO: suse detection
// TODO: slackware detecion // TODO: slackware detecion
}else if pathExists("/etc/arch-release"){ } else if pathExists("/etc/arch-release") {
platform = "arch" platform = "arch"
// TODO: exherbo detection // TODO: exherbo detection
}else if lsb.ID == "RedHat"{ } else if lsb.ID == "RedHat" {
platform = "redhat" platform = "redhat"
version = lsb.Release version = lsb.Release
}else if lsb.ID == "Amazon"{ } else if lsb.ID == "Amazon" {
platform = "amazon" platform = "amazon"
version = lsb.Release version = lsb.Release
}else if lsb.ID == "ScientificSL"{ } else if lsb.ID == "ScientificSL" {
platform = "scientific" platform = "scientific"
version = lsb.Release version = lsb.Release
}else if lsb.ID == "XenServer"{ } else if lsb.ID == "XenServer" {
platform = "xenserver" platform = "xenserver"
version = lsb.Release version = lsb.Release
}else if lsb.ID != ""{ } else if lsb.ID != "" {
platform = strings.ToLower(lsb.ID) platform = strings.ToLower(lsb.ID)
version = lsb.Release version = lsb.Release
} }
switch platform { switch platform {
case "debian", "ubuntu", "linuxmint", "raspbian": case "debian", "ubuntu", "linuxmint", "raspbian":
family = "debian" family = "debian"

@ -44,7 +44,7 @@ func TestHostInfoStat_String(t *testing.T) {
Hostname: "test", Hostname: "test",
Uptime: 3000, Uptime: 3000,
Procs: 100, Procs: 100,
OS: "linux", OS: "linux",
Platform: "ubuntu", Platform: "ubuntu",
} }
e := `{"hostname":"test","uptime":3000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":""}` e := `{"hostname":"test","uptime":3000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":""}`

Loading…
Cancel
Save