]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-stat/main.go
Sort nodes by name in nncp-stat output
[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-2017 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         "io/ioutil"
26         "log"
27         "os"
28         "sort"
29
30         "cypherpunks.ru/nncp"
31         "github.com/dustin/go-humanize"
32 )
33
34 func usage() {
35         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
36         fmt.Fprintln(os.Stderr, "nncp-stat -- show queued Rx/Tx stats\n")
37         fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
38         flag.PrintDefaults()
39 }
40
41 func main() {
42         var (
43                 cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
44                 nodeRaw  = flag.String("node", "", "Process only that node")
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         cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
61         if err != nil {
62                 log.Fatalln("Can not read config:", err)
63         }
64         ctx, err := nncp.CfgParse(cfgRaw)
65         if err != nil {
66                 log.Fatalln("Can not parse config:", err)
67         }
68         ctx.Debug = *debug
69
70         var nodeOnly *nncp.Node
71         if *nodeRaw != "" {
72                 nodeOnly, err = ctx.FindNode(*nodeRaw)
73                 if err != nil {
74                         log.Fatalln("Invalid -node specified:", err)
75                 }
76         }
77
78         nodeNames := make([]string, 0, len(ctx.Neigh))
79         nodeNameToNode := make(map[string]*nncp.Node, len(ctx.Neigh))
80         for _, node := range ctx.Neigh {
81                 nodeNames = append(nodeNames, node.Name)
82                 nodeNameToNode[node.Name] = node
83         }
84         sort.Strings(nodeNames)
85
86         var node *nncp.Node
87         for _, nodeName := range nodeNames {
88                 node = nodeNameToNode[nodeName]
89                 if nodeOnly != nil && *node.Id != *nodeOnly.Id {
90                         continue
91                 }
92                 rxNums := make(map[uint8]int)
93                 rxBytes := make(map[uint8]int64)
94                 for job := range ctx.Jobs(node.Id, nncp.TRx) {
95                         job.Fd.Close()
96                         rxNums[job.PktEnc.Nice] = rxNums[job.PktEnc.Nice] + 1
97                         rxBytes[job.PktEnc.Nice] = rxBytes[job.PktEnc.Nice] + job.Size
98                 }
99                 txNums := make(map[uint8]int)
100                 txBytes := make(map[uint8]int64)
101                 for job := range ctx.Jobs(node.Id, nncp.TTx) {
102                         job.Fd.Close()
103                         txNums[job.PktEnc.Nice] = txNums[job.PktEnc.Nice] + 1
104                         txBytes[job.PktEnc.Nice] = txBytes[job.PktEnc.Nice] + job.Size
105                 }
106                 fmt.Println(node.Name)
107                 for nice := 0; nice < 256; nice++ {
108                         rxNum, rxExists := rxNums[uint8(nice)]
109                         txNum, txExists := txNums[uint8(nice)]
110                         if !(rxExists || txExists) {
111                                 continue
112                         }
113                         fmt.Printf(
114                                 "\tnice:% 3d | Rx: % 10s, % 3d pkts | Tx: % 10s, % 3d pkts\n",
115                                 nice,
116                                 humanize.IBytes(uint64(rxBytes[uint8(nice)])),
117                                 rxNum,
118                                 humanize.IBytes(uint64(txBytes[uint8(nice)])),
119                                 txNum,
120                         )
121                 }
122         }
123 }