Update vendoring via vndr

This commit is contained in:
Alex Ellis
2017-07-12 17:58:36 +01:00
parent e1849a8f49
commit 283d311b08
6506 changed files with 25916 additions and 1506849 deletions

View File

@ -1,5 +0,0 @@
sudo: false
language: go
go:
- 1.6.4
- 1.7.4

View File

@ -1,18 +0,0 @@
# Contributing
Prometheus uses GitHub to manage reviews of pull requests.
* If you have a trivial fix or improvement, go ahead and create a pull request,
addressing (with `@...`) the maintainer of this repository (see
[MAINTAINERS.md](MAINTAINERS.md)) in the description of the pull request.
* If you plan to do something more involved, first discuss your ideas
on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers).
This will avoid unnecessary work and surely give you and us a good deal
of inspiration.
* Relevant coding style guidelines are the [Go Code Review
Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments)
and the _Formatting and style_ section of Peter Bourgon's [Go: Best
Practices for Production
Environments](http://peter.bourgon.org/go-in-production/#formatting-and-style).

View File

@ -1 +0,0 @@
* Tobias Schmidt <tobidt@gmail.com>

View File

@ -1,6 +0,0 @@
ci:
! gofmt -l *.go | read nothing
go vet
go test -v ./...
go get github.com/golang/lint/golint
golint *.go

View File

@ -1,64 +0,0 @@
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package procfs
import (
"strings"
"testing"
)
func TestBuddyInfo(t *testing.T) {
buddyInfo, err := FS("fixtures/buddyinfo/valid").NewBuddyInfo()
if err != nil {
t.Fatal(err)
}
if want, got := "DMA", buddyInfo[0].Zone; want != got {
t.Errorf("want Node 0, Zone %s, got %s", want, got)
}
if want, got := "Normal", buddyInfo[2].Zone; want != got {
t.Errorf("want Node 0, Zone %s, got %s", want, got)
}
if want, got := 4381.0, buddyInfo[2].Sizes[0]; want != got {
t.Errorf("want Node 0, Zone Normal %f, got %f", want, got)
}
if want, got := 572.0, buddyInfo[1].Sizes[1]; want != got {
t.Errorf("want Node 0, Zone DMA32 %f, got %f", want, got)
}
}
func TestBuddyInfoShort(t *testing.T) {
_, err := FS("fixtures/buddyinfo/short").NewBuddyInfo()
if err == nil {
t.Errorf("expected error, but none occurred")
}
if want, got := "invalid number of fields when parsing buddyinfo", err.Error(); want != got {
t.Errorf("wrong error returned, wanted %q, got %q", want, got)
}
}
func TestBuddyInfoSizeMismatch(t *testing.T) {
_, err := FS("fixtures/buddyinfo/sizemismatch").NewBuddyInfo()
if err == nil {
t.Errorf("expected error, but none occurred")
}
if want, got := "mismatch in number of buddyinfo buckets", err.Error(); !strings.HasPrefix(got, want) {
t.Errorf("wrong error returned, wanted prefix %q, got %q", want, got)
}
}

View File

@ -1 +0,0 @@
vim

View File

@ -1 +0,0 @@
/usr/bin/vim

View File

@ -1 +0,0 @@
../../symlinktargets/abc

View File

@ -1 +0,0 @@
../../symlinktargets/def

View File

@ -1 +0,0 @@
../../symlinktargets/xyz

View File

@ -1 +0,0 @@
../../symlinktargets/ghi

View File

@ -1 +0,0 @@
../../symlinktargets/uvw

View File

@ -1,7 +0,0 @@
rchar: 750339
wchar: 818609
syscr: 7405
syscw: 5245
read_bytes: 1024
write_bytes: 2048
cancelled_write_bytes: -1024

View File

@ -1,17 +0,0 @@
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 62898 62898 processes
Max open files 2048 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 62898 62898 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us

View File

@ -1,19 +0,0 @@
device rootfs mounted on / with fstype rootfs
device sysfs mounted on /sys with fstype sysfs
device proc mounted on /proc with fstype proc
device /dev/sda1 mounted on / with fstype ext4
device 192.168.1.1:/srv/test mounted on /mnt/nfs/test with fstype nfs4 statvers=1.1
opts: rw,vers=4.0,rsize=1048576,wsize=1048576,namlen=255,acregmin=3,acregmax=60,acdirmin=30,acdirmax=60,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.5,local_lock=none
age: 13968
caps: caps=0xfff7,wtmult=512,dtsize=32768,bsize=0,namlen=255
nfsv4: bm0=0xfdffafff,bm1=0xf9be3e,bm2=0x0,acl=0x0,pnfs=not configured
sec: flavor=1,pseudoflavor=1
events: 52 226 0 0 1 13 398 0 0 331 0 47 0 0 77 0 0 77 0 0 0 0 0 0 0 0 0
bytes: 1207640230 0 0 0 1210214218 0 295483 0
RPC iostats version: 1.0 p/v: 100003/4 (nfs)
xprt: tcp 832 0 1 0 11 6428 6428 0 12154 0 24 26 5726
per-op statistics
NULL: 0 0 0 0 0 0 0 0
READ: 1298 1298 0 207680 1210292152 6 79386 79407
WRITE: 0 0 0 0 0 0 0 0

View File

@ -1 +0,0 @@
26231 (vim) R 5392 7446 5392 34835 7446 4218880 32533 309516 26 82 1677 44 158 99 20 0 1 0 82375 56274944 1981 18446744073709551615 4194304 6294284 140736914091744 140736914087944 139965136429984 0 0 12288 1870679807 0 0 0 17 0 0 0 31 0 0 8391624 8481048 16420864 140736914093252 140736914093279 140736914093279 140736914096107 0

View File

@ -1 +0,0 @@
ata_sff

View File

@ -1 +0,0 @@
../../symlinktargets/abc

View File

@ -1 +0,0 @@
../../symlinktargets/def

View File

@ -1 +0,0 @@
../../symlinktargets/ghi

View File

@ -1 +0,0 @@
../../symlinktargets/uvw

View File

@ -1 +0,0 @@
../../symlinktargets/xyz

View File

@ -1,17 +0,0 @@
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 29436 29436 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 29436 29436 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us

View File

@ -1 +0,0 @@
33 (ata_sff) S 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 0 -20 1 0 5 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744073709551615 0 0 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0

View File

@ -1,2 +0,0 @@
1020 ((a b ) ( c d) ) R 28378 1020 28378 34842 1020 4218880 286 0 0 0 0 0 0 0 20 0 1 0 10839175 10395648 155 18446744073709551615 4194304 4238788 140736466511168 140736466511168 140609271124624 0 0 0 0 0 0 0 17 5 0 0 0 0 0 6336016 6337300 25579520 140736466515030 140736466515061 140736466515061 140736466518002 0
#!/bin/cat /proc/self/stat

View File

@ -1,3 +0,0 @@
Node 0, zone
Node 0, zone
Node 0, zone

View File

@ -1,3 +0,0 @@
Node 0, zone DMA 1 0 1 0 2 1 1 0 1 1 3
Node 0, zone DMA32 759 572 791 475 194 45 12 0 0 0 0 0
Node 0, zone Normal 4381 1093 185 1530 567 102 4 0 0 0

View File

@ -1,3 +0,0 @@
Node 0, zone DMA 1 0 1 0 2 1 1 0 1 1 3
Node 0, zone DMA32 759 572 791 475 194 45 12 0 0 0 0
Node 0, zone Normal 4381 1093 185 1530 567 102 4 0 0 0 0

View File

@ -1,23 +0,0 @@
extent_alloc 92447 97589 92448 93751
abt 0 0 0 0
blk_map 1767055 188820 184891 92447 92448 2140766 0
bmbt 0 0 0 0
dir 185039 92447 92444 136422
trans 706 944304 0
ig 185045 58807 0 126238 0 33637 22
log 2883 113448 9 17360 739
push_ail 945014 0 134260 15483 0 3940 464 159985 0 40
xstrat 92447 0
rw 107739 94045
attr 4 0 0 0
icluster 8677 7849 135802
vnodes 92601 0 0 0 92444 92444 92444 0
buf 2666287 7122 2659202 3599 2 7085 0 10297 7085
abtb2 184941 1277345 13257 13278 0 0 0 0 0 0 0 0 0 0 2746147
abtc2 345295 2416764 172637 172658 0 0 0 0 0 0 0 0 0 0 21406023
bmbt2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ibt2 343004 1358467 0 0 0 0 0 0 0 0 0 0 0 0 0
fibt2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
qm 0 0 0 0 0 0 0 0
xpc 399724544 92823103 86219234
debug 0

View File

@ -1,26 +0,0 @@
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
md3 : active raid6 sda1[8] sdh1[7] sdg1[6] sdf1[5] sde1[11] sdd1[3] sdc1[10] sdb1[9]
5853468288 blocks super 1.2 level 6, 64k chunk, algorithm 2 [8/8] [UUUUUUUU]
md127 : active raid1 sdi2[0] sdj2[1]
312319552 blocks [2/2] [UU]
md0 : active raid1 sdk[2](S) sdi1[0] sdj1[1]
248896 blocks [2/2] [UU]
md4 : inactive raid1 sda3[0] sdb3[1]
4883648 blocks [2/2] [UU]
md6 : active raid1 sdb2[2] sda2[0]
195310144 blocks [2/1] [U_]
[=>...................] recovery = 8.5% (16775552/195310144) finish=17.0min speed=259783K/sec
md8 : active raid1 sdb1[1] sda1[0]
195310144 blocks [2/2] [UU]
[=>...................] resync = 8.5% (16775552/195310144) finish=17.0min speed=259783K/sec
md7 : active raid6 sdb1[0] sde1[3] sdd1[2] sdc1[1]
7813735424 blocks super 1.2 level 6, 512k chunk, algorithm 2 [4/3] [U_UU]
bitmap: 0/30 pages [0KB], 65536KB chunk
unused devices: <none>

View File

@ -1,14 +0,0 @@
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP C0A80016:0CEA wlc
-> C0A85216:0CEA Tunnel 100 248 2
-> C0A85318:0CEA Tunnel 100 248 2
-> C0A85315:0CEA Tunnel 100 248 1
TCP C0A80039:0CEA wlc
-> C0A85416:0CEA Tunnel 0 0 0
-> C0A85215:0CEA Tunnel 100 1499 0
-> C0A83215:0CEA Tunnel 100 1498 0
TCP C0A80037:0CEA wlc
-> C0A8321A:0CEA Tunnel 0 0 0
-> C0A83120:0CEA Tunnel 100 0 0

View File

@ -1,6 +0,0 @@
Total Incoming Outgoing Incoming Outgoing
Conns Packets Packets Bytes Bytes
16AA370 E33656E5 0 51D8C8883AB3 0
Conns/s Pkts/s Pkts/s Bytes/s Bytes/s
4 1FB3C 0 1282A8F 0

View File

@ -1 +0,0 @@
26231

View File

@ -1,16 +0,0 @@
cpu 301854 612 111922 8979004 3552 2 3944 0 0 0
cpu0 44490 19 21045 1087069 220 1 3410 0 0 0
cpu1 47869 23 16474 1110787 591 0 46 0 0 0
cpu2 46504 36 15916 1112321 441 0 326 0 0 0
cpu3 47054 102 15683 1113230 533 0 60 0 0 0
cpu4 28413 25 10776 1140321 217 0 8 0 0 0
cpu5 29271 101 11586 1136270 672 0 30 0 0 0
cpu6 29152 36 10276 1139721 319 0 29 0 0 0
cpu7 29098 268 10164 1139282 555 0 31 0 0 0
intr 8885917 17 0 0 0 0 0 0 0 1 79281 0 0 0 0 0 0 0 231237 0 0 0 0 250586 103 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 223424 190745 13 906 1283803 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 38014093
btime 1418183276
processes 26442
procs_running 2
procs_blocked 0
softirq 5057579 250191 1481983 1647 211099 186066 0 1783454 622196 12499 508444

View File

@ -1,2 +0,0 @@
This directory contains some empty files that are the symlinks the files in the "fd" directory point to.
They are otherwise ignored by the tests

View File

@ -1,26 +0,0 @@
package procfs
import "testing"
func TestNewFS(t *testing.T) {
if _, err := NewFS("foobar"); err == nil {
t.Error("want NewFS to fail for non-existing mount point")
}
if _, err := NewFS("procfs.go"); err == nil {
t.Error("want NewFS to fail if mount point is not a directory")
}
}
func TestFSXFSStats(t *testing.T) {
stats, err := FS("fixtures").XFSStats()
if err != nil {
t.Fatalf("failed to parse XFS stats: %v", err)
}
// Very lightweight test just to sanity check the path used
// to open XFS stats. Heavier tests in package xfs.
if want, got := uint32(92447), stats.ExtentAllocation.ExtentsAllocated; want != got {
t.Errorf("unexpected extents allocated:\nwant: %d\nhave: %d", want, got)
}
}

View File

@ -33,6 +33,8 @@ type IPVSBackendStatus struct {
LocalAddress net.IP
// The local (virtual) port.
LocalPort uint16
// The local firewall mark
LocalMark string
// The transport protocol (TCP, UDP).
Proto string
// The remote (real) IP address.
@ -142,6 +144,7 @@ func parseIPVSBackendStatus(file io.Reader) ([]IPVSBackendStatus, error) {
status []IPVSBackendStatus
scanner = bufio.NewScanner(file)
proto string
localMark string
localAddress net.IP
localPort uint16
err error
@ -160,10 +163,19 @@ func parseIPVSBackendStatus(file io.Reader) ([]IPVSBackendStatus, error) {
continue
}
proto = fields[0]
localMark = ""
localAddress, localPort, err = parseIPPort(fields[1])
if err != nil {
return nil, err
}
case fields[0] == "FWM":
if len(fields) < 2 {
continue
}
proto = fields[0]
localMark = fields[1]
localAddress = nil
localPort = 0
case fields[0] == "->":
if len(fields) < 6 {
continue
@ -187,6 +199,7 @@ func parseIPVSBackendStatus(file io.Reader) ([]IPVSBackendStatus, error) {
status = append(status, IPVSBackendStatus{
LocalAddress: localAddress,
LocalPort: localPort,
LocalMark: localMark,
RemoteAddress: remoteAddress,
RemotePort: remotePort,
Proto: proto,
@ -200,22 +213,31 @@ func parseIPVSBackendStatus(file io.Reader) ([]IPVSBackendStatus, error) {
}
func parseIPPort(s string) (net.IP, uint16, error) {
tmp := strings.SplitN(s, ":", 2)
var (
ip net.IP
err error
)
if len(tmp) != 2 {
return nil, 0, fmt.Errorf("invalid IP:Port: %s", s)
switch len(s) {
case 13:
ip, err = hex.DecodeString(s[0:8])
if err != nil {
return nil, 0, err
}
case 46:
ip = net.ParseIP(s[1:40])
if ip == nil {
return nil, 0, fmt.Errorf("invalid IPv6 address: %s", s[1:40])
}
default:
return nil, 0, fmt.Errorf("unexpected IP:Port: %s", s)
}
if len(tmp[0]) != 8 && len(tmp[0]) != 32 {
return nil, 0, fmt.Errorf("invalid IP: %s", tmp[0])
portString := s[len(s)-4:]
if len(portString) != 4 {
return nil, 0, fmt.Errorf("unexpected port string format: %s", portString)
}
ip, err := hex.DecodeString(tmp[0])
if err != nil {
return nil, 0, err
}
port, err := strconv.ParseUint(tmp[1], 16, 16)
port, err := strconv.ParseUint(portString, 16, 16)
if err != nil {
return nil, 0, err
}

View File

@ -1,190 +0,0 @@
package procfs
import (
"net"
"testing"
)
var (
expectedIPVSStats = IPVSStats{
Connections: 23765872,
IncomingPackets: 3811989221,
OutgoingPackets: 0,
IncomingBytes: 89991519156915,
OutgoingBytes: 0,
}
expectedIPVSBackendStatuses = []IPVSBackendStatus{
{
LocalAddress: net.ParseIP("192.168.0.22"),
LocalPort: 3306,
RemoteAddress: net.ParseIP("192.168.82.22"),
RemotePort: 3306,
Proto: "TCP",
Weight: 100,
ActiveConn: 248,
InactConn: 2,
},
{
LocalAddress: net.ParseIP("192.168.0.22"),
LocalPort: 3306,
RemoteAddress: net.ParseIP("192.168.83.24"),
RemotePort: 3306,
Proto: "TCP",
Weight: 100,
ActiveConn: 248,
InactConn: 2,
},
{
LocalAddress: net.ParseIP("192.168.0.22"),
LocalPort: 3306,
RemoteAddress: net.ParseIP("192.168.83.21"),
RemotePort: 3306,
Proto: "TCP",
Weight: 100,
ActiveConn: 248,
InactConn: 1,
},
{
LocalAddress: net.ParseIP("192.168.0.57"),
LocalPort: 3306,
RemoteAddress: net.ParseIP("192.168.84.22"),
RemotePort: 3306,
Proto: "TCP",
Weight: 0,
ActiveConn: 0,
InactConn: 0,
},
{
LocalAddress: net.ParseIP("192.168.0.57"),
LocalPort: 3306,
RemoteAddress: net.ParseIP("192.168.82.21"),
RemotePort: 3306,
Proto: "TCP",
Weight: 100,
ActiveConn: 1499,
InactConn: 0,
},
{
LocalAddress: net.ParseIP("192.168.0.57"),
LocalPort: 3306,
RemoteAddress: net.ParseIP("192.168.50.21"),
RemotePort: 3306,
Proto: "TCP",
Weight: 100,
ActiveConn: 1498,
InactConn: 0,
},
{
LocalAddress: net.ParseIP("192.168.0.55"),
LocalPort: 3306,
RemoteAddress: net.ParseIP("192.168.50.26"),
RemotePort: 3306,
Proto: "TCP",
Weight: 0,
ActiveConn: 0,
InactConn: 0,
},
{
LocalAddress: net.ParseIP("192.168.0.55"),
LocalPort: 3306,
RemoteAddress: net.ParseIP("192.168.49.32"),
RemotePort: 3306,
Proto: "TCP",
Weight: 100,
ActiveConn: 0,
InactConn: 0,
},
}
)
func TestIPVSStats(t *testing.T) {
stats, err := FS("fixtures").NewIPVSStats()
if err != nil {
t.Fatal(err)
}
if stats != expectedIPVSStats {
t.Errorf("want %+v, have %+v", expectedIPVSStats, stats)
}
}
func TestParseIPPort(t *testing.T) {
ip := net.ParseIP("192.168.0.22")
port := uint16(3306)
gotIP, gotPort, err := parseIPPort("C0A80016:0CEA")
if err != nil {
t.Fatal(err)
}
if !(gotIP.Equal(ip) && port == gotPort) {
t.Errorf("want %s:%d, have %s:%d", ip, port, gotIP, gotPort)
}
}
func TestParseIPPortInvalid(t *testing.T) {
testcases := []string{
"",
"C0A80016",
"C0A800:1234",
"FOOBARBA:1234",
"C0A80016:0CEA:1234",
}
for _, s := range testcases {
ip, port, err := parseIPPort(s)
if ip != nil || port != uint16(0) || err == nil {
t.Errorf("Expected error for input %s, have ip = %s, port = %v, err = %v", s, ip, port, err)
}
}
}
func TestParseIPPortIPv6(t *testing.T) {
ip := net.ParseIP("dead:beef::1")
port := uint16(8080)
gotIP, gotPort, err := parseIPPort("DEADBEEF000000000000000000000001:1F90")
if err != nil {
t.Fatal(err)
}
if !(gotIP.Equal(ip) && port == gotPort) {
t.Errorf("want %s:%d, have %s:%d", ip, port, gotIP, gotPort)
}
}
func TestIPVSBackendStatus(t *testing.T) {
backendStats, err := FS("fixtures").NewIPVSBackendStatus()
if err != nil {
t.Fatal(err)
}
if want, have := len(expectedIPVSBackendStatuses), len(backendStats); want != have {
t.Fatalf("want %d backend statuses, have %d", want, have)
}
for idx, expect := range expectedIPVSBackendStatuses {
if !backendStats[idx].LocalAddress.Equal(expect.LocalAddress) {
t.Errorf("want LocalAddress %s, have %s", expect.LocalAddress, backendStats[idx].LocalAddress)
}
if backendStats[idx].LocalPort != expect.LocalPort {
t.Errorf("want LocalPort %d, have %d", expect.LocalPort, backendStats[idx].LocalPort)
}
if !backendStats[idx].RemoteAddress.Equal(expect.RemoteAddress) {
t.Errorf("want RemoteAddress %s, have %s", expect.RemoteAddress, backendStats[idx].RemoteAddress)
}
if backendStats[idx].RemotePort != expect.RemotePort {
t.Errorf("want RemotePort %d, have %d", expect.RemotePort, backendStats[idx].RemotePort)
}
if backendStats[idx].Proto != expect.Proto {
t.Errorf("want Proto %s, have %s", expect.Proto, backendStats[idx].Proto)
}
if backendStats[idx].Weight != expect.Weight {
t.Errorf("want Weight %d, have %d", expect.Weight, backendStats[idx].Weight)
}
if backendStats[idx].ActiveConn != expect.ActiveConn {
t.Errorf("want ActiveConn %d, have %d", expect.ActiveConn, backendStats[idx].ActiveConn)
}
if backendStats[idx].InactConn != expect.InactConn {
t.Errorf("want InactConn %d, have %d", expect.InactConn, backendStats[idx].InactConn)
}
}
}

View File

@ -1,31 +0,0 @@
package procfs
import (
"testing"
)
func TestMDStat(t *testing.T) {
mdStates, err := FS("fixtures").ParseMDStat()
if err != nil {
t.Fatalf("parsing of reference-file failed entirely: %s", err)
}
refs := map[string]MDStat{
"md3": {"md3", "active", 8, 8, 5853468288, 5853468288},
"md127": {"md127", "active", 2, 2, 312319552, 312319552},
"md0": {"md0", "active", 2, 2, 248896, 248896},
"md4": {"md4", "inactive", 2, 2, 4883648, 4883648},
"md6": {"md6", "active", 1, 2, 195310144, 16775552},
"md8": {"md8", "active", 2, 2, 195310144, 16775552},
"md7": {"md7", "active", 3, 4, 7813735424, 7813735424},
}
if want, have := len(refs), len(mdStates); want != have {
t.Errorf("want %d parsed md-devices, have %d", want, have)
}
for _, md := range mdStates {
if want, have := refs[md.Name], md; want != have {
t.Errorf("%s: want %v, have %v", md.Name, want, have)
}
}
}

View File

@ -1,273 +0,0 @@
package procfs
import (
"fmt"
"reflect"
"strings"
"testing"
"time"
)
func TestMountStats(t *testing.T) {
tests := []struct {
name string
s string
mounts []*Mount
invalid bool
}{
{
name: "no devices",
s: `hello`,
},
{
name: "device has too few fields",
s: `device foo`,
invalid: true,
},
{
name: "device incorrect format",
s: `device rootfs BAD on / with fstype rootfs`,
invalid: true,
},
{
name: "device incorrect format",
s: `device rootfs mounted BAD / with fstype rootfs`,
invalid: true,
},
{
name: "device incorrect format",
s: `device rootfs mounted on / BAD fstype rootfs`,
invalid: true,
},
{
name: "device incorrect format",
s: `device rootfs mounted on / with BAD rootfs`,
invalid: true,
},
{
name: "device rootfs cannot have stats",
s: `device rootfs mounted on / with fstype rootfs stats`,
invalid: true,
},
{
name: "NFSv4 device with too little info",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs4 statvers=1.1\nhello",
invalid: true,
},
{
name: "NFSv4 device with bad bytes",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs4 statvers=1.1\nbytes: 0",
invalid: true,
},
{
name: "NFSv4 device with bad events",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs4 statvers=1.1\nevents: 0",
invalid: true,
},
{
name: "NFSv4 device with bad per-op stats",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs4 statvers=1.1\nper-op statistics\nFOO 0",
invalid: true,
},
{
name: "NFSv4 device with bad transport stats",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs4 statvers=1.1\nxprt: tcp",
invalid: true,
},
{
name: "NFSv4 device with bad transport version",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs4 statvers=foo\nxprt: tcp 0",
invalid: true,
},
{
name: "NFSv4 device with bad transport stats version 1.0",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs4 statvers=1.0\nxprt: tcp 0 0 0 0 0 0 0 0 0 0 0 0 0",
invalid: true,
},
{
name: "NFSv4 device with bad transport stats version 1.1",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs4 statvers=1.1\nxprt: tcp 0 0 0 0 0 0 0 0 0 0",
invalid: true,
},
{
name: "NFSv3 device with transport stats version 1.0 OK",
s: "device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs statvers=1.0\nxprt: tcp 1 2 3 4 5 6 7 8 9 10",
mounts: []*Mount{{
Device: "192.168.1.1:/srv",
Mount: "/mnt/nfs",
Type: "nfs",
Stats: &MountStatsNFS{
StatVersion: "1.0",
Transport: NFSTransportStats{
Port: 1,
Bind: 2,
Connect: 3,
ConnectIdleTime: 4,
IdleTime: 5 * time.Second,
Sends: 6,
Receives: 7,
BadTransactionIDs: 8,
CumulativeActiveRequests: 9,
CumulativeBacklog: 10,
},
},
}},
},
{
name: "device rootfs OK",
s: `device rootfs mounted on / with fstype rootfs`,
mounts: []*Mount{{
Device: "rootfs",
Mount: "/",
Type: "rootfs",
}},
},
{
name: "NFSv3 device with minimal stats OK",
s: `device 192.168.1.1:/srv mounted on /mnt/nfs with fstype nfs statvers=1.1`,
mounts: []*Mount{{
Device: "192.168.1.1:/srv",
Mount: "/mnt/nfs",
Type: "nfs",
Stats: &MountStatsNFS{
StatVersion: "1.1",
},
}},
},
{
name: "fixtures OK",
mounts: []*Mount{
{
Device: "rootfs",
Mount: "/",
Type: "rootfs",
},
{
Device: "sysfs",
Mount: "/sys",
Type: "sysfs",
},
{
Device: "proc",
Mount: "/proc",
Type: "proc",
},
{
Device: "/dev/sda1",
Mount: "/",
Type: "ext4",
},
{
Device: "192.168.1.1:/srv/test",
Mount: "/mnt/nfs/test",
Type: "nfs4",
Stats: &MountStatsNFS{
StatVersion: "1.1",
Age: 13968 * time.Second,
Bytes: NFSBytesStats{
Read: 1207640230,
ReadTotal: 1210214218,
ReadPages: 295483,
},
Events: NFSEventsStats{
InodeRevalidate: 52,
DnodeRevalidate: 226,
VFSOpen: 1,
VFSLookup: 13,
VFSAccess: 398,
VFSReadPages: 331,
VFSWritePages: 47,
VFSFlush: 77,
VFSFileRelease: 77,
},
Operations: []NFSOperationStats{
{
Operation: "NULL",
},
{
Operation: "READ",
Requests: 1298,
Transmissions: 1298,
BytesSent: 207680,
BytesReceived: 1210292152,
CumulativeQueueTime: 6 * time.Millisecond,
CumulativeTotalResponseTime: 79386 * time.Millisecond,
CumulativeTotalRequestTime: 79407 * time.Millisecond,
},
{
Operation: "WRITE",
},
},
Transport: NFSTransportStats{
Port: 832,
Connect: 1,
IdleTime: 11 * time.Second,
Sends: 6428,
Receives: 6428,
CumulativeActiveRequests: 12154,
MaximumRPCSlotsUsed: 24,
CumulativeSendingQueue: 26,
CumulativePendingQueue: 5726,
},
},
},
},
},
}
for i, tt := range tests {
t.Logf("[%02d] test %q", i, tt.name)
var mounts []*Mount
var err error
if tt.s != "" {
mounts, err = parseMountStats(strings.NewReader(tt.s))
} else {
proc, e := FS("fixtures").NewProc(26231)
if e != nil {
t.Fatalf("failed to create proc: %v", err)
}
mounts, err = proc.MountStats()
}
if tt.invalid && err == nil {
t.Error("expected an error, but none occurred")
}
if !tt.invalid && err != nil {
t.Errorf("unexpected error: %v", err)
}
if want, have := tt.mounts, mounts; !reflect.DeepEqual(want, have) {
t.Errorf("mounts:\nwant:\n%v\nhave:\n%v", mountsStr(want), mountsStr(have))
}
}
}
func mountsStr(mounts []*Mount) string {
var out string
for i, m := range mounts {
out += fmt.Sprintf("[%d] %q on %q (%q)", i, m.Device, m.Mount, m.Type)
stats, ok := m.Stats.(*MountStatsNFS)
if !ok {
out += "\n"
continue
}
out += fmt.Sprintf("\n\t- v%s, age: %s", stats.StatVersion, stats.Age)
out += fmt.Sprintf("\n\t- bytes: %v", stats.Bytes)
out += fmt.Sprintf("\n\t- events: %v", stats.Events)
out += fmt.Sprintf("\n\t- transport: %v", stats.Transport)
out += fmt.Sprintf("\n\t- per-operation stats:")
for _, o := range stats.Operations {
out += fmt.Sprintf("\n\t\t- %v", o)
}
out += "\n"
}
return out
}

View File

@ -1,33 +0,0 @@
package procfs
import "testing"
func TestProcIO(t *testing.T) {
p, err := FS("fixtures").NewProc(26231)
if err != nil {
t.Fatal(err)
}
s, err := p.NewIO()
if err != nil {
t.Fatal(err)
}
for _, test := range []struct {
name string
want int64
have int64
}{
{name: "RChar", want: 750339, have: int64(s.RChar)},
{name: "WChar", want: 818609, have: int64(s.WChar)},
{name: "SyscR", want: 7405, have: int64(s.SyscR)},
{name: "SyscW", want: 5245, have: int64(s.SyscW)},
{name: "ReadBytes", want: 1024, have: int64(s.ReadBytes)},
{name: "WriteBytes", want: 2048, have: int64(s.WriteBytes)},
{name: "CancelledWriteBytes", want: -1024, have: s.CancelledWriteBytes},
} {
if test.want != test.have {
t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
}
}
}

View File

@ -1,31 +0,0 @@
package procfs
import "testing"
func TestNewLimits(t *testing.T) {
p, err := FS("fixtures").NewProc(26231)
if err != nil {
t.Fatal(err)
}
l, err := p.NewLimits()
if err != nil {
t.Fatal(err)
}
for _, test := range []struct {
name string
want int
have int
}{
{name: "cpu time", want: -1, have: l.CPUTime},
{name: "open files", want: 2048, have: l.OpenFiles},
{name: "msgqueue size", want: 819200, have: l.MsqqueueSize},
{name: "nice priority", want: 0, have: l.NicePriority},
{name: "address space", want: -1, have: l.AddressSpace},
} {
if test.want != test.have {
t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
}
}
}

View File

@ -1,110 +0,0 @@
package procfs
import (
"os"
"testing"
)
func TestProcStat(t *testing.T) {
p, err := FS("fixtures").NewProc(26231)
if err != nil {
t.Fatal(err)
}
s, err := p.NewStat()
if err != nil {
t.Fatal(err)
}
for _, test := range []struct {
name string
want int
have int
}{
{name: "pid", want: 26231, have: s.PID},
{name: "user time", want: 1677, have: int(s.UTime)},
{name: "system time", want: 44, have: int(s.STime)},
{name: "start time", want: 82375, have: int(s.Starttime)},
{name: "virtual memory size", want: 56274944, have: s.VSize},
{name: "resident set size", want: 1981, have: s.RSS},
} {
if test.want != test.have {
t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
}
}
}
func TestProcStatComm(t *testing.T) {
s1, err := testProcStat(26231)
if err != nil {
t.Fatal(err)
}
if want, have := "vim", s1.Comm; want != have {
t.Errorf("want comm %s, have %s", want, have)
}
s2, err := testProcStat(584)
if err != nil {
t.Fatal(err)
}
if want, have := "(a b ) ( c d) ", s2.Comm; want != have {
t.Errorf("want comm %s, have %s", want, have)
}
}
func TestProcStatVirtualMemory(t *testing.T) {
s, err := testProcStat(26231)
if err != nil {
t.Fatal(err)
}
if want, have := 56274944, s.VirtualMemory(); want != have {
t.Errorf("want virtual memory %d, have %d", want, have)
}
}
func TestProcStatResidentMemory(t *testing.T) {
s, err := testProcStat(26231)
if err != nil {
t.Fatal(err)
}
if want, have := 1981*os.Getpagesize(), s.ResidentMemory(); want != have {
t.Errorf("want resident memory %d, have %d", want, have)
}
}
func TestProcStatStartTime(t *testing.T) {
s, err := testProcStat(26231)
if err != nil {
t.Fatal(err)
}
time, err := s.StartTime()
if err != nil {
t.Fatal(err)
}
if want, have := 1418184099.75, time; want != have {
t.Errorf("want start time %f, have %f", want, have)
}
}
func TestProcStatCPUTime(t *testing.T) {
s, err := testProcStat(26231)
if err != nil {
t.Fatal(err)
}
if want, have := 17.21, s.CPUTime(); want != have {
t.Errorf("want cpu time %f, have %f", want, have)
}
}
func testProcStat(pid int) (ProcStat, error) {
p, err := FS("fixtures").NewProc(pid)
if err != nil {
return ProcStat{}, err
}
return p.NewStat()
}

View File

@ -1,160 +0,0 @@
package procfs
import (
"reflect"
"sort"
"testing"
)
func TestSelf(t *testing.T) {
fs := FS("fixtures")
p1, err := fs.NewProc(26231)
if err != nil {
t.Fatal(err)
}
p2, err := fs.Self()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(p1, p2) {
t.Errorf("want process %v, have %v", p1, p2)
}
}
func TestAllProcs(t *testing.T) {
procs, err := FS("fixtures").AllProcs()
if err != nil {
t.Fatal(err)
}
sort.Sort(procs)
for i, p := range []*Proc{{PID: 584}, {PID: 26231}} {
if want, have := p.PID, procs[i].PID; want != have {
t.Errorf("want processes %d, have %d", want, have)
}
}
}
func TestCmdLine(t *testing.T) {
for _, tt := range []struct {
process int
want []string
}{
{process: 26231, want: []string{"vim", "test.go", "+10"}},
{process: 26232, want: []string{}},
} {
p1, err := FS("fixtures").NewProc(tt.process)
if err != nil {
t.Fatal(err)
}
c1, err := p1.CmdLine()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tt.want, c1) {
t.Errorf("want cmdline %v, have %v", tt.want, c1)
}
}
}
func TestComm(t *testing.T) {
for _, tt := range []struct {
process int
want string
}{
{process: 26231, want: "vim"},
{process: 26232, want: "ata_sff"},
} {
p1, err := FS("fixtures").NewProc(tt.process)
if err != nil {
t.Fatal(err)
}
c1, err := p1.Comm()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tt.want, c1) {
t.Errorf("want comm %v, have %v", tt.want, c1)
}
}
}
func TestExecutable(t *testing.T) {
for _, tt := range []struct {
process int
want string
}{
{process: 26231, want: "/usr/bin/vim"},
{process: 26232, want: ""},
} {
p, err := FS("fixtures").NewProc(tt.process)
if err != nil {
t.Fatal(err)
}
exe, err := p.Executable()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tt.want, exe) {
t.Errorf("want absolute path to cmdline %v, have %v", tt.want, exe)
}
}
}
func TestFileDescriptors(t *testing.T) {
p1, err := FS("fixtures").NewProc(26231)
if err != nil {
t.Fatal(err)
}
fds, err := p1.FileDescriptors()
if err != nil {
t.Fatal(err)
}
sort.Sort(byUintptr(fds))
if want := []uintptr{0, 1, 2, 3, 10}; !reflect.DeepEqual(want, fds) {
t.Errorf("want fds %v, have %v", want, fds)
}
}
func TestFileDescriptorTargets(t *testing.T) {
p1, err := FS("fixtures").NewProc(26231)
if err != nil {
t.Fatal(err)
}
fds, err := p1.FileDescriptorTargets()
if err != nil {
t.Fatal(err)
}
sort.Strings(fds)
var want = []string{
"../../symlinktargets/abc",
"../../symlinktargets/def",
"../../symlinktargets/ghi",
"../../symlinktargets/uvw",
"../../symlinktargets/xyz",
}
if !reflect.DeepEqual(want, fds) {
t.Errorf("want fds %v, have %v", want, fds)
}
}
func TestFileDescriptorsLen(t *testing.T) {
p1, err := FS("fixtures").NewProc(26231)
if err != nil {
t.Fatal(err)
}
l, err := p1.FileDescriptorsLen()
if err != nil {
t.Fatal(err)
}
if want, have := 5, l; want != have {
t.Errorf("want fds %d, have %d", want, have)
}
}
type byUintptr []uintptr
func (a byUintptr) Len() int { return len(a) }
func (a byUintptr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byUintptr) Less(i, j int) bool { return a[i] < a[j] }

View File

@ -3,15 +3,66 @@ package procfs
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// CPUStat shows how much time the cpu spend in various stages.
type CPUStat struct {
User float64
Nice float64
System float64
Idle float64
Iowait float64
IRQ float64
SoftIRQ float64
Steal float64
Guest float64
GuestNice float64
}
// SoftIRQStat represent the softirq statistics as exported in the procfs stat file.
// A nice introduction can be found at https://0xax.gitbooks.io/linux-insides/content/interrupts/interrupts-9.html
// It is possible to get per-cpu stats by reading /proc/softirqs
type SoftIRQStat struct {
Hi uint64
Timer uint64
NetTx uint64
NetRx uint64
Block uint64
BlockIoPoll uint64
Tasklet uint64
Sched uint64
Hrtimer uint64
Rcu uint64
}
// Stat represents kernel/system statistics.
type Stat struct {
// Boot time in seconds since the Epoch.
BootTime int64
BootTime uint64
// Summed up cpu statistics.
CPUTotal CPUStat
// Per-CPU statistics.
CPU []CPUStat
// Number of times interrupts were handled, which contains numbered and unnumbered IRQs.
IRQTotal uint64
// Number of times a numbered IRQ was triggered.
IRQ []uint64
// Number of times a context switch happened.
ContextSwitches uint64
// Number of times a process was created.
ProcessCreated uint64
// Number of processes currently running.
ProcessesRunning uint64
// Number of processes currently blocked (waiting for IO).
ProcessesBlocked uint64
// Number of times a softirq was scheduled.
SoftIRQTotal uint64
// Detailed softirq statistics.
SoftIRQ SoftIRQStat
}
// NewStat returns kernel/system statistics read from /proc/stat.
@ -24,33 +75,145 @@ func NewStat() (Stat, error) {
return fs.NewStat()
}
// Parse a cpu statistics line and returns the CPUStat struct plus the cpu id (or -1 for the overall sum).
func parseCPUStat(line string) (CPUStat, int64, error) {
cpuStat := CPUStat{}
var cpu string
count, err := fmt.Sscanf(line, "%s %f %f %f %f %f %f %f %f %f %f",
&cpu,
&cpuStat.User, &cpuStat.Nice, &cpuStat.System, &cpuStat.Idle,
&cpuStat.Iowait, &cpuStat.IRQ, &cpuStat.SoftIRQ, &cpuStat.Steal,
&cpuStat.Guest, &cpuStat.GuestNice)
if err != nil && err != io.EOF {
return CPUStat{}, -1, fmt.Errorf("couldn't parse %s (cpu): %s", line, err)
}
if count == 0 {
return CPUStat{}, -1, fmt.Errorf("couldn't parse %s (cpu): 0 elements parsed", line)
}
cpuStat.User /= userHZ
cpuStat.Nice /= userHZ
cpuStat.System /= userHZ
cpuStat.Idle /= userHZ
cpuStat.Iowait /= userHZ
cpuStat.IRQ /= userHZ
cpuStat.SoftIRQ /= userHZ
cpuStat.Steal /= userHZ
cpuStat.Guest /= userHZ
cpuStat.GuestNice /= userHZ
if cpu == "cpu" {
return cpuStat, -1, nil
}
cpuID, err := strconv.ParseInt(cpu[3:], 10, 64)
if err != nil {
return CPUStat{}, -1, fmt.Errorf("couldn't parse %s (cpu/cpuid): %s", line, err)
}
return cpuStat, cpuID, nil
}
// Parse a softirq line.
func parseSoftIRQStat(line string) (SoftIRQStat, uint64, error) {
softIRQStat := SoftIRQStat{}
var total uint64
var prefix string
_, err := fmt.Sscanf(line, "%s %d %d %d %d %d %d %d %d %d %d %d",
&prefix, &total,
&softIRQStat.Hi, &softIRQStat.Timer, &softIRQStat.NetTx, &softIRQStat.NetRx,
&softIRQStat.Block, &softIRQStat.BlockIoPoll,
&softIRQStat.Tasklet, &softIRQStat.Sched,
&softIRQStat.Hrtimer, &softIRQStat.Rcu)
if err != nil {
return SoftIRQStat{}, 0, fmt.Errorf("couldn't parse %s (softirq): %s", line, err)
}
return softIRQStat, total, nil
}
// NewStat returns an information about current kernel/system statistics.
func (fs FS) NewStat() (Stat, error) {
// See https://www.kernel.org/doc/Documentation/filesystems/proc.txt
f, err := os.Open(fs.Path("stat"))
if err != nil {
return Stat{}, err
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
if !strings.HasPrefix(line, "btime") {
stat := Stat{}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(scanner.Text())
// require at least <key> <value>
if len(parts) < 2 {
continue
}
fields := strings.Fields(line)
if len(fields) != 2 {
return Stat{}, fmt.Errorf("couldn't parse %s line %s", f.Name(), line)
switch {
case parts[0] == "btime":
if stat.BootTime, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s (btime): %s", parts[1], err)
}
case parts[0] == "intr":
if stat.IRQTotal, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s (intr): %s", parts[1], err)
}
numberedIRQs := parts[2:]
stat.IRQ = make([]uint64, len(numberedIRQs))
for i, count := range numberedIRQs {
if stat.IRQ[i], err = strconv.ParseUint(count, 10, 64); err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s (intr%d): %s", count, i, err)
}
}
case parts[0] == "ctxt":
if stat.ContextSwitches, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s (ctxt): %s", parts[1], err)
}
case parts[0] == "processes":
if stat.ProcessCreated, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s (processes): %s", parts[1], err)
}
case parts[0] == "procs_running":
if stat.ProcessesRunning, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s (procs_running): %s", parts[1], err)
}
case parts[0] == "procs_blocked":
if stat.ProcessesBlocked, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s (procs_blocked): %s", parts[1], err)
}
case parts[0] == "softirq":
softIRQStats, total, err := parseSoftIRQStat(line)
if err != nil {
return Stat{}, err
}
stat.SoftIRQTotal = total
stat.SoftIRQ = softIRQStats
case strings.HasPrefix(parts[0], "cpu"):
cpuStat, cpuID, err := parseCPUStat(line)
if err != nil {
return Stat{}, err
}
if cpuID == -1 {
stat.CPUTotal = cpuStat
} else {
for int64(len(stat.CPU)) <= cpuID {
stat.CPU = append(stat.CPU, CPUStat{})
}
stat.CPU[cpuID] = cpuStat
}
}
i, err := strconv.ParseInt(fields[1], 10, 32)
if err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s: %s", fields[1], err)
}
return Stat{BootTime: i}, nil
}
if err := s.Err(); err != nil {
if err := scanner.Err(); err != nil {
return Stat{}, fmt.Errorf("couldn't parse %s: %s", f.Name(), err)
}
return Stat{}, fmt.Errorf("couldn't parse %s, missing btime", f.Name())
return stat, nil
}

View File

@ -1,14 +0,0 @@
package procfs
import "testing"
func TestStat(t *testing.T) {
s, err := FS("fixtures").NewStat()
if err != nil {
t.Fatal(err)
}
if want, have := int64(1418183276), s.BootTime; want != have {
t.Errorf("want boot time %d, have %d", want, have)
}
}

View File

@ -1,16 +0,0 @@
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package sysfs provides functions to retrieve system and kernel metrics
// from the pseudo-filesystem sys.
package sysfs

View File

@ -1 +0,0 @@
extent_alloc 1 0 0 0

View File

@ -1 +0,0 @@
extent_alloc 2 0 0 0

View File

@ -1,82 +0,0 @@
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sysfs
import (
"fmt"
"os"
"path/filepath"
"github.com/prometheus/procfs/xfs"
)
// FS represents the pseudo-filesystem sys, which provides an interface to
// kernel data structures.
type FS string
// DefaultMountPoint is the common mount point of the sys filesystem.
const DefaultMountPoint = "/sys"
// NewFS returns a new FS mounted under the given mountPoint. It will error
// if the mount point can't be read.
func NewFS(mountPoint string) (FS, error) {
info, err := os.Stat(mountPoint)
if err != nil {
return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
}
if !info.IsDir() {
return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
}
return FS(mountPoint), nil
}
// Path returns the path of the given subsystem relative to the sys root.
func (fs FS) Path(p ...string) string {
return filepath.Join(append([]string{string(fs)}, p...)...)
}
// XFSStats retrieves XFS filesystem runtime statistics for each mounted XFS
// filesystem. Only available on kernel 4.4+. On older kernels, an empty
// slice of *xfs.Stats will be returned.
func (fs FS) XFSStats() ([]*xfs.Stats, error) {
matches, err := filepath.Glob(fs.Path("fs/xfs/*/stats/stats"))
if err != nil {
return nil, err
}
stats := make([]*xfs.Stats, 0, len(matches))
for _, m := range matches {
f, err := os.Open(m)
if err != nil {
return nil, err
}
// "*" used in glob above indicates the name of the filesystem.
name := filepath.Base(filepath.Dir(filepath.Dir(m)))
// File must be closed after parsing, regardless of success or
// failure. Defer is not used because of the loop.
s, err := xfs.ParseStats(f)
_ = f.Close()
if err != nil {
return nil, err
}
s.Name = name
stats = append(stats, s)
}
return stats, nil
}

View File

@ -1,66 +0,0 @@
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sysfs
import "testing"
func TestNewFS(t *testing.T) {
if _, err := NewFS("foobar"); err == nil {
t.Error("want NewFS to fail for non-existing mount point")
}
if _, err := NewFS("doc.go"); err == nil {
t.Error("want NewFS to fail if mount point is not a directory")
}
}
func TestFSXFSStats(t *testing.T) {
stats, err := FS("fixtures").XFSStats()
if err != nil {
t.Fatalf("failed to parse XFS stats: %v", err)
}
tests := []struct {
name string
allocated uint32
}{
{
name: "sda1",
allocated: 1,
},
{
name: "sdb1",
allocated: 2,
},
}
const expect = 2
if l := len(stats); l != expect {
t.Fatalf("unexpected number of XFS stats: %d", l)
}
if l := len(tests); l != expect {
t.Fatalf("unexpected number of tests: %d", l)
}
for i, tt := range tests {
if want, got := tt.name, stats[i].Name; want != got {
t.Errorf("unexpected stats name:\nwant: %q\nhave: %q", want, got)
}
if want, got := tt.allocated, stats[i].ExtentAllocation.ExtentsAllocated; want != got {
t.Errorf("unexpected extents allocated:\nwant: %d\nhave: %d", want, got)
}
}
}

187
gateway/vendor/github.com/prometheus/procfs/xfrm.go generated vendored Normal file
View File

@ -0,0 +1,187 @@
// Copyright 2017 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package procfs
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
// XfrmStat models the contents of /proc/net/xfrm_stat.
type XfrmStat struct {
// All errors which are not matched by other
XfrmInError int
// No buffer is left
XfrmInBufferError int
// Header Error
XfrmInHdrError int
// No state found
// i.e. either inbound SPI, address, or IPSEC protocol at SA is wrong
XfrmInNoStates int
// Transformation protocol specific error
// e.g. SA Key is wrong
XfrmInStateProtoError int
// Transformation mode specific error
XfrmInStateModeError int
// Sequence error
// e.g. sequence number is out of window
XfrmInStateSeqError int
// State is expired
XfrmInStateExpired int
// State has mismatch option
// e.g. UDP encapsulation type is mismatched
XfrmInStateMismatch int
// State is invalid
XfrmInStateInvalid int
// No matching template for states
// e.g. Inbound SAs are correct but SP rule is wrong
XfrmInTmplMismatch int
// No policy is found for states
// e.g. Inbound SAs are correct but no SP is found
XfrmInNoPols int
// Policy discards
XfrmInPolBlock int
// Policy error
XfrmInPolError int
// All errors which are not matched by others
XfrmOutError int
// Bundle generation error
XfrmOutBundleGenError int
// Bundle check error
XfrmOutBundleCheckError int
// No state was found
XfrmOutNoStates int
// Transformation protocol specific error
XfrmOutStateProtoError int
// Transportation mode specific error
XfrmOutStateModeError int
// Sequence error
// i.e sequence number overflow
XfrmOutStateSeqError int
// State is expired
XfrmOutStateExpired int
// Policy discads
XfrmOutPolBlock int
// Policy is dead
XfrmOutPolDead int
// Policy Error
XfrmOutPolError int
XfrmFwdHdrError int
XfrmOutStateInvalid int
XfrmAcquireError int
}
// NewXfrmStat reads the xfrm_stat statistics.
func NewXfrmStat() (XfrmStat, error) {
fs, err := NewFS(DefaultMountPoint)
if err != nil {
return XfrmStat{}, err
}
return fs.NewXfrmStat()
}
// NewXfrmStat reads the xfrm_stat statistics from the 'proc' filesystem.
func (fs FS) NewXfrmStat() (XfrmStat, error) {
file, err := os.Open(fs.Path("net/xfrm_stat"))
if err != nil {
return XfrmStat{}, err
}
defer file.Close()
var (
x = XfrmStat{}
s = bufio.NewScanner(file)
)
for s.Scan() {
fields := strings.Fields(s.Text())
if len(fields) != 2 {
return XfrmStat{}, fmt.Errorf(
"couldnt parse %s line %s", file.Name(), s.Text())
}
name := fields[0]
value, err := strconv.Atoi(fields[1])
if err != nil {
return XfrmStat{}, err
}
switch name {
case "XfrmInError":
x.XfrmInError = value
case "XfrmInBufferError":
x.XfrmInBufferError = value
case "XfrmInHdrError":
x.XfrmInHdrError = value
case "XfrmInNoStates":
x.XfrmInNoStates = value
case "XfrmInStateProtoError":
x.XfrmInStateProtoError = value
case "XfrmInStateModeError":
x.XfrmInStateModeError = value
case "XfrmInStateSeqError":
x.XfrmInStateSeqError = value
case "XfrmInStateExpired":
x.XfrmInStateExpired = value
case "XfrmInStateInvalid":
x.XfrmInStateInvalid = value
case "XfrmInTmplMismatch":
x.XfrmInTmplMismatch = value
case "XfrmInNoPols":
x.XfrmInNoPols = value
case "XfrmInPolBlock":
x.XfrmInPolBlock = value
case "XfrmInPolError":
x.XfrmInPolError = value
case "XfrmOutError":
x.XfrmOutError = value
case "XfrmInStateMismatch":
x.XfrmInStateMismatch = value
case "XfrmOutBundleGenError":
x.XfrmOutBundleGenError = value
case "XfrmOutBundleCheckError":
x.XfrmOutBundleCheckError = value
case "XfrmOutNoStates":
x.XfrmOutNoStates = value
case "XfrmOutStateProtoError":
x.XfrmOutStateProtoError = value
case "XfrmOutStateModeError":
x.XfrmOutStateModeError = value
case "XfrmOutStateSeqError":
x.XfrmOutStateSeqError = value
case "XfrmOutStateExpired":
x.XfrmOutStateExpired = value
case "XfrmOutPolBlock":
x.XfrmOutPolBlock = value
case "XfrmOutPolDead":
x.XfrmOutPolDead = value
case "XfrmOutPolError":
x.XfrmOutPolError = value
case "XfrmFwdHdrError":
x.XfrmFwdHdrError = value
case "XfrmOutStateInvalid":
x.XfrmOutStateInvalid = value
case "XfrmAcquireError":
x.XfrmAcquireError = value
}
}
return x, s.Err()
}

View File

@ -1,442 +0,0 @@
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package xfs_test
import (
"reflect"
"strings"
"testing"
"github.com/prometheus/procfs"
"github.com/prometheus/procfs/xfs"
)
func TestParseStats(t *testing.T) {
tests := []struct {
name string
s string
fs bool
stats *xfs.Stats
invalid bool
}{
{
name: "empty file OK",
},
{
name: "short or empty lines and unknown labels ignored",
s: "one\n\ntwo 1 2 3\n",
stats: &xfs.Stats{},
},
{
name: "bad uint32",
s: "extent_alloc XXX",
invalid: true,
},
{
name: "bad uint64",
s: "xpc XXX",
invalid: true,
},
{
name: "extent_alloc bad",
s: "extent_alloc 1",
invalid: true,
},
{
name: "extent_alloc OK",
s: "extent_alloc 1 2 3 4",
stats: &xfs.Stats{
ExtentAllocation: xfs.ExtentAllocationStats{
ExtentsAllocated: 1,
BlocksAllocated: 2,
ExtentsFreed: 3,
BlocksFreed: 4,
},
},
},
{
name: "abt bad",
s: "abt 1",
invalid: true,
},
{
name: "abt OK",
s: "abt 1 2 3 4",
stats: &xfs.Stats{
AllocationBTree: xfs.BTreeStats{
Lookups: 1,
Compares: 2,
RecordsInserted: 3,
RecordsDeleted: 4,
},
},
},
{
name: "blk_map bad",
s: "blk_map 1",
invalid: true,
},
{
name: "blk_map OK",
s: "blk_map 1 2 3 4 5 6 7",
stats: &xfs.Stats{
BlockMapping: xfs.BlockMappingStats{
Reads: 1,
Writes: 2,
Unmaps: 3,
ExtentListInsertions: 4,
ExtentListDeletions: 5,
ExtentListLookups: 6,
ExtentListCompares: 7,
},
},
},
{
name: "bmbt bad",
s: "bmbt 1",
invalid: true,
},
{
name: "bmbt OK",
s: "bmbt 1 2 3 4",
stats: &xfs.Stats{
BlockMapBTree: xfs.BTreeStats{
Lookups: 1,
Compares: 2,
RecordsInserted: 3,
RecordsDeleted: 4,
},
},
},
{
name: "dir bad",
s: "dir 1",
invalid: true,
},
{
name: "dir OK",
s: "dir 1 2 3 4",
stats: &xfs.Stats{
DirectoryOperation: xfs.DirectoryOperationStats{
Lookups: 1,
Creates: 2,
Removes: 3,
Getdents: 4,
},
},
},
{
name: "trans bad",
s: "trans 1",
invalid: true,
},
{
name: "trans OK",
s: "trans 1 2 3",
stats: &xfs.Stats{
Transaction: xfs.TransactionStats{
Sync: 1,
Async: 2,
Empty: 3,
},
},
},
{
name: "ig bad",
s: "ig 1",
invalid: true,
},
{
name: "ig OK",
s: "ig 1 2 3 4 5 6 7",
stats: &xfs.Stats{
InodeOperation: xfs.InodeOperationStats{
Attempts: 1,
Found: 2,
Recycle: 3,
Missed: 4,
Duplicate: 5,
Reclaims: 6,
AttributeChange: 7,
},
},
},
{
name: "log bad",
s: "log 1",
invalid: true,
},
{
name: "log OK",
s: "log 1 2 3 4 5",
stats: &xfs.Stats{
LogOperation: xfs.LogOperationStats{
Writes: 1,
Blocks: 2,
NoInternalBuffers: 3,
Force: 4,
ForceSleep: 5,
},
},
},
{
name: "rw bad",
s: "rw 1",
invalid: true,
},
{
name: "rw OK",
s: "rw 1 2",
stats: &xfs.Stats{
ReadWrite: xfs.ReadWriteStats{
Read: 1,
Write: 2,
},
},
},
{
name: "attr bad",
s: "attr 1",
invalid: true,
},
{
name: "attr OK",
s: "attr 1 2 3 4",
stats: &xfs.Stats{
AttributeOperation: xfs.AttributeOperationStats{
Get: 1,
Set: 2,
Remove: 3,
List: 4,
},
},
},
{
name: "icluster bad",
s: "icluster 1",
invalid: true,
},
{
name: "icluster OK",
s: "icluster 1 2 3",
stats: &xfs.Stats{
InodeClustering: xfs.InodeClusteringStats{
Iflush: 1,
Flush: 2,
FlushInode: 3,
},
},
},
{
name: "vnodes bad",
s: "vnodes 1",
invalid: true,
},
{
name: "vnodes (missing free) OK",
s: "vnodes 1 2 3 4 5 6 7",
stats: &xfs.Stats{
Vnode: xfs.VnodeStats{
Active: 1,
Allocate: 2,
Get: 3,
Hold: 4,
Release: 5,
Reclaim: 6,
Remove: 7,
},
},
},
{
name: "vnodes (with free) OK",
s: "vnodes 1 2 3 4 5 6 7 8",
stats: &xfs.Stats{
Vnode: xfs.VnodeStats{
Active: 1,
Allocate: 2,
Get: 3,
Hold: 4,
Release: 5,
Reclaim: 6,
Remove: 7,
Free: 8,
},
},
},
{
name: "buf bad",
s: "buf 1",
invalid: true,
},
{
name: "buf OK",
s: "buf 1 2 3 4 5 6 7 8 9",
stats: &xfs.Stats{
Buffer: xfs.BufferStats{
Get: 1,
Create: 2,
GetLocked: 3,
GetLockedWaited: 4,
BusyLocked: 5,
MissLocked: 6,
PageRetries: 7,
PageFound: 8,
GetRead: 9,
},
},
},
{
name: "xpc bad",
s: "xpc 1",
invalid: true,
},
{
name: "xpc OK",
s: "xpc 1 2 3",
stats: &xfs.Stats{
ExtendedPrecision: xfs.ExtendedPrecisionStats{
FlushBytes: 1,
WriteBytes: 2,
ReadBytes: 3,
},
},
},
{
name: "fixtures OK",
fs: true,
stats: &xfs.Stats{
ExtentAllocation: xfs.ExtentAllocationStats{
ExtentsAllocated: 92447,
BlocksAllocated: 97589,
ExtentsFreed: 92448,
BlocksFreed: 93751,
},
AllocationBTree: xfs.BTreeStats{
Lookups: 0,
Compares: 0,
RecordsInserted: 0,
RecordsDeleted: 0,
},
BlockMapping: xfs.BlockMappingStats{
Reads: 1767055,
Writes: 188820,
Unmaps: 184891,
ExtentListInsertions: 92447,
ExtentListDeletions: 92448,
ExtentListLookups: 2140766,
ExtentListCompares: 0,
},
BlockMapBTree: xfs.BTreeStats{
Lookups: 0,
Compares: 0,
RecordsInserted: 0,
RecordsDeleted: 0,
},
DirectoryOperation: xfs.DirectoryOperationStats{
Lookups: 185039,
Creates: 92447,
Removes: 92444,
Getdents: 136422,
},
Transaction: xfs.TransactionStats{
Sync: 706,
Async: 944304,
Empty: 0,
},
InodeOperation: xfs.InodeOperationStats{
Attempts: 185045,
Found: 58807,
Recycle: 0,
Missed: 126238,
Duplicate: 0,
Reclaims: 33637,
AttributeChange: 22,
},
LogOperation: xfs.LogOperationStats{
Writes: 2883,
Blocks: 113448,
NoInternalBuffers: 9,
Force: 17360,
ForceSleep: 739,
},
ReadWrite: xfs.ReadWriteStats{
Read: 107739,
Write: 94045,
},
AttributeOperation: xfs.AttributeOperationStats{
Get: 4,
Set: 0,
Remove: 0,
List: 0,
},
InodeClustering: xfs.InodeClusteringStats{
Iflush: 8677,
Flush: 7849,
FlushInode: 135802,
},
Vnode: xfs.VnodeStats{
Active: 92601,
Allocate: 0,
Get: 0,
Hold: 0,
Release: 92444,
Reclaim: 92444,
Remove: 92444,
Free: 0,
},
Buffer: xfs.BufferStats{
Get: 2666287,
Create: 7122,
GetLocked: 2659202,
GetLockedWaited: 3599,
BusyLocked: 2,
MissLocked: 7085,
PageRetries: 0,
PageFound: 10297,
GetRead: 7085,
},
ExtendedPrecision: xfs.ExtendedPrecisionStats{
FlushBytes: 399724544,
WriteBytes: 92823103,
ReadBytes: 86219234,
},
},
},
}
for _, tt := range tests {
var (
stats *xfs.Stats
err error
)
if tt.s != "" {
stats, err = xfs.ParseStats(strings.NewReader(tt.s))
}
if tt.fs {
stats, err = procfs.FS("../fixtures").XFSStats()
}
if tt.invalid && err == nil {
t.Error("expected an error, but none occurred")
}
if !tt.invalid && err != nil {
t.Errorf("unexpected error: %v", err)
}
if want, have := tt.stats, stats; !reflect.DeepEqual(want, have) {
t.Errorf("unexpected XFS stats:\nwant:\n%v\nhave:\n%v", want, have)
}
}
}