]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/cmd/nncp-stat/main.go
Forbid any later GNU GPL versions autousage
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-stat / main.go
index f5b02875ef36675e213faccc6767c8b4ada10674..bcda35526921efbf6f4ee535eaf4586306d8a33b 100644 (file)
@@ -1,11 +1,10 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, version 3 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -16,15 +15,15 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-// Show queued NNCP Rx/Tx stats
+// Show queued NNCP Rx/Tx stats.
 package main
 
 import (
        "flag"
        "fmt"
-       "io/ioutil"
        "log"
        "os"
+       "sort"
 
        "cypherpunks.ru/nncp"
        "github.com/dustin/go-humanize"
@@ -32,18 +31,19 @@ import (
 
 func usage() {
        fmt.Fprintf(os.Stderr, nncp.UsageHeader())
-       fmt.Fprintln(os.Stderr, "nncp-stat -- show queued Rx/Tx stats\n")
+       fmt.Fprintf(os.Stderr, "nncp-stat -- show queued Rx/Tx stats\n\n")
        fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
        flag.PrintDefaults()
 }
 
 func main() {
        var (
-               cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
-               nodeRaw  = flag.String("node", "", "Process only that node")
-               debug    = flag.Bool("debug", false, "Print debug messages")
-               version  = flag.Bool("version", false, "Print version information")
-               warranty = flag.Bool("warranty", false, "Print warranty information")
+               cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
+               nodeRaw   = flag.String("node", "", "Process only that node")
+               spoolPath = flag.String("spool", "", "Override path to spool")
+               debug     = flag.Bool("debug", false, "Print debug messages")
+               version   = flag.Bool("version", false, "Print version information")
+               warranty  = flag.Bool("warranty", false, "Print warranty information")
        )
        flag.Usage = usage
        flag.Parse()
@@ -56,15 +56,10 @@ func main() {
                return
        }
 
-       cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
+       ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", false, *debug)
        if err != nil {
-               log.Fatalln("Can not read config:", err)
+               log.Fatalln("Error during initialization:", err)
        }
-       ctx, err := nncp.CfgParse(cfgRaw)
-       if err != nil {
-               log.Fatalln("Can not parse config:", err)
-       }
-       ctx.Debug = *debug
 
        var nodeOnly *nncp.Node
        if *nodeRaw != "" {
@@ -74,37 +69,48 @@ func main() {
                }
        }
 
-       for nodeId, node := range ctx.Neigh {
-               if nodeOnly != nil && nodeId != *nodeOnly.Id {
+       nodeNames := make([]string, 0, len(ctx.Neigh))
+       nodeNameToNode := make(map[string]*nncp.Node, len(ctx.Neigh))
+       for _, node := range ctx.Neigh {
+               nodeNames = append(nodeNames, node.Name)
+               nodeNameToNode[node.Name] = node
+       }
+       sort.Strings(nodeNames)
+
+       var node *nncp.Node
+       for _, nodeName := range nodeNames {
+               node = nodeNameToNode[nodeName]
+               if nodeOnly != nil && *node.Id != *nodeOnly.Id {
                        continue
                }
                rxNums := make(map[uint8]int)
                rxBytes := make(map[uint8]int64)
-               for job := range ctx.Jobs(&nodeId, nncp.TRx) {
+               for job := range ctx.Jobs(node.Id, nncp.TRx) {
                        job.Fd.Close()
                        rxNums[job.PktEnc.Nice] = rxNums[job.PktEnc.Nice] + 1
                        rxBytes[job.PktEnc.Nice] = rxBytes[job.PktEnc.Nice] + job.Size
                }
                txNums := make(map[uint8]int)
                txBytes := make(map[uint8]int64)
-               for job := range ctx.Jobs(&nodeId, nncp.TTx) {
+               for job := range ctx.Jobs(node.Id, nncp.TTx) {
                        job.Fd.Close()
                        txNums[job.PktEnc.Nice] = txNums[job.PktEnc.Nice] + 1
                        txBytes[job.PktEnc.Nice] = txBytes[job.PktEnc.Nice] + job.Size
                }
                fmt.Println(node.Name)
-               for nice := 0; nice < 256; nice++ {
-                       rxNum, rxExists := rxNums[uint8(nice)]
-                       txNum, txExists := txNums[uint8(nice)]
+               var nice uint8
+               for nice = 1; nice > 0; nice++ {
+                       rxNum, rxExists := rxNums[nice]
+                       txNum, txExists := txNums[nice]
                        if !(rxExists || txExists) {
                                continue
                        }
                        fmt.Printf(
-                               "\tnice:% 3d | Rx: % 10s, % 3d pkts | Tx: % 10s, % 3d pkts\n",
-                               nice,
-                               humanize.IBytes(uint64(rxBytes[uint8(nice)])),
+                               "\tnice:% 4s | Rx: % 10s, % 3d pkts | Tx: % 10s, % 3d pkts\n",
+                               nncp.NicenessFmt(nice),
+                               humanize.IBytes(uint64(rxBytes[nice])),
                                rxNum,
-                               humanize.IBytes(uint64(txBytes[uint8(nice)])),
+                               humanize.IBytes(uint64(txBytes[nice])),
                                txNum,
                        )
                }