summaryrefslogtreecommitdiff
path: root/bin/swift-recon
blob: bbfbc1ad07bd9f4fcf0543c32341a01123dc7673 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#! /usr/bin/env python
"""
    cmdline utility to perform cluster reconnaissance
"""


from eventlet.green import urllib2
from swift.common.ring import Ring
from urlparse import urlparse
try:
    import simplejson as json
except ImportError:
    import json
from hashlib import md5
import datetime
import eventlet
import optparse
import sys
import os


class Scout(object):
    """
    Obtain swift recon information
    """

    def __init__(self, recon_type, verbose=False, suppress_errors=False,
                    timeout=5):
        recon_uri = ["ringmd5", "async", "replication", "load", "diskusage",
            "unmounted", "quarantined", "sockstat"]
        if recon_type not in recon_uri:
            raise Exception("Invalid scout type requested")
        else:
            self.recon_type = recon_type
        self.verbose = verbose
        self.suppress_errors = suppress_errors
        self.timeout = timeout

    def scout_host(self, base_url, recon_type):
        """
        Perform the actual HTTP request to obtain swift recon telemtry.

        :param base_url: the base url of the host you wish to check. str of the
                        format 'http://127.0.0.1:6000/recon/'
        :param recon_type: the swift recon check to request.
        :returns: tuple of (recon url used, response body, and status)
        """
        url = base_url + recon_type
        try:
            body = urllib2.urlopen(url, timeout=self.timeout).read()
            content = json.loads(body)
            if self.verbose:
                print "-> %s: %s" % (url, content)
            status = 200
        except urllib2.HTTPError as err:
            if not self.suppress_errors or self.verbose:
                print "-> %s: %s" % (url, err)
            content = err
            status = err.code
        except urllib2.URLError as err:
            if not self.suppress_errors or self.verbose:
                print "-> %s: %s" % (url, err)
            content = err
            status = -1
        return url, content, status

    def scout(self, host):
        """
        Obtain telemetry from a host running the swift recon middleware.

        :param host: host to check
        :returns: tuple of (recon url used, response body, and status)
        """
        base_url = "http://%s:%s/recon/" % (host[0], host[1])
        url, content, status = self.scout_host(base_url, self.recon_type)
        return url, content, status


class SwiftRecon(object):
    """
    Retrieve and report cluster info from hosts running recon middleware.
    """

    def __init__(self):
        self.verbose = False
        self.suppress_errors = False
        self.timeout = 5
        self.pool_size = 30
        self.pool = eventlet.GreenPool(self.pool_size)

    def get_devices(self, zone_filter, swift_dir, ring_name):
        """
        Get a list of hosts in the ring

        :param zone_filter: Only list zones matching given filter
        :param swift_dir: Directory of swift config, usually /etc/swift
        :param ring_name: Name of the ring, such as 'object'
        :returns: a set of tuples containing the ip and port of hosts
        """
        ring_data = Ring(swift_dir, ring_name=ring_name)
        if zone_filter:
            ips = set((n['ip'], n['port']) for n in ring_data.devs if n \
                if n['zone'] == zone_filter)
        else:
            ips = set((n['ip'], n['port']) for n in ring_data.devs if n)
        return ips

    def get_ringmd5(self, hosts, ringfile):
        """
        Compare ring md5sum's with those on remote host

        :param hosts: set of hosts to check. in the format of:
            set([('127.0.0.1', 6020), ('127.0.0.2', 6030)])
        :param ringfile: The local ring file to compare the md5sum with.
        """
        stats = {}
        matches = 0
        errors = 0
        md5sum = md5()
        with open(ringfile, 'rb') as f:
            block = f.read(4096)
            while block:
                md5sum.update(block)
                block = f.read(4096)
        ring_sum = md5sum.hexdigest()
        recon = Scout("ringmd5", self.verbose, self.suppress_errors,
                        self.timeout)
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print "[%s] Checking ring md5sum's on %s hosts..." % (now, len(hosts))
        if self.verbose:
            print "-> On disk md5sum: %s" % ring_sum
        for url, response, status in self.pool.imap(recon.scout, hosts):
            if status == 200:
                stats[url] = response[ringfile]
                if response[ringfile] != ring_sum:
                    print "!! %s (%s) doesn't match on disk md5sum" % \
                        (url, response[ringfile])
                else:
                    matches = matches + 1
                    if self.verbose:
                        print "-> %s matches." % url
            else:
                errors = errors + 1
        print "%s/%s hosts matched, %s error[s] while checking hosts." % \
                (matches, len(hosts), errors)
        print "=" * 79

    def async_check(self, hosts):
        """
        Obtain and print async pending statistics

        :param hosts: set of hosts to check. in the format of:
            set([('127.0.0.1', 6020), ('127.0.0.2', 6030)])
        """
        stats = {}
        recon = Scout("async", self.verbose, self.suppress_errors,
                        self.timeout)
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print "[%s] Checking async pendings on %s hosts..." % (now, len(hosts))
        for url, response, status in self.pool.imap(recon.scout, hosts):
            if status == 200:
                stats[url] = response['async_pending']
        if len(stats) > 0:
            low = min(stats.values())
            high = max(stats.values())
            total = sum(stats.values())
            average = total / len(stats)
            print "Async stats: low: %d, high: %d, avg: %d, total: %d" % (low,
                high, average, total)
        else:
            print "Error: No hosts available or returned valid information."
        print "=" * 79

    def umount_check(self, hosts):
        """
        Check for and print unmounted drives

        :param hosts: set of hosts to check. in the format of:
            set([('127.0.0.1', 6020), ('127.0.0.2', 6030)])
        """
        stats = {}
        recon = Scout("unmounted", self.verbose, self.suppress_errors,
                        self.timeout)
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print "[%s] Getting unmounted drives from %s hosts..." % \
            (now, len(hosts))
        for url, response, status in self.pool.imap(recon.scout, hosts):
            if status == 200:
                for i in response:
                    stats[url] = i['device']
        for host in stats:
            node = urlparse(host).netloc
            print "Not mounted: %s on %s" % (stats[host], node)
        print "=" * 79

    def replication_check(self, hosts):
        """
        Obtain and print replication statistics

        :param hosts: set of hosts to check. in the format of:
            set([('127.0.0.1', 6020), ('127.0.0.2', 6030)])
        """
        stats = {}
        recon = Scout("replication", self.verbose, self.suppress_errors,
                        self.timeout)
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print "[%s] Checking replication times on %s hosts..." % \
            (now, len(hosts))
        for url, response, status in self.pool.imap(recon.scout, hosts):
            if status == 200:
                stats[url] = response['object_replication_time']
        if len(stats) > 0:
            low = min(stats.values())
            high = max(stats.values())
            total = sum(stats.values())
            average = total / len(stats)
            print "[Replication Times] shortest: %s, longest: %s, avg: %s" % \
                (low, high, average)
        else:
            print "Error: No hosts available or returned valid information."
        print "=" * 79

    def load_check(self, hosts):
        """
        Obtain and print load average statistics

        :param hosts: set of hosts to check. in the format of:
            set([('127.0.0.1', 6020), ('127.0.0.2', 6030)])
        """
        load1 = {}
        load5 = {}
        load15 = {}
        recon = Scout("load", self.verbose, self.suppress_errors,
                        self.timeout)
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print "[%s] Checking load avg's on %s hosts..." % (now, len(hosts))
        for url, response, status in self.pool.imap(recon.scout, hosts):
            if status == 200:
                load1[url] = response['1m']
                load5[url] = response['5m']
                load15[url] = response['15m']
        stats = {"1m": load1, "5m": load5, "15m": load15}
        for item in stats:
            if len(stats[item]) > 0:
                low = min(stats[item].values())
                high = max(stats[item].values())
                total = sum(stats[item].values())
                average = total / len(stats[item])
                print "[%s load average] lowest: %s, highest: %s, avg: %s" % \
                    (item, low, high, average)
            else:
                print "Error: No hosts available or returned valid info."
        print "=" * 79

    def quarantine_check(self, hosts):
        """
        Obtain and print quarantine statistics

        :param hosts: set of hosts to check. in the format of:
            set([('127.0.0.1', 6020), ('127.0.0.2', 6030)])
        """
        objq = {}
        conq = {}
        acctq = {}
        recon = Scout("quarantined", self.verbose, self.suppress_errors,
                        self.timeout)
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print "[%s] Checking quarantine on %s hosts..." % (now, len(hosts))
        for url, response, status in self.pool.imap(recon.scout, hosts):
            if status == 200:
                objq[url] = response['objects']
                conq[url] = response['containers']
                acctq[url] = response['accounts']
        stats = {"objects": objq, "containers": conq, "accounts": acctq}
        for item in stats:
            if len(stats[item]) > 0:
                low = min(stats[item].values())
                high = max(stats[item].values())
                total = sum(stats[item].values())
                average = total / len(stats[item])
                print ("[Quarantined %s] low: %d, high: %d, avg: %d, total: %d"
                        % (item, low, high, average, total))
            else:
                print "Error: No hosts available or returned valid info."
        print "=" * 79

    def socket_usage(self, hosts):
        """
        Obtain and print /proc/net/sockstat statistics

        :param hosts: set of hosts to check. in the format of:
            set([('127.0.0.1', 6020), ('127.0.0.2', 6030)])
        """
        inuse4 = {}
        mem = {}
        inuse6 = {}
        timewait = {}
        orphan = {}
        recon = Scout("sockstat", self.verbose, self.suppress_errors,
                        self.timeout)
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print "[%s] Checking socket usage on %s hosts..." % (now, len(hosts))
        for url, response, status in self.pool.imap(recon.scout, hosts):
            if status == 200:
                inuse4[url] = response['tcp_in_use']
                mem[url] = response['tcp_mem_allocated_bytes']
                inuse6[url] = response['tcp6_in_use']
                timewait[url] = response['time_wait']
                orphan[url] = response['orphan']
        stats = {"tcp_in_use": inuse4, "tcp_mem_allocated_bytes": mem,
                    "tcp6_in_use": inuse6, "time_wait": timewait,
                    "orphan": orphan}
        for item in stats:
            if len(stats[item]) > 0:
                low = min(stats[item].values())
                high = max(stats[item].values())
                total = sum(stats[item].values())
                average = total / len(stats[item])
                print "[%s] low: %d, high: %d, avg: %d, total: %d" % \
                    (item, low, high, average, total)
            else:
                print "Error: No hosts or info available."
        print "=" * 79

    def disk_usage(self, hosts):
        """
        Obtain and print disk usage statistics

        :param hosts: set of hosts to check. in the format of:
            set([('127.0.0.1', 6020), ('127.0.0.2', 6030)])
        """
        stats = {}
        highs = []
        lows = []
        raw_total_used = []
        raw_total_avail = []
        averages = []
        percents = {}
        recon = Scout("diskusage", self.verbose, self.suppress_errors,
                        self.timeout)
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print "[%s] Checking disk usage on %s hosts..." % (now, len(hosts))
        for url, response, status in self.pool.imap(recon.scout, hosts):
            if status == 200:
                hostusage = []
                for entry in response:
                    if entry['mounted']:
                        used = float(entry['used']) / float(entry['size']) \
                            * 100.0
                        raw_total_used.append(entry['used'])
                        raw_total_avail.append(entry['avail'])
                        hostusage.append(round(used, 2))
                stats[url] = hostusage

        for url in stats:
            if len(stats[url]) > 0:
                #get per host hi/los for another day
                low = min(stats[url])
                high = max(stats[url])
                total = sum(stats[url])
                average = total / len(stats[url])
                highs.append(high)
                lows.append(low)
                averages.append(average)
                for percent in stats[url]:
                    percents[int(percent)] = percents.get(int(percent), 0) + 1
            else:
                print "-> %s: Error. No drive info available." % url

        if len(lows) > 0:
            low = min(lows)
            high = max(highs)
            average = sum(averages) / len(averages)
            #dist graph shamelessly stolen from https://github.com/gholt/tcod
            print "Distribution Graph:"
            mul = 69.0 / max(percents.values())
            for percent in sorted(percents):
                print '% 3d%%%5d %s' % (percent, percents[percent], \
                    '*' * int(percents[percent] * mul))
            raw_used = sum(raw_total_used)
            raw_avail = sum(raw_total_avail)
            raw_total = raw_used + raw_avail
            print "Disk usage: space used: %s of %s" % (raw_used, raw_total)
            print "Disk usage: space free: %s of %s" % (raw_avail, raw_total)
            print "Disk usage: lowest: %s%%, highest: %s%%, avg: %s%%" % \
                (low, high, average)
        else:
            print "Error: No hosts available or returned valid information."
        print "=" * 79

    def main(self):
        """
        Retrieve and report cluster info from hosts running recon middleware.
        """
        print "=" * 79
        usage = '''
        usage: %prog [-v] [--suppress] [-a] [-r] [-u] [-d] [-l] [--objmd5]
        '''
        args = optparse.OptionParser(usage)
        args.add_option('--verbose', '-v', action="store_true",
            help="Print verbose info")
        args.add_option('--suppress', action="store_true",
            help="Suppress most connection related errors")
        args.add_option('--async', '-a', action="store_true",
            help="Get async stats")
        args.add_option('--replication', '-r', action="store_true",
            help="Get replication stats")
        args.add_option('--unmounted', '-u', action="store_true",
            help="Check cluster for unmounted devices")
        args.add_option('--diskusage', '-d', action="store_true",
            help="Get disk usage stats")
        args.add_option('--loadstats', '-l', action="store_true",
            help="Get cluster load average stats")
        args.add_option('--quarantined', '-q', action="store_true",
            help="Get cluster quarantine stats")
        args.add_option('--objmd5', action="store_true",
            help="Get md5sums of object.ring.gz and compare to local copy")
        args.add_option('--sockstat', action="store_true",
            help="Get cluster socket usage stats")
        args.add_option('--all', action="store_true",
            help="Perform all checks. Equal to -arudlq --objmd5 --sockstat")
        args.add_option('--zone', '-z', type="int",
            help="Only query servers in specified zone")
        args.add_option('--timeout', '-t', type="int", metavar="SECONDS",
            help="Time to wait for a response from a server", default=5)
        args.add_option('--swiftdir', default="/etc/swift",
            help="Default = /etc/swift")
        options, arguments = args.parse_args()

        if len(sys.argv) <= 1:
            args.print_help()
            sys.exit(0)

        swift_dir = options.swiftdir
        obj_ring = os.path.join(swift_dir, 'object.ring.gz')

        self.verbose = options.verbose
        self.suppress_errors = options.suppress
        self.timeout = options.timeout

        if options.zone:
            hosts = self.get_devices(options.zone, swift_dir, 'object')
        else:
            hosts = self.get_devices(None, swift_dir, 'object')

        if options.all:
            self.async_check(hosts)
            self.umount_check(hosts)
            self.replication_check(hosts)
            self.load_check(hosts)
            self.disk_usage(hosts)
            self.get_ringmd5(hosts, obj_ring)
            self.quarantine_check(hosts)
            self.socket_usage(hosts)
        else:
            if options.async:
                self.async_check(hosts)
            if options.unmounted:
                self.umount_check(hosts)
            if options.replication:
                self.replication_check(hosts)
            if options.loadstats:
                self.load_check(hosts)
            if options.diskusage:
                self.disk_usage(hosts)
            if options.objmd5:
                self.get_ringmd5(hosts, obj_ring)
            if options.quarantined:
                self.quarantine_check(hosts)
            if options.sockstat:
                self.socket_usage(hosts)


if __name__ == '__main__':
    try:
        reconnoiter = SwiftRecon()
        reconnoiter.main()
    except KeyboardInterrupt:
        print '\n'