I'm unmarshalling a yaml file snmp.yml. and I was wondering if I could get suggestions on creating better structs. This is what I have for now, but I'm guessing the structs I'm creating for Metric is fine but the SNMPyaml needs better restructuring to be fully able to consume the unmarshalled data correctly.
Any suggestions/feedback here is highly appreciated. Thank you in advance!
package system
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
y "gopkg.in/yaml.v2"
)
//SNMPyaml struct
type SNMPyaml struct {
Metrics Metric `yaml:"metrics"`
}
//Metric exportable
type Metric struct {
Name string `yaml:"name,omitempty"`
Oid string `yaml:"oid"`
Type string `yaml:"type"`
Help string `yaml:"help,omitempty"`
}
// Yamlparser
func Yamlparser() {
// Read the snmp.yml file
absPath, _ := filepath.Abs("./app/snmp.yml")
yamlFile, yamlerror := ioutil.ReadFile(absPath)
if yamlerror != nil {
log.Fatalf("ioutil err: %v", yamlerror)
}
//Unmarshall
var c SNMPyaml
err := y.Unmarshal(yamlFile, &c)
if err != nil {
log.Fatal(err)
}
fmt.Print(c)
}
metrics:
- name: sysStatClientCurConns
oid: 1.3.6.1.4.1.3375.2.1.1.2.1.8
type: gauge
indexes:
- labelname: sysStatClientCurConns
type: gauge
- name: sysClientsslStatCurConns
oid: 1.3.6.1.4.1.3375.2.1.1.2.9.2
type: gauge
indexes:
- labelname: sysClientsslStatCurConns
type: gauge
- name: sysClientSslStatTotNativeConns
oid: 1.3.6.1.4.1.3375.2.1.1.2.9.6
type: gauge
indexes:
- labelname: sysClientSslStatTotNativeConns
type: gauge
The error I'm getting for this is:
2019/07/31 23:25:58 yaml: line 25: mapping values are not allowed in this context
exit status 1