]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-stat/main.go
Remove huge usage headers, -warranty exists anyway
[nncp.git] / src / cmd / nncp-stat / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 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         "github.com/dustin/go-humanize"
29         "go.cypherpunks.ru/nncp/v8"
30 )
31
32 func usage() {
33         fmt.Fprint(os.Stderr, "nncp-stat -- show queued Rx/Tx stats\n\n")
34         fmt.Fprintf(os.Stderr, "Usage: %s [options] [-pkt] [-node NODE]\nOptions:\n", os.Args[0])
35         flag.PrintDefaults()
36 }
37
38 func jobPrint(xx nncp.TRxTx, job nncp.Job, suffix string) {
39         fmt.Printf(
40                 "\t%s %s%s %s (nice: %s)\n",
41                 string(xx),
42                 nncp.Base32Codec.EncodeToString(job.HshValue[:]), suffix,
43                 humanize.IBytes(uint64(job.Size)),
44                 nncp.NicenessFmt(job.PktEnc.Nice),
45         )
46 }
47
48 func main() {
49         var (
50                 showPkt   = flag.Bool("pkt", false, "Show packets listing")
51                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
52                 nodeRaw   = flag.String("node", "", "Process only that node")
53                 spoolPath = flag.String("spool", "", "Override path to spool")
54                 debug     = flag.Bool("debug", false, "Print debug messages")
55                 version   = flag.Bool("version", false, "Print version information")
56                 warranty  = flag.Bool("warranty", false, "Print warranty information")
57         )
58         log.SetFlags(log.Lshortfile)
59         flag.Usage = usage
60         flag.Parse()
61         if *warranty {
62                 fmt.Println(nncp.Warranty)
63                 return
64         }
65         if *version {
66                 fmt.Println(nncp.VersionGet())
67                 return
68         }
69
70         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, "", false, false, false, *debug)
71         if err != nil {
72                 log.Fatalln("Error during initialization:", err)
73         }
74
75         var nodeOnly *nncp.Node
76         if *nodeRaw != "" {
77                 nodeOnly, err = ctx.FindNode(*nodeRaw)
78                 if err != nil {
79                         log.Fatalln("Invalid -node specified:", err)
80                 }
81         }
82
83         nodeNames := make([]string, 0, len(ctx.Neigh))
84         nodeNameToNode := make(map[string]*nncp.Node, len(ctx.Neigh))
85         for _, node := range ctx.Neigh {
86                 nodeNames = append(nodeNames, node.Name)
87                 nodeNameToNode[node.Name] = node
88         }
89         sort.Strings(nodeNames)
90
91         ctx.Umask()
92         var node *nncp.Node
93         for _, nodeName := range nodeNames {
94                 node = nodeNameToNode[nodeName]
95                 if nodeOnly != nil && *node.Id != *nodeOnly.Id {
96                         continue
97                 }
98                 fmt.Println(node.Name)
99                 rxNums := make(map[uint8]int)
100                 rxBytes := make(map[uint8]int64)
101                 noCKNums := make(map[uint8]int)
102                 noCKBytes := make(map[uint8]int64)
103                 partNums := 0
104                 partBytes := int64(0)
105                 for job := range ctx.Jobs(node.Id, nncp.TRx) {
106                         if *showPkt {
107                                 jobPrint(nncp.TRx, job, "")
108                         }
109                         rxNums[job.PktEnc.Nice] = rxNums[job.PktEnc.Nice] + 1
110                         rxBytes[job.PktEnc.Nice] = rxBytes[job.PktEnc.Nice] + job.Size
111                 }
112                 for job := range ctx.JobsNoCK(node.Id) {
113                         if *showPkt {
114                                 jobPrint(nncp.TRx, job, ".nock")
115                         }
116                         noCKNums[job.PktEnc.Nice] = noCKNums[job.PktEnc.Nice] + 1
117                         noCKBytes[job.PktEnc.Nice] = noCKBytes[job.PktEnc.Nice] + job.Size
118                 }
119                 for job := range ctx.JobsPart(node.Id) {
120                         if *showPkt {
121                                 fmt.Printf(
122                                         "\t%s %s.part %s\n",
123                                         string(nncp.TRx),
124                                         nncp.Base32Codec.EncodeToString(job.HshValue[:]),
125                                         humanize.IBytes(uint64(job.Size)),
126                                 )
127                         }
128                         partNums++
129                         partBytes += job.Size
130                 }
131                 txNums := make(map[uint8]int)
132                 txBytes := make(map[uint8]int64)
133                 for job := range ctx.Jobs(node.Id, nncp.TTx) {
134                         if *showPkt {
135                                 jobPrint(nncp.TTx, job, "")
136                         }
137                         txNums[job.PktEnc.Nice] = txNums[job.PktEnc.Nice] + 1
138                         txBytes[job.PktEnc.Nice] = txBytes[job.PktEnc.Nice] + job.Size
139                 }
140                 var nice uint8
141                 if partNums > 0 {
142                         fmt.Printf(
143                                 "\tpart: % 10s, % 3d pkts\n",
144                                 humanize.IBytes(uint64(partBytes)), partNums,
145                         )
146                 }
147                 for nice = 1; nice > 0; nice++ {
148                         rxNum, rxExists := rxNums[nice]
149                         txNum, txExists := txNums[nice]
150                         noCKNum, noCKExists := noCKNums[nice]
151                         if !(rxExists || txExists || noCKExists) {
152                                 continue
153                         }
154                         fmt.Printf(
155                                 "\tnice:% 4s | Rx: % 10s, % 3d pkts | Tx: % 10s, % 3d pkts",
156                                 nncp.NicenessFmt(nice),
157                                 humanize.IBytes(uint64(rxBytes[nice])),
158                                 rxNum,
159                                 humanize.IBytes(uint64(txBytes[nice])),
160                                 txNum,
161                         )
162                         if noCKExists {
163                                 fmt.Printf(
164                                         " | NoCK: % 10s, % 3d pkts",
165                                         humanize.IBytes(uint64(noCKBytes[nice])),
166                                         noCKNum,
167                                 )
168                         }
169                         fmt.Printf("\n")
170                 }
171         }
172 }