0

I am trying to make some approximations discard.

The function given below is throwing attribute error. What might be the issue? Kindly help me if possible.

def calculate_tps(filename,identity):
  ##command='tshark -r %s -R -T pdml diameter'
  first_time = None
  last_time=None
  tshark='tshark -r %s -R \'(diameter.Destination-Host==%s)&& (diameter.CC-Request-Type==3)\' -T pdml'% (filename,identity)
  time = None
  req = 0
  for line in os.popen(tshark, 'r'):
    m=pattern.search(line)    
    if m:
      #print m.group(1)
      if m.group(1) == 'timestamp':
    time=m.group(2)
    if not first_time:
      first_time=time
      elif m.group(1) == 'diameter.Session-Id':
        req +=1 
    if(req % 100) ==0:
      print 'processed 1000 requests req number %s ' % req

  # making some approximations discard ms 
  first = long(first_time.split('.').pop(0))
  last =  long(time.split('.').pop(0))
  time_elapsed = first -last
  print "%s requests in %s seconds, tps = %s " % ( req, time_elapsed, req/time_elapsed)   

Here is the full code. I am trying to calculate number of request per second using tshark.

#!/usr/bin/python
import re
import sys
import commands

    import os
    import time


    pattern = re.compile('<field name="(timestamp|diameter.Session-Id)".*value="([^"]+)')


    from optparse import OptionParser
    def parse_options():
      parser = OptionParser()
     # parser.add_option("-f", "--file", dest="filename",
     #                 help="input pcap file", metavar="FILE")
      parser.add_option("-i", "--identity", dest="identity",
                      help="diameter identity to filter on", metavar="IDENTITY")

      opts, args = parser.parse_args()
      return opts, args

    def calculate_tps(filename,identity):
      ##command='tshark -r %s -R -T pdml diameter'
      first_time = None
      last_time=None
      tshark='tshark -r %s -R \'(diameter.Destination-Host==%s)&& (diameter.CC-Request-Type==3)\' -T pdml'% (filename,identity)
      time = None
      req = 0
      for line in os.popen(tshark, 'r'):
        m=pattern.search(line)    
        if m:
          #print m.group(1)
          if m.group(1) == 'timestamp':
        time=m.group(2)
        print 'time is %s' % time
        if not first_time:
          first_time=time
          prin
          elif m.group(1) == 'diameter.Session-Id':
            req +=1 
        if(req % 100) ==0:
          print 'processed 1000 requests req number %s ' % req

      # making some approximations discard ms 
      first = long(first_time.split('.').pop(0))
      last =  long(time.split('.').pop(0))
      time_elapsed = first -last
      print "%s requests in %s seconds, tps = %s " % ( req, time_elapsed,
      req/time_elapsed)       

    def main():
      global options
      options, args = parse_options()
      if len(args) < 1:
        print >>sys.stderr, "missing pcap file.Please specify the pcap"
        sys.exit(1)   
      #print options.identity  



      start_time = time.time()
      calculate_tps(args.pop(0), options.identity) 
      end_time = time.time()
      print 'completed in %s', (end_time - start_time) 

    if __name__ == '__main__':
       main()
4
  • It means your values in first_time or time are not strings, so they do not have a .split method. Commented Jan 27, 2014 at 18:53
  • You're probably never opening the file properly, so you never go into the for loop and reassign time to something usable. Thus the initialized value of None is used and fails. Commented Jan 27, 2014 at 18:54
  • you have serious indentation problems with the posted code. also, it would be nice if you provide the contents of pattern, and also the traceback. Commented Jan 27, 2014 at 19:00
  • given above is the code that I am working on. Kindly help if possible Commented Jan 27, 2014 at 21:07

2 Answers 2

1

When you make the calls here:

first = long(first_time.split('.').pop(0))
last =  long(time.split('.').pop(0))

You've assumed that each of these is a string. They appear to not have been reassigned during your loop over the file contents, suggesting that your matches are not taking place. You could wrap those in a conditional check with something like this:

if None in (first_time, time):
  print "Uh oh, one of first_time or time is None - ", first_time, time

That would give you an indicator of the problem at the very least.

Sign up to request clarification or add additional context in comments.

Comments

0

This is because your first and last are not string. So there is no attribute split of the type. So if you need split attribute, you better change them to string type.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.