package asaModels import ( "regexp" "strconv" "strings" ) type ( Logging struct { Enabled bool `json:"enabled"` Timestamp bool `json:"timestamp"` PermitHostdown bool `json:"permit_hostdown"` Facility int `json:"facility"` BufferSize int `json:"buffer_size"` DisabledMessages []int `json:"disabled_messages"` Hosts []LoggingHost `json:"hosts"` Lists []LoggingList `json:"lists"` Destinations []LoggingObject `json:"destinations"` Classes []LoggingObject `json:"classes"` Populated bool } LoggingList struct { Name string `json:"name"` Level string `json:"level,omitempty"` Messages []interface{} `json:"messages"` Classes []LoggingObject `json:"classes,omitempty"` } LoggingHost struct { Segment string `json:"segment"` Host string `json:"host"` Port int `json:"port,omitempty"` Protocol int `json:"protocol,omitempty"` } LoggingObject struct { Name string `json:"name"` Level string `json:"level"` Destination string `json:"destination,omitempty"` } ) /* logging list RS-BUFFER level informational class vpn logging list RS-BUFFER level informational class vpnc logging list RS-BUFFER-LOG level informational class vpn logging list RS-BUFFER-LOG level informational class vpnc logging host DMZ 10.10.10.10 17/1514 logging host DMZ 10.10.10.100 logging class svc buffered debugging no logging message 305005 */ //HostExists takes a host IP and interface name and returns if it is configured and the segment matches func (l Logging) HostExists(host, seg string) bool { for _, cHost := range l.Hosts { if cHost.Host == host && cHost.Segment == seg { return true } } return false } func (l *Logging) Parse(s string) error { var match []string lists := make(map[string][]string) classes := make(map[string][]string) split := strings.Split(s, "\n") for i := 0; i < len(split); i++ { if strings.Contains(strings.ToLower(split[i]), "logging enable") { l.Enabled = true } if strings.Contains(strings.ToLower(split[i]), "logging timestamp") { l.Timestamp = true } if strings.Contains(strings.ToLower(split[i]), "logging permit-hostdown") { l.PermitHostdown = true } match = regexp.MustCompile(`(?i)no logging message (\d+)`).FindStringSubmatch(split[i]) if len(match) > 0 { f, err := strconv.Atoi(match[1]) if err != nil { return err } l.DisabledMessages = append(l.DisabledMessages, f) } match = regexp.MustCompile(`(?i)logging facility (\d+)`).FindStringSubmatch(split[i]) if len(match) > 0 { f, err := strconv.Atoi(match[1]) if err != nil { return err } l.Facility = f } match = regexp.MustCompile(`(?i)logging buffer-size (\d+)`).FindStringSubmatch(split[i]) if len(match) > 0 { n, err := strconv.Atoi(match[1]) if err != nil { return err } l.BufferSize = n } match = regexp.MustCompile(`(?i)logging host (\S+) (\S+)(.*|)`).FindStringSubmatch(split[i]) if len(match) > 0 { lh := LoggingHost{ Segment: match[1], Host: match[2], } innerMatch := regexp.MustCompile(`(\d+)/(\d+)`).FindStringSubmatch(match[3]) if len(innerMatch) == 3 { protocol, err := strconv.Atoi(innerMatch[1]) if err != nil { return err } port, err := strconv.Atoi(innerMatch[2]) if err != nil { return err } lh.Protocol = protocol lh.Port = port } l.Hosts = append(l.Hosts, lh) } // logging trap warnings match = regexp.MustCompile(`(?i)logging (monitor|trap|mail|asdm|console|buffered) (\S+)`).FindStringSubmatch(split[i]) if len(match) > 0 { l.Destinations = append(l.Destinations, LoggingObject{ Name: match[1], Level: match[2], }) } match = regexp.MustCompile(`(?i)logging list (\S+) (.*)`).FindStringSubmatch(split[i]) if len(match) == 3 { lists[match[1]] = append(lists[match[1]], match[2]) } match = regexp.MustCompile(`(?i)logging class (\S+) (.*)`).FindStringSubmatch(split[i]) if len(match) == 3 { classes[match[1]] = append(classes[match[1]], match[2]) } match = regexp.MustCompile(`(?i)logging class (\S+) (\S+) (\S+)`).FindStringSubmatch(split[i]) if len(match) == 4 { l.Classes = append(l.Classes, LoggingObject{ Name: match[1], Destination: match[2], Level: match[3], }) } } for k, list := range lists { l.Lists = append(l.Lists, MakeList(k, list)) } return nil } func MakeList(name string, raw []string) LoggingList { var ll LoggingList // var match []string ll.Name = name for _, line := range raw { match := regexp.MustCompile(`(?i)level (\S+)$`).FindStringSubmatch(line) if len(match) > 0 { ll.Level = match[1] } match = regexp.MustCompile(`(?i)message (\S+)$`).FindStringSubmatch(line) if len(match) > 0 { n, _ := strconv.Atoi(match[1]) ll.Messages = append(ll.Messages, n) } match = regexp.MustCompile(`(?i)level (\S+) class (\S+)`).FindStringSubmatch(line) if len(match) > 0 { ll.Classes = append(ll.Classes, LoggingObject{ Level: match[1], Name: match[2], }) } // logging list RS-BUFFER-LOG level informational class vpn // logging list RS-BUFFER-LOG level informational class vpnc } return ll } func MakeClass(name string, raw []string) LoggingObject { var lo LoggingObject lo.Name = name return lo }