-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
129 lines (105 loc) · 2.64 KB
/
main.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
package main
import (
"errors"
"fmt"
"net"
"net/http"
"os"
log "github.com/Sirupsen/logrus"
"github.com/jesk78/anyflow/proto/netflow"
"github.com/prometheus/client_golang/prometheus"
)
type Packet struct {
Raw []byte
Saddr *net.UDPAddr
Proto string
}
func CheckError(err error) {
if err != nil {
log.Fatalf("Error: ", err)
os.Exit(0)
}
}
func Parse(b []byte, addr *net.UDPAddr) (*Packet, error) {
p := new(Packet)
// parse for flow netflowcol
switch b[1] {
case 9:
*p = Packet{Raw: b, Saddr: addr, Proto: "nf9"}
default:
return p, errors.New("No flow packet")
}
return p, nil
}
func receivePackets(c *net.UDPConn) {
buf := make([]byte, 9000)
for {
n, addr, err := c.ReadFromUDP(buf)
if err != nil {
log.Errorf("Error: ", err)
continue
}
packetSourceIP := addr.IP.String()
packetsTotal.WithLabelValues(packetSourceIP).Inc()
log.Infof("Packet source: ", packetSourceIP)
p, err := Parse(buf[:n], addr)
if err != nil {
log.Errorf("Error parsing packet: ", err)
continue
}
switch p.Proto {
case "nf9":
nf, err := netflow.New(p.Raw, p.Saddr)
if err != nil {
log.Errorf("Error parsing netflow nf9 packet: ", err)
continue
}
if !nf.HasFlows() {
log.Debug("No flows in nf9 packet")
continue
}
records, err := nf.GetFlows()
if err != nil {
log.Errorf("Error getting flows from packet: ", err)
continue
}
log.Infof("Number of flow packet records: ", len(records))
for i, r := range records {
for _, v := range r.Values {
log.Infof("Flow record: %d type: %v value: %v", i, v.GetType(), v.GetValue())
}
}
}
}
}
func init() {
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
prometheus.MustRegister(packetsTotal)
}
func main() {
httpListenAddress := ":8080"
flowListenAddress := ":10001"
log.Infof("Flow listening on %s", flowListenAddress)
ServerAddr, err := net.ResolveUDPAddr("udp", flowListenAddress)
CheckError(err)
ServerConn, err := net.ListenUDP("udp", ServerAddr)
CheckError(err)
defer ServerConn.Close()
go receivePackets(ServerConn)
http.Handle("/metrics", prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Anyflow Metrics Server</title></head>
<body>
<h1>Anyflow Metrics Server</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
log.Infof("HTTP listening on %s", httpListenAddress)
if err := http.ListenAndServe(httpListenAddress, nil); err != nil {
panic(fmt.Errorf("Error starting HTTP server: %s", err))
}
}