Parse scans and retrieves the opened connections, from /proc/net/ files
(proto string)
| 82 | |
| 83 | // Parse scans and retrieves the opened connections, from /proc/net/ files |
| 84 | func Parse(proto string) ([]Entry, error) { |
| 85 | filename := core.ConcatStrings("/proc/net/", proto) |
| 86 | fd, err := os.Open(filename) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | defer fd.Close() |
| 91 | |
| 92 | entries := make([]Entry, 0) |
| 93 | scanner := bufio.NewScanner(fd) |
| 94 | for lineno := 0; scanner.Scan(); lineno++ { |
| 95 | // skip column names |
| 96 | if lineno == 0 { |
| 97 | continue |
| 98 | } |
| 99 | |
| 100 | line := core.Trim(scanner.Text()) |
| 101 | m := parser.FindStringSubmatch(line) |
| 102 | if m == nil { |
| 103 | log.Warning("Could not parse netstat line from %s: %s", filename, line) |
| 104 | continue |
| 105 | } |
| 106 | |
| 107 | entries = append(entries, NewEntry( |
| 108 | proto, |
| 109 | hexToIP(m[1]), |
| 110 | hexToInt(m[2]), |
| 111 | hexToIP(m[3]), |
| 112 | hexToInt(m[4]), |
| 113 | decToInt(m[5]), |
| 114 | decToInt(m[6]), |
| 115 | )) |
| 116 | } |
| 117 | |
| 118 | return entries, nil |
| 119 | } |