implements String() which return as JSON to every structs.

pull/4/head
Shirou WAKAYAMA 11 years ago
parent 02a91378b8
commit 8098298111

@ -26,7 +26,6 @@ Usage
:: ::
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/shirou/gopsutil" "github.com/shirou/gopsutil"
@ -35,12 +34,11 @@ Usage
func main() { func main() {
v, _ := gopsutil.VirtualMemory() v, _ := gopsutil.VirtualMemory()
// return value is struct // almost every return value is struct
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent) fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
// convert to JSON // convert to JSON. String() is also implemented
d, _ := json.Marshal(v) fmt.Println(d)
fmt.Printf("%s\n", d)
} }
The output is below. The output is below.

@ -1,6 +1,7 @@
package gopsutil package gopsutil
import ( import (
"encoding/json"
"runtime" "runtime"
) )
@ -22,3 +23,8 @@ type CPUTimesStat struct {
func CPUCounts(logical bool) (int, error) { func CPUCounts(logical bool) (int, error) {
return runtime.NumCPU(), nil return runtime.NumCPU(), nil
} }
func (c CPUTimesStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}

@ -1,5 +1,9 @@
package gopsutil package gopsutil
import (
"encoding/json"
)
type DiskUsageStat struct { type DiskUsageStat struct {
Path string `json:"path"` Path string `json:"path"`
Total uint64 `json:"total"` Total uint64 `json:"total"`
@ -24,3 +28,18 @@ type DiskIOCountersStat struct {
WriteTime uint64 `json:"writeTime"` WriteTime uint64 `json:"writeTime"`
Name string `json:"name"` Name string `json:"name"`
} }
func (d DiskUsageStat) String() string {
s, _ := json.Marshal(d)
return string(s)
}
func (d DiskPartitionStat) String() string {
s, _ := json.Marshal(d)
return string(s)
}
func (d DiskIOCountersStat) String() string {
s, _ := json.Marshal(d)
return string(s)
}

@ -1,7 +1,7 @@
package gopsutil package gopsutil
import ( import (
// "fmt" "fmt"
"runtime" "runtime"
"testing" "testing"
) )
@ -11,12 +11,11 @@ func TestDisk_usage(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
path = "C:" path = "C:"
} }
_, err := DiskUsage(path) v, err := DiskUsage(path)
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
// d, _ := json.Marshal(v) fmt.Println(v)
// fmt.Printf("%s\n", d)
} }
func TestDisk_partitions(t *testing.T) { func TestDisk_partitions(t *testing.T) {

@ -1,5 +1,9 @@
package gopsutil package gopsutil
import (
"encoding/json"
)
// A HostInfoStat describes the host status. // A HostInfoStat describes the host status.
// This is not in the psutil but it useful. // This is not in the psutil but it useful.
type HostInfoStat struct { type HostInfoStat struct {
@ -14,3 +18,13 @@ type UserStat struct {
Host string `json:"host"` Host string `json:"host"`
Started int `json:"started"` Started int `json:"started"`
} }
func (h HostInfoStat) String() string {
s, _ := json.Marshal(h)
return string(s)
}
func (u UserStat) String() string {
s, _ := json.Marshal(u)
return string(s)
}

@ -1,7 +1,16 @@
package gopsutil package gopsutil
import (
"encoding/json"
)
type LoadAvgStat struct { type LoadAvgStat struct {
Load1 float64 `json:"load1"` Load1 float64 `json:"load1"`
Load5 float64 `json:"load5"` Load5 float64 `json:"load5"`
Load15 float64 `json:"load15"` Load15 float64 `json:"load15"`
} }
func (l LoadAvgStat) String() string {
s, _ := json.Marshal(l)
return string(s)
}

@ -1,5 +1,9 @@
package gopsutil package gopsutil
import (
"encoding/json"
)
type VirtualMemoryStat struct { type VirtualMemoryStat struct {
Total uint64 `json:"total"` Total uint64 `json:"total"`
Available uint64 `json:"available"` Available uint64 `json:"available"`
@ -22,3 +26,13 @@ type SwapMemoryStat struct {
Sin uint64 `json:"sin"` Sin uint64 `json:"sin"`
Sout uint64 `json:"sout"` Sout uint64 `json:"sout"`
} }
func (m VirtualMemoryStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}
func (m SwapMemoryStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}

@ -1,7 +1,6 @@
package gopsutil package gopsutil
import ( import (
"encoding/json"
"fmt" "fmt"
"testing" "testing"
) )
@ -11,8 +10,7 @@ func TestVirtual_memory(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
d, _ := json.Marshal(v) fmt.Println(v)
fmt.Printf("%s\n", d)
} }
func TestSwap_memory(t *testing.T) { func TestSwap_memory(t *testing.T) {
@ -20,6 +18,5 @@ func TestSwap_memory(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
d, _ := json.Marshal(v) fmt.Println(v)
fmt.Printf("%s\n", d)
} }

@ -1,5 +1,9 @@
package gopsutil package gopsutil
import (
"encoding/json"
)
type NetIOCountersStat struct { type NetIOCountersStat struct {
Name string `json:"name"` // interface name Name string `json:"name"` // interface name
BytesSent uint64 `json:"bytes_sent"` // number of bytes sent BytesSent uint64 `json:"bytes_sent"` // number of bytes sent
@ -26,3 +30,18 @@ type NetConnectionStat struct {
Status string `json:"status"` Status string `json:"status"`
Pid int32 `json:"pid"` Pid int32 `json:"pid"`
} }
func (n NetConnectionStat) String() string {
s, _ := json.Marshal(n)
return string(s)
}
func (n NetIOCountersStat) String() string {
s, _ := json.Marshal(n)
return string(s)
}
func (a Addr) String() string {
s, _ := json.Marshal(a)
return string(s)
}

@ -1,5 +1,9 @@
package gopsutil package gopsutil
import (
"encoding/json"
)
type Process struct { type Process struct {
Pid int32 `json:"pid"` Pid int32 `json:"pid"`
} }
@ -27,6 +31,31 @@ type IOCountersStat struct {
WriteBytes int32 `json:"write_bytes"` WriteBytes int32 `json:"write_bytes"`
} }
func (p Process) String() string {
s, _ := json.Marshal(p)
return string(s)
}
func (o OpenFilesStat) String() string {
s, _ := json.Marshal(o)
return string(s)
}
func (m MemoryInfoStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}
func (r RlimitStat) String() string {
s, _ := json.Marshal(r)
return string(s)
}
func (i IOCountersStat) String() string {
s, _ := json.Marshal(i)
return string(s)
}
func PidExists(pid int32) (bool, error) { func PidExists(pid int32) (bool, error) {
pids, err := Pids() pids, err := Pids()
if err != nil { if err != nil {

@ -1,7 +1,6 @@
package gopsutil package gopsutil
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
@ -54,8 +53,7 @@ func Test_NewProcess(t *testing.T) {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
d, _ := json.Marshal(ret) fmt.Println(ret)
fmt.Println(string(d))
} }
func Test_Process_memory_maps(t *testing.T) { func Test_Process_memory_maps(t *testing.T) {

Loading…
Cancel
Save