Merge branch 'master' of github.com:shirou/gopsutil

pull/4/head
Shirou WAKAYAMA 11 years ago
commit 41d2beb271

@ -3,7 +3,6 @@ package gopsutil
import (
"fmt"
"testing"
)
func TestCpu_times(t *testing.T) {

@ -3,9 +3,9 @@
package gopsutil
import (
"errors"
"syscall"
"unsafe"
"errors"
)
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
@ -17,7 +17,7 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
return ret, err
}
fs := make([]Statfs, 0, count)
fs := make([]Statfs, count)
_, err = Getfsstat(fs, MNT_WAIT)
for _, stat := range fs {
@ -83,8 +83,33 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
}
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
return nil, errors.New("not implemented yet")
// statinfo->devinfo->devstat
// /usr/include/devinfo.h
// get length
count, err := Getfsstat(nil, MNT_WAIT)
if err != nil {
return nil, err
}
fs := make([]Statfs, count)
_, err = Getfsstat(fs, MNT_WAIT)
ret := make(map[string]DiskIOCountersStat, 0)
return ret, errors.New("not implemented yet")
for _, stat := range fs {
name := byteToString(stat.FMntonname[:])
d := DiskIOCountersStat{
Name: name,
ReadCount: stat.FSyncwrites + stat.FAsyncwrites,
WriteCount: stat.FSyncreads + stat.FAsyncreads,
}
ret[name] = d
}
return ret, nil
}
// Getfsstat is borrowed from pkg/syscall/syscall_freebsd.go

@ -53,3 +53,54 @@ type Statfs struct {
FMntfromname [MNAMELEN]byte /* mounted filesystem */
FMntonname [MNAMELEN]byte /* directory on which mounted */
}
// /usr/include/devstat.h
// devstat_getdevs()
// kern.devstat.all -> devstats list struct
// struct devinfo {
// struct devstat *devices;
// u_int8_t *mem_ptr;
// long generation;
// int numdevs;
// };
//
// struct statinfo {
// long cp_time[CPUSTATES];
// long tk_nin;
// long tk_nout;
// struct devinfo *dinfo;
// long double snap_time;
// };
// /usr/include/devinfo.h
// struct devinfo_dev {
// devinfo_handle_t dd_handle; /* device handle */
// devinfo_handle_t dd_parent; /* parent handle */
// char *dd_name; /* name of device */
// char *dd_desc; /* device description */
// char *dd_drivername; /* name of attached driver */
// char *dd_pnpinfo; /* pnp info from parent bus */
// char *dd_location; /* Where bus thinks dev at */
// uint32_t dd_devflags; /* API flags */
// uint16_t dd_flags; /* internal dev flags */
// device_state_t dd_state; /* attachment state of dev */
// };
//
// struct devinfo_rman {
// devinfo_handle_t dm_handle; /* resource manager handle */
// u_long dm_start; /* resource start */
// u_long dm_size; /* resource size */
// char *dm_desc; /* resource description */
// };
//
// struct devinfo_res {
// devinfo_handle_t dr_handle; /* resource handle */
// devinfo_handle_t dr_rman; /* resource manager handle */
// devinfo_handle_t dr_device; /* owning device */
// u_long dr_start; /* region start */
// u_long dr_size; /* region size */
// };

@ -22,7 +22,7 @@ func TestDisk_usage(t *testing.T) {
func TestDisk_partitions(t *testing.T) {
ret, err := DiskPartitions(false)
if err != nil {
if err != nil || len(ret) == 0 {
t.Errorf("error %v", err)
}
empty := DiskPartitionStat{}
@ -35,7 +35,7 @@ func TestDisk_partitions(t *testing.T) {
func TestDisk_io_counters(t *testing.T) {
ret, err := DiskIOCounters()
if err != nil {
if err != nil || len(ret) == 0 {
t.Errorf("error %v", err)
}
empty := DiskIOCountersStat{}

@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"syscall"
@ -155,19 +156,22 @@ func GetPlatformInformation() (string, string, string, error) {
family := ""
version := ""
lsb, _ := getLSB()
lsb, err := getLSB()
if err != nil {
lsb = &LSB{}
}
if pathExists("/etc/oracle-release") {
platform = "oracle"
contents, err := readLines("/etc/oracle-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
version = getRedhatishVersion(contents)
}
} else if pathExists("/etc/enterprise-release") {
platform = "oracle"
contents, err := readLines("/etc/enterprise-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
version = getRedhatishVersion(contents)
}
} else if pathExists("/etc/debian_version") {
if lsb.ID == "Ubuntu" {
@ -190,20 +194,20 @@ func GetPlatformInformation() (string, string, string, error) {
} else if pathExists("/etc/redhat-release") {
contents, err := readLines("/etc/redhat-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
platform, _ = getRedhatishPlatform(contents)
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if pathExists("/etc/system-release") {
contents, err := readLines("/etc/system-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
platform, _ = getRedhatishPlatform(contents)
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if pathExists("/etc/gentoo-release") {
platform = "gentoo"
contents, err := readLines("/etc/gentoo-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
version = getRedhatishVersion(contents)
}
// TODO: suse detection
// TODO: slackware detecion
@ -250,12 +254,28 @@ func GetPlatformInformation() (string, string, string, error) {
}
func getRedhatishVersion(contents []string) (string, error) {
return "", nil
func getRedhatishVersion(contents []string) string {
c := strings.ToLower(strings.Join(contents, ""))
if strings.Contains(c, "rawhide") {
return "rawhide"
}
if matches := regexp.MustCompile(`release (\d[\d.]*)`).FindStringSubmatch(c); matches != nil {
return matches[1]
} else {
return ""
}
}
func getRedhatishPlatform(contents []string) (string, error) {
return "", nil
func getRedhatishPlatform(contents []string) string {
c := strings.ToLower(strings.Join(contents, ""))
if strings.Contains(c, "red hat") {
return "redhat"
}
f := strings.Split(c, " ")
return f[0]
}
func GetVirtualization() (string, string, error) {

@ -0,0 +1,61 @@
// +build linux
package gopsutil
import (
"testing"
)
func TestGetRedhatishVersion(t *testing.T) {
var ret string
c := []string{"Rawhide"}
ret = getRedhatishVersion(c)
if ret != "rawhide" {
t.Errorf("Could not get version rawhide: %v", ret)
}
c = []string{"Fedora release 15 (Lovelock)"}
ret = getRedhatishVersion(c)
if ret != "15" {
t.Errorf("Could not get version fedora: %v", ret)
}
c = []string{"Enterprise Linux Server release 5.5 (Carthage)"}
ret = getRedhatishVersion(c)
if ret != "5.5" {
t.Errorf("Could not get version redhat enterprise: %v", ret)
}
c = []string{""}
ret = getRedhatishVersion(c)
if ret != "" {
t.Errorf("Could not get version with no value: %v", ret)
}
}
func TestGetRedhatishPlatform(t *testing.T) {
var ret string
c := []string{"red hat"}
ret = getRedhatishPlatform(c)
if ret != "redhat" {
t.Errorf("Could not get platform redhat: %v", ret)
}
c = []string{"Fedora release 15 (Lovelock)"}
ret = getRedhatishPlatform(c)
if ret != "fedora" {
t.Errorf("Could not get platform fedora: %v", ret)
}
c = []string{"Enterprise Linux Server release 5.5 (Carthage)"}
ret = getRedhatishPlatform(c)
if ret != "enterprise" {
t.Errorf("Could not get platform redhat enterprise: %v", ret)
}
c = []string{""}
ret = getRedhatishPlatform(c)
if ret != "" {
t.Errorf("Could not get platform with no value: %v", ret)
}
}

@ -2,6 +2,7 @@ package gopsutil
import (
"encoding/json"
"net"
)
type NetIOCountersStat struct {
@ -16,6 +17,7 @@ type NetIOCountersStat struct {
Dropout uint64 `json:"dropout"` // total number of outgoing packets which were dropped (always 0 on OSX and BSD)
}
// Addr is implemented compatibility to psutil
type Addr struct {
IP string `json:"ip"`
Port uint32 `json:"port"`
@ -31,6 +33,19 @@ type NetConnectionStat struct {
Pid int32 `json:"pid"`
}
// NetInterfaceAddr is designed for represent interface addresses
type NetInterfaceAddr struct {
Addr string `json:"addr"`
}
type NetInterfaceStat struct {
MTU int `json:"mtu"` // maximum transmission unit
Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100"
HardwareAddr string `json:"hardwareaddr"` // IEEE MAC-48, EUI-48 and EUI-64 form
Flags []string `json:"flags"` // e.g., FlagUp, FlagLoopback, FlagMulticast
Addrs []NetInterfaceAddr `json:"addrs"`
}
func (n NetIOCountersStat) String() string {
s, _ := json.Marshal(n)
return string(s)
@ -45,3 +60,60 @@ func (a Addr) String() string {
s, _ := json.Marshal(a)
return string(s)
}
func (n NetInterfaceStat) String() string {
s, _ := json.Marshal(n)
return string(s)
}
func (n NetInterfaceAddr) String() string {
s, _ := json.Marshal(n)
return string(s)
}
func NetInterfaces() ([]NetInterfaceStat, error) {
is, err := net.Interfaces()
if err != nil {
return nil, err
}
ret := make([]NetInterfaceStat, 0, len(is))
for _, ifi := range is {
var flags []string
if ifi.Flags&net.FlagUp != 0 {
flags = append(flags, "up")
}
if ifi.Flags&net.FlagBroadcast != 0 {
flags = append(flags, "broadcast")
}
if ifi.Flags&net.FlagLoopback != 0 {
flags = append(flags, "loopback")
}
if ifi.Flags&net.FlagPointToPoint != 0 {
flags = append(flags, "pointtopoint")
}
if ifi.Flags&net.FlagMulticast != 0 {
flags = append(flags, "multicast")
}
r := NetInterfaceStat{
Name: ifi.Name,
MTU: ifi.MTU,
HardwareAddr: ifi.HardwareAddr.String(),
Flags: flags,
}
addrs, err := ifi.Addrs()
if err == nil {
r.Addrs = make([]NetInterfaceAddr, 0, len(addrs))
for _, addr := range addrs {
r.Addrs = append(r.Addrs, NetInterfaceAddr{
Addr: addr.String(),
})
}
}
ret = append(ret, r)
}
return ret, nil
}

@ -52,3 +52,18 @@ func TestNetIOCounters(t *testing.T) {
}
}
}
func TestNetInterfaces(t *testing.T) {
v, err := NetInterfaces()
if err != nil {
t.Errorf("Could not get NetInterfaceStat: %v", err)
}
if len(v) == 0 {
t.Errorf("Could not get NetInterfaceStat: %v", err)
}
for _, vv := range v {
if vv.Name == "" {
t.Errorf("Invalid NetInterface: %v", vv)
}
}
}

Loading…
Cancel
Save