Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 89 lines (74 sloc) 2.534 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#
# Author: david birdsong 2010
# Project URL: http://github.com/davidbirdsong/pygmonlib
#
# License: same as Ganglia
#

import gmreader
import sys
import time

HTTPCodeHandler = None

class HttpCodes(object):
  def __init__(self, logfile, collect_interval, metric_group):
  
    self.descriptors = []
    descriptor = {
      'call_back': self.get_metric,
      'time_max': collect_interval,
      'value_type': 'uint',
      'units': 'ticks',
      'slope': 'both',
      'format': '%u',
      'groups': metric_group,
      'name' : 'http_500'
    }
    self.descriptors.append(descriptor)
    self.input = gmreader.GlogFileTailer(logfile)
    self.http_codes = {}

  def _parse_http_code(self, line):
    """
NOT A GOOD LINE PARSE EXAMPLE
parse out http code from this line
127.0.0.1 - - [24/Sep/2010:21:16:17 -0400] "GET /foo HTTP/1.1" 404 169 "-" "curl/7.20.0 (x86_64-redhat-linux-gnu) libcurl/7.20.0 NSS/3.12.6.2 zlib/1.2.3 c-ares/1.7.0 libidn/1.16 libssh2/1.2.4"

return http code and http code class, ie. (502, 'http_500')
"""
    code = int(line.split('"')[2].split()[0])
    return (code, 'http_%i' % (code - code % 100))

  def return_descriptors(self):
    return self.descriptors

  def get_metric(self, name):
    # input object will return lines written to fd since
    # last read and raise StopIteration once reading would block
    for line in self.input.readlines():
      for code in self._parse_http_code(line):
        self.http_codes.setdefault(code, 0)
        self.http_codes[code] += 1
    value = self.http_codes.get(name, 0)
    self.http_codes[name] = 0
    return value

def metric_init(params):
  collect_interval = params.get('collect_interval', 15)
  metric_group = params.get('metric_group', 'HTTP Codes')
  logfile = params.get('logfile')
  if logfile is None:
    print >> sys.stderr, 'Missing logfile param'
    return []

  global HTTPCodeHandler
  HTTPCodeHandler = HttpCodes(logfile, collect_interval, metric_group)
  return HTTPCodeHandler.return_descriptors()

  
def metric_cleanup():
  global HTTPCodeHandler
  del HTTPCodeHandler
  
if __name__ == '__main__':

  params = {}
  params['logfile'] = sys.argv[1]
  params['metric_group'] = 'X-Gmond-Module-HTTP500-TEST'
  params['collect_interval'] = collect_interval = 30
  descriptors = metric_init(params)
  while True:
    for d in descriptors:
      call_back = d['call_back']
      mname = d['name']
      v = call_back(mname)
      print '%s -> %i' % ( mname, v)
    time.sleep(collect_interval)
    print '\n'
Something went wrong with that request. Please try again.