This repository has been 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.
Read and parse packets from the ulogd named pipe, and serve statistics via the Prometheus client.
- Loading branch information
Showing
2 changed files
with
102 additions
and
2 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
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.Fatal(err) | ||
} | ||
|
||
reader, err := pcapgo.NewReader(pipe) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for { | ||
data, _, err := reader.ReadPacketData() | ||
if err != nil { | ||
log.Fatal(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.Fatal(err) | ||
} | ||
}() | ||
|
||
go gatherMetrics() | ||
|
||
return nil | ||
} |