]> Cypherpunks.ru repositories - nncp.git/blob - 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
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Show queued NNCP Rx/Tx stats.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "log"
25         "os"
26         "sort"
27
28         "cypherpunks.ru/nncp"
29         "github.com/dustin/go-humanize"
30 )
31
32 func usage() {
33         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
34         fmt.Fprintf(os.Stderr, "nncp-stat -- show queued Rx/Tx stats\n\n")
35         fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
36         flag.PrintDefaults()
37 }
38
39 func main() {
40         var (
41                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
42                 nodeRaw   = flag.String("node", "", "Process only that node")
43                 spoolPath = flag.String("spool", "", "Override path to spool")
44                 debug     = flag.Bool("debug", false, "Print debug messages")
45                 version   = flag.Bool("version", false, "Print version information")
46                 warranty  = flag.Bool("warranty", false, "Print warranty information")
47         )
48         flag.Usage = usage
49         flag.Parse()
50         if *warranty {
51                 fmt.Println(nncp.Warranty)
52                 return
53         }
54         if *version {
55                 fmt.Println(nncp.VersionGet())
56                 return
57         }
58
59         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", false, *debug)
60         if err != nil {
61                 log.Fatalln("Error during initialization:", err)
62         }
63
64         var nodeOnly *nncp.Node
65         if *nodeRaw != "" {
66                 nodeOnly, err = ctx.FindNode(*nodeRaw)
67                 if err != nil {
68                         log.Fatalln("Invalid -node specified:", err)
69                 }
70         }
71
72         nodeNames := make([]string, 0, len(ctx.Neigh))
73         nodeNameToNode := make(map[string]*nncp.Node, len(ctx.Neigh))
74         for _, node := range ctx.Neigh {
75                 nodeNames = append(nodeNames, node.Name)
76                 nodeNameToNode[node.Name] = node
77         }
78         sort.Strings(nodeNames)
79
80         var node *nncp.Node
81         for _, nodeName := range nodeNames {
82                 node = nodeNameToNode[nodeName]
83                 if nodeOnly != nil && *node.Id != *nodeOnly.Id {
84                         continue
85                 }
86                 rxNums := make(map[uint8]int)
87                 rxBytes := make(map[uint8]int64)
88                 for job := range ctx.Jobs(node.Id, nncp.TRx) {
89                         job.Fd.Close()
90                         rxNums[job.PktEnc.Nice] = rxNums[job.PktEnc.Nice] + 1
91                         rxBytes[job.PktEnc.Nice] = rxBytes[job.PktEnc.Nice] + job.Size
92                 }
93                 txNums := make(map[uint8]int)
94                 txBytes := make(map[uint8]int64)
95                 for job := range ctx.Jobs(node.Id, nncp.TTx) {
96                         job.Fd.Close()
97                         txNums[job.PktEnc.Nice] = txNums[job.PktEnc.Nice] + 1
98                         txBytes[job.PktEnc.Nice] = txBytes[job.PktEnc.Nice] + job.Size
99                 }
100                 fmt.Println(node.Name)
101                 var nice uint8
102                 for nice = 1; nice > 0; nice++ {
103                         rxNum, rxExists := rxNums[nice]
104                         txNum, txExists := txNums[nice]
105                         if !(rxExists || txExists) {
106                                 continue
107                         }
108                         fmt.Printf(
109                                 "\tnice:% 4s | Rx: % 10s, % 3d pkts | Tx: % 10s, % 3d pkts\n",
110                                 nncp.NicenessFmt(nice),
111                                 humanize.IBytes(uint64(rxBytes[nice])),
112                                 rxNum,
113                                 humanize.IBytes(uint64(txBytes[nice])),
114                                 txNum,
115                         )
116                 }
117         }
118 }