Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ go 1.15

require (
github.com/heroku/slog v0.0.0-20140402171358-7fea2d3038fa
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/xuyu/goredis v0.0.0-20140820021013-517ef62abb23
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
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=
76 changes: 76 additions & 0 deletions netstat_poller.go
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"],
Comment on lines +39 to +52
Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

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

Copy link
Contributor

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.

}

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() {}
2 changes: 2 additions & 0 deletions pollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func NewMultiPoller(measurements chan<- Measurement, config Config) Multi {
mp.RegisterPoller(NewProcessesPoller(measurements, config))
case "self":
mp.RegisterPoller(NewSelfPoller(measurements, config))
case "netstat":
mp.RegisterPoller(NewNetstatPoller(measurements))
case "conntrack":
mp.RegisterPoller(NewConntrackPoller(measurements))
case "syslogngstats":
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/go-ole/go-ole/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions vendor/github.com/go-ole/go-ole/ChangeLog.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/go-ole/go-ole/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions vendor/github.com/go-ole/go-ole/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions vendor/github.com/go-ole/go-ole/appveyor.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading