This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
whois.go
102 lines (86 loc) · 2.9 KB
/
whois.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/ylacancellera/galera-log-explainer/regex"
"github.com/ylacancellera/galera-log-explainer/types"
"github.com/ylacancellera/galera-log-explainer/utils"
)
type whois struct {
Search string `arg:"" name:"search" help:"the identifier (node name, ip, uuid, hash) to search"`
Paths []string `arg:"" name:"paths" help:"paths of the log to use"`
}
func (w *whois) Help() string {
return `Take any type of info pasted from error logs and find out about it.
It will list known node name(s), IP(s), hostname(s), and other known node's UUIDs.
`
}
func (w *whois) Run() error {
toCheck := regex.AllRegexes()
timeline, err := timelineFromPaths(CLI.Whois.Paths, toCheck)
if err != nil {
return errors.Wrap(err, "Found nothing to translate")
}
ctxs := timeline.GetLatestUpdatedContextsByNodes()
ni := whoIs(ctxs, CLI.Whois.Search)
json, err := json.MarshalIndent(ni, "", "\t")
if err != nil {
return err
}
fmt.Println(string(json))
return nil
}
func whoIs(ctxs map[string]types.LogCtx, search string) types.NodeInfo {
ni := types.NodeInfo{Input: search}
if regex.IsNodeUUID(search) {
search = utils.UUIDToShortUUID(search)
}
var (
ips []string
hashes []string
nodenames []string
)
for _, ctx := range ctxs {
if utils.SliceContains(ctx.OwnNames, search) || utils.SliceContains(ctx.OwnHashes, search) || utils.SliceContains(ctx.OwnIPs, search) {
ni.NodeNames = ctx.OwnNames
ni.NodeUUIDs = ctx.OwnHashes
ni.IPs = ctx.OwnIPs
ni.Hostname = ctx.OwnHostname()
}
if nodename, ok := ctx.HashToNodeName[search]; ok {
nodenames = utils.SliceMergeDeduplicate(nodenames, []string{nodename})
hashes = utils.SliceMergeDeduplicate(hashes, []string{search})
}
if ip, ok := ctx.HashToIP[search]; ok {
ips = utils.SliceMergeDeduplicate(ips, []string{ip})
hashes = utils.SliceMergeDeduplicate(hashes, []string{search})
} else if nodename, ok := ctx.IPToNodeName[search]; ok {
nodenames = utils.SliceMergeDeduplicate(nodenames, []string{nodename})
ips = utils.SliceMergeDeduplicate(ips, []string{search})
} else if utils.SliceContains(ctx.AllNodeNames(), search) {
nodenames = utils.SliceMergeDeduplicate(nodenames, []string{search})
}
for _, nodename := range nodenames {
hashes = utils.SliceMergeDeduplicate(hashes, ctx.HashesFromNodeName(nodename))
ips = utils.SliceMergeDeduplicate(ips, ctx.IPsFromNodeName(nodename))
}
for _, ip := range ips {
hashes = utils.SliceMergeDeduplicate(hashes, ctx.HashesFromIP(ip))
nodename, ok := ctx.IPToNodeName[ip]
if ok {
nodenames = utils.SliceMergeDeduplicate(nodenames, []string{nodename})
}
}
for _, hash := range hashes {
nodename, ok := ctx.HashToNodeName[hash]
if ok {
nodenames = utils.SliceMergeDeduplicate(nodenames, []string{nodename})
}
}
}
ni.NodeNames = nodenames
ni.NodeUUIDs = hashes
ni.IPs = ips
return ni
}