proc_netstat.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // Copyright 2022 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package procfs
  14. import (
  15. "bufio"
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "strconv"
  20. "strings"
  21. "github.com/prometheus/procfs/internal/util"
  22. )
  23. // ProcNetstat models the content of /proc/<pid>/net/netstat.
  24. type ProcNetstat struct {
  25. // The process ID.
  26. PID int
  27. TcpExt
  28. IpExt
  29. }
  30. type TcpExt struct { // nolint:revive
  31. SyncookiesSent float64
  32. SyncookiesRecv float64
  33. SyncookiesFailed float64
  34. EmbryonicRsts float64
  35. PruneCalled float64
  36. RcvPruned float64
  37. OfoPruned float64
  38. OutOfWindowIcmps float64
  39. LockDroppedIcmps float64
  40. ArpFilter float64
  41. TW float64
  42. TWRecycled float64
  43. TWKilled float64
  44. PAWSActive float64
  45. PAWSEstab float64
  46. DelayedACKs float64
  47. DelayedACKLocked float64
  48. DelayedACKLost float64
  49. ListenOverflows float64
  50. ListenDrops float64
  51. TCPHPHits float64
  52. TCPPureAcks float64
  53. TCPHPAcks float64
  54. TCPRenoRecovery float64
  55. TCPSackRecovery float64
  56. TCPSACKReneging float64
  57. TCPSACKReorder float64
  58. TCPRenoReorder float64
  59. TCPTSReorder float64
  60. TCPFullUndo float64
  61. TCPPartialUndo float64
  62. TCPDSACKUndo float64
  63. TCPLossUndo float64
  64. TCPLostRetransmit float64
  65. TCPRenoFailures float64
  66. TCPSackFailures float64
  67. TCPLossFailures float64
  68. TCPFastRetrans float64
  69. TCPSlowStartRetrans float64
  70. TCPTimeouts float64
  71. TCPLossProbes float64
  72. TCPLossProbeRecovery float64
  73. TCPRenoRecoveryFail float64
  74. TCPSackRecoveryFail float64
  75. TCPRcvCollapsed float64
  76. TCPDSACKOldSent float64
  77. TCPDSACKOfoSent float64
  78. TCPDSACKRecv float64
  79. TCPDSACKOfoRecv float64
  80. TCPAbortOnData float64
  81. TCPAbortOnClose float64
  82. TCPAbortOnMemory float64
  83. TCPAbortOnTimeout float64
  84. TCPAbortOnLinger float64
  85. TCPAbortFailed float64
  86. TCPMemoryPressures float64
  87. TCPMemoryPressuresChrono float64
  88. TCPSACKDiscard float64
  89. TCPDSACKIgnoredOld float64
  90. TCPDSACKIgnoredNoUndo float64
  91. TCPSpuriousRTOs float64
  92. TCPMD5NotFound float64
  93. TCPMD5Unexpected float64
  94. TCPMD5Failure float64
  95. TCPSackShifted float64
  96. TCPSackMerged float64
  97. TCPSackShiftFallback float64
  98. TCPBacklogDrop float64
  99. PFMemallocDrop float64
  100. TCPMinTTLDrop float64
  101. TCPDeferAcceptDrop float64
  102. IPReversePathFilter float64
  103. TCPTimeWaitOverflow float64
  104. TCPReqQFullDoCookies float64
  105. TCPReqQFullDrop float64
  106. TCPRetransFail float64
  107. TCPRcvCoalesce float64
  108. TCPOFOQueue float64
  109. TCPOFODrop float64
  110. TCPOFOMerge float64
  111. TCPChallengeACK float64
  112. TCPSYNChallenge float64
  113. TCPFastOpenActive float64
  114. TCPFastOpenActiveFail float64
  115. TCPFastOpenPassive float64
  116. TCPFastOpenPassiveFail float64
  117. TCPFastOpenListenOverflow float64
  118. TCPFastOpenCookieReqd float64
  119. TCPFastOpenBlackhole float64
  120. TCPSpuriousRtxHostQueues float64
  121. BusyPollRxPackets float64
  122. TCPAutoCorking float64
  123. TCPFromZeroWindowAdv float64
  124. TCPToZeroWindowAdv float64
  125. TCPWantZeroWindowAdv float64
  126. TCPSynRetrans float64
  127. TCPOrigDataSent float64
  128. TCPHystartTrainDetect float64
  129. TCPHystartTrainCwnd float64
  130. TCPHystartDelayDetect float64
  131. TCPHystartDelayCwnd float64
  132. TCPACKSkippedSynRecv float64
  133. TCPACKSkippedPAWS float64
  134. TCPACKSkippedSeq float64
  135. TCPACKSkippedFinWait2 float64
  136. TCPACKSkippedTimeWait float64
  137. TCPACKSkippedChallenge float64
  138. TCPWinProbe float64
  139. TCPKeepAlive float64
  140. TCPMTUPFail float64
  141. TCPMTUPSuccess float64
  142. TCPWqueueTooBig float64
  143. }
  144. type IpExt struct { // nolint:revive
  145. InNoRoutes float64
  146. InTruncatedPkts float64
  147. InMcastPkts float64
  148. OutMcastPkts float64
  149. InBcastPkts float64
  150. OutBcastPkts float64
  151. InOctets float64
  152. OutOctets float64
  153. InMcastOctets float64
  154. OutMcastOctets float64
  155. InBcastOctets float64
  156. OutBcastOctets float64
  157. InCsumErrors float64
  158. InNoECTPkts float64
  159. InECT1Pkts float64
  160. InECT0Pkts float64
  161. InCEPkts float64
  162. ReasmOverlaps float64
  163. }
  164. func (p Proc) Netstat() (ProcNetstat, error) {
  165. filename := p.path("net/netstat")
  166. data, err := util.ReadFileNoStat(filename)
  167. if err != nil {
  168. return ProcNetstat{PID: p.PID}, err
  169. }
  170. procNetstat, err := parseNetstat(bytes.NewReader(data), filename)
  171. procNetstat.PID = p.PID
  172. return procNetstat, err
  173. }
  174. // parseNetstat parses the metrics from proc/<pid>/net/netstat file
  175. // and returns a ProcNetstat structure.
  176. func parseNetstat(r io.Reader, fileName string) (ProcNetstat, error) {
  177. var (
  178. scanner = bufio.NewScanner(r)
  179. procNetstat = ProcNetstat{}
  180. )
  181. for scanner.Scan() {
  182. nameParts := strings.Split(scanner.Text(), " ")
  183. scanner.Scan()
  184. valueParts := strings.Split(scanner.Text(), " ")
  185. // Remove trailing :.
  186. protocol := strings.TrimSuffix(nameParts[0], ":")
  187. if len(nameParts) != len(valueParts) {
  188. return procNetstat, fmt.Errorf("mismatch field count mismatch in %s: %s",
  189. fileName, protocol)
  190. }
  191. for i := 1; i < len(nameParts); i++ {
  192. value, err := strconv.ParseFloat(valueParts[i], 64)
  193. if err != nil {
  194. return procNetstat, err
  195. }
  196. key := nameParts[i]
  197. switch protocol {
  198. case "TcpExt":
  199. switch key {
  200. case "SyncookiesSent":
  201. procNetstat.TcpExt.SyncookiesSent = value
  202. case "SyncookiesRecv":
  203. procNetstat.TcpExt.SyncookiesRecv = value
  204. case "SyncookiesFailed":
  205. procNetstat.TcpExt.SyncookiesFailed = value
  206. case "EmbryonicRsts":
  207. procNetstat.TcpExt.EmbryonicRsts = value
  208. case "PruneCalled":
  209. procNetstat.TcpExt.PruneCalled = value
  210. case "RcvPruned":
  211. procNetstat.TcpExt.RcvPruned = value
  212. case "OfoPruned":
  213. procNetstat.TcpExt.OfoPruned = value
  214. case "OutOfWindowIcmps":
  215. procNetstat.TcpExt.OutOfWindowIcmps = value
  216. case "LockDroppedIcmps":
  217. procNetstat.TcpExt.LockDroppedIcmps = value
  218. case "ArpFilter":
  219. procNetstat.TcpExt.ArpFilter = value
  220. case "TW":
  221. procNetstat.TcpExt.TW = value
  222. case "TWRecycled":
  223. procNetstat.TcpExt.TWRecycled = value
  224. case "TWKilled":
  225. procNetstat.TcpExt.TWKilled = value
  226. case "PAWSActive":
  227. procNetstat.TcpExt.PAWSActive = value
  228. case "PAWSEstab":
  229. procNetstat.TcpExt.PAWSEstab = value
  230. case "DelayedACKs":
  231. procNetstat.TcpExt.DelayedACKs = value
  232. case "DelayedACKLocked":
  233. procNetstat.TcpExt.DelayedACKLocked = value
  234. case "DelayedACKLost":
  235. procNetstat.TcpExt.DelayedACKLost = value
  236. case "ListenOverflows":
  237. procNetstat.TcpExt.ListenOverflows = value
  238. case "ListenDrops":
  239. procNetstat.TcpExt.ListenDrops = value
  240. case "TCPHPHits":
  241. procNetstat.TcpExt.TCPHPHits = value
  242. case "TCPPureAcks":
  243. procNetstat.TcpExt.TCPPureAcks = value
  244. case "TCPHPAcks":
  245. procNetstat.TcpExt.TCPHPAcks = value
  246. case "TCPRenoRecovery":
  247. procNetstat.TcpExt.TCPRenoRecovery = value
  248. case "TCPSackRecovery":
  249. procNetstat.TcpExt.TCPSackRecovery = value
  250. case "TCPSACKReneging":
  251. procNetstat.TcpExt.TCPSACKReneging = value
  252. case "TCPSACKReorder":
  253. procNetstat.TcpExt.TCPSACKReorder = value
  254. case "TCPRenoReorder":
  255. procNetstat.TcpExt.TCPRenoReorder = value
  256. case "TCPTSReorder":
  257. procNetstat.TcpExt.TCPTSReorder = value
  258. case "TCPFullUndo":
  259. procNetstat.TcpExt.TCPFullUndo = value
  260. case "TCPPartialUndo":
  261. procNetstat.TcpExt.TCPPartialUndo = value
  262. case "TCPDSACKUndo":
  263. procNetstat.TcpExt.TCPDSACKUndo = value
  264. case "TCPLossUndo":
  265. procNetstat.TcpExt.TCPLossUndo = value
  266. case "TCPLostRetransmit":
  267. procNetstat.TcpExt.TCPLostRetransmit = value
  268. case "TCPRenoFailures":
  269. procNetstat.TcpExt.TCPRenoFailures = value
  270. case "TCPSackFailures":
  271. procNetstat.TcpExt.TCPSackFailures = value
  272. case "TCPLossFailures":
  273. procNetstat.TcpExt.TCPLossFailures = value
  274. case "TCPFastRetrans":
  275. procNetstat.TcpExt.TCPFastRetrans = value
  276. case "TCPSlowStartRetrans":
  277. procNetstat.TcpExt.TCPSlowStartRetrans = value
  278. case "TCPTimeouts":
  279. procNetstat.TcpExt.TCPTimeouts = value
  280. case "TCPLossProbes":
  281. procNetstat.TcpExt.TCPLossProbes = value
  282. case "TCPLossProbeRecovery":
  283. procNetstat.TcpExt.TCPLossProbeRecovery = value
  284. case "TCPRenoRecoveryFail":
  285. procNetstat.TcpExt.TCPRenoRecoveryFail = value
  286. case "TCPSackRecoveryFail":
  287. procNetstat.TcpExt.TCPSackRecoveryFail = value
  288. case "TCPRcvCollapsed":
  289. procNetstat.TcpExt.TCPRcvCollapsed = value
  290. case "TCPDSACKOldSent":
  291. procNetstat.TcpExt.TCPDSACKOldSent = value
  292. case "TCPDSACKOfoSent":
  293. procNetstat.TcpExt.TCPDSACKOfoSent = value
  294. case "TCPDSACKRecv":
  295. procNetstat.TcpExt.TCPDSACKRecv = value
  296. case "TCPDSACKOfoRecv":
  297. procNetstat.TcpExt.TCPDSACKOfoRecv = value
  298. case "TCPAbortOnData":
  299. procNetstat.TcpExt.TCPAbortOnData = value
  300. case "TCPAbortOnClose":
  301. procNetstat.TcpExt.TCPAbortOnClose = value
  302. case "TCPDeferAcceptDrop":
  303. procNetstat.TcpExt.TCPDeferAcceptDrop = value
  304. case "IPReversePathFilter":
  305. procNetstat.TcpExt.IPReversePathFilter = value
  306. case "TCPTimeWaitOverflow":
  307. procNetstat.TcpExt.TCPTimeWaitOverflow = value
  308. case "TCPReqQFullDoCookies":
  309. procNetstat.TcpExt.TCPReqQFullDoCookies = value
  310. case "TCPReqQFullDrop":
  311. procNetstat.TcpExt.TCPReqQFullDrop = value
  312. case "TCPRetransFail":
  313. procNetstat.TcpExt.TCPRetransFail = value
  314. case "TCPRcvCoalesce":
  315. procNetstat.TcpExt.TCPRcvCoalesce = value
  316. case "TCPOFOQueue":
  317. procNetstat.TcpExt.TCPOFOQueue = value
  318. case "TCPOFODrop":
  319. procNetstat.TcpExt.TCPOFODrop = value
  320. case "TCPOFOMerge":
  321. procNetstat.TcpExt.TCPOFOMerge = value
  322. case "TCPChallengeACK":
  323. procNetstat.TcpExt.TCPChallengeACK = value
  324. case "TCPSYNChallenge":
  325. procNetstat.TcpExt.TCPSYNChallenge = value
  326. case "TCPFastOpenActive":
  327. procNetstat.TcpExt.TCPFastOpenActive = value
  328. case "TCPFastOpenActiveFail":
  329. procNetstat.TcpExt.TCPFastOpenActiveFail = value
  330. case "TCPFastOpenPassive":
  331. procNetstat.TcpExt.TCPFastOpenPassive = value
  332. case "TCPFastOpenPassiveFail":
  333. procNetstat.TcpExt.TCPFastOpenPassiveFail = value
  334. case "TCPFastOpenListenOverflow":
  335. procNetstat.TcpExt.TCPFastOpenListenOverflow = value
  336. case "TCPFastOpenCookieReqd":
  337. procNetstat.TcpExt.TCPFastOpenCookieReqd = value
  338. case "TCPFastOpenBlackhole":
  339. procNetstat.TcpExt.TCPFastOpenBlackhole = value
  340. case "TCPSpuriousRtxHostQueues":
  341. procNetstat.TcpExt.TCPSpuriousRtxHostQueues = value
  342. case "BusyPollRxPackets":
  343. procNetstat.TcpExt.BusyPollRxPackets = value
  344. case "TCPAutoCorking":
  345. procNetstat.TcpExt.TCPAutoCorking = value
  346. case "TCPFromZeroWindowAdv":
  347. procNetstat.TcpExt.TCPFromZeroWindowAdv = value
  348. case "TCPToZeroWindowAdv":
  349. procNetstat.TcpExt.TCPToZeroWindowAdv = value
  350. case "TCPWantZeroWindowAdv":
  351. procNetstat.TcpExt.TCPWantZeroWindowAdv = value
  352. case "TCPSynRetrans":
  353. procNetstat.TcpExt.TCPSynRetrans = value
  354. case "TCPOrigDataSent":
  355. procNetstat.TcpExt.TCPOrigDataSent = value
  356. case "TCPHystartTrainDetect":
  357. procNetstat.TcpExt.TCPHystartTrainDetect = value
  358. case "TCPHystartTrainCwnd":
  359. procNetstat.TcpExt.TCPHystartTrainCwnd = value
  360. case "TCPHystartDelayDetect":
  361. procNetstat.TcpExt.TCPHystartDelayDetect = value
  362. case "TCPHystartDelayCwnd":
  363. procNetstat.TcpExt.TCPHystartDelayCwnd = value
  364. case "TCPACKSkippedSynRecv":
  365. procNetstat.TcpExt.TCPACKSkippedSynRecv = value
  366. case "TCPACKSkippedPAWS":
  367. procNetstat.TcpExt.TCPACKSkippedPAWS = value
  368. case "TCPACKSkippedSeq":
  369. procNetstat.TcpExt.TCPACKSkippedSeq = value
  370. case "TCPACKSkippedFinWait2":
  371. procNetstat.TcpExt.TCPACKSkippedFinWait2 = value
  372. case "TCPACKSkippedTimeWait":
  373. procNetstat.TcpExt.TCPACKSkippedTimeWait = value
  374. case "TCPACKSkippedChallenge":
  375. procNetstat.TcpExt.TCPACKSkippedChallenge = value
  376. case "TCPWinProbe":
  377. procNetstat.TcpExt.TCPWinProbe = value
  378. case "TCPKeepAlive":
  379. procNetstat.TcpExt.TCPKeepAlive = value
  380. case "TCPMTUPFail":
  381. procNetstat.TcpExt.TCPMTUPFail = value
  382. case "TCPMTUPSuccess":
  383. procNetstat.TcpExt.TCPMTUPSuccess = value
  384. case "TCPWqueueTooBig":
  385. procNetstat.TcpExt.TCPWqueueTooBig = value
  386. }
  387. case "IpExt":
  388. switch key {
  389. case "InNoRoutes":
  390. procNetstat.IpExt.InNoRoutes = value
  391. case "InTruncatedPkts":
  392. procNetstat.IpExt.InTruncatedPkts = value
  393. case "InMcastPkts":
  394. procNetstat.IpExt.InMcastPkts = value
  395. case "OutMcastPkts":
  396. procNetstat.IpExt.OutMcastPkts = value
  397. case "InBcastPkts":
  398. procNetstat.IpExt.InBcastPkts = value
  399. case "OutBcastPkts":
  400. procNetstat.IpExt.OutBcastPkts = value
  401. case "InOctets":
  402. procNetstat.IpExt.InOctets = value
  403. case "OutOctets":
  404. procNetstat.IpExt.OutOctets = value
  405. case "InMcastOctets":
  406. procNetstat.IpExt.InMcastOctets = value
  407. case "OutMcastOctets":
  408. procNetstat.IpExt.OutMcastOctets = value
  409. case "InBcastOctets":
  410. procNetstat.IpExt.InBcastOctets = value
  411. case "OutBcastOctets":
  412. procNetstat.IpExt.OutBcastOctets = value
  413. case "InCsumErrors":
  414. procNetstat.IpExt.InCsumErrors = value
  415. case "InNoECTPkts":
  416. procNetstat.IpExt.InNoECTPkts = value
  417. case "InECT1Pkts":
  418. procNetstat.IpExt.InECT1Pkts = value
  419. case "InECT0Pkts":
  420. procNetstat.IpExt.InECT0Pkts = value
  421. case "InCEPkts":
  422. procNetstat.IpExt.InCEPkts = value
  423. case "ReasmOverlaps":
  424. procNetstat.IpExt.ReasmOverlaps = value
  425. }
  426. }
  427. }
  428. }
  429. return procNetstat, scanner.Err()
  430. }