|
| 1 | +### This script reports jmx ehcache metrics to ganglia. |
| 2 | +### |
| 3 | +### Notes: |
| 4 | +### This script exposes ehcache MBeans to Ganglia. The following |
| 5 | +### are exposed: |
| 6 | +### - CacheHitCount |
| 7 | +### - CacheMissCount |
| 8 | +### |
| 9 | +### Changelog: |
| 10 | +### v1.0.1 - 2010-07-30 |
| 11 | +### * Initial version taken from jmxsh.py v1.0.5 |
| 12 | + |
| 13 | +### Copyright Jamie Isaacs. 2010 |
| 14 | +### License to use, modify, and distribute under the GPL |
| 15 | +### http://www.gnu.org/licenses/gpl.txt |
| 16 | + |
| 17 | +import time |
| 18 | +import subprocess |
| 19 | +import traceback, sys, re |
| 20 | +import tempfile |
| 21 | +import logging |
| 22 | + |
| 23 | +logging.basicConfig(level=logging.ERROR, format="%(asctime)s - %(name)s - %(levelname)s\t Thread-%(thread)d - %(message)s", filename='/tmp/gmond.log', filemode='w') |
| 24 | +#logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s\t Thread-%(thread)d - %(message)s", filename='/tmp/gmond.log2') |
| 25 | +logging.debug('starting up') |
| 26 | + |
| 27 | +last_update = 0 |
| 28 | +stats = {} |
| 29 | +last_val = {} |
| 30 | + |
| 31 | +METRICS = {} |
| 32 | +COMP = {} |
| 33 | +HOST = 'localhost' |
| 34 | +PORT = '8887' |
| 35 | +NAME = PORT |
| 36 | + |
| 37 | +MAX_UPDATE_TIME = 15 |
| 38 | +JMXSH = '/usr/share/java/jmxsh.jar' |
| 39 | + |
| 40 | +def update_stats(): |
| 41 | + logging.debug('updating stats') |
| 42 | + global last_update, stats, last_val |
| 43 | + |
| 44 | + cur_time = time.time() |
| 45 | + |
| 46 | + if cur_time - last_update < MAX_UPDATE_TIME: |
| 47 | + logging.debug(' wait ' + str(int(MAX_UPDATE_TIME - (cur_time - last_update))) + ' seconds') |
| 48 | + return True |
| 49 | + |
| 50 | + ##### |
| 51 | + # Build jmxsh script into tmpfile |
| 52 | + sh = '# jmxsh\njmx_connect -h ' + HOST + ' -p ' + PORT + '\n' |
| 53 | + sh += 'set obj [lindex [split [jmx_list net.sf.ehcache.hibernate] =] 2]\n' |
| 54 | + _mbean = 'net.sf.ehcache:type=SampledCache,SampledCacheManager=${obj},name=' |
| 55 | + for name,mbean_name in METRICS.items(): |
| 56 | + sh += 'puts "' + name + '_hit_count: [jmx_get -m ' + _mbean + mbean_name + ' CacheHitCount]"\n' |
| 57 | + sh += 'puts "' + name + '_miss_count: [jmx_get -m ' + _mbean + mbean_name + ' CacheMissCount]"\n' |
| 58 | + |
| 59 | + #logging.debug(sh) |
| 60 | + |
| 61 | + try: |
| 62 | + # run jmxsh.jar with the temp file as a script |
| 63 | + cmd = "java -jar " + JMXSH + " -q" |
| 64 | + p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 65 | + out, err = p.communicate(sh) |
| 66 | + #logging.debug('cmd: ' + cmd + '\nout:\n' + out + '\nerr: ' + err + '\ncode: ' + str(p.returncode)) |
| 67 | + |
| 68 | + if p.returncode: |
| 69 | + logging.warning('failed executing jmxsh\n' + cmd + '\n' + err) |
| 70 | + return False |
| 71 | + except: |
| 72 | + logging.warning('Error running jmx java\n' + traceback.print_exc(file=sys.stdout)) |
| 73 | + return False |
| 74 | + |
| 75 | + # Calculate diff for each metric |
| 76 | + try: |
| 77 | + # now parse out the values |
| 78 | + for line in out.strip().split('\n'): |
| 79 | + params = line.split(': ') |
| 80 | + name = params[0] |
| 81 | + val = params[1] |
| 82 | + |
| 83 | + val = int(val) |
| 84 | + if name in last_val: |
| 85 | + if val > last_val[name]: |
| 86 | + stats[name] = val - last_val[name] |
| 87 | + else: |
| 88 | + # value was reset since last update |
| 89 | + stats[name] = 0 |
| 90 | + else: |
| 91 | + stats[name] = 0 |
| 92 | + |
| 93 | + last_val[name] = val |
| 94 | + |
| 95 | + except: |
| 96 | + logging.warning('Error parsing\n' + traceback.print_exc(file=sys.stdout)) |
| 97 | + return False |
| 98 | + |
| 99 | + logging.debug('success refreshing stats') |
| 100 | + logging.debug('stats: ' + str(stats)) |
| 101 | + logging.debug('last_val: ' + str(last_val)) |
| 102 | + |
| 103 | + last_update = cur_time |
| 104 | + return True |
| 105 | + |
| 106 | +def get_stat(name): |
| 107 | + logging.debug('getting stat: ' + name) |
| 108 | + |
| 109 | + ret = update_stats() |
| 110 | + |
| 111 | + if ret: |
| 112 | + first = 'jmx_' + NAME + '_' |
| 113 | + if name.startswith(first): |
| 114 | + label = name[len(first):] |
| 115 | + else: |
| 116 | + label = name |
| 117 | + |
| 118 | + try: |
| 119 | + return stats[label] |
| 120 | + except: |
| 121 | + logging.warning('failed to fetch ' + name) |
| 122 | + return 0 |
| 123 | + else: |
| 124 | + return 0 |
| 125 | + |
| 126 | +def metric_init(params): |
| 127 | + global descriptors |
| 128 | + global METRICS,HOST,PORT,NAME |
| 129 | + |
| 130 | + logging.debug('init: ' + str(params)) |
| 131 | + |
| 132 | + try: |
| 133 | + HOST = params.pop('host') |
| 134 | + PORT = params.pop('port') |
| 135 | + NAME = params.pop('name') |
| 136 | + |
| 137 | + except: |
| 138 | + logging.warning('Incorrect parameters') |
| 139 | + |
| 140 | + METRICS = params |
| 141 | + |
| 142 | + update_stats() |
| 143 | + |
| 144 | + # dynamically build our descriptors based on the first run of update_stats() |
| 145 | + descriptions = dict() |
| 146 | + for name in stats: |
| 147 | + descriptions[name] = {} |
| 148 | + |
| 149 | + time_max = 60 |
| 150 | + descriptors = [] |
| 151 | + for label in descriptions: |
| 152 | + if stats.has_key(label): |
| 153 | + |
| 154 | + d = { |
| 155 | + 'name': 'jmx_' + NAME + '_' + label, |
| 156 | + 'call_back': get_stat, |
| 157 | + 'time_max': time_max, |
| 158 | + 'value_type': 'uint', |
| 159 | + 'units': '', |
| 160 | + 'format': '%u', |
| 161 | + 'slope': 'both', |
| 162 | + 'description': label, |
| 163 | + 'groups': 'jmx' |
| 164 | + } |
| 165 | + |
| 166 | + # Apply metric customizations from descriptions |
| 167 | + d.update(descriptions[label]) |
| 168 | + |
| 169 | + descriptors.append(d) |
| 170 | + |
| 171 | + else: |
| 172 | + logging.error("skipped " + label) |
| 173 | + |
| 174 | + #logging.debug('descriptors: ' + str(descriptors)) |
| 175 | + |
| 176 | + return descriptors |
| 177 | + |
| 178 | +def metric_cleanup(): |
| 179 | + logging.shutdown() |
| 180 | + # pass |
| 181 | + |
| 182 | +if __name__ == '__main__': |
| 183 | + from optparse import OptionParser |
| 184 | + import os |
| 185 | + |
| 186 | + logging.debug('running from cmd line') |
| 187 | + parser = OptionParser() |
| 188 | + parser.add_option('-p', '--param', dest='param', default='', help='module parameters') |
| 189 | + parser.add_option('-v', '--value', dest='value', default='', help='module values') |
| 190 | + parser.add_option('-b', '--gmetric-bin', dest='gmetric_bin', default='/usr/bin/gmetric', help='path to gmetric binary') |
| 191 | + parser.add_option('-c', '--gmond-conf', dest='gmond_conf', default='/etc/ganglia/gmond.conf', help='path to gmond.conf') |
| 192 | + parser.add_option('-g', '--gmetric', dest='gmetric', action='store_true', default=False, help='submit via gmetric') |
| 193 | + parser.add_option('-q', '--quiet', dest='quiet', action='store_true', default=False) |
| 194 | + parser.add_option('-t', '--test', dest='test', action='store_true', default=False, help='test the regex list') |
| 195 | + |
| 196 | + (options, args) = parser.parse_args() |
| 197 | + |
| 198 | + _param = options.param.split(',') |
| 199 | + _val = options.value.split('|') |
| 200 | + |
| 201 | + params = {} |
| 202 | + i = 0 |
| 203 | + for name in _param: |
| 204 | + params[name] = _val[i] |
| 205 | + i += 1 |
| 206 | + |
| 207 | + metric_init(params) |
| 208 | + |
| 209 | + if options.test: |
| 210 | + print('') |
| 211 | + print(' waiting ' + str(MAX_UPDATE_TIME) + ' seconds') |
| 212 | + time.sleep(MAX_UPDATE_TIME) |
| 213 | + update_stats() |
| 214 | + |
| 215 | + for d in descriptors: |
| 216 | + v = d['call_back'](d['name']) |
| 217 | + if not options.quiet: |
| 218 | + print ' %s: %s %s [%s]' % (d['name'], d['format'] % v, d['units'], d['description']) |
| 219 | + |
| 220 | + if options.gmetric: |
| 221 | + cmd = "%s --conf=%s --value='%s' --units='%s' --type='%s' --name='%s' --slope='%s'" % \ |
| 222 | + (options.gmetric_bin, option.gmond_conf, v, d['units'], d['value_type'], d['name'], d['slope']) |
| 223 | + os.system(cmd) |
| 224 | + |
0 commit comments