Ejemplo n.º 1
0
def main():
    args = parse_args()
    if args.all or not args.host:
        ans = hostinfo_get('alias')
    else:
        ans = hostinfo_get('host/{}/alias'.format(args.host))
    aliases = ans['aliases']
    output = []
    for alias in aliases:
        output.append("%s %s" % (alias['alias'], alias['host']))
    print "\n".join(output)
    return 0
Ejemplo n.º 2
0
def DisplayCSV(matches, args):
    """Display hosts and other printables in CSV format
    """
    output = []
    if args.showall:
        columns = []
        data = hostinfo_get('key')
        for k in data['keys']:
            columns.append(k['key'])
        columns.sort()
    else:
        columns = args.printout[:]

    if args.header:
        output.append("hostname{}{}".format(args.sep[0],
                                            args.sep[0].join(columns)))

    for host in matches:
        outline = "{}".format(host['hostname'])
        for p in columns:
            outline += args.sep[0]
            if p in host['keyvalues']:
                vals = sorted(host['keyvalues'][p], key=lambda x: x['value'])
            else:
                vals = ''
            outline += '"{}"'.format(args.sep[0].join(
                [c['value'] for c in vals]))

        output.append(outline)
    return "{}\n".format("\n".join(output))
Ejemplo n.º 3
0
def main():
    args = parse_args()
    alias = args.alias.lower()
    ans = hostinfo_get('host/{}'.format(alias))
    host = ans['host']['hostname']
    ans = hostinfo_delete('host/{}/alias/{}'.format(host, alias))
    return 0
Ejemplo n.º 4
0
def DisplayJson(matches, args):
    """ Display hosts and other printables in JSON format
    """
    import json
    if args.showall:
        columns = []
        data = hostinfo_get('key')
        for k in data['keys']:
            columns.append(k['key'])
        columns.sort()
    else:
        columns = args.printout[:]

    data = {}
    for host in matches:
        hname = host['hostname']
        data[hname] = {}
        for p in columns:
            if p not in host['keyvalues']:
                pass
            else:
                data[hname][p] = []
                vals = [c['value'] for c in host['keyvalues'][p]]
                if len(vals) == 1:
                    data[hname][p] = vals[0]
                else:
                    data[hname][p] = vals

    return json.dumps(data)
Ejemplo n.º 5
0
def main():
    args = parse_args()
    outstr = []
    data = hostinfo_get('key')
    keys = data['keys']

    if not keys:
        sys.stderr.write("No keys to show\n")
        sys.exit(1)

    for key in keys:
        if args.keylist:
            if key['key'] not in args.keylist:
                continue
        if args.typeflag:
            outstr.append("%s\t%s" % (key.key, key.get_validtype_display()))
        else:
            notes = "    "
            if key['restricted']:
                notes += "[KEY RESTRICTED]"
            if key['numeric']:
                notes += "[NUMERIC]"
            if key['readonly']:
                notes += "[KEY READ ONLY]"
            outstr.append("%s\t%s\t%s%s" %
                          (key['key'], key['validtype'], key['desc'], notes))
    print("\n".join(outstr))
    return (0)
Ejemplo n.º 6
0
def getHost(hostname, origin=False, times=False, keys=['*']):
    url = 'host/{}/'.format(hostname)
    options = {'keys': keys}
    if times:
        options['show_dates'] = times
    if origin:
        options['show_origin'] = origin
    data = hostinfo_get(url, params=options)
    return data['host']
Ejemplo n.º 7
0
def main():
    args = parse_args()
    key = args.key.lower()
    ans = hostinfo_get('rval/{}/'.format(key))
    if ans is None:
        sys.stderr.write('No key {} found\n'.format(key))
        return 1
    vals = [x['value'] for x in ans['restricted']]

    print "\n".join(sorted(vals))
    return 0
Ejemplo n.º 8
0
def main(argv):
    args = parse_args(argv)
    if args.valuereport:  # Does the query itself
        data = {'hosts': ['dummy']}
    else:
        if args.host:
            data = hostinfo_get('host/{}'.format(args.host[0]))
            data['hosts'] = data['host']
        else:
            if args.criteria:
                crit = "/".join(args.criteria)
                data = hostinfo_get('query/{}'.format(crit))
            else:
                data = hostinfo_get('host')
    output = Display(data['hosts'], args)
    if len(data['hosts']):
        retval = 0
    else:
        retval = 1
    sys.stdout.write("{}".format(output))
    return retval
Ejemplo n.º 9
0
def main():
    args = parse_args()
    host = args.host.lower()
    data = hostinfo_get('host/{}'.format(host))
    if not data:
        sys.stderr.write("Host {} doesn't exist\n".format(host))
        return (1)

    if not args.lethal:
        sys.stderr.write("Didn't do delete as no --lethal specified\n")
        return (0)

    data = hostinfo_delete('host/{}'.format(host))
    return (0)
Ejemplo n.º 10
0
def DisplayValuereport(matches, args):
    """ Display a report about the values a key has and how many hosts have
    that particular value
    """
    valkey = args.valuereport[0]
    data = hostinfo_get('keylist/{}/{}'.format(valkey,
                                               "/".join(args.criteria)))

    outstr = ""

    outstr += "{} set: {} {:.2f}%\n".format(valkey, data['numdef'],
                                            data['pctdef'])
    outstr += "{} unset: {} {:.2f}%\n".format(valkey, data['numundef'],
                                              data['pctundef'])
    outstr += "\n"
    for k, v, p in data['vallist']:
        outstr += "{} {} {:.2f}%\n".format(k, v, p)
    return outstr
Ejemplo n.º 11
0
def getAliases(hostname, args):
    """ Return a list of the aliases that host has """
    url = 'host/{}/alias'.format(hostname)
    data = hostinfo_get(url)
    aliases = [_['alias'] for _ in data['aliases']]
    return aliases