1
0
Fork 0

디스크 추가

This commit is contained in:
Sangbum Kim 2018-07-06 15:01:07 +09:00
parent 6d211fce2c
commit d06269e787
6 changed files with 121 additions and 2 deletions

View File

@ -7,7 +7,7 @@ import (
func main() {
funcs := []func(){
a, b, c, d, e,
a, b, c, d, e, f,
}
for i := 0; i < 1000; i++ {
fmt.Printf("------ %d -------\n", i)

View File

@ -10,7 +10,6 @@ import (
"fmt"
"os"
"time"
"amuz.es/src/infra/goutils/misc"
)
@ -92,3 +91,7 @@ func e() {
fmt.Printf("cpu total: %d\n", ct.Total)
fmt.Printf("cpu user: %d\n", ct.User)
}
func f() {
fmt.Fprintf(os.Stderr, "%s\n", "not supported")
}

View File

@ -6,6 +6,7 @@ import (
"github.com/mackerelio/go-osstat/network"
"github.com/mackerelio/go-osstat/uptime"
"github.com/mackerelio/go-osstat/cpu"
"github.com/mackerelio/go-osstat/disk"
"github.com/hako/durafmt"
"fmt"
@ -104,3 +105,15 @@ func e() {
fmt.Printf("cpu steal: %d\n", ct.Steal)
fmt.Printf("cpu total: %d\n", ct.Total)
}
func f() {
disks, err := disk.Get()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
for _, dsk := range disks {
fmt.Println("disk name: ", dsk.Name)
fmt.Println("disk read: ", misc.FileSizeIEC(dsk.ReadsCompleted))
fmt.Println("disk written: ", misc.FileSizeIEC(dsk.WritesCompleted))
}
}

View File

@ -0,0 +1,58 @@
// +build linux
package disk
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// Get disk I/O statistics
func Get() ([]Stats, error) {
// Reference: Documentation/iostats.txt in the source of Linux
file, err := os.Open("/proc/diskstats")
if err != nil {
return nil, err
}
defer file.Close()
return collectDiskStats(file)
}
// Stats represents disk I/O statistics for linux
type Stats struct {
Name string
ReadsCompleted, WritesCompleted uint64
}
func collectDiskStats(out io.Reader) ([]Stats, error) {
scanner := bufio.NewScanner(out)
var diskStats []Stats
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) < 14 {
continue
}
name := fields[2]
readsCompleted, err := strconv.ParseUint(fields[3], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse reads completed of %s", name)
}
writesCompleted, err := strconv.ParseUint(fields[7], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse writes completed of %s", name)
}
diskStats = append(diskStats, Stats{
Name: name,
ReadsCompleted: readsCompleted,
WritesCompleted: writesCompleted,
})
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("scan error for /proc/diskstats: %s", err)
}
return diskStats, nil
}

View File

@ -0,0 +1,44 @@
// +build linux
package disk
import (
"reflect"
"strings"
"testing"
)
func TestGetDisk(t *testing.T) {
disks, err := Get()
if err != nil {
t.Fatalf("error should be nil but got: %v", err)
}
t.Logf("disks value: %+v", disks)
}
func TestCollectDiskStats(t *testing.T) {
got, err := collectDiskStats(strings.NewReader(
` 202 1 xvda1 750193 3037 28116978 368712 16600606 7233846 424712632 23987908 0 2355636 24345740
202 2 xvda2 1641 9310 87552 1252 6365 3717 80664 24192 0 15040 25428
7 0 loop0 0 0 0 0 0 0 0 0 0 0 0
7 1 loop1 0 0 0 0 0 0 0 0 0 0 0
253 0 dm-0 46095806 0 549095028 2243928 7192424 0 305024576 12521088 0 2728444 14782668
253 628 dm-628 3198 0 75410 1360 30802835 0 3942653176 1334317408 0 70948 1358596768
253 2 dm-2 2022 0 42250 488 30822403 0 3942809696 1364721232 0 93348 1382989868
`))
if err != nil {
t.Fatalf("error should be nil but got: %v", err)
}
expected := []Stats{
{"xvda1", 750193, 16600606},
{"xvda2", 1641, 6365},
{"loop0", 0, 0},
{"loop1", 0, 0},
{"dm-0", 46095806, 7192424},
{"dm-628", 3198, 30802835},
{"dm-2", 2022, 30822403},
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("invalid disk value: %+v (expected: %+v)", got, expected)
}
}

1
vendor/vgo.list vendored
View File

@ -42,6 +42,7 @@ github.com/lestrrat-go/file-rotatelogs/internal/option
github.com/lestrrat-go/strftime
# github.com/mackerelio/go-osstat v0.0.0-20180312130411-192e3c5eacaf
github.com/mackerelio/go-osstat/cpu
github.com/mackerelio/go-osstat/disk
github.com/mackerelio/go-osstat/loadavg
github.com/mackerelio/go-osstat/memory
github.com/mackerelio/go-osstat/network