This repository has been archived by the owner on Nov 29, 2020. It is now read-only.
forked from abh/geodns
/
process-stats.go
216 lines (176 loc) · 4.04 KB
/
process-stats.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"bufio"
"encoding/json"
"flag"
"log"
"net/http"
"os"
"strings"
"sync"
"github.com/hpcloud/tail"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/abh/geodns/countries"
"github.com/abh/geodns/querylog"
)
// TODO:
// Add vendor yes/no
// add server region tag (identifier)?
const UserAgent = "geodns-logs/2.0"
func main() {
log.Printf("Starting %q", UserAgent)
identifierFlag := flag.String("identifier", "", "identifier (hostname, pop name or similar)")
// verboseFlag := flag.Bool("verbose", false, "verbose output")
flag.Parse()
var serverID string
// var serverGroups []string
if len(*identifierFlag) > 0 {
ids := strings.Split(*identifierFlag, ",")
serverID = ids[0]
if len(ids) > 1 {
// serverGroups = ids[1:]
}
}
if len(serverID) == 0 {
var err error
serverID, err = os.Hostname()
if err != nil {
log.Printf("Could not get hostname: %s", err)
os.Exit(2)
}
}
queries = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "dns_logs_total",
Help: "Number of served queries",
},
[]string{"zone", "vendor", "usercc", "poolcc", "qtype"},
)
prometheus.MustRegister(queries)
buildInfo := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "geodns_logs_build_info",
Help: "GeoDNS logs build information (in labels)",
},
[]string{"Version"},
)
prometheus.MustRegister(buildInfo)
buildInfo.WithLabelValues(UserAgent).Set(1)
http.Handle("/metrics", promhttp.Handler())
go func() {
err := http.ListenAndServe(":8054", nil)
if err != nil {
log.Printf("could not start http server: %s", err)
}
}()
if len(flag.Args()) < 1 {
log.Printf("filename to process required")
os.Exit(2)
}
filename := flag.Arg(0)
logf, err := tail.TailFile(filename, tail.Config{
// Location: &tail.SeekInfo{-1, 0},
Poll: true, // inotify is flaky on EL6, so try this ...
ReOpen: true,
MustExist: false,
Follow: true,
})
if err != nil {
log.Printf("Could not tail '%s': %s", filename, err)
}
in := make(chan string)
go processChan(in, nil)
for line := range logf.Lines {
if line.Err != nil {
log.Printf("Error tailing file: %s", line.Err)
}
in <- line.Text
}
}
var extraValidLabels = map[string]struct{}{
"uk": struct{}{},
"_status": struct{}{},
"_country": struct{}{},
"www": struct{}{},
"nag-test": struct{}{},
}
func validCC(label string) bool {
if _, ok := countries.CountryContinent[label]; ok {
return true
}
if _, ok := countries.ContinentCountries[label]; ok {
return true
}
if _, ok := countries.RegionGroupRegions[label]; ok {
return true
}
if _, ok := countries.RegionGroups[label]; ok {
return true
}
if _, ok := extraValidLabels[label]; ok {
return true
}
return false
}
func getPoolCC(label string) (string, bool) {
l := dns.SplitDomainName(label)
// log.Printf("LABEL: %+v", l)
if len(l) == 0 {
return "", true
}
for _, cc := range l {
if validCC(cc) {
return cc, true
}
}
if len(l[0]) == 1 && strings.ContainsAny(l[0], "01234") {
if len(l) == 1 {
return "", true
}
}
// log.Printf("LABEL '%s' unhandled cc...", label)
return "", false
}
func processChan(in chan string, wg *sync.WaitGroup) error {
e := querylog.Entry{}
stats := NewStats()
for line := range in {
err := json.Unmarshal([]byte(line), &e)
if err != nil {
log.Printf("Can't unmarshal '%s': %s", line, err)
return err
}
e.Name = strings.ToLower(e.Name)
// fmt.Printf("%s %s\n", e.Origin, e.Name)
err = stats.Add(&e)
if err != nil {
return err
}
}
if wg != nil {
wg.Done()
}
return nil
}
func processFile(file string, out chan<- *Stats) error {
fh, err := os.Open(file)
if err != nil {
return err
}
in := make(chan string)
wg := sync.WaitGroup{}
wg.Add(1)
go processChan(in, &wg)
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
in <- scanner.Text()
}
if err := scanner.Err(); err != nil {
log.Println("reading standard input:", err)
}
close(in)
wg.Wait()
return nil
}