Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 229 lines (194 sloc) 8.297 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 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
#!/usr/bin/python
# -*- python -*-
import os
import sys
import time

class Task(object):
    privileged = False
    num_samples = 0
    interval = 0
    def __init__(self, description, command, **kwargs):
        self.description = description
        self.command = command
        self.__dict__.update(kwargs)

    def execute(self, fp):
        """Run the task"""
        import subprocess
        p = subprocess.Popen(self.command, bufsize=-1, stdout=fp, stderr=fp,
                             shell=True)
        return p.wait()

    def will_run(self):
        """Determine if this task will run on this platform."""
        return sys.platform in self.platforms


class TaskRunner(object):
    default_name = "membase.log"
    def __init__(self, verbosity=0):
        self.files = {}
        self.tasks = {}
        self.verbosity = verbosity

    def get_file(self, filename):
        if filename in self.files:
            fp = self.files[filename]
        else:
            import tempfile
            fp = tempfile.NamedTemporaryFile(mode='w+')
            self.files[filename] = fp

        return fp

    def header(self, fp, title, subtitle):
        separator = '=' * 78
        print >> fp, separator
        print >> fp, title
        print >> fp, subtitle
        print >> fp, separator
        fp.flush()

    def log_result(self, result):
        if result == 0:
            print >> sys.stderr, "OK"
        else:
            print >> sys.stderr, "Exit code %d" % result

    def run(self, task):
        """Run a task with a file descriptor corresponding to its log file"""
        if task.will_run():
            print >> sys.stderr, "%s (%s) - " % (task.description, task.command),
            if task.privileged and os.getuid() != 0:
                print >> sys.stderr, "skipped (needs root privs)"
                return

            if hasattr(task, 'log_file'):
                filename = task.log_file
            else:
                filename = self.default_name

            fp = self.get_file(filename)
            self.header(fp, task.description, task.command)
            result = task.execute(fp)
            fp.flush()
            self.log_result(result)
            for i in xrange(2, task.num_samples + 2):
                print >> sys.stderr, "Taking sample %d after %f seconds - " % \
                    (i, task.interval)
                time.sleep(task.interval)
                result = task.execute(fp)
                self.log_result(result)
        elif self.verbosity >= 2:
            print >> sys.stderr, 'Skipping "%s" (%s): not for platform %s' \
                % (task.description, task.command, sys.platform)

    def zip(self, filename):
        """Write all our logs to a zipfile"""
        from zipfile import ZipFile, ZIP_DEFLATED
        zf = ZipFile(filename, mode='w', compression=ZIP_DEFLATED)
        for name, fp in self.files.iteritems():
            zf.write(fp.name, name)


class SolarisTask(Task):
    platforms = ['sunos5', 'solaris']


class LinuxTask(Task):
    platforms = ['linux2']


class WindowsTask(Task):
    platforms = ['win32', 'cygwin']


class MacOSXTask(Task):
    platforms = ['darwin']


class UnixTask(SolarisTask, LinuxTask, MacOSXTask):
    platforms = SolarisTask.platforms + LinuxTask.platforms + MacOSXTask.platforms


class AllOsTask(UnixTask, WindowsTask):
    platforms = UnixTask.platforms + WindowsTask.platforms


_tasks = [
    UnixTask("uname", "uname -a"),
    WindowsTask("System information", "systeminfo"),
    WindowsTask("Computer system", "wmic computersystem"),
    WindowsTask("Computer OS", "wmic os"),
    UnixTask("Directory structure",
         "ls -lR /opt/membase /var/opt/membase /etc/opt/membase"),
    SolarisTask("Process list snapshot", "prstat -a -c -n 100 -t -v -L 1 10"),
    SolarisTask("Process list", "ps -ef"),
    SolarisTask("Service configuration", "svcs -a"),
    SolarisTask("Swap configuration", "swap -l"),
    SolarisTask("Disk activity", "zpool iostat 1 10"),
    SolarisTask("Disk activity", "iostat -E 1 10"),
    LinuxTask("Process list snapshot", "top -H -n 1"),
    LinuxTask("Process list ", "ps -AwwL -o user,pid,lwp,ppid,nlwp,pcpu,pri,nice,vsize,rss,tty,stat,wchan:12,start,bsdtime,command"),
    LinuxTask("Swap configuration", "free -t"),
    LinuxTask("Swap configuration", "swapon -s"),
    LinuxTask("Kernel modules", "lsmod"),
    LinuxTask("Installed software", "rpm -qa"),
    MacOSXTask("Process list snapshot", "top -l 1"),
    MacOSXTask("Disk activity", "iostat 1 10"),
    MacOSXTask("Process list ",
         "ps -Aww -o user,pid,lwp,ppid,nlwp,pcpu,pri,nice,vsize,rss,tty,"
         "stat,wchan:12,start,bsdtime,command"),
    WindowsTask("Service list", "wmic service where state=\"running\" GET caption, name, state"),
    WindowsTask("Process list", "wmic process"),
    WindowsTask("Swap settings", "wmic pagefile"),
    WindowsTask("Disk partition", "wmic partition"),
    WindowsTask("Disk volumes", "wmic volume"),
    UnixTask("Network configuration", "ifconfig -a", interval=10,
         num_samples=1),
    WindowsTask("Network configuration", "ipconfig /all", interval=10,
         num_samples=1),
    UnixTask("Network status", "netstat -an"),
    WindowsTask("Network status", "netstat -an"),
    AllOsTask("Network routing table", "netstat -rn"),
    UnixTask("Arp cache", "arp -na"),
    WindowsTask("Arp cache", "arp -a"),
    WindowsTask("Network Interface Controller", "wmic nic"),
    WindowsTask("Network Adapter", "wmic nicconfig"),
    WindowsTask("Active network connection", "wmic netuse"),
    WindowsTask("Protocols", "wmic netprotocol"),
    WindowsTask("Cache memory", "wmic memcache"),
    WindowsTask("Physical memory", "wmic memphysical"),
    WindowsTask("Physical memory chip info", "wmic memorychip"),
    WindowsTask("Local storage devices", "wmic logicaldisk"),
    WindowsTask("Physical memory", "wmic memphysical"),
    UnixTask("Filesystem", "df -ha"),
    UnixTask("System activity reporter", "sar 1 10"),
    UnixTask("System paging activity", "vmstat 1 10"),
    UnixTask("System uptime", "uptime"),
    UnixTask("northscale user definition", "getent passwd northscale"),
    UnixTask("northscale user limits", "su northscale -c \"ulimit -a\"",
         privileged=True),
    UnixTask("Interrupt status", "intrstat 1 10"),
    UnixTask("Processor status", "mpstat 1 10"),
    UnixTask("System log", "/var/adm/messages"),
    UnixTask("Kernel log buffer", "dmesg"),
    AllOsTask("memcached stats all",
              "stats -a localhost:11210 all _admin _admin",
              log_file="stats.log"),
    AllOsTask("memcached stats dispatcher",
              "stats -a localhost:11210 dispatcher _admin _admin",
              log_file="stats.log"),
    AllOsTask("memcached stats hash",
              "stats -a localhost:11210 hash _admin _admin",
              log_file="stats.log"),
    AllOsTask("memcached stats tap",
              "stats -a localhost:11210 tap _admin _admin",
              log_file="stats.log"),
    AllOsTask("memcached stats timings",
              "stats -a localhost:11210 timings _admin _admin",
              log_file="stats.log"),
    AllOsTask("membase logs", "browse_logs", log_file="ns_server.log"),
    ]


def main():
    from optparse import OptionParser
    parser = OptionParser("usage: %prog [options] output_file")
    parser.add_option("-v", dest="verbosity", help="increase verbosity level",
                      action="count", default=0)
    options, args = parser.parse_args()

    if len(args) != 1:
        parser.error("incorrect number of arguments")

    mydir = os.path.dirname(sys.argv[0])
    management_dir = os.path.join(mydir, '..', 'ep_engine', 'management')
    if os.name == 'posix':
        path = [mydir,
                management_dir,
                '/bin',
                '/usr/bin',
                '/usr/sbin',
                '/opt/membase/bin',
                os.environ['PATH']]
        os.environ['PATH'] = ':'.join(path)
    elif os.name == 'nt':
        path = [mydir, management_dir, os.environ['PATH']]
        os.environ['PATH'] = ';'.join(path)

    runner = TaskRunner(verbosity=options.verbosity)
    for task in _tasks:
        runner.run(task)

    runner.zip(args[0])


if __name__ == '__main__':
    main()
Something went wrong with that request. Please try again.