proc_netstat.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. TCPRcvQDrop *float64
  109. TCPOFOQueue *float64
  110. TCPOFODrop *float64
  111. TCPOFOMerge *float64
  112. TCPChallengeACK *float64
  113. TCPSYNChallenge *float64
  114. TCPFastOpenActive *float64
  115. TCPFastOpenActiveFail *float64
  116. TCPFastOpenPassive *float64
  117. TCPFastOpenPassiveFail *float64
  118. TCPFastOpenListenOverflow *float64
  119. TCPFastOpenCookieReqd *float64
  120. TCPFastOpenBlackhole *float64
  121. TCPSpuriousRtxHostQueues *float64
  122. BusyPollRxPackets *float64
  123. TCPAutoCorking *float64
  124. TCPFromZeroWindowAdv *float64
  125. TCPToZeroWindowAdv *float64
  126. TCPWantZeroWindowAdv *float64
  127. TCPSynRetrans *float64
  128. TCPOrigDataSent *float64
  129. TCPHystartTrainDetect *float64
  130. TCPHystartTrainCwnd *float64
  131. TCPHystartDelayDetect *float64
  132. TCPHystartDelayCwnd *float64
  133. TCPACKSkippedSynRecv *float64
  134. TCPACKSkippedPAWS *float64
  135. TCPACKSkippedSeq *float64
  136. TCPACKSkippedFinWait2 *float64
  137. TCPACKSkippedTimeWait *float64
  138. TCPACKSkippedChallenge *float64
  139. TCPWinProbe *float64
  140. TCPKeepAlive *float64
  141. TCPMTUPFail *float64
  142. TCPMTUPSuccess *float64
  143. TCPWqueueTooBig *float64
  144. }
  145. type IpExt struct { // nolint:revive
  146. InNoRoutes *float64
  147. InTruncatedPkts *float64
  148. InMcastPkts *float64
  149. OutMcastPkts *float64
  150. InBcastPkts *float64
  151. OutBcastPkts *float64
  152. InOctets *float64
  153. OutOctets *float64
  154. InMcastOctets *float64
  155. OutMcastOctets *float64
  156. InBcastOctets *float64
  157. OutBcastOctets *float64
  158. InCsumErrors *float64
  159. InNoECTPkts *float64
  160. InECT1Pkts *float64
  161. InECT0Pkts *float64
  162. InCEPkts *float64
  163. ReasmOverlaps *float64
  164. }
  165. func (p Proc) Netstat() (ProcNetstat, error) {
  166. filename := p.path("net/netstat")
  167. data, err := util.ReadFileNoStat(filename)
  168. if err != nil {
  169. return ProcNetstat{PID: p.PID}, err
  170. }
  171. procNetstat, err := parseProcNetstat(bytes.NewReader(data), filename)
  172. procNetstat.PID = p.PID
  173. return procNetstat, err
  174. }
  175. // parseProcNetstat parses the metrics from proc/<pid>/net/netstat file
  176. // and returns a ProcNetstat structure.
  177. func parseProcNetstat(r io.Reader, fileName string) (ProcNetstat, error) {
  178. var (
  179. scanner = bufio.NewScanner(r)
  180. procNetstat = ProcNetstat{}
  181. )
  182. for scanner.Scan() {
  183. nameParts := strings.Split(scanner.Text(), " ")
  184. scanner.Scan()
  185. valueParts := strings.Split(scanner.Text(), " ")
  186. // Remove trailing :.
  187. protocol := strings.TrimSuffix(nameParts[0], ":")
  188. if len(nameParts) != len(valueParts) {
  189. return procNetstat, fmt.Errorf("%w: mismatch field count mismatch in %s: %s",
  190. ErrFileParse, fileName, protocol)
  191. }
  192. for i := 1; i < len(nameParts); i++ {
  193. value, err := strconv.ParseFloat(valueParts[i], 64)
  194. if err != nil {
  195. return procNetstat, err
  196. }
  197. key := nameParts[i]
  198. switch protocol {
  199. case "TcpExt":
  200. switch key {
  201. case "SyncookiesSent":
  202. procNetstat.TcpExt.SyncookiesSent = &value
  203. case "SyncookiesRecv":
  204. procNetstat.TcpExt.SyncookiesRecv = &value
  205. case "SyncookiesFailed":
  206. procNetstat.TcpExt.SyncookiesFailed = &value
  207. case "EmbryonicRsts":
  208. procNetstat.TcpExt.EmbryonicRsts = &value
  209. case "PruneCalled":
  210. procNetstat.TcpExt.PruneCalled = &value
  211. case "RcvPruned":
  212. procNetstat.TcpExt.RcvPruned = &value
  213. case "OfoPruned":
  214. procNetstat.TcpExt.OfoPruned = &value
  215. case "OutOfWindowIcmps":
  216. procNetstat.TcpExt.OutOfWindowIcmps = &value
  217. case "LockDroppedIcmps":
  218. procNetstat.TcpExt.LockDroppedIcmps = &value
  219. case "ArpFilter":
  220. procNetstat.TcpExt.ArpFilter = &value
  221. case "TW":
  222. procNetstat.TcpExt.TW = &value
  223. case "TWRecycled":
  224. procNetstat.TcpExt.TWRecycled = &value
  225. case "TWKilled":
  226. procNetstat.TcpExt.TWKilled = &value
  227. case "PAWSActive":
  228. procNetstat.TcpExt.PAWSActive = &value
  229. case "PAWSEstab":
  230. procNetstat.TcpExt.PAWSEstab = &value
  231. case "DelayedACKs":
  232. procNetstat.TcpExt.DelayedACKs = &value
  233. case "DelayedACKLocked":
  234. procNetstat.TcpExt.DelayedACKLocked = &value
  235. case "DelayedACKLost":
  236. procNetstat.TcpExt.DelayedACKLost = &value
  237. case "ListenOverflows":
  238. procNetstat.TcpExt.ListenOverflows = &value
  239. case "ListenDrops":
  240. procNetstat.TcpExt.ListenDrops = &value
  241. case "TCPHPHits":
  242. procNetstat.TcpExt.TCPHPHits = &value
  243. case "TCPPureAcks":
  244. procNetstat.TcpExt.TCPPureAcks = &value
  245. case "TCPHPAcks":
  246. procNetstat.TcpExt.TCPHPAcks = &value
  247. case "TCPRenoRecovery":
  248. procNetstat.TcpExt.TCPRenoRecovery = &value
  249. case "TCPSackRecovery":
  250. procNetstat.TcpExt.TCPSackRecovery = &value
  251. case "TCPSACKReneging":
  252. procNetstat.TcpExt.TCPSACKReneging = &value
  253. case "TCPSACKReorder":
  254. procNetstat.TcpExt.TCPSACKReorder = &value
  255. case "TCPRenoReorder":
  256. procNetstat.TcpExt.TCPRenoReorder = &value
  257. case "TCPTSReorder":
  258. procNetstat.TcpExt.TCPTSReorder = &value
  259. case "TCPFullUndo":
  260. procNetstat.TcpExt.TCPFullUndo = &value
  261. case "TCPPartialUndo":
  262. procNetstat.TcpExt.TCPPartialUndo = &value
  263. case "TCPDSACKUndo":
  264. procNetstat.TcpExt.TCPDSACKUndo = &value
  265. case "TCPLossUndo":
  266. procNetstat.TcpExt.TCPLossUndo = &value
  267. case "TCPLostRetransmit":
  268. procNetstat.TcpExt.TCPLostRetransmit = &value
  269. case "TCPRenoFailures":
  270. procNetstat.TcpExt.TCPRenoFailures = &value
  271. case "TCPSackFailures":
  272. procNetstat.TcpExt.TCPSackFailures = &value
  273. case "TCPLossFailures":
  274. procNetstat.TcpExt.TCPLossFailures = &value
  275. case "TCPFastRetrans":
  276. procNetstat.TcpExt.TCPFastRetrans = &value
  277. case "TCPSlowStartRetrans":
  278. procNetstat.TcpExt.TCPSlowStartRetrans = &value
  279. case "TCPTimeouts":
  280. procNetstat.TcpExt.TCPTimeouts = &value
  281. case "TCPLossProbes":
  282. procNetstat.TcpExt.TCPLossProbes = &value
  283. case "TCPLossProbeRecovery":
  284. procNetstat.TcpExt.TCPLossProbeRecovery = &value
  285. case "TCPRenoRecoveryFail":
  286. procNetstat.TcpExt.TCPRenoRecoveryFail = &value
  287. case "TCPSackRecoveryFail":
  288. procNetstat.TcpExt.TCPSackRecoveryFail = &value
  289. case "TCPRcvCollapsed":
  290. procNetstat.TcpExt.TCPRcvCollapsed = &value
  291. case "TCPDSACKOldSent":
  292. procNetstat.TcpExt.TCPDSACKOldSent = &value
  293. case "TCPDSACKOfoSent":
  294. procNetstat.TcpExt.TCPDSACKOfoSent = &value
  295. case "TCPDSACKRecv":
  296. procNetstat.TcpExt.TCPDSACKRecv = &value
  297. case "TCPDSACKOfoRecv":
  298. procNetstat.TcpExt.TCPDSACKOfoRecv = &value
  299. case "TCPAbortOnData":
  300. procNetstat.TcpExt.TCPAbortOnData = &value
  301. case "TCPAbortOnClose":
  302. procNetstat.TcpExt.TCPAbortOnClose = &value
  303. case "TCPDeferAcceptDrop":
  304. procNetstat.TcpExt.TCPDeferAcceptDrop = &value
  305. case "IPReversePathFilter":
  306. procNetstat.TcpExt.IPReversePathFilter = &value
  307. case "TCPTimeWaitOverflow":
  308. procNetstat.TcpExt.TCPTimeWaitOverflow = &value
  309. case "TCPReqQFullDoCookies":
  310. procNetstat.TcpExt.TCPReqQFullDoCookies = &value
  311. case "TCPReqQFullDrop":
  312. procNetstat.TcpExt.TCPReqQFullDrop = &value
  313. case "TCPRetransFail":
  314. procNetstat.TcpExt.TCPRetransFail = &value
  315. case "TCPRcvCoalesce":
  316. procNetstat.TcpExt.TCPRcvCoalesce = &value
  317. case "TCPRcvQDrop":
  318. procNetstat.TcpExt.TCPRcvQDrop = &value
  319. case "TCPOFOQueue":
  320. procNetstat.TcpExt.TCPOFOQueue = &value
  321. case "TCPOFODrop":
  322. procNetstat.TcpExt.TCPOFODrop = &value
  323. case "TCPOFOMerge":
  324. procNetstat.TcpExt.TCPOFOMerge = &value
  325. case "TCPChallengeACK":
  326. procNetstat.TcpExt.TCPChallengeACK = &value
  327. case "TCPSYNChallenge":
  328. procNetstat.TcpExt.TCPSYNChallenge = &value
  329. case "TCPFastOpenActive":
  330. procNetstat.TcpExt.TCPFastOpenActive = &value
  331. case "TCPFastOpenActiveFail":
  332. procNetstat.TcpExt.TCPFastOpenActiveFail = &value
  333. case "TCPFastOpenPassive":
  334. procNetstat.TcpExt.TCPFastOpenPassive = &value
  335. case "TCPFastOpenPassiveFail":
  336. procNetstat.TcpExt.TCPFastOpenPassiveFail = &value
  337. case "TCPFastOpenListenOverflow":
  338. procNetstat.TcpExt.TCPFastOpenListenOverflow = &value
  339. case "TCPFastOpenCookieReqd":
  340. procNetstat.TcpExt.TCPFastOpenCookieReqd = &value
  341. case "TCPFastOpenBlackhole":
  342. procNetstat.TcpExt.TCPFastOpenBlackhole = &value
  343. case "TCPSpuriousRtxHostQueues":
  344. procNetstat.TcpExt.TCPSpuriousRtxHostQueues = &value
  345. case "BusyPollRxPackets":
  346. procNetstat.TcpExt.BusyPollRxPackets = &value
  347. case "TCPAutoCorking":
  348. procNetstat.TcpExt.TCPAutoCorking = &value
  349. case "TCPFromZeroWindowAdv":
  350. procNetstat.TcpExt.TCPFromZeroWindowAdv = &value
  351. case "TCPToZeroWindowAdv":
  352. procNetstat.TcpExt.TCPToZeroWindowAdv = &value
  353. case "TCPWantZeroWindowAdv":
  354. procNetstat.TcpExt.TCPWantZeroWindowAdv = &value
  355. case "TCPSynRetrans":
  356. procNetstat.TcpExt.TCPSynRetrans = &value
  357. case "TCPOrigDataSent":
  358. procNetstat.TcpExt.TCPOrigDataSent = &value
  359. case "TCPHystartTrainDetect":
  360. procNetstat.TcpExt.TCPHystartTrainDetect = &value
  361. case "TCPHystartTrainCwnd":
  362. procNetstat.TcpExt.TCPHystartTrainCwnd = &value
  363. case "TCPHystartDelayDetect":
  364. procNetstat.TcpExt.TCPHystartDelayDetect = &value
  365. case "TCPHystartDelayCwnd":
  366. procNetstat.TcpExt.TCPHystartDelayCwnd = &value
  367. case "TCPACKSkippedSynRecv":
  368. procNetstat.TcpExt.TCPACKSkippedSynRecv = &value
  369. case "TCPACKSkippedPAWS":
  370. procNetstat.TcpExt.TCPACKSkippedPAWS = &value
  371. case "TCPACKSkippedSeq":
  372. procNetstat.TcpExt.TCPACKSkippedSeq = &value
  373. case "TCPACKSkippedFinWait2":
  374. procNetstat.TcpExt.TCPACKSkippedFinWait2 = &value
  375. case "TCPACKSkippedTimeWait":
  376. procNetstat.TcpExt.TCPACKSkippedTimeWait = &value
  377. case "TCPACKSkippedChallenge":
  378. procNetstat.TcpExt.TCPACKSkippedChallenge = &value
  379. case "TCPWinProbe":
  380. procNetstat.TcpExt.TCPWinProbe = &value
  381. case "TCPKeepAlive":
  382. procNetstat.TcpExt.TCPKeepAlive = &value
  383. case "TCPMTUPFail":
  384. procNetstat.TcpExt.TCPMTUPFail = &value
  385. case "TCPMTUPSuccess":
  386. procNetstat.TcpExt.TCPMTUPSuccess = &value
  387. case "TCPWqueueTooBig":
  388. procNetstat.TcpExt.TCPWqueueTooBig = &value
  389. }
  390. case "IpExt":
  391. switch key {
  392. case "InNoRoutes":
  393. procNetstat.IpExt.InNoRoutes = &value
  394. case "InTruncatedPkts":
  395. procNetstat.IpExt.InTruncatedPkts = &value
  396. case "InMcastPkts":
  397. procNetstat.IpExt.InMcastPkts = &value
  398. case "OutMcastPkts":
  399. procNetstat.IpExt.OutMcastPkts = &value
  400. case "InBcastPkts":
  401. procNetstat.IpExt.InBcastPkts = &value
  402. case "OutBcastPkts":
  403. procNetstat.IpExt.OutBcastPkts = &value
  404. case "InOctets":
  405. procNetstat.IpExt.InOctets = &value
  406. case "OutOctets":
  407. procNetstat.IpExt.OutOctets = &value
  408. case "InMcastOctets":
  409. procNetstat.IpExt.InMcastOctets = &value
  410. case "OutMcastOctets":
  411. procNetstat.IpExt.OutMcastOctets = &value
  412. case "InBcastOctets":
  413. procNetstat.IpExt.InBcastOctets = &value
  414. case "OutBcastOctets":
  415. procNetstat.IpExt.OutBcastOctets = &value
  416. case "InCsumErrors":
  417. procNetstat.IpExt.InCsumErrors = &value
  418. case "InNoECTPkts":
  419. procNetstat.IpExt.InNoECTPkts = &value
  420. case "InECT1Pkts":
  421. procNetstat.IpExt.InECT1Pkts = &value
  422. case "InECT0Pkts":
  423. procNetstat.IpExt.InECT0Pkts = &value
  424. case "InCEPkts":
  425. procNetstat.IpExt.InCEPkts = &value
  426. case "ReasmOverlaps":
  427. procNetstat.IpExt.ReasmOverlaps = &value
  428. }
  429. }
  430. }
  431. }
  432. return procNetstat, scanner.Err()
  433. }