MCPcopy Create free account
hub / github.com/prometheus/procfs / parseNetIPSocketLine

Function parseNetIPSocketLine

net_ip_socket.go:163–248  ·  view source on GitHub ↗

parseNetIPSocketLine parses a single line, represented by a list of fields.

(fields []string, isUDP bool)

Source from the content-addressed store, hash-verified

161
162// parseNetIPSocketLine parses a single line, represented by a list of fields.
163func parseNetIPSocketLine(fields []string, isUDP bool) (*netIPSocketLine, error) {
164 line := &netIPSocketLine{}
165 if len(fields) < 10 {
166 return nil, fmt.Errorf(
167 "%w: Less than 10 columns found %q",
168 ErrFileParse,
169 strings.Join(fields, " "),
170 )
171 }
172 var err error // parse error
173
174 // sl
175 s := strings.Split(fields[0], ":")
176 if len(s) != 2 {
177 return nil, fmt.Errorf("%w: Unable to parse sl field in line %q", ErrFileParse, fields[0])
178 }
179
180 if line.Sl, err = strconv.ParseUint(s[0], 0, 64); err != nil {
181 return nil, fmt.Errorf("%w: Unable to parse sl field in %q: %w", ErrFileParse, line.Sl, err)
182 }
183 // local_address
184 l := strings.Split(fields[1], ":")
185 if len(l) != 2 {
186 return nil, fmt.Errorf("%w: Unable to parse local_address field in %q", ErrFileParse, fields[1])
187 }
188 if line.LocalAddr, err = parseIP(l[0]); err != nil {
189 return nil, err
190 }
191 if line.LocalPort, err = strconv.ParseUint(l[1], 16, 64); err != nil {
192 return nil, fmt.Errorf("%w: Unable to parse local_address port value line %q: %w", ErrFileParse, line.LocalPort, err)
193 }
194
195 // remote_address
196 r := strings.Split(fields[2], ":")
197 if len(r) != 2 {
198 return nil, fmt.Errorf("%w: Unable to parse rem_address field in %q", ErrFileParse, fields[1])
199 }
200 if line.RemAddr, err = parseIP(r[0]); err != nil {
201 return nil, err
202 }
203 if line.RemPort, err = strconv.ParseUint(r[1], 16, 64); err != nil {
204 return nil, fmt.Errorf("%w: Cannot parse rem_address port value in %q: %w", ErrFileParse, line.RemPort, err)
205 }
206
207 // st
208 if line.St, err = strconv.ParseUint(fields[3], 16, 64); err != nil {
209 return nil, fmt.Errorf("%w: Cannot parse st value in %q: %w", ErrFileParse, line.St, err)
210 }
211
212 // tx_queue and rx_queue
213 q := strings.Split(fields[4], ":")
214 if len(q) != 2 {
215 return nil, fmt.Errorf(
216 "%w: Missing colon for tx/rx queues in socket line %q",
217 ErrFileParse,
218 fields[4],
219 )
220 }

Callers 3

newNetIPSocketFunction · 0.85
newNetIPSocketSummaryFunction · 0.85

Calls 1

parseIPFunction · 0.85

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…