/
main.go
222 lines (208 loc) · 5.3 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"encoding/csv"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"gopkg.in/yaml.v2"
of_snmpv2 "github.com/cisco-cx/of/pkg/v2/snmp"
)
func main() {
if len(os.Args) != 2 {
usage()
}
inputFile := os.Args[1]
// Open input file
r, err := os.Open(inputFile)
if err != nil {
log.Fatalln("Failed to open input file, ", err.Error())
return
}
defer r.Close()
// Parse config.
cfg := of_snmpv2.V2Config{}
err = yaml.NewDecoder(r).Decode(&cfg)
if err != nil {
log.Fatalln("Failed to decode input file, ", err.Error())
return
}
// Generate a list of maps to allow render CSV easier
var rows []map[string]string = make([]map[string]string, 0)
for _, cfg := range cfg {
for _, alert := range cfg.Alerts {
allMods := append(alert.LabelMods, cfg.Defaults.LabelMods...)
allMods = append(allMods, cfg.Defaults.AnnotationMods...)
allAlertNames := searchDefaultAlertNames(&allMods)
if allAlertNames == nil {
log.Fatalln("Couldn't find the alert name", inputFile)
}
for _, alertName := range *allAlertNames {
row, err := getRowFromMods(alertName, &allMods)
if err != nil {
log.Fatalf("Error filling the row %s, %v\n", row, err)
}
rows = append(rows, row)
}
}
}
renderCSV(&rows)
}
// renderCSV renders the CSV output in the stdout
func renderCSV(rows *[]map[string]string) {
severityToOSS := map[string]string{
"critical": "Critical",
"error": "Critical",
"major": "Major",
"minor": "Minor",
"notice": "Minor",
"informational": "Minor",
"warning": "Warning",
"info": "Info",
}
severityToOSSClasification := map[string]string{
"critical": "Outage",
"error": "Outage",
"major": "Deterioration",
"minor": "Deterioration",
"notice": "Notification",
"informational": "Notification",
"warning": "Notification",
"info": "Normal",
}
severityToOSSServiceAffected := map[string]string{
"critical": "Yes",
"error": "Yes",
"major": "Yes",
"minor": "No",
"notice": "No",
"informational": "No",
"warning": "No",
"info": "No",
}
severityToOSSIncident := map[string]string{
"critical": "Yes",
"error": "Yes",
"major": "Yes",
"minor": "Yes",
"notice": "Yes",
"informational": "No",
"warning": "Yes",
"info": "No",
}
header := []string{
"Alarm Code",
"Alarm Name",
"Labels",
"EMS",
"Classification",
"Service Affected",
"Category",
"MO",
"Default Severity",
"Alarm Type",
"Alarming Delay (minutes)",
"Estimated time to Close(Minutes)",
"Incident Creation",
"TT Delay (minutes)",
"WO Delay (minutes)",
"Clear Name",
"Clear Trap",
"Software Release",
"Description",
"Impact",
"Suggestion",
"Generic Alarm Name",
"Probable Cause",
}
writer := csv.NewWriter(os.Stdout)
defer writer.Flush()
if err := writer.Write(header); err != nil {
log.Fatalln("error writing csv:", err)
}
for _, label := range *rows {
var labelToString []string
for key, value := range label {
if key == "alertname" || key == "alert_severity" || key == "alert_description" {
continue
}
labelToString = append(labelToString, fmt.Sprintf("%s=%s", key, value))
}
sort.Strings(labelToString)
row := []string{
label["alertname"],
label["alertname"],
strings.Join(labelToString, " "),
"OF",
severityToOSSClasification[label["alert_severity"]],
severityToOSSServiceAffected[label["alert_severity"]],
"-",
"-",
severityToOSS[label["alert_severity"]],
"-",
"-",
"-",
severityToOSSIncident[label["alert_severity"]],
"-",
"-",
"-",
"Yes",
"-",
label["alert_description"],
"-",
"-",
"-",
"-",
}
if err := writer.Write(row); err != nil {
log.Fatalln("error writing csv:", err)
}
}
}
// searchDefaultAlertNames searches for the alertname, first it tries looking in the
// alert, if it is not defined then it gets all the alertnames defined in the default
// values
func searchDefaultAlertNames(mods *[]of_snmpv2.Mod) *[]string {
var alertNames []string
for _, mod := range *mods {
// alertname defined in the alert as a set mod
if mod.Key == "alertname" {
alertNames = append(alertNames, mod.Value)
return &alertNames
}
// alertname defined in the default values
if mod.ToKey == "alertname" {
for alertName, _ := range mod.Map {
alertNames = append(alertNames, alertName)
}
return &alertNames
}
}
return nil
}
// getRowFromMods returns a map[string]string filled with all mods passed as parameter.
// It's used later for rendering a row in the csv output
func getRowFromMods(alertName string, allMods *[]of_snmpv2.Mod) (map[string]string, error) {
var row map[string]string = map[string]string{
"alertname": alertName,
}
for _, mod := range *allMods {
if mod.Type == of_snmpv2.Set {
row[mod.Key] = mod.Value
} else if mod.Type == of_snmpv2.Copy && len(mod.Map) == 0 {
row[mod.ToKey] = "*"
} else if mod.Type == of_snmpv2.Copy && len(mod.Map[alertName]) != 0 {
row[mod.ToKey] = mod.Map[alertName]
} else {
return nil, errors.New(fmt.Sprintf("Mod %v unsupported", mod))
}
}
return row, nil
}
func usage() {
fmt.Printf("%s <input_file.yaml>\n", filepath.Base(os.Args[0]))
os.Exit(1)
}