-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pull request on adding missing statistic under solaris/illumos #1381
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
//go:build solaris | ||
// +build solaris | ||
|
||
package net | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/shirou/gopsutil/v3/internal/common" | ||
) | ||
|
||
// NetIOCounters returnes network I/O statistics for every network | ||
// interface installed on the system. If pernic argument is false, | ||
// return only sum of all information (which name is 'all'). If true, | ||
// every network interface installed on the system is returned | ||
// separately. | ||
func IOCounters(pernic bool) ([]IOCountersStat, error) { | ||
return IOCountersWithContext(context.Background(), pernic) | ||
} | ||
|
||
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { | ||
// collect all the net class's links with below statistics | ||
filterstr := "/^(?!vnic)/::phys:/^rbytes64$|^ipackets64$|^idrops64$|^ierrors$|^obytes64$|^opackets64$|^odrops64$|^oerrors$/" | ||
if runtime.GOOS == "illumos" { | ||
filterstr = "/[^vnic]/::mac:/^rbytes64$|^ipackets64$|^idrops64$|^ierrors$|^obytes64$|^opackets64$|^odrops64$|^oerrors$/" | ||
} | ||
kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-c", "net", "-p", filterstr) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot execute kstat: %w", err) | ||
} | ||
|
||
lines := strings.Split(strings.TrimSpace(string(kstatSysOut)), "\n") | ||
if len(lines) == 0 { | ||
return nil, fmt.Errorf("no interface found") | ||
} | ||
rbytes64arr := make(map[string]uint64) | ||
ipackets64arr := make(map[string]uint64) | ||
idrops64arr := make(map[string]uint64) | ||
ierrorsarr := make(map[string]uint64) | ||
obytes64arr := make(map[string]uint64) | ||
opackets64arr := make(map[string]uint64) | ||
odrops64arr := make(map[string]uint64) | ||
oerrorsarr := make(map[string]uint64) | ||
|
||
re := regexp.MustCompile(`[:\s]+`) | ||
for _, line := range lines { | ||
fields := re.Split(line, -1) | ||
interfaceName := fields[0] | ||
instance := fields[1] | ||
switch fields[3] { | ||
case "rbytes64": | ||
rbytes64arr[interfaceName+instance], err = strconv.ParseUint(fields[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse rbytes64: %w", err) | ||
} | ||
case "ipackets64": | ||
ipackets64arr[interfaceName+instance], err = strconv.ParseUint(fields[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse ipackets64: %w", err) | ||
} | ||
case "idrops64": | ||
idrops64arr[interfaceName+instance], err = strconv.ParseUint(fields[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse idrops64: %w", err) | ||
} | ||
case "ierrors": | ||
ierrorsarr[interfaceName+instance], err = strconv.ParseUint(fields[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse ierrors: %w", err) | ||
} | ||
case "obytes64": | ||
obytes64arr[interfaceName+instance], err = strconv.ParseUint(fields[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse obytes64: %w", err) | ||
} | ||
case "opackets64": | ||
opackets64arr[interfaceName+instance], err = strconv.ParseUint(fields[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse opackets64: %w", err) | ||
} | ||
case "odrops64": | ||
odrops64arr[interfaceName+instance], err = strconv.ParseUint(fields[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse odrops64: %w", err) | ||
} | ||
case "oerrors": | ||
oerrorsarr[interfaceName+instance], err = strconv.ParseUint(fields[4], 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot parse oerrors: %w", err) | ||
} | ||
} | ||
} | ||
ret := make([]IOCountersStat, 0) | ||
for k := range rbytes64arr { | ||
nic := IOCountersStat{ | ||
Name: k, | ||
BytesRecv: rbytes64arr[k], | ||
PacketsRecv: ipackets64arr[k], | ||
Errin: ierrorsarr[k], | ||
Dropin: idrops64arr[k], | ||
BytesSent: obytes64arr[k], | ||
PacketsSent: opackets64arr[k], | ||
Errout: oerrorsarr[k], | ||
Dropout: odrops64arr[k], | ||
} | ||
ret = append(ret, nic) | ||
} | ||
|
||
if !pernic { | ||
return getIOCountersAll(ret) | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
func Connections(kind string) ([]ConnectionStat, error) { | ||
return ConnectionsWithContext(context.Background(), kind) | ||
} | ||
|
||
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { | ||
return []ConnectionStat{}, common.ErrNotImplementedError | ||
} | ||
|
||
func FilterCounters() ([]FilterStat, error) { | ||
return FilterCountersWithContext(context.Background()) | ||
} | ||
|
||
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { | ||
return []FilterStat{}, common.ErrNotImplementedError | ||
} | ||
|
||
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { | ||
return ProtoCountersWithContext(context.Background(), protocols) | ||
} | ||
|
||
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { | ||
return []ProtoCountersStat{}, common.ErrNotImplementedError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about to pass
ctx
instead of creating a new context in this function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Shirou,
Thank for your review and comment; I have made the suggested change and submitted the commit.
For the tested OS environments, below is what I did for the Solaris and illumos environments:
For both environments, I tested with the go1.15 illumos/amd64 (and go1.13 illumos/amd64) and tests result look good.