forked from soarpenguin/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetstat.py
More file actions
277 lines (257 loc) · 13.5 KB
/
Copy pathnetstat.py
File metadata and controls
277 lines (257 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
import re
import resource
import sys
import time
#from collectors.lib import utils
def main():
"""Main loop"""
sys.stdin.close()
interval = 15
page_size = resource.getpagesize()
try:
sockstat = open("/proc/net/sockstat")
netstat = open("/proc/net/netstat")
snmp = open("/proc/net/snmp")
except IOError, e:
print >>sys.stderr, "open failed: %s" % e
return 13 # Ask tcollector to not re-start us.
#utils.drop_privileges()
# Note: up until v2.6.37-rc2 most of the values were 32 bits.
# The first value is pretty useless since it accounts for some
# socket types but not others. So we don't report it because it's
# more confusing than anything else and it's not well documented
# what type of sockets are or aren't included in this count.
regexp = re.compile("sockets: used \d+\n"
"TCP: inuse (?P<tcp_inuse>\d+) orphan (?P<orphans>\d+)"
" tw (?P<tw_count>\d+) alloc (?P<tcp_sockets>\d+)"
" mem (?P<tcp_pages>\d+)\n"
"UDP: inuse (?P<udp_inuse>\d+)"
# UDP memory accounting was added in v2.6.25-rc1
"(?: mem (?P<udp_pages>\d+))?\n"
# UDP-Lite (RFC 3828) was added in v2.6.20-rc2
"(?:UDPLITE: inuse (?P<udplite_inuse>\d+)\n)?"
"RAW: inuse (?P<raw_inuse>\d+)\n"
"FRAG: inuse (?P<ip_frag_nqueues>\d+)"
" memory (?P<ip_frag_mem>\d+)\n")
def print_sockstat(metric, value, tags=""): # Note: tags must start with ' '
if value is not None:
print "net.sockstat.%s %d %s%s" % (metric, ts, value, tags)
# If a line in /proc/net/{netstat,snmp} doesn't start with a word in that
# dict, we'll ignore it. We use the value to build the metric name.
known_statstypes = {
"TcpExt:": "tcp",
"IpExt:": "ip", # We don't collect anything from here for now.
"Ip:": "ip", # We don't collect anything from here for now.
"Icmp:": "icmp", # We don't collect anything from here for now.
"IcmpMsg:": "icmpmsg", # We don't collect anything from here for now.
"Tcp:": "tcp", # We don't collect anything from here for now.
"Udp:": "udp",
"UdpLite:": "udplite", # We don't collect anything from here for now.
}
# Any stat in /proc/net/{netstat,snmp} that doesn't appear in this dict will
# be ignored. If we find a match, we'll use the (metricname, tags).
tcp_stats = {
# An application wasn't able to accept a connection fast enough, so
# the kernel couldn't store an entry in the queue for this connection.
# Instead of dropping it, it sent a cookie to the client.
"SyncookiesSent": ("syncookies", "type=sent"),
# After sending a cookie, it came back to us and passed the check.
"SyncookiesRecv": ("syncookies", "type=received"),
# After sending a cookie, it came back to us but looked invalid.
"SyncookiesFailed": ("syncookies", "type=failed"),
# When a socket is using too much memory (rmem), the kernel will first
# discard any out-of-order packet that has been queued (with SACK).
"OfoPruned": ("memory.prune", "type=drop_ofo_queue"),
# If the kernel is really really desperate and cannot give more memory
# to this socket even after dropping the ofo queue, it will simply
# discard the packet it received. This is Really Bad.
"RcvPruned": ("memory.prune", "type=drop_received"),
# We waited for another packet to send an ACK, but didn't see any, so
# a timer ended up sending a delayed ACK.
"DelayedACKs": ("delayedack", "type=sent"),
# We wanted to send a delayed ACK but failed because the socket was
# locked. So the timer was reset.
"DelayedACKLocked": ("delayedack", "type=locked"),
# We sent a delayed and duplicated ACK because the remote peer
# retransmitted a packet, thinking that it didn't get to us.
"DelayedACKLost": ("delayedack", "type=lost"),
# We completed a 3WHS but couldn't put the socket on the accept queue,
# so we had to discard the connection.
"ListenOverflows": ("failed_accept", "reason=full_acceptq"),
# We couldn't accept a connection because one of: we had no route to
# the destination, we failed to allocate a socket, we failed to
# allocate a new local port bind bucket. Note: this counter
# also include all the increments made to ListenOverflows...
"ListenDrops": ("failed_accept", "reason=other"),
# A packet was lost and we used Forward RTO-Recovery to retransmit.
"TCPForwardRetrans": ("retransmit", "type=forward"),
# A packet was lost and we fast-retransmitted it.
"TCPFastRetrans": ("retransmit", "type=fast"),
# A packet was lost and we retransmitted after a slow start.
"TCPSlowStartRetrans": ("retransmit", "type=slowstart"),
# A packet was lost and we recovered after a fast retransmit.
"TCPRenoRecovery": ("packetloss.recovery", "type=fast_retransmit"),
# A packet was lost and we recovered by using selective
# acknowledgements.
"TCPSackRecovery": ("packetloss.recovery", "type=sack"),
# We detected re-ordering using FACK (Forward ACK -- the highest
# sequence number known to have been received by the peer when using
# SACK -- FACK is used during congestion control).
"TCPFACKReorder": ("reording", "detectedby=fack"),
# We detected re-ordering using SACK.
"TCPSACKReorder": ("reording", "detectedby=sack"),
# We detected re-ordering using fast retransmit.
"TCPRenoReorder": ("reording", "detectedby=fast_retransmit"),
# We detected re-ordering using the timestamp option.
"TCPTSReorder": ("reording", "detectedby=timestamp"),
# We detected some erroneous retransmits and undid our CWND reduction.
"TCPFullUndo": ("congestion.recovery", "type=full_undo"),
# We detected some erroneous retransmits, a partial ACK arrived while
# we were fast retransmitting, so we were able to partially undo some
# of our CWND reduction.
"TCPPartialUndo": ("congestion.recovery", "type=hoe_heuristic"),
# We detected some erroneous retransmits, a D-SACK arrived and ACK'ed
# all the retransmitted data, so we undid our CWND reduction.
"TCPDSACKUndo": ("congestion.recovery", "type=sack"),
# We detected some erroneous retransmits, a partial ACK arrived, so we
# undid our CWND reduction.
"TCPLossUndo": ("congestion.recovery", "type=ack"),
# We received an unexpected SYN so we sent a RST to the peer.
"TCPAbortOnSyn": ("abort", "type=unexpected_syn"),
# We were in FIN_WAIT1 yet we received a data packet with a sequence
# number that's beyond the last one for this connection, so we RST'ed.
"TCPAbortOnData": ("abort", "type=data_after_fin_wait1"),
# We received data but the user has closed the socket, so we have no
# wait of handing it to them, so we RST'ed.
"TCPAbortOnClose": ("abort", "type=data_after_close"),
# This is Really Bad. It happens when there are too many orphaned
# sockets (not attached a FD) and the kernel has to drop a connection.
# Sometimes it will send a reset to the peer, sometimes it wont.
"TCPAbortOnMemory": ("abort", "type=out_of_memory"),
# The connection timed out really hard.
"TCPAbortOnTimeout": ("abort", "type=timeout"),
# We killed a socket that was closed by the application and lingered
# around for long enough.
"TCPAbortOnLinger": ("abort", "type=linger"),
# We tried to send a reset, probably during one of teh TCPABort*
# situations above, but we failed e.g. because we couldn't allocate
# enough memory (very bad).
"TCPAbortFailed": ("abort.failed", None),
# Number of times a socket was put in "memory pressure" due to a non
# fatal memory allocation failure (reduces the send buffer size etc).
"TCPMemoryPressures": ("memory.pressure", None),
# We got a completely invalid SACK block and discarded it.
"TCPSACKDiscard": ("invalid_sack", "type=invalid"),
# We got a duplicate SACK while retransmitting so we discarded it.
"TCPDSACKIgnoredOld": ("invalid_sack", "type=retransmit"),
# We got a duplicate SACK and discarded it.
"TCPDSACKIgnoredNoUndo": ("invalid_sack", "type=olddup"),
# We received something but had to drop it because the socket's
# receive queue was full.
"TCPBacklogDrop": ("receive.queue.full", None),
}
known_stats = {
"tcp": tcp_stats,
"ip": {
},
"icmp": {
},
"icmpmsg": {
},
"udp": {
# Total UDP datagrams received by this host
"InDatagrams": ("datagrams", "direction=in"),
# UDP datagrams received on a port with no listener
"NoPorts": ("errors", "direction=in reason=noport"),
# Total UDP datagrams that could not be delivered to an application
# Note: this counter also increments for RcvbufErrors
"InErrors": ("errors", "direction=in reason=other"),
# Total UDP datagrams sent from this host
"OutDatagrams": ("datagrams", "direction=out"),
# Datagrams for which not enough socket buffer memory to receive
"RcvbufErrors": ("errors", "direction=in reason=nomem"),
# Datagrams for which not enough socket buffer memory to transmit
"SndbufErrors": ("errors", "direction=out reason=nomem"),
},
"udplite": {
},
}
def print_netstat(statstype, metric, value, tags=""):
if tags:
space = " "
else:
tags = space = ""
print "net.stat.%s.%s %d %s%s%s" % (statstype, metric, ts, value,
space, tags)
def parse_stats(stats, filename):
statsdikt = {}
# /proc/net/{netstat,snmp} have a retarded column-oriented format. It
# looks like this:
# Header: SomeMetric OtherMetric
# Header: 1 2
# OtherHeader: ThirdMetric FooBar
# OtherHeader: 42 51
# OtherHeader: FourthMetric
# OtherHeader: 4
# We first pair the lines together, then create a dict for each type:
# {"SomeMetric": "1", "OtherMetric": "2"}
lines = stats.splitlines()
assert len(lines) % 2 == 0, repr(lines)
for header, data in zip(*(iter(lines),) * 2):
header = header.split()
data = data.split()
assert header[0] == data[0], repr((header, data))
assert len(header) == len(data), repr((header, data))
if header[0] not in known_statstypes:
print >>sys.stderr, ("Unrecoginized line in %s:"
" %r (file=%r)" % (filename, header, stats))
continue
statstype = header.pop(0)
data.pop(0)
stats = dict(zip(header, data))
statsdikt.setdefault(known_statstypes[statstype], {}).update(stats)
for statstype, stats in statsdikt.iteritems():
# Undo the kernel's double counting
if "ListenDrops" in stats:
stats["ListenDrops"] = int(stats["ListenDrops"]) - int(stats.get("ListenOverflows", 0))
elif "RcvbufErrors" in stats:
stats["InErrors"] = int(stats.get("InErrors", 0)) - int(stats["RcvbufErrors"])
for stat, (metric, tags) in known_stats[statstype].iteritems():
value = stats.get(stat)
if value is not None:
print_netstat(statstype, metric, value, tags)
while True:
ts = int(time.time())
sockstat.seek(0)
netstat.seek(0)
snmp.seek(0)
data = sockstat.read()
netstats = netstat.read()
snmpstats = snmp.read()
m = re.match(regexp, data)
if not m:
print >>sys.stderr, "Cannot parse sockstat: %r" % data
return 13
# The difference between the first two values is the number of
# sockets allocated vs the number of sockets actually in use.
print_sockstat("num_sockets", m.group("tcp_sockets"), " type=tcp")
print_sockstat("num_timewait", m.group("tw_count"))
print_sockstat("sockets_inuse", m.group("tcp_inuse"), " type=tcp")
print_sockstat("sockets_inuse", m.group("udp_inuse"), " type=udp")
print_sockstat("sockets_inuse", m.group("udplite_inuse"), " type=udplite")
print_sockstat("sockets_inuse", m.group("raw_inuse"), " type=raw")
print_sockstat("num_orphans", m.group("orphans"))
print_sockstat("memory", int(m.group("tcp_pages")) * page_size,
" type=tcp")
if m.group("udp_pages") is not None:
print_sockstat("memory", int(m.group("udp_pages")) * page_size,
" type=udp")
print_sockstat("memory", m.group("ip_frag_mem"), " type=ipfrag")
print_sockstat("ipfragqueues", m.group("ip_frag_nqueues"))
parse_stats(netstats, netstat.name)
parse_stats(snmpstats, snmp.name)
sys.stdout.flush()
time.sleep(interval)
if __name__ == "__main__":
sys.exit(main())