This repository was archived by the owner on Mar 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from weaveworks/issues/7-blocked-connection-me…
…trics Expose blocked connection metrics
- Loading branch information
Showing
6 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[global] | ||
logfile="/dev/null" | ||
plugin="/usr/lib/ulogd/ulogd_inppkt_NFLOG.so" | ||
plugin="/usr/lib/ulogd/ulogd_raw2packet_BASE.so" | ||
plugin="/usr/lib/ulogd/ulogd_output_PCAP.so" | ||
stack=log1:NFLOG,base1:BASE,pcap1:PCAP | ||
|
||
[log1] | ||
group=86 | ||
|
||
[pcap1] | ||
file="/var/log/ulogd.pcap" | ||
sync=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package metrics | ||
|
||
import ( | ||
log "github.com/Sirupsen/logrus" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/layers" | ||
"github.com/google/gopacket/pcapgo" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
var ( | ||
blockedConnections = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "weavenpc_blocked_connections_total", | ||
Help: "Connection attempts blocked by policy controller.", | ||
}, | ||
[]string{"protocol", "dport"}, | ||
) | ||
) | ||
|
||
func gatherMetrics() { | ||
pipe, err := os.Open("/var/log/ulogd.pcap") | ||
if err != nil { | ||
log.Fatalf("Failed to open pcap: %v", err) | ||
} | ||
|
||
reader, err := pcapgo.NewReader(pipe) | ||
if err != nil { | ||
log.Fatalf("Failed to read pcap header: %v", err) | ||
} | ||
|
||
for { | ||
data, _, err := reader.ReadPacketData() | ||
if err != nil { | ||
log.Fatalf("Failed to read pcap packet: %v", err) | ||
} | ||
|
||
packet := gopacket.NewPacket(data, layers.LayerTypeIPv4, gopacket.Default) | ||
|
||
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil { | ||
tcp, _ := tcpLayer.(*layers.TCP) | ||
if tcp.SYN && !tcp.ACK { // Only plain SYN constitutes a NEW TCP connection | ||
blockedConnections.With(prometheus.Labels{"protocol": "tcp", "dport": strconv.Itoa(int(tcp.DstPort))}).Inc() | ||
continue | ||
} | ||
} | ||
|
||
if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer != nil { | ||
udp, _ := udpLayer.(*layers.UDP) | ||
blockedConnections.With(prometheus.Labels{"protocol": "udp", "dport": strconv.Itoa(int(udp.DstPort))}).Inc() | ||
continue | ||
} | ||
} | ||
} | ||
|
||
func Start(addr string) error { | ||
if err := prometheus.Register(blockedConnections); err != nil { | ||
return err | ||
} | ||
|
||
http.Handle("/metrics", promhttp.Handler()) | ||
|
||
go func() { | ||
log.Infof("Serving /metrics on %s", addr) | ||
if err := http.ListenAndServe(addr, nil); err != nil { | ||
log.Fatalf("Failed to bind metrics server: %v", err) | ||
} | ||
}() | ||
|
||
go gatherMetrics() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ulogd | ||
|
||
import ( | ||
log "github.com/Sirupsen/logrus" | ||
"io" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func waitForExit(cmd *exec.Cmd) { | ||
if err := cmd.Wait(); err != nil { | ||
log.Fatalf("ulogd terminated: %v", err) | ||
} | ||
log.Fatal("ulogd terminated normally") | ||
} | ||
|
||
func Start() error { | ||
cmd := exec.Command("/usr/sbin/ulogd", "-v") | ||
stdout, err := cmd.StderrPipe() | ||
if err != nil { | ||
return err | ||
} | ||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
go io.Copy(os.Stdout, stdout) | ||
go waitForExit(cmd) | ||
return nil | ||
} |