|
3 | 3 | // you may not use this file except in compliance with the License. |
4 | 4 | // You may obtain a copy of the License at |
5 | 5 | // |
6 | | -// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
7 | 7 | // |
8 | 8 | // Unless required by applicable law or agreed to in writing, software |
9 | 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
@@ -127,71 +127,81 @@ func getData(queryString string) ([]byte, error) { |
127 | 127 |
|
128 | 128 | func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error { |
129 | 129 | tcpData, err := getData("net.inet.tcp.stats") |
130 | | - if err == nil && len(tcpData) >= int(unsafe.Sizeof(C.struct_tcpstat{})) { |
131 | | - tcpStats := *(*C.struct_tcpstat)(unsafe.Pointer(&tcpData[0])) |
132 | | - |
133 | | - ch <- prometheus.MustNewConstMetric( |
134 | | - bsdNetstatTcpSendPacketsTotal, |
135 | | - prometheus.CounterValue, |
136 | | - float64(tcpStats.tcps_sndtotal), |
137 | | - ) |
138 | | - ch <- prometheus.MustNewConstMetric( |
139 | | - bsdNetstatTcpRecvPacketsTotal, |
140 | | - prometheus.CounterValue, |
141 | | - float64(tcpStats.tcps_rcvtotal), |
142 | | - ) |
143 | | - ch <- prometheus.MustNewConstMetric( |
144 | | - bsdNetstatTcpConnectionAttempts, |
145 | | - prometheus.CounterValue, |
146 | | - float64(tcpStats.tcps_connattempt), |
147 | | - ) |
148 | | - ch <- prometheus.MustNewConstMetric( |
149 | | - bsdNetstatTcpConnectionAccepts, |
150 | | - prometheus.CounterValue, |
151 | | - float64(tcpStats.tcps_accepts), |
152 | | - ) |
153 | | - ch <- prometheus.MustNewConstMetric( |
154 | | - bsdNetstatTcpConnectionDrops, |
155 | | - prometheus.CounterValue, |
156 | | - float64(tcpStats.tcps_drops), |
157 | | - ) |
158 | | - ch <- prometheus.MustNewConstMetric( |
159 | | - bsdNetstatTcpRetransmitPackets, |
160 | | - prometheus.CounterValue, |
161 | | - float64(tcpStats.tcps_sndrexmitpack), |
162 | | - ) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("failed to get TCP stats: %w", err) |
| 132 | + } |
| 133 | + if len(tcpData) < int(unsafe.Sizeof(C.struct_tcpstat{})) { |
| 134 | + return fmt.Errorf("TCP data size mismatch: got %d, want >= %d", len(tcpData), unsafe.Sizeof(C.struct_tcpstat{})) |
163 | 135 | } |
164 | 136 |
|
| 137 | + tcpStats := *(*C.struct_tcpstat)(unsafe.Pointer(&tcpData[0])) |
| 138 | + |
| 139 | + ch <- prometheus.MustNewConstMetric( |
| 140 | + bsdNetstatTcpSendPacketsTotal, |
| 141 | + prometheus.CounterValue, |
| 142 | + float64(tcpStats.tcps_sndtotal), |
| 143 | + ) |
| 144 | + ch <- prometheus.MustNewConstMetric( |
| 145 | + bsdNetstatTcpRecvPacketsTotal, |
| 146 | + prometheus.CounterValue, |
| 147 | + float64(tcpStats.tcps_rcvtotal), |
| 148 | + ) |
| 149 | + ch <- prometheus.MustNewConstMetric( |
| 150 | + bsdNetstatTcpConnectionAttempts, |
| 151 | + prometheus.CounterValue, |
| 152 | + float64(tcpStats.tcps_connattempt), |
| 153 | + ) |
| 154 | + ch <- prometheus.MustNewConstMetric( |
| 155 | + bsdNetstatTcpConnectionAccepts, |
| 156 | + prometheus.CounterValue, |
| 157 | + float64(tcpStats.tcps_accepts), |
| 158 | + ) |
| 159 | + ch <- prometheus.MustNewConstMetric( |
| 160 | + bsdNetstatTcpConnectionDrops, |
| 161 | + prometheus.CounterValue, |
| 162 | + float64(tcpStats.tcps_drops), |
| 163 | + ) |
| 164 | + ch <- prometheus.MustNewConstMetric( |
| 165 | + bsdNetstatTcpRetransmitPackets, |
| 166 | + prometheus.CounterValue, |
| 167 | + float64(tcpStats.tcps_sndrexmitpack), |
| 168 | + ) |
| 169 | + |
165 | 170 | udpData, err := getData("net.inet.udp.stats") |
166 | | - if err == nil && len(udpData) >= int(unsafe.Sizeof(C.struct_udpstat{})) { |
167 | | - udpStats := *(*C.struct_udpstat)(unsafe.Pointer(&udpData[0])) |
168 | | - |
169 | | - ch <- prometheus.MustNewConstMetric( |
170 | | - bsdNetstatUdpSendPacketsTotal, |
171 | | - prometheus.CounterValue, |
172 | | - float64(udpStats.udps_opackets), |
173 | | - ) |
174 | | - ch <- prometheus.MustNewConstMetric( |
175 | | - bsdNetstatUdpRecvPacketsTotal, |
176 | | - prometheus.CounterValue, |
177 | | - float64(udpStats.udps_ipackets), |
178 | | - ) |
179 | | - ch <- prometheus.MustNewConstMetric( |
180 | | - bsdNetstatUdpHeaderDrops, |
181 | | - prometheus.CounterValue, |
182 | | - float64(udpStats.udps_hdrops), |
183 | | - ) |
184 | | - ch <- prometheus.MustNewConstMetric( |
185 | | - bsdNetstatUdpBadChecksum, |
186 | | - prometheus.CounterValue, |
187 | | - float64(udpStats.udps_badsum), |
188 | | - ) |
189 | | - ch <- prometheus.MustNewConstMetric( |
190 | | - bsdNetstatUdpNoPort, |
191 | | - prometheus.CounterValue, |
192 | | - float64(udpStats.udps_noport), |
193 | | - ) |
| 171 | + if err != nil { |
| 172 | + return fmt.Errorf("failed to get UDP stats: %w", err) |
| 173 | + } |
| 174 | + if len(udpData) < int(unsafe.Sizeof(C.struct_udpstat{})) { |
| 175 | + return fmt.Errorf("UDP data size mismatch: got %d, want >= %d", len(udpData), unsafe.Sizeof(C.struct_udpstat{})) |
194 | 176 | } |
195 | 177 |
|
| 178 | + udpStats := *(*C.struct_udpstat)(unsafe.Pointer(&udpData[0])) |
| 179 | + |
| 180 | + ch <- prometheus.MustNewConstMetric( |
| 181 | + bsdNetstatUdpSendPacketsTotal, |
| 182 | + prometheus.CounterValue, |
| 183 | + float64(udpStats.udps_opackets), |
| 184 | + ) |
| 185 | + ch <- prometheus.MustNewConstMetric( |
| 186 | + bsdNetstatUdpRecvPacketsTotal, |
| 187 | + prometheus.CounterValue, |
| 188 | + float64(udpStats.udps_ipackets), |
| 189 | + ) |
| 190 | + ch <- prometheus.MustNewConstMetric( |
| 191 | + bsdNetstatUdpHeaderDrops, |
| 192 | + prometheus.CounterValue, |
| 193 | + float64(udpStats.udps_hdrops), |
| 194 | + ) |
| 195 | + ch <- prometheus.MustNewConstMetric( |
| 196 | + bsdNetstatUdpBadChecksum, |
| 197 | + prometheus.CounterValue, |
| 198 | + float64(udpStats.udps_badsum), |
| 199 | + ) |
| 200 | + ch <- prometheus.MustNewConstMetric( |
| 201 | + bsdNetstatUdpNoPort, |
| 202 | + prometheus.CounterValue, |
| 203 | + float64(udpStats.udps_noport), |
| 204 | + ) |
| 205 | + |
196 | 206 | return nil |
197 | 207 | } |
0 commit comments