forked from platinasystems/vnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror_sflow_config.go
134 lines (106 loc) · 2.69 KB
/
mirror_sflow_config.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
// This file contains Data Structures and Methods required for parsing msconfig.cfg file.
// msconfig.cfg file persists mirror and sflow related configuration in form of destination profiles, mirror profiles and sflow profiles.
package vnet
import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
type MirrorConfigData struct {
Name string
Src string
Dst string
Type string
Active string
}
type DestConfigData struct {
Name string
Encap string
Agent_IP string
Binded_to string
Used_by string
}
type SflowConfigData struct {
Name string
Src string
Active string
Cpu string
Mirror string
Rate string
Dst string
}
type MirrorConfigFileData struct {
Mirror []MirrorConfigData
}
type DestConfigFileData struct {
Dest []DestConfigData
}
type SflowConfigFileData struct {
Sflow []SflowConfigData
}
type ConfigFileData struct{
MirrorCfgFileData MirrorConfigFileData
DestCfgFileData DestConfigFileData
SflowCfgFileData SflowConfigFileData
}
func (cfd *ConfigFileData) ReadConfigFile() (err error){
var fileData []byte
// Open file
file, err := os.Open("/etc/goes/msconfig.cfg")
if err != nil {
return err
}
// Close file at the end of function
defer file.Close()
// Read config file
fileData, err = ioutil.ReadFile(file.Name())
if err != nil {
return err
}
// Parse mirror config from file fileData
yaml.Unmarshal(fileData, &cfd.MirrorCfgFileData)
if err != nil {
err = errors.New("yaml unmarshal failed while reading mirror config.")
return err
}
// Parse destination config from file fileData
yaml.Unmarshal(fileData, &cfd.DestCfgFileData)
if err != nil {
err = errors.New("yaml unmarshal failed while reading destination config.")
return err
}
// Parse sflow config from file fileData
yaml.Unmarshal(fileData, &cfd.SflowCfgFileData)
if err != nil {
err = errors.New("yaml unmarshal failed while reading sflow config.")
return err
}
return err
}
func (cfd *ConfigFileData) WriteConfigFile() (err error){
// Create file
file, err := os.Create("/etc/goes/msconfig.cfg")
if err != nil {
fmt.Println(err)
}
// Close file at the end of function
defer file.Close()
// Write to file in yaml format
mirrorYamlData, _ := yaml.Marshal(&cfd.MirrorCfgFileData.Mirror)
destYamlData, _ := yaml.Marshal(&cfd.DestCfgFileData.Dest)
sflowYamlData,_ := yaml.Marshal(&cfd.SflowCfgFileData.Sflow)
fileData := ""
if len(cfd.DestCfgFileData.Dest) > 0 {
fileData += "dest:\n" + (string)(destYamlData)
}
if len(cfd.MirrorCfgFileData.Mirror) > 0 {
fileData += "\n\n" + "mirror:\n" + (string)(mirrorYamlData)
}
if len(cfd.SflowCfgFileData.Sflow) > 0 {
fileData += "\n\n" + "sflow:\n" + (string)(sflowYamlData)
}
file.Write(([]byte)(fileData))
return err
}