| 1 |
import sys |
| 2 |
import traceback |
| 3 |
import os |
| 4 |
import re |
| 5 |
|
| 6 |
|
| 7 |
############################################################################### |
| 8 |
# Explanation of metrics in /proc/meminfo can be found here |
| 9 |
# |
| 10 |
# http://www.redhat.com/advice/tips/meminfo.html |
| 11 |
# and |
| 12 |
# http://unixfoo.blogspot.com/2008/02/know-about-procmeminfo.html |
| 13 |
# and |
| 14 |
# http://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-proc-meminfo.html |
| 15 |
############################################################################### |
| 16 |
|
| 17 |
meminfo_file = "/proc/meminfo" |
| 18 |
|
| 19 |
|
| 20 |
def metrics_handler(name): |
| 21 |
try: |
| 22 |
file = open(meminfo_file, 'r') |
| 23 |
|
| 24 |
except IOError: |
| 25 |
return 0 |
| 26 |
|
| 27 |
value = 0 |
| 28 |
|
| 29 |
if name == 'mem_swap_used': |
| 30 |
return metrics_handler('mem_swap_total') - metrics_handler('mem_swap_free') |
| 31 |
for line in file: |
| 32 |
parts = re.split("\s+", line) |
| 33 |
if parts[0] == metric_map[name]['name'] + ":": |
| 34 |
# All of the measurements are in kBytes. We want to change them over |
| 35 |
# to Bytes |
| 36 |
if metric_map[name]['units'] == "Bytes": |
| 37 |
value = float(parts[1]) * 1024 |
| 38 |
else: |
| 39 |
value = parts[1] |
| 40 |
|
| 41 |
return float(value) |
| 42 |
|
| 43 |
|
| 44 |
def create_desc(skel, prop): |
| 45 |
d = skel.copy() |
| 46 |
for k, v in prop.iteritems(): |
| 47 |
d[k] = v |
| 48 |
return d |
| 49 |
|
| 50 |
|
| 51 |
def metric_init(params): |
| 52 |
global descriptors, metric_map, Desc_Skel |
| 53 |
|
| 54 |
descriptors = [] |
| 55 |
|
| 56 |
Desc_Skel = { |
| 57 |
'name' : 'XXX', |
| 58 |
'orig_name' : 'XXX', |
| 59 |
'call_back' : metrics_handler, |
| 60 |
'time_max' : 60, |
| 61 |
'value_type' : 'float', |
| 62 |
'format' : '%.0f', |
| 63 |
'units' : 'XXX', |
| 64 |
'slope' : 'both', # zero|positive|negative|both |
| 65 |
'description' : 'XXX', |
| 66 |
'groups' : 'memory', |
| 67 |
} |
| 68 |
|
| 69 |
descriptors.append( create_desc(Desc_Skel, { |
| 70 |
"name" : "mem_total", |
| 71 |
"orig_name" : "MemTotal", |
| 72 |
"units" : "Bytes", |
| 73 |
"description": "Total usable ram", |
| 74 |
})) |
| 75 |
|
| 76 |
descriptors.append(create_desc(Desc_Skel, { |
| 77 |
"name" : "mem_free", |
| 78 |
"orig_name" : "MemFree", |
| 79 |
"units" : "Bytes", |
| 80 |
"description": "The amount of physical RAM left unused by the system. ", |
| 81 |
})) |
| 82 |
|
| 83 |
descriptors.append(create_desc(Desc_Skel, { |
| 84 |
"name" : "mem_buffers", |
| 85 |
"orig_name" : "Buffers", |
| 86 |
"units" : "Bytes", |
| 87 |
"description": "Buffers used", |
| 88 |
})) |
| 89 |
|
| 90 |
descriptors.append(create_desc(Desc_Skel, { |
| 91 |
"name" : "mem_cached", |
| 92 |
"orig_name" : "Cached", |
| 93 |
"units" : "Bytes", |
| 94 |
"description": "Cached Memory", |
| 95 |
})) |
| 96 |
|
| 97 |
descriptors.append(create_desc(Desc_Skel, { |
| 98 |
"name" : "mem_swap_cached", |
| 99 |
"orig_name" : "SwapCached", |
| 100 |
"units" : "Bytes", |
| 101 |
"description": "Amount of Swap used as cache memory. Memory that once was swapped out, is swapped back in, but is still in the swapfile", |
| 102 |
})) |
| 103 |
|
| 104 |
descriptors.append(create_desc(Desc_Skel, { |
| 105 |
"name" : "mem_active", |
| 106 |
"orig_name" : "Active", |
| 107 |
"units" : "Bytes", |
| 108 |
"description": "Memory that has been used more recently and usually not reclaimed unless absolutely necessary.", |
| 109 |
})) |
| 110 |
|
| 111 |
descriptors.append(create_desc(Desc_Skel, { |
| 112 |
"name" : "mem_inactive", |
| 113 |
"orig_name" : "Inactive", |
| 114 |
"units" : "Bytes", |
| 115 |
"description": "The total amount of buffer or page cache memory that are free and available", |
| 116 |
})) |
| 117 |
|
| 118 |
descriptors.append(create_desc(Desc_Skel, { |
| 119 |
"name" : "mem_total_anon", |
| 120 |
"orig_name" : "Active(anon)", |
| 121 |
"units" : "Bytes", |
| 122 |
"description": "Active(anon)", |
| 123 |
})) |
| 124 |
|
| 125 |
descriptors.append(create_desc(Desc_Skel, { |
| 126 |
"name" : "mem_inactive_anon", |
| 127 |
"orig_name" : "Inactive(anon)", |
| 128 |
"units" : "Bytes", |
| 129 |
"description": "Inactive(anon)", |
| 130 |
})) |
| 131 |
|
| 132 |
descriptors.append(create_desc(Desc_Skel, { |
| 133 |
"name" : "mem_active_file", |
| 134 |
"orig_name" : "Active(file)", |
| 135 |
"units" : "Bytes", |
| 136 |
"description": "Active(file)", |
| 137 |
})) |
| 138 |
|
| 139 |
descriptors.append(create_desc(Desc_Skel, { |
| 140 |
"name" : "mem_inactive_file", |
| 141 |
"orig_name" : "Inactive(file)", |
| 142 |
"units" : "Bytes", |
| 143 |
"description": "Inactive(file)", |
| 144 |
})) |
| 145 |
|
| 146 |
descriptors.append(create_desc(Desc_Skel, { |
| 147 |
"name" : "mem_unevictable", |
| 148 |
"orig_name" : "Unevictable", |
| 149 |
"units" : "Bytes", |
| 150 |
"description": "Unevictable", |
| 151 |
})) |
| 152 |
|
| 153 |
descriptors.append(create_desc(Desc_Skel, { |
| 154 |
"name" : "mem_mlocked", |
| 155 |
"orig_name" : "Mlocked", |
| 156 |
"units" : "Bytes", |
| 157 |
"description": "Mlocked", |
| 158 |
})) |
| 159 |
|
| 160 |
descriptors.append(create_desc(Desc_Skel, { |
| 161 |
"name" : "mem_swap_total", |
| 162 |
"orig_name" : "SwapTotal", |
| 163 |
"units" : "Bytes", |
| 164 |
"description": "Total amount of physical swap memory", |
| 165 |
})) |
| 166 |
|
| 167 |
descriptors.append(create_desc(Desc_Skel, { |
| 168 |
"name" : "mem_swap_free", |
| 169 |
"orig_name" : "SwapFree", |
| 170 |
"units" : "Bytes", |
| 171 |
"description": "Total amount of swap memory free", |
| 172 |
})) |
| 173 |
|
| 174 |
# /proc/meminfo does not provide a 'SwapUsed', instead relying on |
| 175 |
# the user to do the simple subtraction. This isn't hard, but |
| 176 |
# monitoring & graphing tools that use ganglia metrics often do |
| 177 |
# not have a mechanism to do compound operations. |
| 178 |
descriptors.append(create_desc(Desc_Skel, { |
| 179 |
"name" : "mem_swap_used", |
| 180 |
"orig_name" : "NA", |
| 181 |
"units" : "Bytes", |
| 182 |
"description": "Calculated metric. SwapTotal - SwapFree", |
| 183 |
})) |
| 184 |
|
| 185 |
descriptors.append(create_desc(Desc_Skel, { |
| 186 |
"name" : "mem_dirty", |
| 187 |
"orig_name" : "Dirty", |
| 188 |
"units" : "Bytes", |
| 189 |
"description": "The total amount of memory waiting to be written back to the disk. ", |
| 190 |
})) |
| 191 |
|
| 192 |
descriptors.append(create_desc(Desc_Skel, { |
| 193 |
"name" : "mem_writeback", |
| 194 |
"orig_name" : "Writeback", |
| 195 |
"units" : "Bytes", |
| 196 |
"description": "The total amount of memory actively being written back to the disk.", |
| 197 |
})) |
| 198 |
|
| 199 |
descriptors.append(create_desc(Desc_Skel, { |
| 200 |
"name" : "mem_anonpages", |
| 201 |
"orig_name" : "AnonPages", |
| 202 |
"units" : "Bytes", |
| 203 |
"description": "AnonPages", |
| 204 |
})) |
| 205 |
|
| 206 |
descriptors.append(create_desc(Desc_Skel, { |
| 207 |
"name" : "mem_mapped", |
| 208 |
"orig_name" : "Mapped", |
| 209 |
"units" : "Bytes", |
| 210 |
"description": "Mapped", |
| 211 |
})) |
| 212 |
|
| 213 |
descriptors.append(create_desc(Desc_Skel, { |
| 214 |
"name" : "mem_shmem", |
| 215 |
"orig_name" : "Shmem", |
| 216 |
"units" : "Bytes", |
| 217 |
"description": "Shmem", |
| 218 |
})) |
| 219 |
|
| 220 |
descriptors.append(create_desc(Desc_Skel, { |
| 221 |
"name" : "mem_slab", |
| 222 |
"orig_name" : "Slab", |
| 223 |
"units" : "Bytes", |
| 224 |
"description": "Slab", |
| 225 |
})) |
| 226 |
|
| 227 |
descriptors.append(create_desc(Desc_Skel, { |
| 228 |
"name" : "mem_s_reclaimable", |
| 229 |
"orig_name" : "SReclaimable", |
| 230 |
"units" : "Bytes", |
| 231 |
"description": "SReclaimable", |
| 232 |
})) |
| 233 |
|
| 234 |
descriptors.append(create_desc(Desc_Skel, { |
| 235 |
"name" : "mem_s_unreclaimable", |
| 236 |
"orig_name" : "SUnreclaim", |
| 237 |
"units" : "Bytes", |
| 238 |
"description": "SUnreclaim", |
| 239 |
})) |
| 240 |
|
| 241 |
descriptors.append(create_desc(Desc_Skel, { |
| 242 |
"name" : "mem_kernel_stack", |
| 243 |
"orig_name" : "KernelStack", |
| 244 |
"units" : "Bytes", |
| 245 |
"description": "KernelStack", |
| 246 |
})) |
| 247 |
|
| 248 |
descriptors.append(create_desc(Desc_Skel, { |
| 249 |
"name" : "mem_page_tables", |
| 250 |
"orig_name" : "PageTables", |
| 251 |
"units" : "Bytes", |
| 252 |
"description": "PageTables", |
| 253 |
})) |
| 254 |
|
| 255 |
descriptors.append(create_desc(Desc_Skel, { |
| 256 |
"name" : "mem_nfs_unstable", |
| 257 |
"orig_name" : "NFS_Unstable", |
| 258 |
"units" : "Bytes", |
| 259 |
"description": "NFS_Unstable", |
| 260 |
})) |
| 261 |
|
| 262 |
descriptors.append(create_desc(Desc_Skel, { |
| 263 |
"name" : "mem_bounce", |
| 264 |
"orig_name" : "Bounce", |
| 265 |
"units" : "Bytes", |
| 266 |
"description": "Bounce", |
| 267 |
})) |
| 268 |
|
| 269 |
descriptors.append(create_desc(Desc_Skel, { |
| 270 |
"name" : "mem_writeback_tmp", |
| 271 |
"orig_name" : "WritebackTmp", |
| 272 |
"units" : "Bytes", |
| 273 |
"description": "WritebackTmp", |
| 274 |
})) |
| 275 |
|
| 276 |
descriptors.append(create_desc(Desc_Skel, { |
| 277 |
"name" : "mem_commit_limit", |
| 278 |
"orig_name" : "CommitLimit", |
| 279 |
"units" : "Bytes", |
| 280 |
"description": "CommitLimit", |
| 281 |
})) |
| 282 |
|
| 283 |
descriptors.append(create_desc(Desc_Skel, { |
| 284 |
"name" : "mem_committed_as", |
| 285 |
"orig_name" : "Committed_AS", |
| 286 |
"units" : "Bytes", |
| 287 |
"description": "Committed_AS", |
| 288 |
})) |
| 289 |
|
| 290 |
descriptors.append(create_desc(Desc_Skel, { |
| 291 |
"name" : "mem_vmalloc_total", |
| 292 |
"orig_name" : "VmallocTotal", |
| 293 |
"units" : "Bytes", |
| 294 |
"description": "VmallocTotal", |
| 295 |
})) |
| 296 |
|
| 297 |
descriptors.append(create_desc(Desc_Skel, { |
| 298 |
"name" : "mem_vmalloc_used", |
| 299 |
"orig_name" : "VmallocUsed", |
| 300 |
"units" : "Bytes", |
| 301 |
"description": "VmallocUsed", |
| 302 |
})) |
| 303 |
|
| 304 |
descriptors.append(create_desc(Desc_Skel, { |
| 305 |
"name" : "mem_vmalloc_chunk", |
| 306 |
"orig_name" : "VmallocChunk", |
| 307 |
"units" : "Bytes", |
| 308 |
"description": "VmallocChunk", |
| 309 |
})) |
| 310 |
|
| 311 |
descriptors.append(create_desc(Desc_Skel, { |
| 312 |
"name" : "mem_hardware_corrupted", |
| 313 |
"orig_name" : "HardwareCorrupted", |
| 314 |
"units" : "Bytes", |
| 315 |
"description": "HardwareCorrupted", |
| 316 |
})) |
| 317 |
|
| 318 |
descriptors.append(create_desc(Desc_Skel, { |
| 319 |
"name" : "mem_hugepages_total", |
| 320 |
"orig_name" : "HugePages_Total", |
| 321 |
"units" : "pages", |
| 322 |
"description": "HugePages_Total", |
| 323 |
})) |
| 324 |
|
| 325 |
descriptors.append(create_desc(Desc_Skel, { |
| 326 |
"name" : "mem_hugepages_free", |
| 327 |
"orig_name" : "HugePages_Free", |
| 328 |
"units" : "pages", |
| 329 |
"description": "HugePages_Free", |
| 330 |
})) |
| 331 |
|
| 332 |
descriptors.append(create_desc(Desc_Skel, { |
| 333 |
"name" : "mem_hugepage_rsvd", |
| 334 |
"orig_name" : "HugePages_Rsvd", |
| 335 |
"units" : "pages", |
| 336 |
"description": "HugePages_Rsvd", |
| 337 |
})) |
| 338 |
|
| 339 |
descriptors.append(create_desc(Desc_Skel, { |
| 340 |
"name" : "mem_hugepages_surp", |
| 341 |
"orig_name" : "HugePages_Surp", |
| 342 |
"units" : "pages", |
| 343 |
"description": "HugePages_Surp", |
| 344 |
})) |
| 345 |
|
| 346 |
descriptors.append(create_desc(Desc_Skel, { |
| 347 |
"name" : "mem_hugepage_size", |
| 348 |
"orig_name" : "Hugepagesize", |
| 349 |
"units" : "Bytes", |
| 350 |
"description": "Hugepagesize", |
| 351 |
})) |
| 352 |
|
| 353 |
descriptors.append(create_desc(Desc_Skel, { |
| 354 |
"name" : "mem_directmap_4k", |
| 355 |
"orig_name" : "DirectMap4k", |
| 356 |
"units" : "Bytes", |
| 357 |
"description": "DirectMap4k", |
| 358 |
})) |
| 359 |
|
| 360 |
descriptors.append(create_desc(Desc_Skel, { |
| 361 |
"name" : "mem_directmap_2M", |
| 362 |
"orig_name" : "DirectMap2M", |
| 363 |
"units" : "Bytes", |
| 364 |
"description": "DirectMap2M", |
| 365 |
})) |
| 366 |
|
| 367 |
# We need a metric_map that maps metric_name to the index in /proc/meminfo |
| 368 |
metric_map = {} |
| 369 |
|
| 370 |
for d in descriptors: |
| 371 |
metric_name = d['name'] |
| 372 |
metric_map[metric_name] = {"name": d['orig_name'], "units": d['units']} |
| 373 |
|
| 374 |
return descriptors |
| 375 |
|
| 376 |
|
| 377 |
def metric_cleanup(): |
| 378 |
'''Clean up the metric module.''' |
| 379 |
pass |
| 380 |
|
| 381 |
|
| 382 |
# This code is for debugging and unit testing |
| 383 |
if __name__ == '__main__': |
| 384 |
metric_init({}) |
| 385 |
for d in descriptors: |
| 386 |
v = d['call_back'](d['name']) |
| 387 |
print 'value for %s is %f' % (d['name'], v) |