net_aix.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // +build aix
  2. package net
  3. import (
  4. "context"
  5. "fmt"
  6. "os/exec"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. "github.com/shirou/gopsutil/internal/common"
  12. )
  13. func parseNetstatI(output string) ([]IOCountersStat, error) {
  14. lines := strings.Split(string(output), "\n")
  15. ret := make([]IOCountersStat, 0, len(lines)-1)
  16. exists := make([]string, 0, len(ret))
  17. // Check first line is header
  18. if len(lines) > 0 && strings.Fields(lines[0])[0] != "Name" {
  19. return nil, fmt.Errorf("not a 'netstat -i' output")
  20. }
  21. for _, line := range lines[1:] {
  22. values := strings.Fields(line)
  23. if len(values) < 1 || values[0] == "Name" {
  24. continue
  25. }
  26. if common.StringsHas(exists, values[0]) {
  27. // skip if already get
  28. continue
  29. }
  30. exists = append(exists, values[0])
  31. if len(values) < 9 {
  32. continue
  33. }
  34. base := 1
  35. // sometimes Address is omitted
  36. if len(values) < 10 {
  37. base = 0
  38. }
  39. parsed := make([]uint64, 0, 5)
  40. vv := []string{
  41. values[base+3], // Ipkts == PacketsRecv
  42. values[base+4], // Ierrs == Errin
  43. values[base+5], // Opkts == PacketsSent
  44. values[base+6], // Oerrs == Errout
  45. values[base+8], // Drops == Dropout
  46. }
  47. for _, target := range vv {
  48. if target == "-" {
  49. parsed = append(parsed, 0)
  50. continue
  51. }
  52. t, err := strconv.ParseUint(target, 10, 64)
  53. if err != nil {
  54. return nil, err
  55. }
  56. parsed = append(parsed, t)
  57. }
  58. n := IOCountersStat{
  59. Name: values[0],
  60. PacketsRecv: parsed[0],
  61. Errin: parsed[1],
  62. PacketsSent: parsed[2],
  63. Errout: parsed[3],
  64. Dropout: parsed[4],
  65. }
  66. ret = append(ret, n)
  67. }
  68. return ret, nil
  69. }
  70. func IOCounters(pernic bool) ([]IOCountersStat, error) {
  71. return IOCountersWithContext(context.Background(), pernic)
  72. }
  73. func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
  74. netstat, err := exec.LookPath("netstat")
  75. if err != nil {
  76. return nil, err
  77. }
  78. out, err := invoke.CommandWithContext(ctx, netstat, "-idn")
  79. if err != nil {
  80. return nil, err
  81. }
  82. iocounters, err := parseNetstatI(string(out))
  83. if err != nil {
  84. return nil, err
  85. }
  86. if pernic == false {
  87. return getIOCountersAll(iocounters)
  88. }
  89. return iocounters, nil
  90. }
  91. // NetIOCountersByFile is an method which is added just a compatibility for linux.
  92. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
  93. return IOCountersByFileWithContext(context.Background(), pernic, filename)
  94. }
  95. func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
  96. return IOCounters(pernic)
  97. }
  98. func FilterCounters() ([]FilterStat, error) {
  99. return FilterCountersWithContext(context.Background())
  100. }
  101. func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
  102. return nil, common.ErrNotImplementedError
  103. }
  104. func ConntrackStats(percpu bool) ([]ConntrackStat, error) {
  105. return ConntrackStatsWithContext(context.Background(), percpu)
  106. }
  107. func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) {
  108. return nil, common.ErrNotImplementedError
  109. }
  110. func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
  111. return ProtoCountersWithContext(context.Background(), protocols)
  112. }
  113. func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
  114. return nil, common.ErrNotImplementedError
  115. }
  116. func parseNetstatNetLine(line string) (ConnectionStat, error) {
  117. f := strings.Fields(line)
  118. if len(f) < 5 {
  119. return ConnectionStat{}, fmt.Errorf("wrong line,%s", line)
  120. }
  121. var netType, netFamily uint32
  122. switch f[0] {
  123. case "tcp", "tcp4":
  124. netType = syscall.SOCK_STREAM
  125. netFamily = syscall.AF_INET
  126. case "udp", "udp4":
  127. netType = syscall.SOCK_DGRAM
  128. netFamily = syscall.AF_INET
  129. case "tcp6":
  130. netType = syscall.SOCK_STREAM
  131. netFamily = syscall.AF_INET6
  132. case "udp6":
  133. netType = syscall.SOCK_DGRAM
  134. netFamily = syscall.AF_INET6
  135. default:
  136. return ConnectionStat{}, fmt.Errorf("unknown type, %s", f[0])
  137. }
  138. laddr, raddr, err := parseNetstatAddr(f[3], f[4], netFamily)
  139. if err != nil {
  140. return ConnectionStat{}, fmt.Errorf("failed to parse netaddr, %s %s", f[3], f[4])
  141. }
  142. n := ConnectionStat{
  143. Fd: uint32(0), // not supported
  144. Family: uint32(netFamily),
  145. Type: uint32(netType),
  146. Laddr: laddr,
  147. Raddr: raddr,
  148. Pid: int32(0), // not supported
  149. }
  150. if len(f) == 6 {
  151. n.Status = f[5]
  152. }
  153. return n, nil
  154. }
  155. var portMatch = regexp.MustCompile(`(.*)\.(\d+)$`)
  156. // This function only works for netstat returning addresses with a "."
  157. // before the port (0.0.0.0.22 instead of 0.0.0.0:22).
  158. func parseNetstatAddr(local string, remote string, family uint32) (laddr Addr, raddr Addr, err error) {
  159. parse := func(l string) (Addr, error) {
  160. matches := portMatch.FindStringSubmatch(l)
  161. if matches == nil {
  162. return Addr{}, fmt.Errorf("wrong addr, %s", l)
  163. }
  164. host := matches[1]
  165. port := matches[2]
  166. if host == "*" {
  167. switch family {
  168. case syscall.AF_INET:
  169. host = "0.0.0.0"
  170. case syscall.AF_INET6:
  171. host = "::"
  172. default:
  173. return Addr{}, fmt.Errorf("unknown family, %d", family)
  174. }
  175. }
  176. lport, err := strconv.Atoi(port)
  177. if err != nil {
  178. return Addr{}, err
  179. }
  180. return Addr{IP: host, Port: uint32(lport)}, nil
  181. }
  182. laddr, err = parse(local)
  183. if remote != "*.*" { // remote addr exists
  184. raddr, err = parse(remote)
  185. if err != nil {
  186. return laddr, raddr, err
  187. }
  188. }
  189. return laddr, raddr, err
  190. }
  191. func parseNetstatUnixLine(f []string) (ConnectionStat, error) {
  192. if len(f) < 8 {
  193. return ConnectionStat{}, fmt.Errorf("wrong number of fields: expected >=8 got %d", len(f))
  194. }
  195. var netType uint32
  196. switch f[1] {
  197. case "dgram":
  198. netType = syscall.SOCK_DGRAM
  199. case "stream":
  200. netType = syscall.SOCK_STREAM
  201. default:
  202. return ConnectionStat{}, fmt.Errorf("unknown type: %s", f[1])
  203. }
  204. // Some Unix Socket don't have any address associated
  205. addr := ""
  206. if len(f) == 9 {
  207. addr = f[8]
  208. }
  209. c := ConnectionStat{
  210. Fd: uint32(0), // not supported
  211. Family: uint32(syscall.AF_UNIX),
  212. Type: uint32(netType),
  213. Laddr: Addr{
  214. IP: addr,
  215. },
  216. Status: "NONE",
  217. Pid: int32(0), // not supported
  218. }
  219. return c, nil
  220. }
  221. // Return true if proto is the corresponding to the kind parameter
  222. // Only for Inet lines
  223. func hasCorrectInetProto(kind, proto string) bool {
  224. switch kind {
  225. case "all", "inet":
  226. return true
  227. case "unix":
  228. return false
  229. case "inet4":
  230. return !strings.HasSuffix(proto, "6")
  231. case "inet6":
  232. return strings.HasSuffix(proto, "6")
  233. case "tcp":
  234. return proto == "tcp" || proto == "tcp4" || proto == "tcp6"
  235. case "tcp4":
  236. return proto == "tcp" || proto == "tcp4"
  237. case "tcp6":
  238. return proto == "tcp6"
  239. case "udp":
  240. return proto == "udp" || proto == "udp4" || proto == "udp6"
  241. case "udp4":
  242. return proto == "udp" || proto == "udp4"
  243. case "udp6":
  244. return proto == "udp6"
  245. }
  246. return false
  247. }
  248. func parseNetstatA(output string, kind string) ([]ConnectionStat, error) {
  249. var ret []ConnectionStat
  250. lines := strings.Split(string(output), "\n")
  251. for _, line := range lines {
  252. fields := strings.Fields(line)
  253. if len(fields) < 1 {
  254. continue
  255. }
  256. if strings.HasPrefix(fields[0], "f1") {
  257. // Unix lines
  258. if len(fields) < 2 {
  259. // every unix connections have two lines
  260. continue
  261. }
  262. c, err := parseNetstatUnixLine(fields)
  263. if err != nil {
  264. return nil, fmt.Errorf("failed to parse Unix Address (%s): %s", line, err)
  265. }
  266. ret = append(ret, c)
  267. } else if strings.HasPrefix(fields[0], "tcp") || strings.HasPrefix(fields[0], "udp") {
  268. // Inet lines
  269. if !hasCorrectInetProto(kind, fields[0]) {
  270. continue
  271. }
  272. // On AIX, netstat display some connections with "*.*" as local addresses
  273. // Skip them as they aren't real connections.
  274. if fields[3] == "*.*" {
  275. continue
  276. }
  277. c, err := parseNetstatNetLine(line)
  278. if err != nil {
  279. return nil, fmt.Errorf("failed to parse Inet Address (%s): %s", line, err)
  280. }
  281. ret = append(ret, c)
  282. } else {
  283. // Header lines
  284. continue
  285. }
  286. }
  287. return ret, nil
  288. }
  289. func Connections(kind string) ([]ConnectionStat, error) {
  290. return ConnectionsWithContext(context.Background(), kind)
  291. }
  292. func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
  293. args := []string{"-na"}
  294. switch strings.ToLower(kind) {
  295. default:
  296. fallthrough
  297. case "":
  298. kind = "all"
  299. case "all":
  300. // nothing to add
  301. case "inet", "inet4", "inet6":
  302. args = append(args, "-finet")
  303. case "tcp", "tcp4", "tcp6":
  304. args = append(args, "-finet")
  305. case "udp", "udp4", "udp6":
  306. args = append(args, "-finet")
  307. case "unix":
  308. args = append(args, "-funix")
  309. }
  310. netstat, err := exec.LookPath("netstat")
  311. if err != nil {
  312. return nil, err
  313. }
  314. out, err := invoke.CommandWithContext(ctx, netstat, args...)
  315. if err != nil {
  316. return nil, err
  317. }
  318. ret, err := parseNetstatA(string(out), kind)
  319. if err != nil {
  320. return nil, err
  321. }
  322. return ret, nil
  323. }
  324. func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
  325. return ConnectionsMaxWithContext(context.Background(), kind, max)
  326. }
  327. func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
  328. return []ConnectionStat{}, common.ErrNotImplementedError
  329. }
  330. // Return a list of network connections opened, omitting `Uids`.
  331. // WithoutUids functions are reliant on implementation details. They may be altered to be an alias for Connections or be
  332. // removed from the API in the future.
  333. func ConnectionsWithoutUids(kind string) ([]ConnectionStat, error) {
  334. return ConnectionsWithoutUidsWithContext(context.Background(), kind)
  335. }
  336. func ConnectionsWithoutUidsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
  337. return ConnectionsMaxWithoutUidsWithContext(ctx, kind, 0)
  338. }
  339. func ConnectionsMaxWithoutUidsWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
  340. return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, 0, max)
  341. }
  342. func ConnectionsPidWithoutUids(kind string, pid int32) ([]ConnectionStat, error) {
  343. return ConnectionsPidWithoutUidsWithContext(context.Background(), kind, pid)
  344. }
  345. func ConnectionsPidWithoutUidsWithContext(ctx context.Context, kind string, pid int32) ([]ConnectionStat, error) {
  346. return ConnectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, 0)
  347. }
  348. func ConnectionsPidMaxWithoutUids(kind string, pid int32, max int) ([]ConnectionStat, error) {
  349. return ConnectionsPidMaxWithoutUidsWithContext(context.Background(), kind, pid, max)
  350. }
  351. func ConnectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) {
  352. return connectionsPidMaxWithoutUidsWithContext(ctx, kind, pid, max)
  353. }
  354. func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, pid int32, max int) ([]ConnectionStat, error) {
  355. return []ConnectionStat{}, common.ErrNotImplementedError
  356. }