-
Notifications
You must be signed in to change notification settings - Fork 1
/
varnishncsa.go
47 lines (40 loc) · 1.09 KB
/
varnishncsa.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
package main
import (
"fmt"
"log"
"os/exec"
"bufio"
"os"
"github.com/quipo/statsd"
"time"
)
type Varnishncsa struct {
StatsdServer string
StatsdPrefix string
StatsdSendInterval int
}
func (varnishncsa *Varnishncsa) Connect() {
statsdclient := statsd.NewStatsdClient(varnishncsa.StatsdServer, varnishncsa.StatsdPrefix)
statsdclient.CreateSocket()
interval := time.Duration(int(time.Millisecond) * varnishncsa.StatsdSendInterval)
stats := statsd.NewStatsdBuffer(interval, statsdclient)
defer stats.Close()
cmd := exec.Command("varnishncsa", "-F", `%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i" %{Varnish:time_firstbyte}x`)
stdout, outerr := cmd.StdoutPipe()
if outerr != nil {
log.Fatal(outerr)
}
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
r := Record{scanner.Text(), stats}
r.Process()
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "There was an error with the scanner attached to varnishncsa", err)
}
cmd.Wait()
}