forked from prometheus/snmp_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (135 loc) · 4.79 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
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
"github.com/prometheus/snmp_exporter/config"
)
// Generate a snmp_exporter config and write it out.
func generateConfig(nodes *Node, nameToNode map[string]*Node, logger log.Logger) error {
outputPath, err := filepath.Abs(*outputPath)
if err != nil {
return fmt.Errorf("unable to determine absolute path for output")
}
content, err := os.ReadFile("generator.yml")
if err != nil {
return fmt.Errorf("error reading yml config: %s", err)
}
cfg := &Config{}
err = yaml.UnmarshalStrict(content, cfg)
if err != nil {
return fmt.Errorf("error parsing yml config: %s", err)
}
outputConfig := config.Config{}
for name, m := range cfg.Modules {
level.Info(logger).Log("msg", "Generating config for module", "module", name)
// Give each module a copy of the tree so that it can be modified.
mNodes := nodes.Copy()
// Build the map with new pointers.
mNameToNode := map[string]*Node{}
walkNode(mNodes, func(n *Node) {
mNameToNode[n.Oid] = n
mNameToNode[n.Label] = n
})
out, err := generateConfigModule(m, mNodes, mNameToNode, logger)
if err != nil {
return err
}
outputConfig[name] = out
outputConfig[name].WalkParams = m.WalkParams
level.Info(logger).Log("msg", "Generated metrics", "module", name, "metrics", len(outputConfig[name].Metrics))
}
config.DoNotHideSecrets = true
out, err := yaml.Marshal(outputConfig)
config.DoNotHideSecrets = false
if err != nil {
return fmt.Errorf("error marshaling yml: %s", err)
}
// Check the generated config to catch auth/version issues.
err = yaml.UnmarshalStrict(out, &config.Config{})
if err != nil {
return fmt.Errorf("error parsing generated config: %s", err)
}
f, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("error opening output file: %s", err)
}
out = append([]byte("# WARNING: This file was auto-generated using snmp_exporter generator, manual changes will be lost.\n"), out...)
_, err = f.Write(out)
if err != nil {
return fmt.Errorf("error writing to output file: %s", err)
}
level.Info(logger).Log("msg", "Config written", "file", outputPath)
return nil
}
var (
failOnParseErrors = kingpin.Flag("fail-on-parse-errors", "Exit with a non-zero status if there are MIB parsing errors").Default("false").Bool()
generateCommand = kingpin.Command("generate", "Generate snmp.yml from generator.yml")
outputPath = generateCommand.Flag("output-path", "Path to to write resulting config file").Default("snmp.yml").Short('o').String()
parseErrorsCommand = kingpin.Command("parse_errors", "Debug: Print the parse errors output by NetSNMP")
dumpCommand = kingpin.Command("dump", "Debug: Dump the parsed and prepared MIBs")
)
func main() {
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
command := kingpin.Parse()
logger := promlog.New(promlogConfig)
parseOutput, err := initSNMP(logger)
if err != nil {
level.Error(logger).Log("msg", "Error initializing netsnmp", "err", err)
os.Exit(1)
}
parseOutput = strings.TrimSpace(parseOutput)
parseErrors := len(parseOutput) != 0
if parseErrors {
level.Warn(logger).Log("msg", "NetSNMP reported parse error(s)", "errors", len(strings.Split(parseOutput, "\n")))
}
nodes := getMIBTree()
nameToNode := prepareTree(nodes, logger)
switch command {
case generateCommand.FullCommand():
err := generateConfig(nodes, nameToNode, logger)
if err != nil {
level.Error(logger).Log("msg", "Error generating config netsnmp", "err", err)
os.Exit(1)
}
case parseErrorsCommand.FullCommand():
fmt.Println(parseOutput)
case dumpCommand.FullCommand():
walkNode(nodes, func(n *Node) {
t := n.Type
if n.FixedSize != 0 {
t = fmt.Sprintf("%s(%d)", n.Type, n.FixedSize)
}
implied := ""
if n.ImpliedIndex {
implied = "(implied)"
}
fmt.Printf("%s %s %s %q %q %s%s %v %s\n",
n.Oid, n.Label, t, n.TextualConvention, n.Hint, n.Indexes, implied, n.EnumValues, n.Description)
})
}
if *failOnParseErrors && parseErrors {
os.Exit(1)
}
}