forked from openshift/origin-aggregated-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-logs.go
139 lines (117 loc) · 3.96 KB
/
check-logs.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
args := os.Args[1:]
kibana_pod := args[0]
es_svc := args[1]
index := args[2]
filePath := args[3] // set to "journal" to look in the journal
querySize := args[4]
userName := args[5]
userToken := args[6]
testIP := args[7]
journal := os.Getenv("USE_JOURNAL")
verbose := false
if os.Getenv("VERBOSE") != "" {
verbose = true
}
//we want the hostname witout a domain for the most generic search
hostname, _ := os.Hostname()
hostname = strings.Split(hostname, ".")[0]
// instead of receiving jsonStream as an Arg, we'll make the call ourselves...
proxyHeaders := `-H 'X-Proxy-Remote-User: ` + userName + `' -H 'Authorization: Bearer ` + userToken + `' -H 'X-Forwarded-For: ` + testIP + `'`
queryCommand := `oc exec ` + kibana_pod + ` -- curl -s --key /etc/kibana/keys/key --cert /etc/kibana/keys/cert --cacert /etc/kibana/keys/ca ` + proxyHeaders + ` -XGET "https://` + es_svc + `/` + index + `.*/_search?q=hostname:` + hostname + `&fields=message&size=` + querySize + `"`
if verbose {
fmt.Printf("Executing command [%s]\n", queryCommand)
}
queryCmdName := "bash"
queryCmdArgs := []string{"-c", queryCommand}
queryCmd := exec.Command(queryCmdName, queryCmdArgs...)
byt, err := queryCmd.Output()
if err != nil {
fmt.Printf("output [%s]\nerror [%v]", byt, err)
panic(err)
}
type Fields struct {
Message []string `json:"message"`
}
type Hit struct {
Index string `json:"_index"`
Type string `json:"_type"`
Id string `json:"_id"`
Score float32 `json:"_score"`
Fields Fields `json:"fields"`
}
type Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Failed int `json:"failed"`
}
type Hits struct {
Total int `json:"total"`
Max_score float32 `json:"max_score"`
Hits []Hit `json:"hits"`
}
type ESResponse struct {
Took int `json:"took"`
Timed_out bool `json:"timed_out"`
Shards Shards `json:"_shards"`
Hits Hits `json:"hits"`
}
dat := ESResponse{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
var missesBuffer bytes.Buffer
totalEntries := len(dat.Hits.Hits)
foundEntries := totalEntries
for _, record := range dat.Hits.Hits {
// for each message, we need to check the logs
message := record.Fields.Message[0]
searchCmd := ""
if filePath == "journal" || journal == "true" {
// escape certain characters that were being interpreted by bash
message = strings.Replace(message, `\`, `\\`, -1)
message = strings.Replace(message, `"`, `\"`, -1)
message = strings.Replace(message, "`", `\`+"`", -1)
message = strings.Replace(message, `$`, `\$`, -1)
searchCmd = `journalctl MESSAGE="` + message + `"`
} else {
// escape certain characters that were being interpreted by grep -e "" or bash
message = strings.Replace(message, `\`, `\\\\`, -1)
message = strings.Replace(message, `[`, `\[`, -1)
message = strings.Replace(message, `]`, `\]`, -1)
message = strings.Replace(message, `*`, `\*`, -1)
message = strings.Replace(message, `"`, `\"`, -1)
message = strings.Replace(message, "`", `\`+"`", -1)
message = strings.Replace(message, `$`, `\$`, -1)
searchCmd = `grep "` + message + `" ` + filePath
}
cmdName := "bash"
cmdArgs := []string{"-c", searchCmd}
cmd := exec.Command(cmdName, cmdArgs...)
_, err := cmd.Output()
if err != nil {
foundEntries -= 1
missesBuffer.WriteString(" ! Log entry '")
missesBuffer.WriteString(message)
missesBuffer.WriteString("' was not found in path\n")
}
}
if foundEntries == totalEntries {
if totalEntries == 0 {
fmt.Printf("Failure - no log entries found in Elasticsearch %s for index %s\n", es_svc, index)
} else {
fmt.Printf("Success - [%v/%v] log entries found in %s\n", foundEntries, totalEntries, filePath)
}
} else {
fmt.Printf("Failure - [%v/%v] log entries found in %s\n%s", foundEntries, totalEntries, filePath, missesBuffer.String())
}
}