Created
November 10, 2021 12:19
-
-
Save onetown/5c632be08bba289145360de782e4769f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package netns | |
import ( | |
"regexp" | |
"strconv" | |
"strings" | |
"dev.azure.com/raisether/raisether/_git/raisether/pkg/utils" | |
"dev.azure.com/raisether/raisether/_git/raisether/raisether/jet/models" | |
) | |
// ParseIfconfig for node | |
func ParseIfconfig(nsname string) []*models.Interface { | |
ifaces := []*models.Interface{} | |
var out string | |
if nsname == "" { | |
out = utils.Command("ifconfig -a") | |
} else { | |
out = utils.Commandf("ip netns exec %s ifconfig -a", nsname) | |
} | |
blob := strings.Split(out, "\n\n") | |
for _, b := range blob { | |
re := regexp.MustCompile("(.*?)\\:") | |
match := re.FindStringSubmatch(b) | |
if len(match) < 2 { | |
continue // we didn't got iface name here | |
} | |
ifa := &models.Interface{ | |
BiosName: strings.TrimSpace(match[1]), | |
Nsname: nsname, | |
} | |
// parse flags | |
re = regexp.MustCompile("\\<(.*?)\\>") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
for _, s := range strings.Split(match[1], ",") { | |
str := strings.TrimSpace(s) | |
switch str { | |
case "UP": | |
ifa.UP = true | |
case "LOOPBACK": | |
ifa.Loopback = true | |
case "RUNNING": | |
ifa.Running = true | |
case "MULTICAST": | |
ifa.Multicast = true | |
case "PROMISC": | |
ifa.Promisc = true | |
} | |
} | |
} | |
// parse mtu | |
re = regexp.MustCompile("mtu\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.Atoi(match[1]); err == nil { | |
ifa.MTU = n | |
} else { | |
log.Error("parse mtu error for iface " + ifa.BiosName) | |
} | |
} | |
// parse txqueuelen | |
re = regexp.MustCompile("txqueuelen\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.Atoi(match[1]); err == nil { | |
ifa.TxQueueLen = n | |
} else { | |
log.Error("parse txqueuelen error for iface " + ifa.BiosName) | |
} | |
} | |
// parse recv packets | |
re = regexp.MustCompile("RX\\spackets\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.ParseUint(match[1], 10, 64); err == nil { | |
ifa.RecvPackets = n | |
} else { | |
log.Error("parse recv packets error for iface " + ifa.BiosName) | |
} | |
} | |
// parse recv bytes | |
re = regexp.MustCompile("RX\\spackets.*?bytes\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.ParseUint(match[1], 10, 64); err == nil { | |
ifa.RecvBytes = n | |
} else { | |
log.Error("parse recv bytes error for iface " + ifa.BiosName) | |
} | |
} | |
// parse recv errors | |
re = regexp.MustCompile("RX\\serrors\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.ParseUint(match[1], 10, 64); err == nil { | |
ifa.RecvError = n | |
} else { | |
log.Error("parse recv errors error for iface " + ifa.BiosName) | |
} | |
} | |
// parse recv dropped | |
re = regexp.MustCompile("RX\\serrors.*?dropped\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.ParseUint(match[1], 10, 64); err == nil { | |
ifa.RecvDropped = n | |
} else { | |
log.Error("parse recv dropped error for iface " + ifa.BiosName) | |
} | |
} | |
// parse send packets | |
re = regexp.MustCompile("TX\\spackets\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.ParseUint(match[1], 10, 64); err == nil { | |
ifa.SendPackets = n | |
} else { | |
log.Error("parse send packets error for iface " + ifa.BiosName) | |
} | |
} | |
re = regexp.MustCompile("TX\\spackets.*?bytes\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.ParseUint(match[1], 10, 64); err == nil { | |
ifa.SendBytes = n | |
} else { | |
log.Error("parse send bytes error for iface " + ifa.BiosName) | |
} | |
} | |
// parse recv errors | |
re = regexp.MustCompile("TX\\serrors\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.ParseUint(match[1], 10, 64); err == nil { | |
ifa.RecvError = n | |
} else { | |
log.Error("parse send errors error for iface " + ifa.BiosName) | |
} | |
} | |
// parse recv dropped | |
re = regexp.MustCompile("TX\\serrors.*?dropped\\s(\\d+)") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
if n, err := strconv.ParseUint(match[1], 10, 64); err == nil { | |
ifa.RecvDropped = n | |
} else { | |
log.Error("parse send dropped error for iface " + ifa.BiosName) | |
} | |
} | |
re = regexp.MustCompile("ether\\s(.*?)\\s") | |
match = re.FindStringSubmatch(b) | |
if len(match) == 2 { | |
ifa.Mac = match[1] | |
} | |
if ifa.Loopback { | |
ifa.Driver = "loopback" | |
} else { | |
ifa.ParseDriver() | |
} | |
ifa.ParseIPAddress() | |
ifaces = append(ifaces, ifa) | |
} | |
return ifaces | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment