/
snmp.go
129 lines (104 loc) · 2.73 KB
/
snmp.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 (
"bytes"
"encoding/json"
"fmt"
"github.com/alouca/gosnmp"
"net/http"
"os"
"sync"
"time"
)
// latch for
var w sync.WaitGroup
func pollDevice(name string, device *Device, platform string) {
fmt.Printf("community: %s\n", device.SnmpCommunity)
s, err := gosnmp.NewGoSNMP(device.Host, device.SnmpCommunity, gosnmp.Version1, 5)
if err != nil {
fmt.Printf("err : %s\n", err)
return
}
for {
var values = make(map[string]interface{})
fmt.Printf("Polling device with model %s\n", device.Model)
// get the model
model := Models[device.Model]
for k, v := range model {
fmt.Printf("polling '%s'\n", k)
resp, err := s.Get(k)
if err != nil {
fmt.Printf("SNMP err : %s\n", err)
} else {
pdu := resp.Variables[0]
var t = pdu.Type
switch t {
case gosnmp.Integer:
fmt.Println("Integer!")
values[v] = pdu.Value.(int)
case gosnmp.OctetString:
fmt.Println("OctetString!")
values[v] = string(pdu.Value.([]uint8))
default:
fmt.Printf("unknown type: %d (%s)\n", t, v)
}
}
}
fmt.Println("push...")
dataPush(values, platform, device.Identifier, device.Password)
time.Sleep(time.Duration(device.Polling) * time.Second)
}
w.Done()
}
func main() {
fmt.Printf("AirVantage SNMP gateway\n")
if len(os.Args) != 2 {
fmt.Printf("Usage : avsnmp [platform]\n\n platform: edge,na,eu,etc\n")
os.Exit(1)
}
// load configuration from json
LoadModels("conf/model.json")
LoadDevices("conf/devices.json")
w.Add(len(Devices))
// start the web server
go RunHttpServer()
for k, v := range Devices {
go pollDevice(k, &v, os.Args[1])
}
w.Wait()
}
// push a data to airvantage using HTTP REST for devices
func dataPush(values map[string]interface{}, platform string, username string, password string) {
var container [1]map[string]interface{}
container[0] = make(map[string]interface{})
for k, v := range values {
var dataKey [1]map[string]interface{}
dataKey[0] = make(map[string]interface{})
dataKey[0]["timestamp"] = nil
dataKey[0]["value"] = v
container[0][k] = dataKey
}
b, err := json.Marshal(container)
if err == nil {
fmt.Printf("JSon :%s\n", b)
// build an HTTP request
reader := bytes.NewBuffer(b)
r, _ := http.NewRequest("POST", "http://"+platform+".airvantage.net/device/messages", reader)
r.SetBasicAuth(username, password)
client := &http.Client{}
resp, err := client.Do(r)
if err == nil {
fmt.Printf("HTTP status %d\n", resp.StatusCode)
if resp.StatusCode == 200 {
// happy
fmt.Printf("HTTP server Ok\n")
} else {
// unhappy
fmt.Printf("HTTP server error : %s\n", resp.Status)
}
} else {
fmt.Printf("HTTP POST error : %s\n", err.Error())
}
} else {
fmt.Printf("fail : %s\n", err)
}
}