|
| 1 | +import sys |
| 2 | +import re |
| 3 | +import time |
| 4 | + |
| 5 | +PARAMS = {} |
| 6 | + |
| 7 | +NAME_PREFIX = 'tcpext_' |
| 8 | + |
| 9 | +METRICS = { |
| 10 | + 'time' : 0, |
| 11 | + 'data' : {} |
| 12 | +} |
| 13 | +LAST_METRICS = dict(METRICS) |
| 14 | +METRICS_CACHE_MAX = 5 |
| 15 | + |
| 16 | +tcpext_stats_pos = { |
| 17 | + 'syncookiessent' : 1, |
| 18 | + 'syncookiesrecv' : 2, |
| 19 | + 'syncookiesfailed' : 3, |
| 20 | + 'embryonicrsts' : 4, |
| 21 | + 'prunecalled' : 5, |
| 22 | + 'rcvpruned' : 6, |
| 23 | + 'ofopruned' : 7, |
| 24 | + 'outofwindowicmps' : 8, |
| 25 | + 'lockdroppedicmps' : 9, |
| 26 | + 'arpfilter' : 10, |
| 27 | + 'tw' : 11, |
| 28 | + 'twrecycled' : 12, |
| 29 | + 'twkilled' : 13, |
| 30 | + 'pawspassive' : 14, |
| 31 | + 'pawsactive' : 15, |
| 32 | + 'pawsestab' : 16, |
| 33 | + 'delayedacks' : 17, |
| 34 | + 'delayedacklocked' : 18, |
| 35 | + 'delayedacklost' : 19, |
| 36 | + 'listenoverflows' : 20, |
| 37 | + 'listendrops' : 21, |
| 38 | + 'tcpprequeued' : 22, |
| 39 | + 'tcpdirectcopyfrombacklog' : 23, |
| 40 | + 'tcpdirectcopyfromprequeue' : 24, |
| 41 | + 'tcpprequeuedropped' : 25, |
| 42 | + 'tcphphits' : 26, |
| 43 | + 'tcphphitstouser' : 27, |
| 44 | + 'tcppureacks' : 28, |
| 45 | + 'tcphpacks' : 29, |
| 46 | + 'tcprenorecovery' : 30, |
| 47 | + 'tcpsackrecovery' : 31, |
| 48 | + 'tcpsackreneging' : 32, |
| 49 | + 'tcpfackreorder' : 33, |
| 50 | + 'tcpsackreorder' : 34, |
| 51 | + 'tcprenoreorder' : 35, |
| 52 | + 'tcptsreorder' : 36, |
| 53 | + 'tcpfullundo' : 37, |
| 54 | + 'tcppartialundo' : 38, |
| 55 | + 'tcpdsackundo' : 39, |
| 56 | + 'tcplossundo' : 40, |
| 57 | + 'tcploss' : 41, |
| 58 | + 'tcplostretransmit' : 42, |
| 59 | + 'tcprenofailures' : 43, |
| 60 | + 'tcpsackfailures' : 44, |
| 61 | + 'tcplossfailures' : 45, |
| 62 | + 'tcpfastretrans' : 46, |
| 63 | + 'tcpforwardretrans' : 47, |
| 64 | + 'tcpslowstartretrans' : 48, |
| 65 | + 'tcptimeouts' : 49, |
| 66 | + 'tcprenorecoveryfail' : 50, |
| 67 | + 'tcpsackrecoveryfail' : 51, |
| 68 | + 'tcpschedulerfailed' : 52, |
| 69 | + 'tcprcvcollapsed' : 53, |
| 70 | + 'tcpdsackoldsent' : 54, |
| 71 | + 'tcpdsackofosent' : 55, |
| 72 | + 'tcpdsackrecv' : 56, |
| 73 | + 'tcpdsackoforecv' : 57, |
| 74 | + 'tcpabortonsyn' : 58, |
| 75 | + 'tcpabortondata' : 59, |
| 76 | + 'tcpabortonclose' : 60, |
| 77 | + 'tcpabortonmemory' : 61, |
| 78 | + 'tcpabortontimeout' : 62, |
| 79 | + 'tcpabortonlinger' : 63, |
| 80 | + 'tcpabortfailed' : 64, |
| 81 | + 'tcpmemorypressures' : 65, |
| 82 | + 'tcpsackdiscard' : 66, |
| 83 | + 'tcpdsackignoredold' : 67, |
| 84 | + 'tcpdsackignorednoundo' : 68, |
| 85 | + 'tcpspuriousrtos' : 69, |
| 86 | + 'tcpmd5notfound' : 70, |
| 87 | + 'tcpmd5unexpected' : 71, |
| 88 | + 'tcpsackshifted' : 72, |
| 89 | + 'tcpsackmerged' : 73, |
| 90 | + 'tcpsackshiftfallback' : 74, |
| 91 | + 'tcpbacklogdrop' : 75, |
| 92 | + 'tcpminttldrop' : 76, |
| 93 | + 'tcpdeferacceptdrop' : 77 |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +############################################################################### |
| 98 | +# Explanation of metrics in /proc/meminfo can be found here |
| 99 | +# |
| 100 | +# http://www.redhat.com/advice/tips/meminfo.html |
| 101 | +# and |
| 102 | +# http://unixfoo.blogspot.com/2008/02/know-about-procmeminfo.html |
| 103 | +# and |
| 104 | +# http://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-proc-meminfo.html |
| 105 | +############################################################################### |
| 106 | +tcpext_file = "/proc/net/netstat" |
| 107 | + |
| 108 | + |
| 109 | +def get_metrics(): |
| 110 | + """Return all metrics""" |
| 111 | + |
| 112 | + global METRICS, LAST_METRICS |
| 113 | + |
| 114 | + if (time.time() - METRICS['time']) > METRICS_CACHE_MAX: |
| 115 | + |
| 116 | + try: |
| 117 | + file = open(tcpext_file, 'r') |
| 118 | + |
| 119 | + except IOError: |
| 120 | + return 0 |
| 121 | + |
| 122 | + # convert to dict |
| 123 | + metrics = {} |
| 124 | + for line in file: |
| 125 | + if re.match("TcpExt: [0-9]", line): |
| 126 | + print line |
| 127 | + metrics = re.split("\s+", line) |
| 128 | + |
| 129 | + # update cache |
| 130 | + LAST_METRICS = dict(METRICS) |
| 131 | + METRICS = { |
| 132 | + 'time': time.time(), |
| 133 | + 'data': metrics |
| 134 | + } |
| 135 | + |
| 136 | + return [METRICS, LAST_METRICS] |
| 137 | + |
| 138 | +def get_value(name): |
| 139 | + """Return a value for the requested metric""" |
| 140 | + |
| 141 | + metrics = get_metrics()[0] |
| 142 | + |
| 143 | + name = name[len(NAME_PREFIX):] # remove prefix from name |
| 144 | + |
| 145 | + try: |
| 146 | + result = metrics['data'][name] |
| 147 | + except StandardError: |
| 148 | + result = 0 |
| 149 | + |
| 150 | + return result |
| 151 | + |
| 152 | + |
| 153 | +def get_delta(name): |
| 154 | + """Return change over time for the requested metric""" |
| 155 | + |
| 156 | + # get metrics |
| 157 | + [curr_metrics, last_metrics] = get_metrics() |
| 158 | + |
| 159 | + name = name[len(NAME_PREFIX):] # remove prefix from name |
| 160 | + index = tcpext_stats_pos[name] |
| 161 | + |
| 162 | + try: |
| 163 | + delta = (float(curr_metrics['data'][index]) - float(last_metrics['data'][index])) /(curr_metrics['time'] - last_metrics['time']) |
| 164 | + if delta < 0: |
| 165 | + print name + " is less 0" |
| 166 | + delta = 0 |
| 167 | + except KeyError: |
| 168 | + delta = 0.0 |
| 169 | + |
| 170 | + return delta |
| 171 | + |
| 172 | + |
| 173 | +def create_desc(skel, prop): |
| 174 | + d = skel.copy() |
| 175 | + for k,v in prop.iteritems(): |
| 176 | + d[k] = v |
| 177 | + return d |
| 178 | + |
| 179 | +def metric_init(params): |
| 180 | + global descriptors, metric_map, Desc_Skel |
| 181 | + |
| 182 | + descriptors = [] |
| 183 | + |
| 184 | + Desc_Skel = { |
| 185 | + 'name' : 'XXX', |
| 186 | + 'call_back' : get_delta, |
| 187 | + 'time_max' : 60, |
| 188 | + 'value_type' : 'float', |
| 189 | + 'format' : '%.4f', |
| 190 | + 'units' : 'count/s', |
| 191 | + 'slope' : 'both', # zero|positive|negative|both |
| 192 | + 'description' : 'XXX', |
| 193 | + 'groups' : 'tcp_extended', |
| 194 | + } |
| 195 | + |
| 196 | + for item in tcpext_stats_pos: |
| 197 | + descriptors.append(create_desc(Desc_Skel, { |
| 198 | + "name" : NAME_PREFIX + item, |
| 199 | + "description": item, |
| 200 | + })) |
| 201 | + |
| 202 | + return descriptors |
| 203 | + |
| 204 | +def metric_cleanup(): |
| 205 | + '''Clean up the metric module.''' |
| 206 | + pass |
| 207 | + |
| 208 | +#This code is for debugging and unit testing |
| 209 | +if __name__ == '__main__': |
| 210 | + descriptors = metric_init(PARAMS) |
| 211 | + while True: |
| 212 | + for d in descriptors: |
| 213 | + v = d['call_back'](d['name']) |
| 214 | + print '%s = %s' % (d['name'], v) |
| 215 | + print 'Sleeping 15 seconds' |
| 216 | + time.sleep(15) |
0 commit comments