xref: /3.0.3-GA/ep-engine/management/cbepctl (revision c1015f71)
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    enable_chk_merge             = True if merging closed checkpoints is enabled.
152
153
154  Available params for set flush_param:
155    access_scanner_enabled       - Enable or disable access scanner task (true/false)
156    alog_sleep_time              - Access scanner interval (minute)
157    alog_task_time               - Access scanner next task time (UTC)
158    backfill_mem_threshold       - Memory threshold (%) on the current bucket quota
159                                   before backfill task is made to back off.
160    bg_fetch_delay               - Delay before executing a bg fetch (test
161                                   feature).
162    compaction_exp_mem_threshold - Memory threshold (%) on the current bucket quota
163                                   after which compaction will not queue expired
164                                   items for deletion.
165    exp_pager_stime              - Expiry Pager Sleeptime.
166    flushall_enabled             - Enable flush operation.
167    pager_active_vb_pcnt         - Percentage of active vbuckets items among
168                                   all ejected items by item pager.
169    max_size                     - Max memory used by the server.
170    mem_high_wat                 - High water mark.
171    mem_low_wat                  - Low water mark.
172    mutation_mem_threshold       - Memory threshold (%) on the current bucket quota
173                                   for accepting a new mutation.
174    timing_log                   - path to log detailed timing stats.
175    warmup_min_memory_threshold  - Memory threshold (%) during warmup to enable
176                                   traffic
177    warmup_min_items_threshold   - Item number threshold (%) during warmup to enable
178                                   traffic
179    max_num_readers              - Override default number of global threads that
180                                   prioritize read operations.
181    max_num_writers              - Override default number of global threads that
182                                   prioritize write operations.
183    max_num_auxio                - Override default number of global threads that
184                                   prioritize auxio operations.
185    max_num_nonio                - Override default number of global threads that
186                                   prioritize nonio operations.
187
188  Available params for "set tap_param":
189    tap_keepalive                - Seconds to hold a named tap connection.
190    tap_throttle_queue_cap       - Max disk write queue size to throttle tap
191                                   streams ('infinite' means no cap).
192    tap_throttle_cap_pcnt        - Percentage of total items in write queue at
193                                   which we throttle tap input
194    tap_throttle_threshold       - Percentage of memory in use to throttle tap
195                                   streams.
196    """)
197
198    c.addCommand('drain', drain, "drain")
199    c.addCommand('set', set_param, 'set type param value')
200    c.addCommand('start', start, 'start')
201    c.addCommand('stop', stop, 'stop')
202    c.addFlag('-a', 'allBuckets', 'iterate over all buckets (requires admin u/p)')
203    c.addOption('-b', 'bucketName', 'the bucket to get stats from (Default: default)')
204    c.addOption('-p', 'password', 'the password for the bucket if one exists')
205    c.execute()
206