Beispiel #1
0
    def testDataSourceAgainstDevice(self, testDevice, REQUEST, write, errorLog):
        """
        Does the majority of the logic for testing a datasource against the device
        @param string testDevice The id of the device we are testing
        @param Dict REQUEST the browers request
        @param Function write The output method we are using to stream the result of the command
        @parma Function errorLog The output method we are using to report errors
        """
        out = REQUEST.RESPONSE
        # Determine which device to execute against
        device = None
        if testDevice:
            # Try to get specified device
            device = self.findDevice(testDevice)
            if not device:
                errorLog(
                    'No device found',
                    'Cannot find device matching %s.' % testDevice,
                    priority=messaging.WARNING
                )
                return self.callZenScreen(REQUEST)
        elif hasattr(self, 'device'):
            # ds defined on a device, use that device
            device = self.device()
        elif hasattr(self, 'getSubDevicesGen'):
            # ds defined on a device class, use any device from the class
            try:
                device = self.getSubDevicesGen().next()
            except StopIteration:
                # No devices in this class, bail out
                pass
        if not device:
            errorLog(
                'No Testable Device',
                'Cannot determine a device against which to test.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)

        # Get the command to run
        command = self.getCommand(device)
        header = ''
        footer = ''
        # Render
        if REQUEST.get('renderTemplate', True):
            header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')

        out.write(str(header))
        write("Executing command\n%s\n   against %s" % (command, device.id))
        write('')
        start = time.time()
        try:
            executeStreamCommand(command, write)
        except:
            write('exception while executing command')
            write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
        write('')
        write('')
        write('DONE in %s seconds' % long(time.time() - start))
        out.write(str(footer))
 def doMailTx(self, device, write):
     """ Test the MAILTX data source
     """
     import ZenPacks.zenoss.ZenMailTx
     # run the command in a separate process
     cmd = '%s %s/Mail.py -d "%s" -s "%s"' % (
         "$ZENHOME/bin/python", ZenPacks.zenoss.ZenMailTx.zpDir, device, self.id)
     executeStreamCommand(cmd, write)
 def run(self):
     device, dsName = self.options.device, self.options.dsName
     if not (device and dsName):
         self.write("Must provide a device and datasource.")
         sys.exit(2)
     cmd = self.getCommand(device, dsName)
     if self.usessh:
         executeSshCommand(device, cmd, self.write)
     else:
         executeStreamCommand(cmd, self.write)
 def run(self):
     device, dsName = self.options.device, self.options.dsName
     if not (device and dsName):
         self.write("Must provide a device and datasource.")
         sys.exit(2)
     cmd = self.getCommand(device, dsName)
     if self.usessh:
         executeSshCommand(device, cmd, self.write)
     else:
         executeStreamCommand(cmd, self.write)
 def stream(self):
     daemons=[d['name'] for d in self.context.dmd.About.getZenossDaemonStates() \
             if d['msg'] == 'Up' and d['name'] not in DEFAULT_MASTER_DAEMONS]
     if 'zenrrdcached' not in daemons:
         daemons.append('zenrrdcached')
     if 'zenrender' not in daemons:
         daemons.append('zenrender')
     if os.path.exists(MASTER_DAEMON_LIST_FILE):
         for line in fileinput.input(MASTER_DAEMON_LIST_FILE):
             line = line.strip()
             if line in daemons:
                 daemons.remove(line)
     df = open('%s/daemons.txt' % zpDir, 'w')
     df.write('%s\n' % '\n'.join(daemons))
     df.close()
     data = unjson(self.request.get('data'))
     ids = data['uids']
     if not ids:
         self.write('No Remote Collectors were selected')
     command = data['command']
     if command == 'add':
         new_id = ids[0]
         self.context.manage_addMonitor(new_id, submon='Performance')
         self.context.Performance[
             new_id].renderurl = 'http://%s:8091' % new_id
         transaction.commit()
     for id in ids:
         self.write('%s Remote Collector %s' % (command.capitalize(), id))
         if command in ('update', 'remove'):
             self.write('Stopping zenoss daemons')
             executeStreamCommand(
                 'ssh root@%s "/sbin/service zenoss stop"' % id,
                 self.write,
                 timeout=COMMAND_TIMEOUT)
         if command in ('add'):
             self.write('Copy Zenoss files to Remote Collector')
             self.write('....zenoss user')
             executeStreamCommand(
                 'rsync --rsh=ssh --recursive --times --owner --group --perms --links /home/zenoss/.bash* root@%s:%s'
                 % (id, '/home/zenoss'),
                 self.write,
                 timeout=COMMAND_TIMEOUT)
             self.write('....zenoss directory')
             executeStreamCommand(
                 'rsync --rsh=ssh --recursive --times --owner --group --perms --links --exclude=perf --exclude=backups --exclude=log --exclude=deps --exclude=lost+found --exclude=var %s root@%s:%s'
                 % (zenDir, id, '/opt'),
                 self.write,
                 timeout=COMMAND_TIMEOUT)
             executeStreamCommand(
                 'ssh -C root@%s "mkdir /opt/zenoss/var; mkdir /opt/zenoss/perf; mkdir /opt/zenoss/log;"'
                 % (id),
                 self.write,
                 timeout=COMMAND_TIMEOUT)
             self.write('....init script')
             executeStreamCommand(
                 'find %s -print | cpio -oc | ssh -C root@%s "cd / && cpio -ic 2>/dev/null"'
                 % (zenScript, id),
                 self.write,
                 timeout=COMMAND_TIMEOUT)
             executeStreamCommand(
                 'ssh -C root@%s "/sbin/chkconfig --add zenoss --level 345"'
                 % (id),
                 self.write,
                 timeout=COMMAND_TIMEOUT)
             self.write('....zenoss sockets')
             executeStreamCommand(
                 'ssh -C root@%s "chmod +s /opt/zenoss/bin/zensocket /opt/zenoss/bin/pyraw /opt/zenoss/bin/nmap; chown zenoss:zenoss /opt/zenoss/var /opt/zenoss/perf /opt/zenoss/log -Rf"'
                 % (id),
                 self.write,
                 timeout=COMMAND_TIMEOUT)
             self.write('Update Remote Collector configuration')
             executeStreamCommand('ssh %s %s %s %s' %
                                  (id, updConfBin, socket.getfqdn(), id),
                                  self.write,
                                  timeout=COMMAND_TIMEOUT)
             self.write('Starting zenoss daemons')
             executeStreamCommand(
                 'ssh root@%s "/sbin/service zenoss start"' % id,
                 self.write,
                 timeout=COMMAND_TIMEOUT)
         if command in ('update'):
             self.write('Revert Remote Collector configuration')
             executeStreamCommand('ssh %s %s localhost localhost' %
                                  (id, updConfZenBin),
                                  self.write,
                                  timeout=COMMAND_TIMEOUT)
             self.write('Remove ZenPacks files from Remote Collector')
             executeStreamCommand('ssh %s rm -fr %s' % (id, zpDir),
                                  self.write,
                                  timeout=COMMAND_TIMEOUT)
             ##TODO: Don't assume the stuff is already there
             self.write('Copy Zenoss files to Remote Collector')
             self.write('....just zenpacks')
             executeStreamCommand(
                 'find %s -print | cpio -oc | ssh -C %s "cd / && cpio -ic 2>/dev/null"'
                 % (zpDir, id),
                 self.write,
                 timeout=COMMAND_TIMEOUT)
             self.write('Update Remote Collector configuration')
             executeStreamCommand('ssh %s %s %s %s' %
                                  (id, updConfBin, socket.getfqdn(), id),
                                  self.write,
                                  timeout=COMMAND_TIMEOUT)
             self.write('Starting zenoss daemons')
             executeStreamCommand(
                 'ssh root@%s "/sbin/service zenoss start"' % id,
                 self.write,
                 timeout=COMMAND_TIMEOUT)
     if command == 'remove':
         self.write(
             'Notice: This does not delete Zenoss from the remote machine!')
         self.write('Disable Zenoss startup')
         executeStreamCommand(
             'ssh -C root@%s "/sbin/chkconfig zenoss off"' % (id),
             self.write,
             timeout=COMMAND_TIMEOUT)
         self.context.manage_removeMonitor(ids=ids, submon='Performance')
         transaction.commit()
     self.write('All Tasks Finished')
     os.unlink('%s/daemons.txt' % zpDir)
def setupRemoteMonitors(ids, templ, REQUEST=None, install=None, remove=None):
    if REQUEST and VERSION < '2.6':
        out = REQUEST.RESPONSE
    else: out = blackhole()
    def write(lines):
        ''' Output (maybe partial) result text.
        '''
        startLine = '<tr><td class="tablevalues">'
        endLine = '</td></tr>\n'
        if out:
            if not isinstance(lines, list):
                lines = [lines]
            for l in lines:
                if not isinstance(l, str):
                    l = str(l)
                l = l.strip()
                l = cgi.escape(l)
                l = l.replace('\n', endLine + startLine)
                out.write(startLine + l + endLine)

    header, footer = templ.split('OUTPUT_TOKEN')
    out.write(str(header))
    for id in ids:
        write('Remote Collector %s'%id)
        write('Stopping zenoss daemons')
        executeStreamCommand('ssh %s "zenoss stop"'%id, write, timeout=240)
        if remove:
            write('Revert Remote Collector configuration')
            executeStreamCommand('ssh %s %s localhost localhost'%(id,
                                            updConfZenBin), write, timeout=240)
        write('Remove ZenPacks files from Remote Collector')
        executeStreamCommand('ssh %s rm -fr %s'%(id, zpDir), write, timeout=240)
        if install:
            write('Copy ZenPacks files to Remote Collector')
#  Copy ZenPacks files with scp
#            executeStreamCommand('scp -r %s %s:%s'%(zpDir, id, zpDir), write,
#                                                                timeout=240)
#  Copy ZenPacks files with cpio compression
            executeStreamCommand('find %s -print | cpio -oc | ssh -C %s "cd / && cpio -ic 2>/dev/null"'%(
                                            zpDir, id), write, timeout=240)
            write('Update Remote Collector configuration')
            executeStreamCommand('ssh %s %s %s %s'%(id, updConfBin,
                                    socket.getfqdn(), id), write, timeout=240)
        write('Starting zenoss daemons')
        executeStreamCommand('ssh %s "zenoss start"'%id, write, timeout=240)
        write('Finish')
    out.write(str(footer))
    def stream(self):
        daemons=[d['name'] for d in self.context.dmd.About.getZenossDaemonStates() \
                if d['msg'] == 'Up' and d['name'] not in DEFAULT_MASTER_DAEMONS]
        if 'zeneventserver' in daemons:
            daemons.remove('zeneventserver') 
        if 'zenrrdcached' not in daemons:
            daemons.append('zenrrdcached')
        if 'zenrender' not in daemons:
            daemons.append('zenrender')
        if os.path.exists(MASTER_DAEMON_LIST_FILE):
            for line in fileinput.input(MASTER_DAEMON_LIST_FILE):
                line = line.strip()
                if line in daemons:
                    daemons.remove(line)
        df = open('%s/daemons.txt'%zpDir, 'w')
        df.write('%s\n'%'\n'.join(daemons))
        df.close()
        data = unjson(self.request.get('data'))
        ids = data['uids']
        if not ids:
            self.write('No Remote Collectors were selected')
        command = data['command']
        if command == 'add':
            new_id = ids[0]
            self.context.manage_addMonitor(new_id, submon='Performance')
#            self.context.Performance[new_id].renderurl = 'http://%s:8090/%s'%(
#                                                    socket.getfqdn(), new_id)
            self.context.Performance[new_id].renderurl='http://%s:8091' % new_id
            transaction.commit()
        for id in ids:
            self.write('%s Remote Collector %s'%(command.capitalize(), id))
            self.write('Stopping zenoss daemons')
            executeStreamCommand('ssh %s "zenoss stop"'%id,
                                        self.write, timeout=COMMAND_TIMEOUT)
            if command in ('update', 'remove'):
                self.write('Revert Remote Collector configuration')
                executeStreamCommand('ssh %s %s localhost localhost'%(id,
                            updConfZenBin), self.write, timeout=COMMAND_TIMEOUT)
            self.write('Remove ZenPacks files from Remote Collector')
            executeStreamCommand('ssh %s rm -fr %s'%(id, zpDir),
                                            self.write, timeout=COMMAND_TIMEOUT)
            if command in ('add', 'update'):
                self.write('Copy ZenPacks files to Remote Collector')
                executeStreamCommand('find %s -print | cpio -oc | ssh -C %s "cd / && cpio -ic 2>/dev/null"'%(
                                zpDir, id), self.write, timeout=COMMAND_TIMEOUT)
                self.write('Update Remote Collector configuration')
                executeStreamCommand('ssh %s %s %s %s'%(id, updConfBin,
                    socket.getfqdn(), id), self.write, timeout=COMMAND_TIMEOUT)
            self.write('Starting zenoss daemons')
            executeStreamCommand('ssh %s "zenoss start"'%id,
                    self.write, timeout=COMMAND_TIMEOUT)
        if command == 'remove':
            self.context.manage_removeMonitor(ids=ids, submon='Performance')
            transaction.commit()
        self.write('All Tasks Finished')
        os.unlink('%s/daemons.txt'%zpDir)
Beispiel #8
0
    def testDataSourceAgainstDevice(self, testDevice, REQUEST, write, errorLog):
        """
        Does the majority of the logic for testing a datasource against the device
        @param string testDevice The id of the device we are testing
        @param Dict REQUEST the browers request
        @param Function write The output method we are using to stream the result of the command
        @parma Function errorLog The output method we are using to report errors
        """
        out = REQUEST.RESPONSE
        # Determine which device to execute against
        device = None
        if testDevice:
            # Try to get specified device
            device = self.findDevice(testDevice)
            if not device:
                errorLog(
                    'No device found',
                    'Cannot find device matching %s.' % testDevice,
                    priority=messaging.WARNING
                )
                return self.callZenScreen(REQUEST)
        elif hasattr(self, 'device'):
            # ds defined on a device, use that device
            device = self.device()
        elif hasattr(self, 'getSubDevicesGen'):
            # ds defined on a device class, use any device from the class
            try:
                device = self.getSubDevicesGen().next()
            except StopIteration:
                # No devices in this class, bail out
                pass
        if not device:
            errorLog(
                'No Testable Device',
                'Cannot determine a device against which to test.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)

        # Get the command to run
        command = None
        if self.sourcetype=='COMMAND':
            # create self.command to compile a command for zminion
            # it's only used by the commandable mixin
            self.command = self.commandTemplate
            zminionCommand = self.compile(self, device)
            # to prevent command injection, get these from self rather than the browser REQUEST
            command = self.getCommand(device, self.get('commandTemplate'))
            displayCommand = command
            if displayCommand and len(displayCommand.split()) > 1:
                displayCommand = "%s [args omitted]" % displayCommand.split()[0]
        elif self.sourcetype=='SNMP':
            snmpinfo = copy(device.getSnmpConnInfo().__dict__)
            if snmpinfo.get('zSnmpCommunity', None):
                # escape dollar sign if any by $ as it's used in zope templating system
                snmpinfo['zSnmpCommunity'] = escapeSpecChars(snmpinfo['zSnmpCommunity']).replace("$", "$$")
            # use the oid from the request or our existing one
            snmpinfo['oid'] = self.get('oid', self.getDescription())
            command = SnmpCommand(snmpinfo)
            displayCommand = command.display.replace("\\", "").replace("$$", "$")
            # modify snmp command to be run with zminion
            zminionCommand = self.compile(command, device)
        else:
            errorLog(
                'Test Failed',
                'Unable to test %s datasources' % self.sourcetype,
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)
        if not command:
            errorLog(
                'Test Failed',
                'Unable to create test command.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)
        header = ''
        footer = ''
        # Render
        if REQUEST.get('renderTemplate', True):
            header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')

        out.write(str(header))

        write("Executing command\n%s\n   against %s" % (displayCommand, device.id))
        write('')
        start = time.time()
        remoteCollector = device.getPerformanceServer().id != 'localhost'
        try:
            if self.usessh:
                if remoteCollector:
                    # if device is on remote collector modify command to be run via zenhub
                    self.command = "/opt/zenoss/bin/zentestds run --device {} --cmd '{}'".format(
                        device.manageIp, self.commandTemplate)
                    zminionCommand = self.compile(self, device)
                    # zminion executes call back to zenhub
                    executeStreamCommand(zminionCommand, write)
                else:
                    executeSshCommand(device, command, write)
            else:
                executeStreamCommand(zminionCommand, write)
        except:
            import sys
            write('exception while executing command')
            write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
        write('')
        write('')
        write('DONE in %s seconds' % long(time.time() - start))
        out.write(str(footer))
    def testDataSourceAgainstDevice(self, testDevice, REQUEST, write, errorLog):
        """
        Does the majority of the logic for testing a datasource against the device
        @param string testDevice The id of the device we are testing
        @param Dict REQUEST the browers request
        @param Function write The output method we are using to stream the result of the command
        @parma Function errorLog The output method we are using to report errors
        """
        out = REQUEST.RESPONSE
        # Determine which device to execute against
        device = None
        if testDevice:
            # Try to get specified device
            device = self.findDevice(testDevice)
            if not device:
                errorLog(
                    'No device found',
                    'Cannot find device matching %s.' % testDevice,
                    priority=messaging.WARNING
                )
                return self.callZenScreen(REQUEST)
        elif hasattr(self, 'device'):
            # ds defined on a device, use that device
            device = self.device()
        elif hasattr(self, 'getSubDevicesGen'):
            # ds defined on a device class, use any device from the class
            try:
                device = self.getSubDevicesGen().next()
            except StopIteration:
                # No devices in this class, bail out
                pass
        if not device:
            errorLog(
                'No Testable Device',
                'Cannot determine a device against which to test.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)

        # Get the command to run
        command = None
        if self.sourcetype=='COMMAND':
            # to prevent command injection, get these from self rather than the browser REQUEST
            command = self.getCommand(device, self.get('commandTemplate'))           
            displayCommand = command
            if displayCommand and len(displayCommand.split()) > 1:
                displayCommand = "%s [args omitted]" % displayCommand.split()[0]
        elif self.sourcetype=='SNMP':
            snmpinfo = copy(device.getSnmpConnInfo().__dict__)
            # use the oid from the request or our existing one
            snmpinfo['oid'] = self.get('oid', self.getDescription())
            command = snmptemplate % snmpinfo
            displayCommand = command
        else:
            errorLog(
                'Test Failed',
                'Unable to test %s datasources' % self.sourcetype,
                priority=messaging.WARNING  
            )
            return self.callZenScreen(REQUEST)
        if not command:
            errorLog(
                'Test Failed',
                'Unable to create test command.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)
        header = ''
        footer = ''
        # Render
        if REQUEST.get('renderTemplate', True):
            header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')

        out.write(str(header))

        write("Executing command\n%s\n   against %s" % (displayCommand, device.id))
        write('')
        start = time.time()
        try:
            executeStreamCommand(command, write)
        except:
            import sys
            write('exception while executing command')
            write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
        write('')
        write('')
        write('DONE in %s seconds' % long(time.time() - start))
        out.write(str(footer))
Beispiel #10
0
def setupRemoteMonitors(ids, templ, REQUEST=None, install=None, remove=None):
    if REQUEST and VERSION < '2.6':
        out = REQUEST.RESPONSE
    else: out = blackhole()
    def write(lines):
        ''' Output (maybe partial) result text.
        '''
        startLine = '<tr><td class="tablevalues">'
        endLine = '</td></tr>\n'
        if out:
            if not isinstance(lines, list):
                lines = [lines]
            for l in lines:
                if not isinstance(l, str):
                    l = str(l)
                l = l.strip()
                l = cgi.escape(l)
                l = l.replace('\n', endLine + startLine)
                out.write(startLine + l + endLine)

    header, footer = templ.split('OUTPUT_TOKEN')
    out.write(str(header))
    for id in ids:
        write('Remote Collector %s'%id)
        write('Stopping zenoss daemons')
        executeStreamCommand('ssh %s "zenoss stop"'%id, write, timeout=240)
        if remove:
            write('Revert Remote Collector configuration')
            executeStreamCommand('ssh %s %s localhost localhost'%(id,
                                            updConfZenBin), write, timeout=240)
        write('Remove ZenPacks files from Remote Collector')
        executeStreamCommand('ssh %s rm -fr %s'%(id, zpDir), write, timeout=240)
        if install:
            write('Copy ZenPacks files to Remote Collector')
#  Copy ZenPacks files with scp
#            executeStreamCommand('scp -r %s %s:%s'%(zpDir, id, zpDir), write,
#                                                                timeout=240)
#  Copy ZenPacks files with cpio compression
            executeStreamCommand('find %s -print | cpio -oc | ssh -C %s "cd / && cpio -ic 2>/dev/null"'%(
                                            zpDir, id), write, timeout=240)
            write('Update Remote Collector configuration')
            executeStreamCommand('ssh %s %s %s %s'%(id, updConfBin,
                                    socket.getfqdn(), id), write, timeout=240)
        write('Starting zenoss daemons')
        executeStreamCommand('ssh %s "zenoss start"'%id, write, timeout=240)
        write('Finish')
    out.write(str(footer))
    def testDataSourceAgainstDevice(self, testDevice, REQUEST, write, errorLog):
        """
        Does the majority of the logic for testing a datasource against the device
        @param string testDevice The id of the device we are testing
        @param Dict REQUEST the browers request
        @param Function write The output method we are using to stream the result of the command
        @parma Function errorLog The output method we are using to report errors
        """ 
        out = REQUEST.RESPONSE
        # Determine which device to execute against
        device = None
        if testDevice:
            # Try to get specified device
            device = self.findDevice(testDevice)
            if not device:
                errorLog(
                    'No device found',
                    'Cannot find device matching %s.' % testDevice,
                    priority=messaging.WARNING
                )
                return self.callZenScreen(REQUEST)
        elif hasattr(self, 'device'):
            # ds defined on a device, use that device
            device = self.device()
        elif hasattr(self, 'getSubDevicesGen'):
            # ds defined on a device class, use any device from the class
            try:
                device = self.getSubDevicesGen().next()
            except StopIteration:
                # No devices in this class, bail out
                pass
        if not device:
            errorLog(
                'No Testable Device',
                'Cannot determine a device against which to test.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)

        snmpinfo = copy(device.getSnmpConnInfo().__dict__)
        # use the oid from the request or our existing one
        snmpinfo['oid'] = REQUEST.get('oid', self.getDescription())
        command = snmptemplate % snmpinfo
        return self.callZenScreen(REQUEST)

        if not command:
            errorLog(
                'Test Failed',
                'Unable to create test command.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)
        header = ''
        footer = ''
        # Render
        if REQUEST.get('renderTemplate', True):
            header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')
            
        out.write(str(header))
            
        write("Executing command\n%s\n   against %s" % (command, device.id))
        write('')
        start = time.time()
        try:
            executeStreamCommand(command, write)
        except:
            import sys
            write('exception while executing command')
            write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
        write('')
        write('')
        write('DONE in %s seconds' % long(time.time() - start))
        out.write(str(footer))
Beispiel #12
0
    def testDataSourceAgainstDevice(self, testDevice, REQUEST, write,
                                    errorLog):
        """
        Does the majority of the logic for testing a datasource against the device
        @param string testDevice The id of the device we are testing
        @param Dict REQUEST the browers request
        @param Function write The output method we are using to stream the result of the command
        @parma Function errorLog The output method we are using to report errors
        """
        out = REQUEST.RESPONSE
        # Determine which device to execute against
        device = None
        if testDevice:
            # Try to get specified device
            device = self.findDevice(testDevice)
            if not device:
                errorLog('No device found',
                         'Cannot find device matching %s.' % testDevice,
                         priority=messaging.WARNING)
                return self.callZenScreen(REQUEST)
        elif hasattr(self, 'device'):
            # ds defined on a device, use that device
            device = self.device()
        elif hasattr(self, 'getSubDevicesGen'):
            # ds defined on a device class, use any device from the class
            try:
                device = self.getSubDevicesGen().next()
            except StopIteration:
                # No devices in this class, bail out
                pass
        if not device:
            errorLog('No Testable Device',
                     'Cannot determine a device against which to test.',
                     priority=messaging.WARNING)
            return self.callZenScreen(REQUEST)

        header = ''
        footer = ''
        # Render
        if REQUEST.get('renderTemplate', True):
            header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')

        out.write(str(header))

        try:
            import sys
            cs = self.getConnectionString(device)
            sql = self.getCommand(device, self.sql).replace('$', '\\$')
            properties = dict([(dp.getAliasNames() and dp.getAliasNames()[0]
                                or dp.id, dp.id)
                               for dp in self.getRRDDataPoints()])
            write('Executing query: "%s"' % sql)
            write('')
            zp = self.dmd.ZenPackManager.packs._getOb(
                'ZenPacks.community.SQLDataSource', None)
            command = "env PYTHONPATH=\"%s\" python %s -c \"%s\" -q \"%s\" -f \"%s\" -a \"%s\"" % (
                os.pathsep.join(sys.path), zp.path('SQLClient.py'), cs, sql,
                " ".join(properties.keys()), " ".join(properties.values()))
            start = time.time()
            executeStreamCommand(command, write)
        except:
            import sys
            write('exception while executing command')
            write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
        write('')
        write('')
        write('DONE in %s seconds' % long(time.time() - start))
        out.write(str(footer))
    def testDataSourceAgainstDevice(self, testDevice, REQUEST, write, errorLog):
        """
        Does the majority of the logic for testing a datasource against the device
        @param string testDevice The id of the device we are testing
        @param Dict REQUEST the browers request
        @param Function write The output method we are using to stream the result of the command
        @parma Function errorLog The output method we are using to report errors
        """ 
        out = REQUEST.RESPONSE
        # Determine which device to execute against
        device = None
        if testDevice:
            # Try to get specified device
            device = self.findDevice(testDevice)
            if not device:
                errorLog(
                    'No device found',
                    'Cannot find device matching %s.' % testDevice,
                    priority=messaging.WARNING
                )
                return self.callZenScreen(REQUEST)
        elif hasattr(self, 'device'):
            # ds defined on a device, use that device
            device = self.device()
        elif hasattr(self, 'getSubDevicesGen'):
            # ds defined on a device class, use any device from the class
            try:
                device = self.getSubDevicesGen().next()
            except StopIteration:
                # No devices in this class, bail out
                pass
        if not device:
            errorLog(
                'No Testable Device',
                'Cannot determine a device against which to test.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)

        header = ''
        footer = ''
        # Render
        if REQUEST.get('renderTemplate', True):
            header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')

        out.write(str(header))

        try:
            import sys
            cs = self.getConnectionString(device)
            sql = RRDDataSource.RRDDataSource.getCommand(self, device, self.sql)
            properties = dict([(
                        dp.getAliasNames() and dp.getAliasNames()[0] or dp.id,
                        dp.id) for dp in self.getRRDDataPoints()])
            write('Executing query: "%s"'%sql)
            write('')
            zp = self.dmd.ZenPackManager.packs._getOb(
                                    'ZenPacks.community.SQLDataSource', None)
            command = "env PYTHONPATH=\"%s\" python %s -c \"%s\" -q \"%s\" -f \"%s\" -a \"%s\""%(
                                                os.pathsep.join(sys.path),
                                                zp.path('SQLClient.py'),cs,sql,
                                                " ".join(properties.keys()),
                                                " ".join(properties.values()))
            start = time.time()
            executeStreamCommand(command, write)
        except:
            import sys
            write('exception while executing command')
            write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
        write('')
        write('')
        write('DONE in %s seconds' % long(time.time() - start))
        out.write(str(footer))
    def testDataSourceAgainstDevice(self, testDevice, REQUEST, write,
                                    errorLog):

        out = REQUEST.RESPONSE
        # Determine which device to execute against
        device = None
        if testDevice:
            # Try to get specified device
            device = self.findDevice(testDevice)
            if not device:
                errorLog('No device found',
                         'Cannot find device matching %s' % testDevice,
                         priority=messaging.WARNING)
                return self.callZenScreen(REQUEST)
        elif hasattr(self, 'device'):
            # ds defined on a device, use that device
            device = self.device()
        elif hasattr(self, 'getSubDevicesGen'):
            # ds defined on a device class, use any device from the class
            try:
                device = self.getSubDevicesGen().next()
            except StopIteration:
                # No devices in this class, bail out
                pass
        if not device:
            errorLog('No Testable Device',
                     'Cannot determine a device to test against.',
                     priority=messaging.WARNING)
            return self.callZenScreen(REQUEST)

        header = ''
        footer = ''
        # Render
        if REQUEST.get('renderTemplate', True):
            header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')

        out.write(str(header))

        # Get the command to run
        command = None
        if self.sourcetype == 'VMware':
            command = self.getCommand(device)
        else:
            errorLog('Test Failure',
                     'Unable to test %s datasources' % self.sourcetype,
                     priority=messaging.WARNING)
            return self.callZenScreen(REQUEST)
        if not command:
            errorLog('Test Failure',
                     'Unable to create test command.',
                     priority=messaging.WARNING)
            return self.callZenScreen(REQUEST)

        write('Executing command %s against %s' % (command, device.id))
        write('')
        start = time.time()
        try:
            executeStreamCommand(command, write)
        except:
            import sys
            write('exception while executing command')
            write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
        write('')
        write('')
        write('DONE in %s seconds' % long(time.time() - start))
        out.write(str(footer))
 def stream(self):
     daemons = [
         d["name"]
         for d in self.context.dmd.About.getZenossDaemonStates()
         if d["msg"] == "Up" and d["name"] not in DEFAULT_MASTER_DAEMONS
     ]
     if "zeneventserver" in daemons:
         daemons.remove("zeneventserver")
         if "zenrrdcached" not in daemons:
             daemons.append("zenrrdcached")
     if os.path.exists(MASTER_DAEMON_LIST_FILE):
         for line in fileinput.input(MASTER_DAEMON_LIST_FILE):
             line = line.strip()
             if line in daemons:
                 daemons.remove(line)
     df = open("%s/daemons.txt" % zpDir, "w")
     df.write("%s\n" % "\n".join(daemons))
     df.close()
     data = unjson(self.request.get("data"))
     ids = data["uids"]
     if not ids:
         self.write("No Remote Collectors were selected")
     command = data["command"]
     if command == "add":
         new_id = ids[0]
         self.context.manage_addMonitor(new_id, submon="Performance")
         #            self.context.Performance[new_id].renderurl = 'http://%s:8090/%s'%(
         #                                                    socket.getfqdn(), new_id)
         self.context.Performance[new_id].renderurl = "http://%s:8091" % new_id
         transaction.commit()
     for id in ids:
         self.write("%s Remote Collector %s" % (command.capitalize(), id))
         self.write("Stopping zenoss daemons")
         executeStreamCommand('ssh %s "zenoss stop"' % id, self.write, timeout=COMMAND_TIMEOUT)
         if command in ("update", "remove"):
             self.write("Revert Remote Collector configuration")
             executeStreamCommand(
                 "ssh %s %s localhost localhost" % (id, updConfZenBin), self.write, timeout=COMMAND_TIMEOUT
             )
         self.write("Remove ZenPacks files from Remote Collector")
         executeStreamCommand("ssh %s rm -fr %s" % (id, zpDir), self.write, timeout=COMMAND_TIMEOUT)
         if command in ("add", "update"):
             self.write("Copy ZenPacks files to Remote Collector")
             executeStreamCommand(
                 'find %s -print | cpio -oc | ssh -C %s "cd / && cpio -ic 2>/dev/null"' % (zpDir, id),
                 self.write,
                 timeout=COMMAND_TIMEOUT,
             )
             self.write("Update Remote Collector configuration")
             executeStreamCommand(
                 "ssh %s %s %s %s" % (id, updConfBin, socket.getfqdn(), id), self.write, timeout=COMMAND_TIMEOUT
             )
         self.write("Starting zenoss daemons")
         executeStreamCommand('ssh %s "zenoss start"' % id, self.write, timeout=COMMAND_TIMEOUT)
     if command == "remove":
         self.context.manage_removeMonitor(ids=ids, submon="Performance")
         transaction.commit()
     self.write("All Tasks Finished")
     os.unlink("%s/daemons.txt" % zpDir)
    def stream(self):
        daemons=[d['name'] for d in self.context.dmd.About.getZenossDaemonStates() \
                if d['msg'] == 'Up' and d['name'] not in DEFAULT_MASTER_DAEMONS]
        if 'zeneventserver' in daemons:
            daemons.remove('zeneventserver') 
            if 'zenrrdcached' not in daemons:
                daemons.append('zenrrdcached') 
        if os.path.exists(MASTER_DAEMON_LIST_FILE):
            for line in fileinput.input(MASTER_DAEMON_LIST_FILE):
                line = line.strip()
                if line in daemons:
                    daemons.remove(line)
        df = open('%s/daemons.txt'%zpDir, 'w')
        df.write('%s\n'%'\n'.join(daemons))
        df.close()
        data = unjson(self.request.get('data'))
        ids = data['uids']
        if not ids:
            self.write('No Remote Collectors were selected')
        command = data['command']
        if command == 'add':
            new_id = ids[0]
            self.context.manage_addMonitor(new_id, submon='Performance')
#            self.context.Performance[new_id].renderurl = 'http://%s:8090/%s'%(
#                                                    socket.getfqdn(), new_id)
            self.context.Performance[new_id].renderurl='http://%s:8091' % new_id
            transaction.commit()
        for id in ids:
            self.write('%s Remote Collector %s'%(command.capitalize(), id))
            self.write('Stopping zenoss daemons')
            executeStreamCommand('ssh %s "zenoss stop"'%id,
                                        self.write, timeout=COMMAND_TIMEOUT)
            if command in ('update', 'remove'):
                self.write('Revert Remote Collector configuration')
                executeStreamCommand('ssh %s %s localhost localhost'%(id,
                            updConfZenBin), self.write, timeout=COMMAND_TIMEOUT)
            self.write('Remove ZenPacks files from Remote Collector')
            executeStreamCommand('ssh %s rm -fr %s'%(id, zpDir),
                                            self.write, timeout=COMMAND_TIMEOUT)
            if command in ('add', 'update'):
                self.write('Copy ZenPacks files to Remote Collector')
                executeStreamCommand('find %s -print | cpio -oc | ssh -C %s "cd / && cpio -ic 2>/dev/null"'%(
                                zpDir, id), self.write, timeout=COMMAND_TIMEOUT)
                self.write('Update Remote Collector configuration')
                executeStreamCommand('ssh %s %s %s %s'%(id, updConfBin,
                    socket.getfqdn(), id), self.write, timeout=COMMAND_TIMEOUT)
            self.write('Starting zenoss daemons')
            executeStreamCommand('ssh %s "zenoss start"'%id,
                    self.write, timeout=COMMAND_TIMEOUT)
        if command == 'remove':
            self.context.manage_removeMonitor(ids=ids, submon='Performance')
            transaction.commit()
        self.write('All Tasks Finished')
        os.unlink('%s/daemons.txt'%zpDir)
	def testDataSourceAgainstDevice(self, testDevice, REQUEST, write, errorLog):

		out = REQUEST.RESPONSE
		# Determine which device to execute against
		device = None
		if testDevice:
			# Try to get specified device
			device = self.findDevice(testDevice)
			if not device:
				errorLog(
					'No device found',
					'Cannot find device matching %s' % testDevice,
					priority=messaging.WARNING
				)
				return self.callZenScreen(REQUEST)
		elif hasattr(self, 'device'):
			# ds defined on a device, use that device
			device = self.device()
		elif hasattr(self, 'getSubDevicesGen'):
			# ds defined on a device class, use any device from the class
			try:
				device = self.getSubDevicesGen().next()
			except StopIteration:
				# No devices in this class, bail out
				pass
		if not device:
			errorLog(
				'No Testable Device',
				'Cannot determine a device to test against.',
				priority=messaging.WARNING
			)
			return self.callZenScreen(REQUEST)

                header = ''
                footer = ''
                # Render
                if REQUEST.get('renderTemplate', True):
                    header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')

                out.write(str(header))

		# Get the command to run
		command = None
		if self.sourcetype=='FORMULA':
			command = self.getCommand(device)
		else:
			errorLog(
                'Test Failure',
                'Unable to test %s datasources' % self.sourcetype,
                priority=messaging.WARNING
            )
			return self.callZenScreen(REQUEST)
		if not command:
			errorLog(
                'Test Failure',
                'Unable to create test command.',
                priority=messaging.WARNING
            )
			return self.callZenScreen(REQUEST)
		# Render
		header = ''
		footer = ''
		out.write(str(header))

		write("Calculating formula '%s' against %s" %(self.dataformula, device.id))
		write('')
		start = time.time()
		try:
			executeStreamCommand(command, write)
		except:
			import sys
			write('Exception while calculating formula')
			write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
		write('')
		write('')
		write('DONE in %s seconds' % long(time.time() - start))
		out.write(str(footer))
    def testDataSourceAgainstDevice(self, testDevice, REQUEST, write, errorLog):
        """
        Does the majority of the logic for testing a datasource against the device
        @param string testDevice The id of the device we are testing
        @param Dict REQUEST the browers request
        @param Function write The output method we are using to stream the result of the command
        @parma Function errorLog The output method we are using to report errors
        """ 
        out = REQUEST.RESPONSE
        # Determine which device to execute against
        device = None
        if testDevice:
            # Try to get specified device
            device = self.findDevice(testDevice)
            if not device:
                errorLog(
                    'No device found',
                    'Cannot find device matching %s.' % testDevice,
                    priority=messaging.WARNING
                )
                return self.callZenScreen(REQUEST)
        elif hasattr(self, 'device'):
            # ds defined on a device, use that device
            device = self.device()
        elif hasattr(self, 'getSubDevicesGen'):
            # ds defined on a device class, use any device from the class
            try:
                device = self.getSubDevicesGen().next()
            except StopIteration:
                # No devices in this class, bail out
                pass
        if not device:
            errorLog(
                'No Testable Device',
                'Cannot determine a device against which to test.',
                priority=messaging.WARNING
            )
            return self.callZenScreen(REQUEST)

        header = ''
        footer = ''
        # Render
        if REQUEST.get('renderTemplate', True):
            header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')

        out.write(str(header))

        try:
            tr, inst, kb, namespace = self.getInstanceInfo(device)
            if not tr: raise
            inst = self.getCommand(device, self.wql)
            if inst.startswith("%s:"%namespace): inst = inst[len(namespace)+1:]
            properties = dict([(
                        dp.getAliasNames() and dp.getAliasNames()[0] or dp.id,
                        dp.id) for dp in self.getRRDDataPoints()])
            url = '//%%s%s/%s'%(device.zWmiProxy or device.manageIp, namespace)
            write('Get %s Instance %s from %s' % (tr, inst, str(url%'')))
            write('')
            creds = '%s:%s@'%(device.zWinUser, device.zWinPassword)
            zp = self.dmd.ZenPackManager.packs._getOb(
                                   'ZenPacks.community.%sDataSource'%tr, None)
            command = "python %s -c \"%s\" -q \'%s\' -f \"%s\" -a \"%s\""%(
                                                zp.path('%sClient.py'%tr),
                                                str(url%creds),
                                                inst.replace("'",'"'),
                                                " ".join(properties.keys()),
                                                " ".join(properties.values()))
            start = time.time()
            executeStreamCommand(command, write)
        except:
            import sys
            write('exception while executing command')
            write('type: %s  value: %s' % tuple(sys.exc_info()[:2]))
        write('')
        write('')
        write('DONE in %s seconds' % long(time.time() - start))
        out.write(str(footer))
 def stream(self):
     daemons=[d['name'] for d in self.context.dmd.About.getZenossDaemonStates() \
             if d['msg'] == 'Up' and d['name'] not in DEFAULT_MASTER_DAEMONS]
     if 'zenrrdcached' not in daemons:
         daemons.append('zenrrdcached')
     if 'zenrender' not in daemons:
         daemons.append('zenrender')
     if os.path.exists(MASTER_DAEMON_LIST_FILE):
         for line in fileinput.input(MASTER_DAEMON_LIST_FILE):
             line = line.strip()
             if line in daemons:
                 daemons.remove(line)
     df = open('%s/daemons.txt'%zpDir, 'w')
     df.write('%s\n'%'\n'.join(daemons))
     df.close()
     data = unjson(self.request.get('data'))
     ids = data['uids']
     if not ids:
         self.write('No Remote Collectors were selected')
     command = data['command']
     if command == 'add':
         new_id = ids[0]
         self.context.manage_addMonitor(new_id, submon='Performance')
         self.context.Performance[new_id].renderurl='http://%s:8091' % new_id
         transaction.commit()
     for id in ids:
         self.write('%s Remote Collector %s' % (command.capitalize(), id))
         if command in ('update', 'remove'):
             self.write('Stopping zenoss daemons')
             executeStreamCommand('ssh root@%s "/sbin/service zenoss stop"' % id,
                                         self.write, timeout=COMMAND_TIMEOUT)
         if command in ('add'):
             self.write('Copy Zenoss files to Remote Collector')
             self.write('....zenoss user')
             executeStreamCommand('rsync --rsh=ssh --recursive --times --owner --group --perms --links /home/zenoss/.bash* root@%s:%s' % (
                             id, '/home/zenoss'), self.write, timeout=COMMAND_TIMEOUT)
             self.write('....zenoss directory')
             executeStreamCommand('rsync --rsh=ssh --recursive --times --owner --group --perms --links --exclude=perf --exclude=backups --exclude=log --exclude=deps --exclude=lost+found --exclude=var %s root@%s:%s' % (
                             zenDir, id, '/opt'), self.write, timeout=COMMAND_TIMEOUT)
             executeStreamCommand('ssh -C root@%s "mkdir /opt/zenoss/var; mkdir /opt/zenoss/perf; mkdir /opt/zenoss/log;"' % (
                             id), self.write, timeout=COMMAND_TIMEOUT)
             self.write('....init script')
             executeStreamCommand('find %s -print | cpio -oc | ssh -C root@%s "cd / && cpio -ic 2>/dev/null"' % (
                             zenScript, id), self.write, timeout=COMMAND_TIMEOUT)
             executeStreamCommand('ssh -C root@%s "/sbin/chkconfig --add zenoss --level 345"' % (
                             id), self.write, timeout=COMMAND_TIMEOUT)
             self.write('....zenoss sockets')
             executeStreamCommand('ssh -C root@%s "chmod +s /opt/zenoss/bin/zensocket /opt/zenoss/bin/pyraw /opt/zenoss/bin/nmap; chown zenoss:zenoss /opt/zenoss/var /opt/zenoss/perf /opt/zenoss/log -Rf"' % (
                             id), self.write, timeout=COMMAND_TIMEOUT)
             self.write('Update Remote Collector configuration')
             executeStreamCommand('ssh %s %s %s %s' % (id, updConfBin,
                 socket.getfqdn(), id), self.write, timeout=COMMAND_TIMEOUT)
             self.write('Starting zenoss daemons')
             executeStreamCommand('ssh root@%s "/sbin/service zenoss start"' % id,
                     self.write, timeout=COMMAND_TIMEOUT)
         if command in ('update'):
             self.write('Revert Remote Collector configuration')
             executeStreamCommand('ssh %s %s localhost localhost' % (id,
                         updConfZenBin), self.write, timeout=COMMAND_TIMEOUT)
             self.write('Remove ZenPacks files from Remote Collector')
             executeStreamCommand('ssh %s rm -fr %s' % (id, zpDir),
                                         self.write, timeout=COMMAND_TIMEOUT)
             ##TODO: Don't assume the stuff is already there
             self.write('Copy Zenoss files to Remote Collector')
             self.write('....just zenpacks')
             executeStreamCommand('find %s -print | cpio -oc | ssh -C %s "cd / && cpio -ic 2>/dev/null"' % (
                             zpDir, id), self.write, timeout=COMMAND_TIMEOUT)
             self.write('Update Remote Collector configuration')
             executeStreamCommand('ssh %s %s %s %s'%(id, updConfBin,
                 socket.getfqdn(), id), self.write, timeout=COMMAND_TIMEOUT)
             self.write('Starting zenoss daemons')
             executeStreamCommand('ssh root@%s "/sbin/service zenoss start"' % id,
                     self.write, timeout=COMMAND_TIMEOUT)
     if command == 'remove':
         self.write('Notice: This does not delete Zenoss from the remote machine!')
         self.write('Disable Zenoss startup')
         executeStreamCommand('ssh -C root@%s "/sbin/chkconfig zenoss off"' % (
                         id), self.write, timeout=COMMAND_TIMEOUT)
         self.context.manage_removeMonitor(ids=ids, submon='Performance')
         transaction.commit()
     self.write('All Tasks Finished')
     os.unlink('%s/daemons.txt' % zpDir)