Пример #1
0
 def loadPrintoutCache(self, columns, matches=None):
     # Load all the information that we have been requested into a cache
     cache = {}
     for p in columns:
         getAK(p)
         cache[p] = {}
         allv = KeyValue.objects.filter(keyid=getAK(p).id).values()
         for val in allv:
             hostid = val['hostid_id']
             if matches and hostid not in matches:
                 continue
             try:
                 cache[p][hostid].append(val)
             except KeyError:
                 cache[p][hostid] = [val]
     return cache
Пример #2
0
 def loadPrintoutCache(self, columns, matches=None):
     # Load all the information that we have been requested into a cache
     cache = {}
     for p in columns:
         getAK(p)
         cache[p] = {}
         allv = KeyValue.objects.filter(keyid=getAK(p).id).values()
         for val in allv:
             hostid = val['hostid_id']
             if matches and hostid not in matches:
                 continue
             try:
                 cache[p][hostid].append(val)
             except KeyError:
                 cache[p][hostid] = [val]
     return cache
 def handle(self, namespace):
     m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue)
     if m:
         key = m.group('key').lower()
         value = m.group('value').lower()
     else:
         key = namespace.keyvalue.lower()
         value = ''
     keyid = getAK(key)
     for host in namespace.host:
         hostid = getHost(host)
         if not hostid:
             raise HostinfoException("Unknown host: %s" % host)
         if value:
             kvlist = KeyValue.objects.filter(hostid=hostid,
                                              keyid=keyid,
                                              value=value)
         else:
             kvlist = KeyValue.objects.filter(hostid=hostid, keyid=keyid)
         if not kvlist:
             raise HostinfoException("Host %s doesn't have key %s" %
                                     (host, key))
         else:
             for kv in kvlist:
                 try:
                     kv.delete(readonlychange=namespace.readonlyupdate)
                 except ReadonlyValueException:
                     raise HostinfoException(
                         "Cannot delete a readonly value")
     return None, 0
 def handle(self, namespace):
     m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue)
     if m:
         key = m.group('key').lower()
         value = m.group('value').lower()
     else:
         key = namespace.keyvalue.lower()
         value = ''
     keyid = getAK(key)
     for host in namespace.host:
         hostid = getHost(host)
         if not hostid:
             raise HostinfoException("Unknown host: %s" % host)
         if value:
             kvlist = KeyValue.objects.filter(hostid=hostid, keyid=keyid, value=value)
         else:
             kvlist = KeyValue.objects.filter(hostid=hostid, keyid=keyid)
         if not kvlist:
             raise HostinfoException("Host %s doesn't have key %s" % (host, key))
         else:
             for kv in kvlist:
                 try:
                     kv.delete(readonlychange=namespace.readonlyupdate)
                 except ReadonlyValueException:
                     raise HostinfoException("Cannot delete a readonly value")
     return None, 0
Пример #5
0
    def DisplayValuereport(self, matches):
        """ Display a report about the values a key has and how many hosts have
        that particular value
        """
        outstr = ""
        values = {}
        hostids = set()  # hostids that match the criteria
        getAK(self.namespace.valuereport[0])
        total = len(matches)
        if total == 0:
            return ""
        nummatch = 0
        kvlist = KeyValue.objects.filter(
            keyid__key=self.namespace.valuereport[0]).values_list(
                'hostid', 'value')

        for hostid, value in kvlist:
            if hostid not in matches:
                continue
            hostids.add(hostid)
            values[value] = values.get(value, 0) + 1
        nummatch = len(hostids)  # Number of hosts that match
        numundef = total - len(hostids)

        tmpvalues = []
        for k, v in values.items():
            p = 100.0 * v / nummatch
            tmpvalues.append((k, v, p))

        tmpvalues.sort()

        outstr += "%s set: %d %0.2f%%\n" % (self.namespace.valuereport[0],
                                            nummatch, 100.0 * nummatch / total)
        outstr += "%s unset: %d %0.2f%%\n" % (
            self.namespace.valuereport[0], numundef, 100.0 * numundef / total)
        outstr += "\n"
        for k, v, p in tmpvalues:
            outstr += "%s %d %0.2f%%\n" % (k, v, p)
        return outstr
Пример #6
0
    def DisplayValuereport(self, matches):
        """ Display a report about the values a key has and how many hosts have
        that particular value
        """
        outstr = ""
        values = {}
        hostids = set()   # hostids that match the criteria
        getAK(self.namespace.valuereport[0])
        total = len(matches)
        if total == 0:
            return ""
        nummatch = 0
        kvlist = KeyValue.objects.filter(
            keyid__key=self.namespace.valuereport[0]).values_list('hostid', 'value')

        for hostid, value in kvlist:
            if hostid not in matches:
                continue
            hostids.add(hostid)
            values[value] = values.get(value, 0)+1
        nummatch = len(hostids)     # Number of hosts that match
        numundef = total-len(hostids)

        tmpvalues = []
        for k, v in values.items():
            p = 100.0*v/nummatch
            tmpvalues.append((k, v, p))

        tmpvalues.sort()

        outstr += "%s set: %d %0.2f%%\n" % (self.namespace.valuereport[0], nummatch, 100.0 * nummatch / total)
        outstr += "%s unset: %d %0.2f%%\n" % (self.namespace.valuereport[0], numundef, 100.0 * numundef / total)
        outstr += "\n"
        for k, v, p in tmpvalues:
            outstr += "%s %d %0.2f%%\n" % (k, v, p)
        return outstr
Пример #7
0
    def handle(self, namespace):
        m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue[0])
        if not m:
            raise HostinfoException("Must be in key=value format, not %s" % namespace.keyvalue[0])
        key = m.group('key').lower()
        value = m.group('value').lower()
        keyid = getAK(key)
        if not namespace.hosts and not namespace.all:
            raise HostinfoException("Must specify a list of hosts or the --all flag")

        kvlist = KeyValue.objects.filter(keyid=keyid, value=value)
        for kv in kvlist:
            if (namespace.hosts and kv.hostid.hostname in namespace.hosts) or not namespace.hosts:
                if not namespace.kidding:
                    kv.value = namespace.newvalue[0]
                    kv.save()
                else:
                    sys.stderr.write("Would replace %s=%s with %s on %s\n" % (kv.keyid, kv.value, namespace.newvalue[0], kv.hostid))
        return None, 0
Пример #8
0
    def DisplayValuereport(self, matches):
        """ Display a report about the values a key has and how many hosts have
        that particular value
        """
        # TODO: Migrate to using calcKeylistVals
        outstr = ""
        values = defaultdict(int)
        hostids = set()   # hostids that match the criteria
        key = getAK(self.namespace.valuereport[0])
        total = len(matches)
        if total == 0:
            return ""
        nummatch = 0
        kvlist = KeyValue.objects.filter(
            keyid__key=self.namespace.valuereport[0]).values_list('hostid', 'value', 'numvalue')

        for hostid, value, numvalue in kvlist:
            hostids.add(hostid)
            if key.numericFlag and numvalue is not None:
                values[numvalue] += 1
            else:
                values[value] += 1
        nummatch = len(hostids)     # Number of hosts that match
        numundef = total-len(hostids)

        tmpvalues = []
        for k, v in values.items():
            p = 100.0*v/nummatch
            tmpvalues.append((k, v, p))

        tmpvalues.sort()

        outstr += "%s set: %d %0.2f%%\n" % (self.namespace.valuereport[0], nummatch, 100.0 * nummatch / total)
        outstr += "%s unset: %d %0.2f%%\n" % (self.namespace.valuereport[0], numundef, 100.0 * numundef / total)
        outstr += "\n"
        for k, v, p in tmpvalues:
            outstr += "%s %d %0.2f%%\n" % (k, v, p)
        return outstr
Пример #9
0
    def handle(self, namespace):
        m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue[0])
        if not m:
            raise HostinfoException("Must be in key=value format, not %s" %
                                    namespace.keyvalue[0])
        key = m.group('key').lower()
        value = m.group('value').lower()
        keyid = getAK(key)
        if not namespace.hosts and not namespace.all:
            raise HostinfoException(
                "Must specify a list of hosts or the --all flag")

        kvlist = KeyValue.objects.filter(keyid=keyid, value=value)
        for kv in kvlist:
            if (namespace.hosts and kv.hostid.hostname
                    in namespace.hosts) or not namespace.hosts:
                if not namespace.kidding:
                    kv.value = namespace.newvalue[0]
                    kv.save()
                else:
                    sys.stderr.write(
                        "Would replace %s=%s with %s on %s\n" %
                        (kv.keyid, kv.value, namespace.newvalue[0], kv.hostid))
        return None, 0
Пример #10
0
    def DisplayXML(self, matches):
        """Display hosts and other printables in XML format
        """
        from xml.sax.saxutils import escape, quoteattr
        outstr = ""

        if self.namespace.showall:
            columns = [k.key for k in AllowedKey.objects.all()]
            columns.sort()
        else:
            columns = self.printout[:]

        cache = self.loadPrintoutCache(columns, matches)
        outstr += "<hostinfo>\n"
        outstr += '  <query date="%s">%s</query>\n' % (time.ctime(), escape(" ".join(sys.argv)))
        for key in columns:
            k = getAK(key)
            outstr += "  <key>\n"
            outstr += "    <name>%s</name>\n" % escape(key)
            outstr += "    <type>%s</type>\n" % k.get_validtype_display()
            outstr += "    <readonlyFlag>%s</readonlyFlag>\n" % k.readonlyFlag
            outstr += "    <auditFlag>%s</auditFlag>\n" % k.auditFlag
            outstr += "    <numericFlag>%s</numericFlag>\n" % k.numericFlag
            outstr += "    <docpage>%s</docpage>\n" % k.docpage
            outstr += "    <desc>%s</desc>\n" % k.desc
            if k.restrictedFlag:
                outstr += "    <restricted>\n"
                rvlist = RestrictedValue.objects.filter(keyid__key=key)
                for rv in rvlist:
                    outstr += "        <value>%s</value>\n" % escape(rv.value)
                outstr += "    </restricted>\n"
            outstr += "  </key>\n"

        for host in matches:
            if self.namespace.aliases:
                aliaslist = getAliases(_hostcache[host].hostname)
            if self.namespace.origin:
                hostorigin = ' origin="%s" ' % _hostcache[host].origin
            else:
                hostorigin = ''
            if self.namespace.times:
                hostdates = ' modified="%s" created="%s" ' % (_hostcache[host].modifieddate, _hostcache[host].createdate)
            else:
                hostdates = ''
            outstr += '  <host docpage="%s" %s%s>\n' % (_hostcache[host].docpage, hostorigin, hostdates)
            outstr += "    <hostname>%s</hostname>\n" % escape(_hostcache[host].hostname)
            if self.namespace.aliases and aliaslist:
                outstr += "    <aliaslist>\n"
                for alias in aliaslist:
                    outstr += "      <alias>%s</alias>\n" % escape(alias)
                outstr += "    </aliaslist>\n"
            outstr += "    <data>\n"
            for p in columns:
                if host not in cache[p] or len(cache[p][host]) == 0:
                    pass
                else:
                    for c in cache[p][host]:
                        outstr += '      <confitem key="%s"' % p
                        if self.namespace.origin:
                            outstr += ' origin=%s' % quoteattr(c['origin'])
                        if self.namespace.times:
                            outstr += ' modified="%s" created="%s"' % (c['modifieddate'], c['createdate'])
                        outstr += '>%s</confitem>\n' % escape(c['value'])

            outstr += "    </data>\n"
            outstr += "  </host>\n"
        outstr += "</hostinfo>\n"
        return outstr
Пример #11
0
    def DisplayXML(self, matches):
        """Display hosts and other printables in XML format
        """
        from xml.sax.saxutils import escape, quoteattr
        outstr = ""

        if self.namespace.showall:
            columns = [k.key for k in AllowedKey.objects.all()]
            columns.sort()
        else:
            columns = self.printout[:]

        cache = self.loadPrintoutCache(columns, matches)
        outstr += "<hostinfo>\n"
        outstr += '  <query date="%s">%s</query>\n' % (
            time.ctime(), escape(" ".join(sys.argv)))
        for key in columns:
            k = getAK(key)
            outstr += "  <key>\n"
            outstr += "    <name>%s</name>\n" % escape(key)
            outstr += "    <type>%s</type>\n" % k.get_validtype_display()
            outstr += "    <readonlyFlag>%s</readonlyFlag>\n" % k.readonlyFlag
            outstr += "    <auditFlag>%s</auditFlag>\n" % k.auditFlag
            outstr += "    <docpage>%s</docpage>\n" % k.docpage
            outstr += "    <desc>%s</desc>\n" % k.desc
            if k.restrictedFlag:
                outstr += "    <restricted>\n"
                rvlist = RestrictedValue.objects.filter(keyid__key=key)
                for rv in rvlist:
                    outstr += "        <value>%s</value>\n" % escape(rv.value)
                outstr += "    </restricted>\n"
            outstr += "  </key>\n"

        for host in matches:
            if self.namespace.aliases:
                aliaslist = getAliases(_hostcache[host].hostname)
            if self.namespace.origin:
                hostorigin = ' origin="%s" ' % _hostcache[host].origin
            else:
                hostorigin = ''
            if self.namespace.times:
                hostdates = ' modified="%s" created="%s" ' % (
                    _hostcache[host].modifieddate, _hostcache[host].createdate)
            else:
                hostdates = ''
            outstr += '  <host docpage="%s" %s%s>\n' % (
                _hostcache[host].docpage, hostorigin, hostdates)
            outstr += "    <hostname>%s</hostname>\n" % escape(
                _hostcache[host].hostname)
            if self.namespace.aliases and aliaslist:
                outstr += "    <aliaslist>\n"
                for alias in aliaslist:
                    outstr += "      <alias>%s</alias>\n" % escape(alias)
                outstr += "    </aliaslist>\n"
            outstr += "    <data>\n"
            for p in columns:
                if host not in cache[p] or len(cache[p][host]) == 0:
                    pass
                else:
                    for c in cache[p][host]:
                        outstr += '      <confitem key="%s"' % p
                        if self.namespace.origin:
                            outstr += ' origin=%s' % quoteattr(c['origin'])
                        if self.namespace.times:
                            outstr += ' modified="%s" created="%s"' % (
                                c['modifieddate'], c['createdate'])
                        outstr += '>%s</confitem>\n' % escape(c['value'])

            outstr += "    </data>\n"
            outstr += "  </host>\n"
        outstr += "</hostinfo>\n"
        return outstr