Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from base_section import BaseSection 

from collections import defaultdict 

import re 

 

from mtools.util.profile_collection import ProfileCollection 

 

class ConnectionSection(BaseSection): 

    """ This section goes through the logfile and extracts information  

        about opened and closed connections. 

    """ 

 

    name = "connections" 

 

    def __init__(self, mloginfo): 

        BaseSection.__init__(self, mloginfo) 

 

        # add --restarts flag to argparser 

        self.mloginfo.argparser_sectiongroup.add_argument('--connections', action='store_true', help='outputs information about opened and closed connections') 

 

 

    @property 

    def active(self): 

        """ return boolean if this section is active. """ 

        return self.mloginfo.args['connections'] 

 

 

    def run(self): 

        """ run this section and print out information. """ 

        if isinstance(self.mloginfo.logfile, ProfileCollection): 

            print 

            print "    not available for system.profile collections" 

            print 

            return 

 

        ip_opened = defaultdict(lambda: 0) 

        ip_closed = defaultdict(lambda: 0) 

        socket_exceptions = 0 

 

        for logevent in self.mloginfo.logfile: 

            line = logevent.line_str 

            pos = line.find('connection accepted') 

            if pos != -1: 

                # connection was opened, increase counter 

                tokens = line[pos:pos+100].split(' ') 

                if tokens[3] == 'anonymous': 

                    ip = 'anonymous' 

                else: 

                    ip, _ = tokens[3].split(':') 

                ip_opened[ip] += 1 

 

            pos = line.find('end connection') 

            if pos != -1: 

                # connection was closed, increase counter 

                tokens = line[pos:pos+100].split(' ') 

                if tokens[2] == 'anonymous': 

                    ip = 'anonymous' 

                else: 

                    ip, _ = tokens[2].split(':') 

                ip_closed[ip] += 1 

 

            if "SocketException" in line: 

                socket_exceptions += 1 

 

 

        # calculate totals 

        total_opened = sum(ip_opened.values()) 

        total_closed = sum(ip_closed.values()) 

 

        unique_ips = set(ip_opened.keys()) 

        unique_ips.update(ip_closed.keys()) 

 

 

        # output statistics 

        print "     total opened:", total_opened 

        print "     total closed:", total_closed 

        print "    no unique IPs:", len(unique_ips) 

        print "socket exceptions:", socket_exceptions 

        print 

 

        for ip in sorted(unique_ips, key=lambda x: ip_opened[x], reverse=True): 

            opened = ip_opened[ip] if ip in ip_opened else 0 

            closed = ip_closed[ip] if ip in ip_closed else 0 

 

            print "%-15s  opened: %-8i  closed: %-8i" % (ip, opened, closed) 

        print