aboutsummaryrefslogtreecommitdiffstats
path: root/sos/plugins/etcd.py
blob: 884aa1bdd42e4b9df34551eccc9f4668cf6cec2c (plain) (blame)
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
# Copyright (C) 2015 Red Hat, Inc. Neependra Khare <nkhare@redhat.com>
# Copyright (C) 2015 Red Hat, Inc. Bryn M. Reeves <bmr@redhat.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from sos.plugins import Plugin, RedHatPlugin


class etcd(Plugin, RedHatPlugin):
    """etcd plugin
    """

    plugin_name = 'etcd'
    packages = ('etcd',)
    profiles = ('system', 'services', 'cluster')

    cmd = 'etcdctl'

    def setup(self):
        etcd_url = self.get_etcd_url()

        self.add_forbidden_path('/etc/etcd/ca')
        self.add_copy_spec('/etc/etcd')

        subcmds = [
           '--version',
           'member list',
           'cluster-health',
           'ls --recursive',
        ]

        self.add_cmd_output(['%s %s' % (self.cmd, sub) for sub in subcmds])

        urls = [
            '/v2/stats/leader',
            '/v2/stats/self',
            '/v2/stats/store',
        ]

        if etcd_url:
            self.add_cmd_output(['curl -s %s%s' % (etcd_url, u) for u in urls])

        self.add_cmd_output("ls -lR /var/lib/etcd/")

    def get_etcd_url(self):
        try:
            with open('/etc/etcd/etcd.conf', 'r') as ef:
                for line in ef:
                    if line.startswith('ETCD_LISTEN_CLIENT_URLS'):
                        return line.split('=')[1].replace('"', '').strip()
        # If we can't read etcd.conf, assume defaults by etcd version
        except:
            # assume v3 is the default
            url = 'http://localhost:2379'
            try:
                ver = self.policy().package_manager.get_pkg_list()['etcd']
                ver = ver['version'][0]
                if ver == '2':
                    url = 'http://localhost:4001'
            except:
                # fallback when etcd is not installed
                pass
            return url

# vim: et ts=5 sw=4