package swModels import ( "regexp" "strconv" "strings" ) var ( MONITORRE = regexp.MustCompile(`(?i)monitor\s+session\s+([0-9]+)\s+(.*?)\n`) MONITORDESTRE = regexp.MustCompile(`(?i)destination\s+interface\s+(.*?)(\s+rx$|\s+tx$|\s+$|$)`) MONITORSRCRE = regexp.MustCompile(`(?i)source\s+(vlan|interface)\s+(.*?)(\s+rx$|\s+tx$|\s+$|$)`) MONITORFILTERRE = regexp.MustCompile(`(?i)filter\s+(\S+)\s+(.*?)(\s+rx$|\s+tx$|$)`) PORTSLOTRE = regexp.MustCompile(`(.*?/)[0-9]+$`) ) type ( MonitorSessions struct { Monitors []*MonitorSession `json:"monitor_sessions"` } MonitorSession struct { Session int `json:"session"` Source *MonitorSrc `json:"source"` Destination *MonitorDst `json:"destination"` Filter *MonitorFilter `json:"filter"` } MonitorSrc struct { Type string `json:"type"` Items []string `json:"items"` Direction string `json:"direction"` } MonitorDst struct { Interface []string `json:"interface"` Direction string `json:"direction"` } MonitorFilter struct { Type string `json:"type"` Options []string `json:"options"` Direction string `json:"direction"` } ) func ParseMonitorSessions(s *string) *MonitorSessions { m := new(MonitorSessions) m.Parse(s) return m } func (m *MonitorSessions) Parse(s *string) { monitors := make(map[string]*MonitorSession) for _, ms := range MONITORRE.FindAllStringSubmatch(*s, -1) { if _, ok := monitors[ms[1]]; !ok { if n, err := strconv.Atoi(ms[1]); err == nil { monitors[ms[1]] = &MonitorSession{ Session: n, } } } if monitors[ms[1]].Destination == nil { monitors[ms[1]].Destination = parseMonitorDest(&ms[2]) } if monitors[ms[1]].Source == nil { monitors[ms[1]].Source = parseMonitorSource(&ms[2]) } if monitors[ms[1]].Filter == nil { monitors[ms[1]].Filter = parseFilter(&ms[2]) } } for _, session := range monitors { m.Monitors = append(m.Monitors, session) } } func parseMonitorSource(s *string) *MonitorSrc { m := MONITORSRCRE.FindStringSubmatch(*s) if len(m) == 0 { return nil } return &MonitorSrc{ Type: m[1], Items: splitMonitorItems(&m[2], m[1]), Direction: monitorDirection(&m[3]), } } func splitMonitorItems(s *string, Type string) []string { items := strings.Split(strings.Replace(*s, " ", "", -1), ",") if Type == "vlan" { return items } var pl []string for _, i := range items { split := strings.Split(i, "-") if len(split) != 0 { port, num := IntFullPre(&split[0]) pl = append(pl, port+num) pn := PORTSLOTRE.FindStringSubmatch(num) var ps string if len(pn) != 0 { ps = pn[1] } for _, pn := range split[1:] { pl = append(pl, port+ps+pn) } } } return pl } func parseFilter(s *string) *MonitorFilter { m := MONITORFILTERRE.FindStringSubmatch(*s) if len(m) == 0 { return nil } return &MonitorFilter{ Type: m[1], Options: strings.Split(strings.Replace(m[2], " ", "", -1), ","), Direction: monitorDirection(&m[3]), } } func parseMonitorDest(s *string) *MonitorDst { m := MONITORDESTRE.FindStringSubmatch(*s) if len(m) == 0 { return nil } return &MonitorDst{ Interface: splitMonitorItems(&m[1], ""), //strings.Split(strings.Replace(m[1], " ", "", -1), ","), Direction: monitorDirection(&m[2]), } } func monitorDirection(s *string) string { if strings.Contains(*s, "tx") { return "tx" } if strings.Contains(*s, "rx") { return "rx" } return "both" } func (m *MonitorSession) IsDestination(dest string) bool { if m.Destination == nil { return false } dest = strings.ToLower(dest) for _, destInt := range m.Destination.Interface { if strings.ToLower(destInt) == dest { return true } } return false } func (m *MonitorSessions) IsDestination(dest string) *MonitorSession { for _, mon := range m.Monitors { if mon.IsDestination(dest) { return mon } } return nil }