Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 55113f38e4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 168 lines (141 sloc) 5.195 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import getopt
import sys
import os
import traceback
import copy

import dbaccessor
import analyzer
import stats_buffer

import listservers
import buckets
import node
import info
import util_cli as util
import mc_bin_client
import simplejson

def parse_opt():
    (cluster, user, password) = ('', '','')

    try:
        (opts, _args) = getopt.getopt(sys.argv[1:],
                                      'c:dp:u:', [
                'cluster=',
                'debug',
                'password=',
                'user='
                ])
    except getopt.GetoptError, err:
        usage(err)

    for (opt, arg) in opts:
        if opt in ('-c', '--cluster'):
            cluster = arg
        if opt in ('-u', '--user'):
            user = arg
        if opt in ('-p', '--password'):
            password = arg
        if opt in ('-d', '--debug'):
            debug = True
    if not cluster:
        usage("please provide a CLUSTER, or use -h for more help.")
    return (cluster, user, password, opts)

def get_stats(mc, stats):
    try:
        node_stats = mc.stats('')
        if node_stats:
            for key, val in node_stats.items():
                stats[key] = val
    except Exception, err:
        #print "ERROR: command: %s: %s:%d, %s" % ('stats all', server, port, err)
        traceback.print_exc()
        #sys.exit(1)
    
    try:
        node_stats = mc.stats('tap')
        if node_stats:
            for key, val in node_stats.items():
                stats[key] = val
    except Exception, err:
        #print "ERROR: command: %s: %s:%d, %s" % ('stats tap', server, port, err)
        traceback.print_exc()
        #sys.exit(1)

def stats_formatter(stats, prefix=" ", cmp=None):
    if stats:
        longest = max((len(x) + 2) for x in stats.keys())
        for stat, val in sorted(stats.items(), cmp=cmp):
            s = stat + ":"
            print "%s%s%s" % (prefix, s.ljust(longest), val)

def collect_data():

    (cluster, user, password, opts) = parse_opt()
    server, port = util.hostport(cluster)

    nodes = []
    commands = {
        'host-list' : listservers.ListServers,
        'server-info' : info.Info,
        'bucket-list' : buckets.Buckets,
        'bucket-stats' : buckets.BucketStats,
        'bucket-node-stats' : buckets.BucketNodeStats,
        }
    
    accessor = dbaccessor.DbAccesor()

    accessor.connect_db()
    accessor.create_databases();

    #get node list and its status
    try:
        cmd = 'host-list'
        c = commands[cmd]()
        nodes = c.runCmd(cmd, server, port, user, password, opts)
    except Exception, err:
        print "ERROR: command: %s: %s:%d, %s" % (cmd, server, port, err)
        sys.exit(1)

    #get each node information
    try:
        cmd = 'server-info'
        c = commands[cmd]()
        for node in nodes:
            (node_server, node_port) = util.hostport(node['hostname'])
            nodeid = accessor.create_or_update_node(node_server, node_port, node['status'])

            if node['status'] == 'healthy':
                node_info = c.runCmd(cmd, node_server, node_port, user, password, opts)
                accessor.process_node_stats(nodeid, node_info)
                #stats = {}
                #mc = mc_bin_client.MemcachedClient(node_server, node['ports']['direct'])
                #get_stats(mc, stats)
            else:
                print "Unhealthy node: %s:%s" %(node_server, node['status'])
    except Exception, err:
        traceback.print_exc()
        #print "ERROR: command: %s: %s:%d, %s" % (cmd, server, port, err)
        sys.exit(1)

    #get each bucket information
    try:
        cmd = 'bucket-list'
        c = commands[cmd]()
        json = c.runCmd(cmd, server, port, user, password, opts)
        for bucket in json:
            (bucket_name, bucket_id) = accessor.process_bucket(bucket)

            # get bucket related stats
            cmd = 'bucket-stats'
            c = buckets.BucketStats(bucket_name)
            json = c.runCmd(cmd, server, port, user, password, opts)
            stats_buffer.buckets_summary[bucket_name] = json

            #retrieve bucket stats per node
            stats_buffer.buckets[bucket_name] = copy.deepcopy(stats_buffer.stats)
            cmd = 'bucket-node-stats'
            for scale, stat_set in stats_buffer.buckets[bucket_name].iteritems():
                for stat in stat_set.iterkeys():
                    c = buckets.BucketNodeStats(bucket_name, stat, scale)
                    json = c.runCmd(cmd, server, port, user, password, opts)
                    stats_buffer.buckets[bucket_name][scale][stat] = json
                #accessor.process_bucket_node_stats(bucket_id, server, stat, json)
    except Exception, err:
        traceback.print_exc()
        #print "ERROR: command: %s: %s:%d, %s" % (cmd, server, port, err)
        sys.exit(1)
    
    accessor.close()


def main():
    
    #make snapshot for the current cluster status
    collect_data()

    #analyze the snapshot and historic data
    performer = analyzer.StatsAnalyzer()
    performer.run_analysis()
    
if __name__ == '__main__':
    main()
Something went wrong with that request. Please try again.