diff --git a/flake.nix b/flake.nix index 5ef6ea1..1826fb7 100644 --- a/flake.nix +++ b/flake.nix @@ -15,7 +15,7 @@ in { tsvnstat = pkgs.buildGoModule { pname = "tsvnstat"; - version = "v0.0.5"; + version = "v0.0.6"; src = ./.; vendorSha256 = "sha256-RNhu1gQ62Hg8Fhiw9uIS+b4YaAHtZMaqbelMKv+oZjM="; diff --git a/generate_images.sh b/generate_images.sh deleted file mode 100755 index fb67042..0000000 --- a/generate_images.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env sh - -set -x - -for i in $@; do - vnstati --style 1 -L -s -o ${i}-s.png $i - vnstati --style 1 -L --fivegraph 576 218 -o ${i}-5g.png $i - vnstati --style 1 -L -hg -o ${i}-hg.png $i - vnstati --style 1 -L -h 24 -o ${i}-h.png $i - vnstati --style 1 -L -d 30 -o ${i}-d.png $i - vnstati --style 1 -L -t 10 -o ${i}-t.png $i - vnstati --style 1 -L -m 12 -o ${i}-m.png $i - vnstati --style 1 -L -y 5 -o ${i}-y.png $i -done diff --git a/main.go b/main.go index 7e8b4c7..b7d13a8 100644 --- a/main.go +++ b/main.go @@ -16,12 +16,18 @@ import ( "tailscale.com/tsnet" ) -//go:embed generate_images.sh -var genScript []byte - //go:embed style.css var style string +func runCmd(cmd string, dir string, args ...string) { + genCmd := exec.Command(cmd, args...) + genCmd.Dir = dir + out, err := genCmd.Output() + if err != nil { + log.Println(string(out), err) + } +} + func main() { tmpDir, err := os.MkdirTemp("", "tsvnstat") if err != nil { @@ -30,29 +36,10 @@ func main() { defer os.RemoveAll(tmpDir) - tmpFile, err := os.CreateTemp("", "generate_images.sh") - if err != nil { - log.Fatal(err) - } - - defer os.Remove(tmpFile.Name()) - - if _, err := tmpFile.Write(genScript); err != nil { - log.Fatal(err) - } - if err := tmpFile.Close(); err != nil { - log.Fatal(err) - } - - err = os.Chmod(tmpFile.Name(), 0700) - if err != nil { - log.Fatal(err) - } - name := flag.String("name", "", "name of service") dir := flag.String("dir", tmpDir, "directory containing vnstat images") key := flag.String("key", "", "path to file containing the api key") - shell := flag.String("sh", "/bin/sh", "path to interpreter") + vnstati := flag.String("vnstati", "/bin/vnstati", "path to vnstati") flag.Parse() s := &tsnet.Server{ @@ -80,27 +67,25 @@ func main() { go func() { for { - log.Printf("running %q in %q", tmpFile.Name(), tmpDir) - ifaces, err := net.Interfaces() if err != nil { log.Fatal("can't get interfaces...", err) } - cmd := []string{tmpFile.Name()} - for _, intf := range ifaces { - cmd = append(cmd, intf.Name) + for _, iface := range ifaces { + if iface.Flags&net.FlagUp == 0 { + continue + } + runCmd(*vnstati, *dir, "--style", "1", "-L", "-s", "-o", fmt.Sprintf("%s-s.png", iface.Name), iface.Name) + runCmd(*vnstati, *dir, "--style", "1", "-L", "--fivegraph", "576", "218", "-o", fmt.Sprintf("%s-5g.png", iface.Name), iface.Name) + runCmd(*vnstati, *dir, "--style", "1", "-L", "-hg", "-o", fmt.Sprintf("%s-hg.png", iface.Name), iface.Name) + runCmd(*vnstati, *dir, "--style", "1", "-L", "-h", "24", "-o", fmt.Sprintf("%s-h.png", iface.Name), iface.Name) + runCmd(*vnstati, *dir, "--style", "1", "-L", "-d", "30", "-o", fmt.Sprintf("%s-d.png", iface.Name), iface.Name) + runCmd(*vnstati, *dir, "--style", "1", "-L", "-t", "10", "-o", fmt.Sprintf("%s-t.png", iface.Name), iface.Name) + runCmd(*vnstati, *dir, "--style", "1", "-L", "-m", "12", "-o", fmt.Sprintf("%s-m.png", iface.Name), iface.Name) + runCmd(*vnstati, *dir, "--style", "1", "-L", "-y", "5", "-o", fmt.Sprintf("%s-y.png", iface.Name), iface.Name) } - genCmd := exec.Command(*shell, cmd...) - genCmd.Dir = *dir - out, err := genCmd.Output() - if err != nil { - log.Fatal("can't run generation script", err) - } - - log.Println(string(out)) - time.Sleep(5 * time.Minute) } }()