forked from Juniper/jtimon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
87 lines (72 loc) · 1.68 KB
/
udp.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
/*
* Copyright (c) 2018, Juniper Networks, Inc.
* All rights reserved.
*/
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"github.com/Juniper/jtimon/protos/qmon"
tt "github.com/Juniper/jtimon/protos/telemetry_top"
"github.com/golang/protobuf/proto"
)
func handleUDPConnection(conn *net.UDPConn) {
buffer := make([]byte, 4096)
n, _, err := conn.ReadFromUDP(buffer)
ts := &tt.TelemetryStream{}
if err := proto.Unmarshal(buffer[:n], ts); err != nil {
log.Fatalln("Failed to parse: ", err)
}
if *outJSON {
b, err := json.MarshalIndent(ts, "", " ")
if err != nil {
log.Fatalln("JSON indent error: ", err)
}
fmt.Printf("%s\n", b)
}
if proto.HasExtension(ts.Enterprise, tt.E_JuniperNetworks) {
jnsi, err := proto.GetExtension(ts.Enterprise, tt.E_JuniperNetworks)
if err != nil {
log.Fatalln("Cant get extension: ", err)
}
switch jns := jnsi.(type) {
case *tt.JuniperNetworksSensors:
if proto.HasExtension(jns, qmon.E_JnprQmonExt) {
qmi, err := proto.GetExtension(jns, qmon.E_JnprQmonExt)
if err != nil {
log.Fatalln("Cant get extension: ", err)
}
switch qm := qmi.(type) {
case *qmon.QueueMonitor:
if *outJSON {
b, err := json.MarshalIndent(qm, "", " ")
if err != nil {
log.Fatalln("JSON indent error: ", err)
}
fmt.Printf("%s\n", b)
}
}
}
}
}
if err != nil {
log.Fatal(err)
}
}
func udpInit() {
address := fmt.Sprintf("%s:%d", "0.0.0.0", *port)
udpAddr, err := net.ResolveUDPAddr("udp4", address)
if err != nil {
log.Fatal(err)
}
ln, err := net.ListenUDP("udp", udpAddr)
if err != nil {
log.Fatal(err)
}
defer ln.Close()
for {
handleUDPConnection(ln)
}
}