vnstat

package module
v0.0.0-...-bd9ef34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2024 License: MPL-2.0 Imports: 7 Imported by: 0

README

vnstat-in-go

This is a simple command line observing library for vnstat written in Go.

Requirements

Installation

go get github.com/snowmerak/vnstat-in-go

Usage

package main

import (
	"context"
	"github.com/snowmerak/vnstat-in-go"
	"log"
	"os"
	"os/signal"
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	w := vnstat.NewWatcher(ctx, vnstat.UnitMbit, 128, nil)
	if err := w.Watch(ctx, "en0", func(t vnstat.Traffic) {
		log.Printf("RxTraffic: %f, RxPacket: %d, TxTraffic: %f, TxPacket: %d\n", t.RxTraffic, t.RxPacket, t.TxTraffic, t.TxPacket)
	}); err != nil {
		log.Fatal(err)
	}

	<-ctx.Done()
}

Documentation

Index

Constants

View Source
const (
	UnitGbit  = 1e9
	UnitGbyte = 1e9 * 8
	UnitMbit  = 1e6
	UnitMbyte = 1e6 * 8
	UnitKbit  = 1e3
	UnitKbyte = 1e3 * 8
	UnitBit   = 1
	UnitByte  = 8
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

type Traffic

type Traffic struct {
	Time      time.Time
	RxTraffic float64
	RxPacket  uint64
	TxTraffic float64
	TxPacket  uint64
}

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

func NewWatcher

func NewWatcher(ctx context.Context, baseUnitSize float64, bufferSize int, logger Logger) *Watcher

func (*Watcher) Watch

func (w *Watcher) Watch(ctx context.Context, interfaceName string, callback func(Traffic)) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL