| 1 |
# This module allows you to collect network stats. These values that are collected from |
| 2 |
# |
| 3 |
# /proc/net/netstat |
| 4 |
|
| 5 |
import sys |
| 6 |
import re |
| 7 |
import time |
| 8 |
import copy |
| 9 |
import string |
| 10 |
|
| 11 |
PARAMS = {} |
| 12 |
|
| 13 |
METRICS = { |
| 14 |
'time': 0, |
| 15 |
'data': {} |
| 16 |
} |
| 17 |
|
| 18 |
stats_files = ["/proc/net/netstat", "/proc/net/snmp"] |
| 19 |
|
| 20 |
LAST_METRICS = copy.deepcopy(METRICS) |
| 21 |
METRICS_CACHE_MAX = 5 |
| 22 |
|
| 23 |
stats_pos = {} |
| 24 |
|
| 25 |
|
| 26 |
def get_metrics(): |
| 27 |
"""Return all metrics""" |
| 28 |
|
| 29 |
global METRICS, LAST_METRICS |
| 30 |
|
| 31 |
if (time.time() - METRICS['time']) > METRICS_CACHE_MAX: |
| 32 |
|
| 33 |
new_metrics = {} |
| 34 |
|
| 35 |
for file in stats_files: |
| 36 |
try: |
| 37 |
file = open(file, 'r') |
| 38 |
|
| 39 |
except IOError: |
| 40 |
return 0 |
| 41 |
|
| 42 |
# convert to dict |
| 43 |
metrics = {} |
| 44 |
for line in file: |
| 45 |
if re.match("(.*): [0-9]", line): |
| 46 |
count = 0 |
| 47 |
metrics = re.split("\s+", line) |
| 48 |
metric_group = metrics[0].replace(":", "").lower() |
| 49 |
new_metrics[metric_group] = dict() |
| 50 |
for value in metrics: |
| 51 |
# Skip first |
| 52 |
if count > 0 and value >= 0 and count in stats_pos[metric_group]: |
| 53 |
metric_name = stats_pos[metric_group][count] |
| 54 |
new_metrics[metric_group][metric_name] = value |
| 55 |
count += 1 |
| 56 |
|
| 57 |
file.close() |
| 58 |
|
| 59 |
# update cache |
| 60 |
LAST_METRICS = copy.deepcopy(METRICS) |
| 61 |
METRICS = { |
| 62 |
'time': time.time(), |
| 63 |
'data': new_metrics |
| 64 |
} |
| 65 |
|
| 66 |
return [METRICS, LAST_METRICS] |
| 67 |
|
| 68 |
|
| 69 |
def get_value(name): |
| 70 |
"""Return a value for the requested metric""" |
| 71 |
|
| 72 |
metrics = get_metrics()[0] |
| 73 |
|
| 74 |
name = name[len(NAME_PREFIX):] # remove prefix from name |
| 75 |
|
| 76 |
try: |
| 77 |
result = metrics['data'][name] |
| 78 |
except StandardError: |
| 79 |
result = 0 |
| 80 |
|
| 81 |
return result |
| 82 |
|
| 83 |
|
| 84 |
def get_delta(name): |
| 85 |
"""Return change over time for the requested metric""" |
| 86 |
|
| 87 |
# get metrics |
| 88 |
[curr_metrics, last_metrics] = get_metrics() |
| 89 |
|
| 90 |
parts = name.split("_") |
| 91 |
group = parts[0] |
| 92 |
metric = "_".join(parts[1:]) |
| 93 |
|
| 94 |
try: |
| 95 |
delta = (float(curr_metrics['data'][group][metric]) - float(last_metrics['data'][group][metric])) / (curr_metrics['time'] - last_metrics['time']) |
| 96 |
if delta < 0: |
| 97 |
print name + " is less 0" |
| 98 |
delta = 0 |
| 99 |
except KeyError: |
| 100 |
delta = 0.0 |
| 101 |
|
| 102 |
return delta |
| 103 |
|
| 104 |
|
| 105 |
def get_tcploss_percentage(name): |
| 106 |
|
| 107 |
# get metrics |
| 108 |
[curr_metrics, last_metrics] = get_metrics() |
| 109 |
|
| 110 |
try: |
| 111 |
pct = 100 * (float(curr_metrics['data']['tcpext']["tcploss"]) - float(last_metrics["data"]['tcpext']["tcploss"])) / (float(curr_metrics['data']['tcp']['outsegs']) + float(curr_metrics['data']['tcp']['insegs']) - float(last_metrics['data']['tcp']['insegs']) - float(last_metrics['data']['tcp']['outsegs'])) |
| 112 |
if pct < 0: |
| 113 |
print name + " is less 0" |
| 114 |
pct = 0 |
| 115 |
except KeyError: |
| 116 |
pct = 0.0 |
| 117 |
except ZeroDivisionError: |
| 118 |
pct = 0.0 |
| 119 |
|
| 120 |
return pct |
| 121 |
|
| 122 |
|
| 123 |
def get_tcpattemptfail_percentage(name): |
| 124 |
|
| 125 |
# get metrics |
| 126 |
[curr_metrics, last_metrics] = get_metrics() |
| 127 |
|
| 128 |
try: |
| 129 |
pct = 100 * (float(curr_metrics['data']['tcp']["attemptfails"]) - float(last_metrics["data"]['tcp']["attemptfails"])) / (float(curr_metrics['data']['tcp']['outsegs']) + float(curr_metrics['data']['tcp']['insegs']) - float(last_metrics['data']['tcp']['insegs']) - float(last_metrics['data']['tcp']['outsegs'])) |
| 130 |
if pct < 0: |
| 131 |
print name + " is less 0" |
| 132 |
pct = 0 |
| 133 |
except Exception: |
| 134 |
pct = 0.0 |
| 135 |
|
| 136 |
return pct |
| 137 |
|
| 138 |
|
| 139 |
def get_retrans_percentage(name): |
| 140 |
|
| 141 |
# get metrics |
| 142 |
[curr_metrics, last_metrics] = get_metrics() |
| 143 |
|
| 144 |
try: |
| 145 |
pct = 100 * (float(curr_metrics['data']['tcp']["retranssegs"]) - float(last_metrics['data']['tcp']["retranssegs"])) / (float(curr_metrics['data']['tcp']['outsegs']) + float(curr_metrics['data']['tcp']['insegs']) - float(last_metrics['data']['tcp']['insegs']) - float(last_metrics['data']['tcp']['outsegs'])) |
| 146 |
if pct < 0: |
| 147 |
print name + " is less 0" |
| 148 |
pct = 0 |
| 149 |
except KeyError: |
| 150 |
pct = 0.0 |
| 151 |
except ZeroDivisionError: |
| 152 |
pct = 0.0 |
| 153 |
|
| 154 |
return pct |
| 155 |
|
| 156 |
|
| 157 |
def create_desc(skel, prop): |
| 158 |
d = skel.copy() |
| 159 |
for k, v in prop.iteritems(): |
| 160 |
d[k] = v |
| 161 |
return d |
| 162 |
|
| 163 |
|
| 164 |
def metric_init(params): |
| 165 |
global descriptors, metric_map, Desc_Skel |
| 166 |
|
| 167 |
descriptors = [] |
| 168 |
|
| 169 |
Desc_Skel = { |
| 170 |
'name' : 'XXX', |
| 171 |
'call_back' : get_delta, |
| 172 |
'time_max' : 60, |
| 173 |
'value_type' : 'float', |
| 174 |
'format' : '%.5f', |
| 175 |
'units' : 'count/s', |
| 176 |
'slope' : 'both', # zero|positive|negative|both |
| 177 |
'description' : 'XXX', |
| 178 |
'groups' : 'XXX', |
| 179 |
} |
| 180 |
|
| 181 |
#################################################################################### |
| 182 |
# Let's figure out what metrics are available |
| 183 |
# |
| 184 |
# Read /proc/net/netstat |
| 185 |
#################################################################################### |
| 186 |
for file in stats_files: |
| 187 |
try: |
| 188 |
file = open(file, 'r') |
| 189 |
|
| 190 |
except IOError: |
| 191 |
return 0 |
| 192 |
|
| 193 |
# Find mapping |
| 194 |
for line in file: |
| 195 |
# Lines with |
| 196 |
if not re.match("(.*): [0-9]", line): |
| 197 |
count = 0 |
| 198 |
mapping = re.split("\s+", line) |
| 199 |
metric_group = mapping[0].replace(":", "").lower() |
| 200 |
stats_pos[metric_group] = dict() |
| 201 |
for metric in mapping: |
| 202 |
# Skip first |
| 203 |
if count > 0 and metric != "": |
| 204 |
lowercase_metric = metric.lower() |
| 205 |
stats_pos[metric_group][count] = lowercase_metric |
| 206 |
count += 1 |
| 207 |
|
| 208 |
file.close() |
| 209 |
|
| 210 |
for group in stats_pos: |
| 211 |
for item in stats_pos[group]: |
| 212 |
descriptors.append(create_desc(Desc_Skel, { |
| 213 |
"name" : group + "_" + stats_pos[group][item], |
| 214 |
"description": stats_pos[group][item], |
| 215 |
'groups' : group |
| 216 |
})) |
| 217 |
|
| 218 |
descriptors.append(create_desc(Desc_Skel, { |
| 219 |
"name" : "tcpext_tcploss_percentage", |
| 220 |
"call_back" : get_tcploss_percentage, |
| 221 |
"description": "TCP percentage loss, tcploss / insegs + outsegs", |
| 222 |
"units" : "pct", |
| 223 |
'groups' : 'tcpext' |
| 224 |
})) |
| 225 |
|
| 226 |
descriptors.append(create_desc(Desc_Skel, { |
| 227 |
"name" : "tcp_attemptfails_percentage", |
| 228 |
"call_back" : get_tcpattemptfail_percentage, |
| 229 |
"description": "TCP attemptfail percentage, tcpattemptfail / insegs + outsegs", |
| 230 |
"units" : "pct", |
| 231 |
'groups' : 'tcp' |
| 232 |
})) |
| 233 |
|
| 234 |
descriptors.append(create_desc(Desc_Skel, { |
| 235 |
"name" : "tcp_retrans_percentage", |
| 236 |
"call_back" : get_retrans_percentage, |
| 237 |
"description": "TCP retrans percentage, retranssegs / insegs + outsegs", |
| 238 |
"units" : "pct", |
| 239 |
'groups' : 'tcp' |
| 240 |
})) |
| 241 |
|
| 242 |
return descriptors |
| 243 |
|
| 244 |
|
| 245 |
def metric_cleanup(): |
| 246 |
'''Clean up the metric module.''' |
| 247 |
pass |
| 248 |
|
| 249 |
|
| 250 |
#This code is for debugging and unit testing |
| 251 |
if __name__ == '__main__': |
| 252 |
descriptors = metric_init(PARAMS) |
| 253 |
while True: |
| 254 |
for d in descriptors: |
| 255 |
v = d['call_back'](d['name']) |
| 256 |
print '%s = %s' % (d['name'], v) |
| 257 |
print 'Sleeping 15 seconds' |
| 258 |
time.sleep(15) |