xref: /3.0.3-GA/ep-engine/management/cbepctl (revision d23bffa8)
1#!/usr/bin/env python
2
3import time
4import sys
5
6import clitool
7import mc_bin_client
8import memcacheConstants
9import sys
10
11def cmd(f):
12    """Decorate a function with code to authenticate based on 1-2
13    arguments past the normal number of arguments."""
14
15    def g(*args, **kwargs):
16        mc = args[0]
17        n = f.func_code.co_argcount
18
19        bucket = kwargs.get('bucketName', None)
20        password = kwargs.get('password', None) or ""
21
22        if bucket:
23            try:
24                mc.sasl_auth_plain(bucket, password)
25            except mc_bin_client.MemcachedError:
26                print "Authentication error for %s" % bucket
27                sys.exit(1)
28
29        if kwargs.get('allBuckets', None):
30            buckets = mc.stats('bucket')
31            for bucket in buckets.iterkeys():
32                print '*' * 78
33                print bucket
34                print
35                mc.bucket_select(bucket)
36                f(*args[:n])
37        else:
38            f(*args[:n])
39
40    return g
41
42@cmd
43def set_param(mc, type, key, val):
44    engine_param = None
45    if type == 'checkpoint_param':
46        engine_param = memcacheConstants.ENGINE_PARAM_CHECKPOINT
47    elif type == 'flush_param':
48        engine_param = memcacheConstants.ENGINE_PARAM_FLUSH
49    elif type == 'tap_param':
50        engine_param = memcacheConstants.ENGINE_PARAM_TAP
51    else:
52        print 'Error: Bad parameter %s' % type
53
54    if key == 'tap_throttle_queue_cap' and val == 'infinite':
55        val = '-1'
56
57    if key == "mem_high_wat" or key == "mem_low_wat":
58        if val.endswith("%"):
59            _x_ = (val[:len(val)-1])
60            if not _x_.isdigit():
61                print 'Error: Invalid parameter %s' % val
62                return
63            if float(_x_) > 100:
64                print 'Error: Bad parameter %s' % val
65                return
66            _quota_ = int(mc.stats()['ep_max_size'])
67            val = str(int(float(_x_)*(_quota_)/100))
68
69    try:
70        mc.set_param(key, val, engine_param)
71        print 'set %s to %s' %(key, val)
72    except mc_bin_client.MemcachedError, error:
73        print 'Error: %s' % error.msg
74    except Exception, e:
75        print 'Generic error (%s)' % e
76
77@cmd
78def stop(mc):
79    try:
80        mc.stop_persistence()
81        stopped = False
82        while not stopped:
83            time.sleep(0.5)
84            try:
85                stats = mc.stats()
86                success = True
87            except:
88                if success:
89                    mc = mc_bin_client.MemcachedClient(mc.host, mc.port)
90                    raise
91                else:
92                    raise
93            success = False
94            if stats['ep_flusher_state'] == 'paused':
95                stopped = True
96        print 'Persistence stopped'
97    except mc_bin_client.MemcachedError, error:
98        print 'Error: %s' % error.msg
99    except Exception, e:
100        print 'Generic error (%s)' % e
101
102@cmd
103def start(mc):
104    try:
105        mc.start_persistence()
106        print 'Persistence started'
107    except mc_bin_client.MemcachedError, error:
108        print 'Error: %s' % error.msg
109    except Exception, e:
110        print 'Generic error (%s)' % e
111
112@cmd
113def drain(mc):
114    try:
115        while True:
116            s = mc.stats()
117            if s['ep_queue_size'] == "0":
118                print("done")
119                return
120            time.sleep(2)
121            sys.stdout.write('.')
122            sys.stdout.flush()
123        print 'Write queues drained'
124    except mc_bin_client.MemcachedError, error:
125        print 'Error: %s' % error.msg
126    except Exception, e:
127        print 'Generic error (%s)' % e
128
129if __name__ == '__main__':
130
131    c = clitool.CliTool("""
132Persistence:
133  stop           - stop persistence
134  start          - start persistence
135  drain          - wait until queues are drained
136
137
138Available params for "set":
139
140  Available params for set checkpoint_param:
141    chk_max_items                - Max number of items allowed in a checkpoint.
142    chk_period                   - Time bound (in sec.) on a checkpoint.
143    item_num_based_new_chk       - true if a new checkpoint can be created based
144                                   on.
145                                   the number of items in the open checkpoint.
146    keep_closed_chks             - true if we want to keep closed checkpoints in
147                                   memory.
148                                   as long as the current memory usage is below
149                                   high water mark.
150    max_checkpoints              - Max number of checkpoints allowed per vbucket.
151
152
153  Available params for set flush_param:
154    alog_sleep_time              - Access scanner interval (minute)
155    alog_task_time               - Access scanner next task time (UTC)
156    bg_fetch_delay               - Delay before executing a bg fetch (test
157                                   feature).
158    couch_response_timeout       - timeout in receiving a response from couchdb.
159    exp_pager_stime              - Expiry Pager Sleeptime.
160    flushall_enabled             - Enable flush operation.
161    pager_active_vb_pcnt         - Percentage of active vbuckets items among
162                                   all ejected items by item pager.
163    max_size                     - Max memory used by the server.
164    mem_high_wat                 - High water mark.
165    mem_low_wat                  - Low water mark.
166    mutation_mem_threshold       - Memory threshold (%) on the current bucket quota
167                                   for accepting a new mutation.
168    timing_log                   - path to log detailed timing stats.
169    warmup_min_memory_threshold  - Memory threshold (%) during warmup to enable
170                                   traffic
171    warmup_min_items_threshold   - Item number threshold (%) during warmup to enable
172                                   traffic
173
174  Available params for "set tap_param":
175    tap_keepalive                - Seconds to hold a named tap connection.
176    tap_throttle_queue_cap       - Max disk write queue size to throttle tap
177                                   streams ('infinite' means no cap).
178    tap_throttle_cap_pcnt        - Percentage of total items in write queue at
179                                   which we throttle tap input
180    tap_throttle_threshold       - Percentage of memory in use to throttle tap
181                                   streams.
182    """)
183
184    c.addCommand('drain', drain, "drain")
185    c.addCommand('set', set_param, 'set type param value')
186    c.addCommand('start', start, 'start')
187    c.addCommand('stop', stop, 'stop')
188    c.addFlag('-a', 'allBuckets', 'iterate over all buckets (requires admin u/p)')
189    c.addOption('-b', 'bucketName', 'the bucket to get stats from (Default: default)')
190    c.addOption('-p', 'password', 'the password for the bucket if one exists')
191    c.execute()
192