百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

go-netflow

> 编程语言
开源

使用 netflow,捕获进出流量,类似于 c Nethogs。

347 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

使用 netflow,捕获进出流量,类似于 c Nethogs。

go-netflow

go-netflow, capture process in/out traffic, similar to c Nethogs.

使用 golang 实现进程级流量监控

refer

refer logic design link

  • https://zhuanlan.zhihu.com/p/49981590

refer nethogs source link

  • https://github.com/raboof/nethogs

dep

bash
yum install libpcap
yum install libpcap-devel

cli usage

netflow cli run:

go run cmd/main.go

stdout:

…

sdk simple usage:

go
package main

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/rfyiamcool/go-netflow"
)

func main() {
	nf, err := netflow.New(
		netflow.WithCaptureTimeout(5 * time.Second),
	)
	if err != nil {
		panic(err)
	}

	err = nf.Start()
	if err != nil {
		panic(err)
	}
	defer nf.Stop()

	<-nf.Done()

	var (
		limit     = 5
		recentSec = 5
	)

	rank, err := nf.GetProcessRank(limit, recentSec)
	if err != nil {
		panic(err)
	}

	bs, err := json.MarshalIndent(rank, "", "    ")
	if err != nil {
		panic(err)
	}

	fmt.Println(string(bs))
}

how to use sdk of go-netflow:

set pcap filename

Don't save pcap file by default.

WithStorePcap option is used to save pcap file, use tcpdump -nnr {filename} command to read pcap file.

WithStorePcap(fpath string)

set custom pcap bpf filter.

WithPcapFilter(filter string)

set custom pcap bpf filter.

example:

  • host xiaorui.cc and port 80
  • src host 123.56.223.52 and (dst port 3389 or 22)
WithPcapFilter(filter string)

limit netflow cpu/mem resource.

WithLimitCgroup(cpu float64, mem int)

set time to capturing packet.

WithCaptureTimeout(dur time.Duration)

set time to rescan process and inode data.

WithSyncInterval(dur time.Duration)

set the number of worker to consume pcap queue.

WithWorkerNum(num int)

set custom context.

WithCtx(ctx context.Context)

set custom devices to capture.

WithBindDevices(devs []string)

set pcap queue size. if the queue is full, new packet is thrown away.

WithQueueSize(size int)

types

netflow.Interface

go
type Interface interface {
	Start() error
	Stop()
	Done() <-chan struct{}
	LoadCounter() int64
	GetProcessRank(int, int) ([]*Process, error)
}

netflow.Process

go
type Process struct {
	Name         string
	Pid          string
	Exe          string
	State        string
	Inodes       []string
	TrafficStats *trafficStatsEntry
	Ring         []*trafficEntry
}

netflow.trafficStatsEntry

go
type trafficStatsEntry struct {
	In         int64 `json:"in"`
	Out        int64 `json:"out"`
	InRate     int64 `json:"in_rate"`
	OutRate    int64 `json:"out_rate"`
	InputEWMA  int64 `json:"input_ewma" valid:"-"`
	OutputEWMA int64 `json:"output_ewma" valid:"-"`
}

netflow.trafficEntry

go
type trafficEntry struct {
	Timestamp int64 `json:"timestamp"`
	In        int64 `json:"in"`
	Out       int64 `json:"out"`
}

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Gonethogspcap

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言