-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add netstat_poller #68
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= | ||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= | ||
github.com/heroku/slog v0.0.0-20140402171358-7fea2d3038fa h1:udpqQXzEVyYxKVOfRKCfx8FyneqHrvYEtzXc0LHxgRs= | ||
github.com/heroku/slog v0.0.0-20140402171358-7fea2d3038fa/go.mod h1:qakOkduMaAhe2ypljIuLEinhnl5nCnyz7iXMZHwdr14= | ||
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= | ||
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= | ||
github.com/xuyu/goredis v0.0.0-20140820021013-517ef62abb23 h1:0+1EhmoODl7T8NbEJ7/9WgQNCW05HKI0y+/kDR9bECE= | ||
github.com/xuyu/goredis v0.0.0-20140820021013-517ef62abb23/go.mod h1:Ew+jSwVYyBT+GrKWGzlld2VGZqUy5cgGQATzqHpqGvg= | ||
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= | ||
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= | ||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= | ||
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package shh | ||
|
||
import ( | ||
"syscall" | ||
"time" | ||
|
||
"github.com/heroku/slog" | ||
"github.com/shirou/gopsutil/net" | ||
) | ||
|
||
type Netstat struct { | ||
measurements chan<- Measurement | ||
} | ||
|
||
func NewNetstatPoller(measurements chan<- Measurement) Netstat { | ||
return Netstat{measurements: measurements} | ||
} | ||
|
||
func getStats() (map[string]int, error) { | ||
conns, err := net.Connections("all") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
counts := make(map[string]int) | ||
counts["UDP"] = 0 | ||
for _, conn := range conns { | ||
if conn.Type == syscall.SOCK_DGRAM { | ||
counts["UDP"]++ | ||
continue | ||
} | ||
c, ok := counts[conn.Status] | ||
if !ok { | ||
counts[conn.Status] = 0 | ||
} | ||
counts[conn.Status] = c + 1 | ||
} | ||
|
||
fields := map[string]int{ | ||
"tcp_established": counts["ESTABLISHED"], | ||
"tcp_syn_sent": counts["SYN_SENT"], | ||
"tcp_syn_recv": counts["SYN_RECV"], | ||
"tcp_fin_wait1": counts["FIN_WAIT1"], | ||
"tcp_fin_wait2": counts["FIN_WAIT2"], | ||
"tcp_time_wait": counts["TIME_WAIT"], | ||
"tcp_close": counts["CLOSE"], | ||
"tcp_close_wait": counts["CLOSE_WAIT"], | ||
"tcp_last_ack": counts["LAST_ACK"], | ||
"tcp_listen": counts["LISTEN"], | ||
"tcp_closing": counts["CLOSING"], | ||
"tcp_none": counts["NONE"], | ||
"udp_socket": counts["UDP"], | ||
} | ||
|
||
return fields, nil | ||
} | ||
|
||
func (poller Netstat) Poll(tick time.Time) { | ||
ctx := slog.Context{"poller": poller.Name(), "fn": "Poll", "tick": tick} | ||
|
||
stats, err := getStats() | ||
if err != nil { | ||
LogError(ctx, err, "Error reading netconn") | ||
return | ||
} | ||
|
||
for field, _ := range stats { | ||
poller.measurements <- GaugeMeasurement{tick, poller.Name(), []string{field}, uint64(stats[field]), Connections} | ||
} | ||
} | ||
|
||
func (poller Netstat) Name() string { | ||
return "netstat" | ||
} | ||
|
||
func (poller Netstat) Exit() {} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
What happens if one of these
counts[KEY_NAME]
is not present on the map? Are we confident these headings are the same across different distros and/or OSes?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.
Good question, this library is providing this interface https://github.com/shirou/gopsutil - even if the value is 0 the count field and label is still there. These are all standard tcp connection state machine states so these should be consistent across all OSes and distros.
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.
There does appear to be subtle differences between the different statuses as you go between Oses.
ref: windows
ref: linux
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.
In my opinion, we can focus on linux and leave windows aside. If someone really wants to run shh on windows, they are my guest to fix it if the netstat poller would become a problem.