/
nginx2stathat.go
97 lines (84 loc) · 2.23 KB
/
nginx2stathat.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
package main
import (
"flag"
"fmt"
"github.com/ActiveState/tail"
"github.com/hgfischer/nginx2stathat/loghit"
"github.com/stathat/go"
"log/syslog"
"os"
)
// Command line flags and arguments
var (
statPrefix = flag.String("prefix", "", "Stat prefix. Ex.: \"`hostname -s` live site\"")
parserRoutines = flag.Int("parsers", 4, "Number of parallel routines parsing log lines and queueing them to the posters")
posterRoutines = flag.Int("posters", 4, "Number of parallel routines sending results to StatHat")
ezKey string
accessLog string
)
// Print command line help and exit application
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] [EZ Key] [access.log]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\nFlags:\n")
flag.PrintDefaults()
os.Exit(1)
}
// Parse command line
func parseCommandLine() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 2 {
fmt.Fprintf(os.Stderr, "Error: Missing arguments\n")
flag.Usage()
}
ezKey = flag.Arg(0)
accessLog = flag.Arg(1)
}
// Parse lines from tail and add to another channel
func parseLines(lines <-chan *tail.Line, hits chan<- *loghit.LogHit, errors chan<- error) {
for line := range lines {
logHit, err := loghit.New(line.Text)
if err != nil {
errors <- err
} else {
hits <- logHit
}
}
}
// Send some stats to StatHat. Currently only HTTP status counts
func postStats(prefix, ezKey string, hits <-chan *loghit.LogHit) {
for hit := range hits {
var stat string
if len(prefix) > 0 {
stat = fmt.Sprintf("%s: HTTP %d", prefix, hit.Status)
} else {
stat = fmt.Sprintf("HTTP %d", hit.Status)
}
stathat.PostEZCountTime(stat, ezKey, 1, hit.LocalTime.Unix())
}
}
// MAIN
func main() {
parseCommandLine()
t, err := tail.TailFile(accessLog, tail.Config{Follow: true, ReOpen: true, MustExist: false})
if err != nil {
panic(err)
}
hits := make(chan *loghit.LogHit)
defer close(hits)
errors := make(chan error)
defer close(errors)
for i := 0; i < *parserRoutines; i++ {
go parseLines(t.Lines, hits, errors)
}
for i := 0; i < *posterRoutines; i++ {
go postStats(*statPrefix, ezKey, hits)
}
logWriter, err := syslog.New(syslog.LOG_ERR, "nginx2stathat")
if err != nil {
panic(err)
}
for err := range errors {
logWriter.Err(err.Error())
}
}