stop using a shell script to complicate things

This commit is contained in:
Aaron Bieber 2022-11-17 15:55:16 -07:00
parent 6658d29123
commit 04b11abad8
No known key found for this signature in database
3 changed files with 23 additions and 52 deletions

View File

@ -15,7 +15,7 @@
in { in {
tsvnstat = pkgs.buildGoModule { tsvnstat = pkgs.buildGoModule {
pname = "tsvnstat"; pname = "tsvnstat";
version = "v0.0.5"; version = "v0.0.6";
src = ./.; src = ./.;
vendorSha256 = "sha256-RNhu1gQ62Hg8Fhiw9uIS+b4YaAHtZMaqbelMKv+oZjM="; vendorSha256 = "sha256-RNhu1gQ62Hg8Fhiw9uIS+b4YaAHtZMaqbelMKv+oZjM=";

View File

@ -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

59
main.go
View File

@ -16,12 +16,18 @@ import (
"tailscale.com/tsnet" "tailscale.com/tsnet"
) )
//go:embed generate_images.sh
var genScript []byte
//go:embed style.css //go:embed style.css
var style string 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() { func main() {
tmpDir, err := os.MkdirTemp("", "tsvnstat") tmpDir, err := os.MkdirTemp("", "tsvnstat")
if err != nil { if err != nil {
@ -30,29 +36,10 @@ func main() {
defer os.RemoveAll(tmpDir) 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") name := flag.String("name", "", "name of service")
dir := flag.String("dir", tmpDir, "directory containing vnstat images") dir := flag.String("dir", tmpDir, "directory containing vnstat images")
key := flag.String("key", "", "path to file containing the api key") 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() flag.Parse()
s := &tsnet.Server{ s := &tsnet.Server{
@ -80,27 +67,25 @@ func main() {
go func() { go func() {
for { for {
log.Printf("running %q in %q", tmpFile.Name(), tmpDir)
ifaces, err := net.Interfaces() ifaces, err := net.Interfaces()
if err != nil { if err != nil {
log.Fatal("can't get interfaces...", err) log.Fatal("can't get interfaces...", err)
} }
cmd := []string{tmpFile.Name()} for _, iface := range ifaces {
for _, intf := range ifaces { if iface.Flags&net.FlagUp == 0 {
cmd = append(cmd, intf.Name) 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) time.Sleep(5 * time.Minute)
} }
}() }()