-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (146 loc) · 3.96 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"text/template"
"time"
"github.com/sleepinggenius2/gosmi"
"github.com/sleepinggenius2/gosmi/types"
)
type stringArray []string
func (a stringArray) String() string {
return strings.Join(a, ",")
}
func (a *stringArray) Set(value string) error {
*a = append(*a, value)
return nil
}
type trapConfig struct {
Module string
Fields stringArray
Enums []trapField
Traps []trapConfigTrap
}
type trapConfigTrap struct {
Name string
Oid types.Oid
Fields stringArray
Description string
}
type trapField struct {
Module string
Name string
Oid types.Oid
Values map[int64]string
}
func main() {
var err error
var config trapConfig
// Parse commandline flags
paths := stringArray{"/usr/share/snmp/mibs"}
flag.Var(&paths, "p", "Path to add")
module := flag.String("m", "REQUIRED", "Module to process")
directory := flag.String("d", "", "Directory to save generated config file. (defaults to stdout if none given)")
flag.Parse()
// Load module
InitSMI(paths)
config.Module, err = gosmi.LoadModule(*module)
if err != nil {
log.Printf("Loading %s failed: %v", *module, err)
os.Exit(2)
}
// Get traps from module
trapNodes, err := FindModuleTraps(config.Module)
if err != nil {
log.Printf("Finding traps failed: %v", err)
os.Exit(2)
}
if len(trapNodes) == 0 {
log.Printf("No traps found in module %s", config.Module)
os.Exit(1)
}
config.Traps = make([]trapConfigTrap, 0, len(trapNodes))
for _, node := range trapNodes {
trapConfig := ParseTrapToConfig(node)
config.Traps = append(config.Traps, trapConfig)
log.Printf("Trap %s::%s [%v]\n", config.Module, trapConfig.Name, trapConfig.Fields)
}
// Get list of fields to convert/translate
fields := GetAllTrapFields(trapNodes)
config.Fields = make([]string, 0, len(fields))
for fieldName, trapField := range fields {
config.Fields = append(config.Fields, fieldName)
if len(trapField.Values) > 0 {
config.Enums = append(config.Enums, trapField)
}
}
// Generate telegraf config file
configFunctions := template.FuncMap{
"join": strings.Join,
"now": time.Now,
}
t, err := template.New("telegraf.toml.tmpl").Funcs(configFunctions).ParseFiles("telegraf.toml.tmpl")
if err != nil {
log.Printf("Template parsing failed: %v", err)
os.Exit(2)
}
var wr *os.File
if *directory != "" {
wr, err = os.Create(*directory + string(os.PathSeparator) + config.Module + ".toml")
if err != nil {
log.Printf("Creating config file failed: %v", err)
os.Exit(2)
}
} else {
wr = os.Stdout
}
if err = t.Execute(wr, config); err != nil {
log.Printf("Generating config failed: %v", err)
os.Exit(2)
}
wr.Close()
}
func InitSMI(paths stringArray) {
gosmi.Init()
gosmi.SetPath(strings.Join(paths, string(os.PathListSeparator)))
}
func FindModuleTraps(module string) ([]gosmi.SmiNode, error) {
m, err := gosmi.GetModule(module)
if err != nil {
return nil, fmt.Errorf("cannot get module %s: %w", module, err)
}
return m.GetNodes(types.NodeNotification), nil
}
func ParseTrapToConfig(trap gosmi.SmiNode) (config trapConfigTrap) {
config.Name = trap.Name
config.Description = trap.Description
config.Oid = trap.Oid
trapFields := trap.GetNotificationObjects()
config.Fields = make(stringArray, len(trapFields))
for i, field := range trapFields {
config.Fields[i] = field.Name
}
return config
}
func GetAllTrapFields(nodes []gosmi.SmiNode) map[string]trapField {
trapFields := make(map[string]trapField)
for _, trapNode := range nodes {
for _, objectNode := range trapNode.GetNotificationObjects() {
trapFields[objectNode.Name] = trapField{
Name: objectNode.Name,
Module: objectNode.GetModule().Name,
Oid: objectNode.Oid,
Values: make(map[int64]string),
}
if objectNode.Type != nil && objectNode.Type.BaseType == types.BaseTypeEnum {
for _, value := range objectNode.Type.Enum.Values {
trapFields[objectNode.Name].Values[value.Value] = value.Name
}
}
}
}
return trapFields
}