| 1 |
#/****************************************************************************** |
| 2 |
#* Portions Copyright (C) 2007 Novell, Inc. All rights reserved. |
| 3 |
#* |
| 4 |
#* Redistribution and use in source and binary forms, with or without |
| 5 |
#* modification, are permitted provided that the following conditions are met: |
| 6 |
#* |
| 7 |
#* - Redistributions of source code must retain the above copyright notice, |
| 8 |
#* this list of conditions and the following disclaimer. |
| 9 |
#* |
| 10 |
#* - Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
#* this list of conditions and the following disclaimer in the documentation |
| 12 |
#* and/or other materials provided with the distribution. |
| 13 |
#* |
| 14 |
#* - Neither the name of Novell, Inc. nor the names of its |
| 15 |
#* contributors may be used to endorse or promote products derived from this |
| 16 |
#* software without specific prior written permission. |
| 17 |
#* |
| 18 |
#* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' |
| 19 |
#* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
#* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
#* ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc. OR THE CONTRIBUTORS |
| 22 |
#* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
#* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
#* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
#* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
#* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
#* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
#* POSSIBILITY OF SUCH DAMAGE. |
| 29 |
#* |
| 30 |
#* Author: Brad Nicholes (bnicholes novell.com) |
| 31 |
#******************************************************************************/ |
| 32 |
|
| 33 |
import os |
| 34 |
|
| 35 |
OBSOLETE_POPEN = False |
| 36 |
try: |
| 37 |
import subprocess |
| 38 |
except ImportError: |
| 39 |
import popen2 |
| 40 |
OBSOLETE_POPEN = True |
| 41 |
|
| 42 |
import threading |
| 43 |
import time |
| 44 |
|
| 45 |
_WorkerThread = None # Worker thread object |
| 46 |
_glock = threading.Lock() # Synchronization lock |
| 47 |
_refresh_rate = 30 # Refresh rate of the netstat data |
| 48 |
|
| 49 |
#Global dictionary storing the counts of the last connection state |
| 50 |
# read from the netstat output |
| 51 |
_conns = {'tcp_established': 0, |
| 52 |
'tcp_listen': 0, |
| 53 |
'tcp_timewait': 0, |
| 54 |
'tcp_closewait': 0, |
| 55 |
'tcp_synsent': 0, |
| 56 |
'tcp_synrecv': 0, |
| 57 |
'tcp_synwait': 0, |
| 58 |
'tcp_finwait1': 0, |
| 59 |
'tcp_finwait2': 0, |
| 60 |
'tcp_closed': 0, |
| 61 |
'tcp_lastack': 0, |
| 62 |
'tcp_closing': 0, |
| 63 |
'tcp_unknown': 0} |
| 64 |
|
| 65 |
|
| 66 |
def TCP_Connections(name): |
| 67 |
'''Return the requested connection type status.''' |
| 68 |
global _WorkerThread |
| 69 |
|
| 70 |
if _WorkerThread is None: |
| 71 |
print 'Error: No netstat data gathering thread created for metric %s' % name |
| 72 |
return 0 |
| 73 |
|
| 74 |
if not _WorkerThread.running and not _WorkerThread.shuttingdown: |
| 75 |
try: |
| 76 |
_WorkerThread.start() |
| 77 |
except (AssertionError, RuntimeError): |
| 78 |
pass |
| 79 |
|
| 80 |
#Read the last connection total for the state requested. The metric |
| 81 |
# name passed in matches the dictionary slot for the state value. |
| 82 |
_glock.acquire() |
| 83 |
ret = int(_conns[name]) |
| 84 |
_glock.release() |
| 85 |
return ret |
| 86 |
|
| 87 |
#Metric descriptions |
| 88 |
_descriptors = [{'name': 'tcp_established', |
| 89 |
'call_back': TCP_Connections, |
| 90 |
'time_max': 20, |
| 91 |
'value_type': 'uint', |
| 92 |
'units': 'Sockets', |
| 93 |
'slope': 'both', |
| 94 |
'format': '%u', |
| 95 |
'description': 'Total number of established TCP connections', |
| 96 |
'groups': 'network', |
| 97 |
}, |
| 98 |
{'name': 'tcp_listen', |
| 99 |
'call_back': TCP_Connections, |
| 100 |
'time_max': 20, |
| 101 |
'value_type': 'uint', |
| 102 |
'units': 'Sockets', |
| 103 |
'slope': 'both', |
| 104 |
'format': '%u', |
| 105 |
'description': 'Total number of listening TCP connections', |
| 106 |
'groups': 'network', |
| 107 |
}, |
| 108 |
{'name': 'tcp_timewait', |
| 109 |
'call_back': TCP_Connections, |
| 110 |
'time_max': 20, |
| 111 |
'value_type': 'uint', |
| 112 |
'units': 'Sockets', |
| 113 |
'slope': 'both', |
| 114 |
'format': '%u', |
| 115 |
'description': 'Total number of time_wait TCP connections', |
| 116 |
'groups': 'network', |
| 117 |
}, |
| 118 |
{'name': 'tcp_closewait', |
| 119 |
'call_back': TCP_Connections, |
| 120 |
'time_max': 20, |
| 121 |
'value_type': 'uint', |
| 122 |
'units': 'Sockets', |
| 123 |
'slope': 'both', |
| 124 |
'format': '%u', |
| 125 |
'description': 'Total number of close_wait TCP connections', |
| 126 |
'groups': 'network', |
| 127 |
}, |
| 128 |
{'name': 'tcp_synsent', |
| 129 |
'call_back': TCP_Connections, |
| 130 |
'time_max': 20, |
| 131 |
'value_type': 'uint', |
| 132 |
'units': 'Sockets', |
| 133 |
'slope': 'both', |
| 134 |
'format': '%u', |
| 135 |
'description': 'Total number of syn_sent TCP connections', |
| 136 |
'groups': 'network', |
| 137 |
}, |
| 138 |
{'name': 'tcp_synrecv', |
| 139 |
'call_back': TCP_Connections, |
| 140 |
'time_max': 20, |
| 141 |
'value_type': 'uint', |
| 142 |
'units': 'Sockets', |
| 143 |
'slope': 'both', |
| 144 |
'format': '%u', |
| 145 |
'description': 'Total number of syn_recv TCP connections', |
| 146 |
'groups': 'network', |
| 147 |
}, |
| 148 |
{'name': 'tcp_synwait', |
| 149 |
'call_back': TCP_Connections, |
| 150 |
'time_max': 20, |
| 151 |
'value_type': 'uint', |
| 152 |
'units': 'Sockets', |
| 153 |
'slope': 'both', |
| 154 |
'format': '%u', |
| 155 |
'description': 'Total number of syn_wait TCP connections', |
| 156 |
'groups': 'network', |
| 157 |
}, |
| 158 |
{'name': 'tcp_finwait1', |
| 159 |
'call_back': TCP_Connections, |
| 160 |
'time_max': 20, |
| 161 |
'value_type': 'uint', |
| 162 |
'units': 'Sockets', |
| 163 |
'slope': 'both', |
| 164 |
'format': '%u', |
| 165 |
'description': 'Total number of fin_wait1 TCP connections', |
| 166 |
'groups': 'network', |
| 167 |
}, |
| 168 |
{'name': 'tcp_finwait2', |
| 169 |
'call_back': TCP_Connections, |
| 170 |
'time_max': 20, |
| 171 |
'value_type': 'uint', |
| 172 |
'units': 'Sockets', |
| 173 |
'slope': 'both', |
| 174 |
'format': '%u', |
| 175 |
'description': 'Total number of fin_wait2 TCP connections', |
| 176 |
'groups': 'network', |
| 177 |
}, |
| 178 |
{'name': 'tcp_closed', |
| 179 |
'call_back': TCP_Connections, |
| 180 |
'time_max': 20, |
| 181 |
'value_type': 'uint', |
| 182 |
'units': 'Sockets', |
| 183 |
'slope': 'both', |
| 184 |
'format': '%u', |
| 185 |
'description': 'Total number of closed TCP connections', |
| 186 |
'groups': 'network', |
| 187 |
}, |
| 188 |
{'name': 'tcp_lastack', |
| 189 |
'call_back': TCP_Connections, |
| 190 |
'time_max': 20, |
| 191 |
'value_type': 'uint', |
| 192 |
'units': 'Sockets', |
| 193 |
'slope': 'both', |
| 194 |
'format': '%u', |
| 195 |
'description': 'Total number of last_ack TCP connections', |
| 196 |
'groups': 'network', |
| 197 |
}, |
| 198 |
{'name': 'tcp_closing', |
| 199 |
'call_back': TCP_Connections, |
| 200 |
'time_max': 20, |
| 201 |
'value_type': 'uint', |
| 202 |
'units': 'Sockets', |
| 203 |
'slope': 'both', |
| 204 |
'format': '%u', |
| 205 |
'description': 'Total number of closing TCP connections', |
| 206 |
'groups': 'network', |
| 207 |
}, |
| 208 |
{'name': 'tcp_unknown', |
| 209 |
'call_back': TCP_Connections, |
| 210 |
'time_max': 20, |
| 211 |
'value_type': 'uint', |
| 212 |
'units': 'Sockets', |
| 213 |
'slope': 'both', |
| 214 |
'format': '%u', |
| 215 |
'description': 'Total number of unknown TCP connections', |
| 216 |
'groups': 'network', |
| 217 |
}] |
| 218 |
|
| 219 |
|
| 220 |
class NetstatThread(threading.Thread): |
| 221 |
'''This thread continually gathers the current states of the tcp socket |
| 222 |
connections on the machine. The refresh rate is controlled by the |
| 223 |
RefreshRate parameter that is passed in through the gmond.conf file.''' |
| 224 |
|
| 225 |
def __init__(self): |
| 226 |
threading.Thread.__init__(self) |
| 227 |
self.running = False |
| 228 |
self.shuttingdown = False |
| 229 |
self.popenChild = None |
| 230 |
|
| 231 |
def shutdown(self): |
| 232 |
self.shuttingdown = True |
| 233 |
if self.popenChild != None: |
| 234 |
try: |
| 235 |
self.popenChild.wait() |
| 236 |
except OSError, e: |
| 237 |
if e.errno == 10: # No child processes |
| 238 |
pass |
| 239 |
|
| 240 |
if not self.running: |
| 241 |
return |
| 242 |
self.join() |
| 243 |
|
| 244 |
def run(self): |
| 245 |
global _conns, _refresh_rate |
| 246 |
|
| 247 |
#Array positions for the connection type and state data |
| 248 |
# acquired from the netstat output. |
| 249 |
tcp_at = 0 |
| 250 |
tcp_state_at = 5 |
| 251 |
|
| 252 |
#Make a temporary copy of the tcp connecton dictionary. |
| 253 |
tempconns = _conns.copy() |
| 254 |
|
| 255 |
#Set the state of the running thread |
| 256 |
self.running = True |
| 257 |
|
| 258 |
#Continue running until a shutdown event is indicated |
| 259 |
while not self.shuttingdown: |
| 260 |
if self.shuttingdown: |
| 261 |
break |
| 262 |
|
| 263 |
#Zero out the temporary connection state dictionary. |
| 264 |
for conn in tempconns: |
| 265 |
tempconns[conn] = 0 |
| 266 |
|
| 267 |
#Call the netstat utility and split the output into separate lines |
| 268 |
if not OBSOLETE_POPEN: |
| 269 |
self.popenChild = subprocess.Popen(["netstat", '-t', '-a', '-n'], stdout=subprocess.PIPE) |
| 270 |
lines = self.popenChild.communicate()[0].split('\n') |
| 271 |
else: |
| 272 |
self.popenChild = popen2.Popen3("netstat -t -a -n") |
| 273 |
lines = self.popenChild.fromchild.readlines() |
| 274 |
|
| 275 |
try: |
| 276 |
self.popenChild.wait() |
| 277 |
except OSError, e: |
| 278 |
if e.errno == 10: # No child process |
| 279 |
continue |
| 280 |
|
| 281 |
#Iterate through the netstat output looking for the 'tcp' keyword in the tcp_at |
| 282 |
# position and the state information in the tcp_state_at position. Count each |
| 283 |
# occurance of each state. |
| 284 |
for tcp in lines: |
| 285 |
# skip empty lines |
| 286 |
if tcp == '': |
| 287 |
continue |
| 288 |
|
| 289 |
line = tcp.split() |
| 290 |
if line[tcp_at] == 'tcp': |
| 291 |
if line[tcp_state_at] == 'ESTABLISHED': |
| 292 |
tempconns['tcp_established'] += 1 |
| 293 |
elif line[tcp_state_at] == 'LISTEN': |
| 294 |
tempconns['tcp_listen'] += 1 |
| 295 |
elif line[tcp_state_at] == 'TIME_WAIT': |
| 296 |
tempconns['tcp_timewait'] += 1 |
| 297 |
elif line[tcp_state_at] == 'CLOSE_WAIT': |
| 298 |
tempconns['tcp_closewait'] += 1 |
| 299 |
elif line[tcp_state_at] == 'SYN_SENT': |
| 300 |
tempconns['tcp_synsent'] += 1 |
| 301 |
elif line[tcp_state_at] == 'SYN_RECV': |
| 302 |
tempconns['tcp_synrecv'] += 1 |
| 303 |
elif line[tcp_state_at] == 'SYN_WAIT': |
| 304 |
tempconns['tcp_synwait'] += 1 |
| 305 |
elif line[tcp_state_at] == 'FIN_WAIT1': |
| 306 |
tempconns['tcp_finwait1'] += 1 |
| 307 |
elif line[tcp_state_at] == 'FIN_WAIT2': |
| 308 |
tempconns['tcp_finwait2'] += 1 |
| 309 |
elif line[tcp_state_at] == 'CLOSED': |
| 310 |
tempconns['tcp_closed'] += 1 |
| 311 |
elif line[tcp_state_at] == 'LAST_ACK': |
| 312 |
tempconns['tcp_lastack'] += 1 |
| 313 |
elif line[tcp_state_at] == 'CLOSING': |
| 314 |
tempconns['tcp_closing'] += 1 |
| 315 |
elif line[tcp_state_at] == 'UNKNOWN': |
| 316 |
tempconns['tcp_unknown'] += 1 |
| 317 |
|
| 318 |
#Acquire a lock and copy the temporary connection state dictionary |
| 319 |
# to the global state dictionary. |
| 320 |
_glock.acquire() |
| 321 |
for conn in _conns: |
| 322 |
_conns[conn] = tempconns[conn] |
| 323 |
_glock.release() |
| 324 |
|
| 325 |
#Wait for the refresh_rate period before collecting the netstat data again. |
| 326 |
if not self.shuttingdown: |
| 327 |
time.sleep(_refresh_rate) |
| 328 |
|
| 329 |
#Set the current state of the thread after a shutdown has been indicated. |
| 330 |
self.running = False |
| 331 |
|
| 332 |
|
| 333 |
def metric_init(params): |
| 334 |
'''Initialize the tcp connection status module and create the |
| 335 |
metric definition dictionary object for each metric.''' |
| 336 |
global _refresh_rate, _WorkerThread |
| 337 |
|
| 338 |
#Read the refresh_rate from the gmond.conf parameters. |
| 339 |
if 'RefreshRate' in params: |
| 340 |
_refresh_rate = int(params['RefreshRate']) |
| 341 |
|
| 342 |
#Start the worker thread |
| 343 |
_WorkerThread = NetstatThread() |
| 344 |
|
| 345 |
#Return the metric descriptions to Gmond |
| 346 |
return _descriptors |
| 347 |
|
| 348 |
|
| 349 |
def metric_cleanup(): |
| 350 |
'''Clean up the metric module.''' |
| 351 |
|
| 352 |
#Tell the worker thread to shutdown |
| 353 |
_WorkerThread.shutdown() |
| 354 |
|
| 355 |
|
| 356 |
#This code is for debugging and unit testing |
| 357 |
if __name__ == '__main__': |
| 358 |
params = {'Refresh': '20'} |
| 359 |
metric_init(params) |
| 360 |
while True: |
| 361 |
try: |
| 362 |
for d in _descriptors: |
| 363 |
v = d['call_back'](d['name']) |
| 364 |
print 'value for %s is %u' % (d['name'], v) |
| 365 |
time.sleep(5) |
| 366 |
except KeyboardInterrupt: |
| 367 |
os._exit(1) |