-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
120 lines (107 loc) · 1.97 KB
/
utils.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
package tarpan
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
g "github.com/soniah/gosnmp"
)
// Utility functions
func loadConfig(path string) (*DataSet, error) {
var dataset DataSet
fd, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("File Error: ", err)
return nil, err
}
json_err := json.Unmarshal(fd, &dataset)
if json_err != nil {
fmt.Println("Config Format Error: ", json_err)
return nil, json_err
}
return &dataset, err
}
func validateIP(address string) error {
var err error
err = nil
addr := net.ParseIP(address)
if addr == nil {
return errors.New("invalid ip address was passed")
}
return err
}
func validatePort(port uint16) (uint16, error) {
return port, nil
}
func getSnmpVersionString(snmpVersion g.SnmpVersion) string {
var version string
switch snmpVersion {
case g.Version1:
version = "1"
case g.Version2c:
version = "2c"
case g.Version3:
version = "3"
default:
version = ""
}
return version
}
func getAsn1BERName(ber g.Asn1BER) (string, error) {
var name string
switch ber {
case 0x00:
name = "UnknownType"
case 0x01:
name = "Boolean"
case 0x02:
name = "Integer"
case 0x03:
name = "BitString"
case 0x04:
name = "OctetString"
case 0x05:
name = "Null"
case 0x06:
name = "ObjectIdentifier"
case 0x07:
name = "ObjectDescription"
case 0x40:
name = "IPAddress"
case 0x41:
name = "Counter32"
case 0x42:
name = "Gauge32"
case 0x43:
name = "TimeTicks"
case 0x44:
name = "Opaque"
case 0x45:
name = "NsapAddress"
case 0x46:
name = "Counter64"
case 0x47:
name = "Uinteger32"
case 0x80:
name = "NoSuchObject"
case 0x81:
name = "NoSuchInstance"
case 0x82:
name = "EndOfMibView"
default:
return "", errors.New("Unknown BER")
}
return name, nil
}
func formatSnmpValue(ber g.Asn1BER, value string) string {
switch ber {
case g.Counter32, g.Gauge32, g.Counter64:
return value
case g.NoSuchObject, g.NoSuchInstance:
return "-"
default:
return value
}
return ""
}