forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuzieq.go
More file actions
117 lines (102 loc) · 2.72 KB
/
suzieq.go
File metadata and controls
117 lines (102 loc) · 2.72 KB
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
package suzieq
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
type SuzieQ struct {
Url string `toml:"url"`
Token string `toml:"token"`
EnableTLS bool `toml:"enable_tls"`
Port int `toml:"port"`
Log telegraf.Logger
Interval *time.Ticker `toml:"interval"`
done chan bool
}
type NetworkRoutes []struct {
Action string `json:"action"`
Hostname string `json:"hostname"`
Ipvers int64 `json:"ipvers"`
Namespace string `json:"namespace"`
NexthopIps []string `json:"nexthopIps"`
Oifs []string `json:"oifs"`
Preference int64 `json:"preference"`
Prefix string `json:"prefix"`
Protocol string `json:"protocol"`
Source string `json:"source"`
Timestamp int64 `json:"timestamp"`
Vrf string `json:"vrf"`
}
func (c *SuzieQ) Description() string {
return "a SuzieQ Plugin"
}
func (c *SuzieQ) SampleConfig() string {
return `
## Indicate if everything is fine
Add values later here.
`
}
// Init is for setup, and validating config.
func (c *SuzieQ) Init() error {
return nil
}
func (c *SuzieQ) Start(acc telegraf.Accumulator) error {
c.Interval = time.NewTicker(time.Second * 10) // Example: adjust interval every 10 seconds
return nil
}
func (c *SuzieQ) Stop() {
c.Interval.Stop()
}
func (c *SuzieQ) Gather(acc telegraf.Accumulator) error {
req, err := http.NewRequest("GET", "https://"+c.Url+":"+strconv.Itoa(c.Port)+"/api/v2/route/show?access_token="+c.Token, nil)
if err != nil {
fmt.Println(err)
}
req.Header.Add("Accept", "application/json")
//To do tls certs in case of if else
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
responseData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Cannot marshall data", err)
}
JsonResponse := NetworkRoutes{}
//Unmarshall it
err = json.Unmarshal(responseData, &JsonResponse)
// Show how type values work.
var fields = make(map[string]interface{}, len(NetworkRoutes{}))
var tags = map[string]string{}
for _, v := range JsonResponse {
tags["Device"] = v.Hostname
tags["VRF"] = v.Vrf
tags["RoutingProtocol"] = v.Protocol
tags["Timestamp"] = strconv.Itoa(int(v.Timestamp))
for _, nh := range v.NexthopIps {
tags["nexthops"] = nh
}
for _, oi := range v.Oifs {
tags["OutgoingInterfaces"] = oi
}
fields["Prefix"] = v.Prefix
acc.AddFields("NetworkRoutes", fields, tags)
}
return nil
}
func init() {
inputs.Add("suzieq", func() telegraf.Input {
return &SuzieQ{}
})
}