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