| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# (c) 2011-2016 Dennis Kaarsemaker <[email protected]> |
| 4 |
# see COPYING for license details |
| 5 |
|
| 6 |
try: |
| 7 |
import ConfigParser |
| 8 |
except ImportError: |
| 9 |
import configparser as ConfigParser |
| 10 |
import hpilo |
| 11 |
import optparse |
| 12 |
import datetime |
| 13 |
import elasticsearch |
| 14 |
import multiprocessing |
| 15 |
import re |
| 16 |
import os |
| 17 |
import sys |
| 18 |
|
| 19 |
import servers # If this import fails, look at servers.py.example |
| 20 |
|
| 21 |
def main(): |
| 22 |
usage = """%prog [options]""" |
| 23 |
p = optparse.OptionParser(usage=usage) |
| 24 |
p.add_option("-l", "--login", dest="login", default=None, |
| 25 |
help="Username to access the iLO") |
| 26 |
p.add_option("-p", "--password", dest="password", default=None, |
| 27 |
help="Password to access the iLO") |
| 28 |
p.add_option("-c", "--config", dest="config", default="~/.ilo.conf", |
| 29 |
help="File containing authentication and config details", metavar="FILE") |
| 30 |
p.add_option("-t", "--timeout", dest="timeout", type="int", default=60, |
| 31 |
help="Timeout for iLO connections") |
| 32 |
p.add_option("-P", "--protocol", dest="protocol", choices=("http","raw"), default=None, |
| 33 |
help="Use the specified protocol instead of autodetecting") |
| 34 |
p.add_option("-d", "--debug", dest="debug", action="count", default=0, |
| 35 |
help="Output debug information, repeat to see all XML data") |
| 36 |
p.add_option("-o", "--port", dest="port", type="int", default=443, |
| 37 |
help="SSL port to connect to") |
| 38 |
p.add_option("-x", "--processes", dest="processes", type="int", default=50, |
| 39 |
help="How many servers to scan in parallel") |
| 40 |
|
| 41 |
opts, args = p.parse_args() |
| 42 |
|
| 43 |
if args: |
| 44 |
p.print_help() |
| 45 |
p.exit(1) |
| 46 |
|
| 47 |
if not os.path.exists(os.path.expanduser(opts.config)): |
| 48 |
p.error("Configuration file does not exist") |
| 49 |
|
| 50 |
config = ConfigParser.ConfigParser() |
| 51 |
config.read(os.path.expanduser(opts.config)) |
| 52 |
|
| 53 |
# Do we have login information |
| 54 |
login = None |
| 55 |
password = None |
| 56 |
if config.has_option('ilo', 'login'): |
| 57 |
login = config.get('ilo', 'login') |
| 58 |
if config.has_option('ilo', 'password'): |
| 59 |
password = config.get('ilo', 'password') |
| 60 |
if opts.login: |
| 61 |
login = opts.login |
| 62 |
if opts.password: |
| 63 |
password = opts.password |
| 64 |
if not login or not password: |
| 65 |
p.error("No login details provided") |
| 66 |
|
| 67 |
# Connect to ES and create our index |
| 68 |
es_host = None |
| 69 |
if config.has_option('elasticsearch', 'host'): |
| 70 |
es_host = config.get('elasticsearch', 'host') |
| 71 |
es = elasticsearch.Elasticsearch(es_host) |
| 72 |
if not es.indices.exists('hpilo'): |
| 73 |
print "Creating hpilo index" |
| 74 |
es.indices.create(index='hpilo') |
| 75 |
mapping = { |
| 76 |
"_timestamp": { "enabled": True, "path": "last_updated" }, |
| 77 |
"properties": { |
| 78 |
"status": { "type": "string", "index": "not_analyzed" }, |
| 79 |
} |
| 80 |
} |
| 81 |
add_string_property(mapping, "network_settings.nic_speed") |
| 82 |
add_string_property(mapping, "host_data.fields.value") |
| 83 |
add_string_property(mapping, "xmldata.bladesystem.manager.rack") |
| 84 |
es.indices.put_mapping(index='hpilo', doc_type='hpilo', body={'hpilo': mapping}) |
| 85 |
|
| 86 |
# Scan all servers and import data |
| 87 |
ilos = servers.get_all_ilos() |
| 88 |
ilos = [(ilo, login, password, opts.timeout, opts.port, |
| 89 |
opts.protocol, opts.debug, es_host) for ilo in ilos] |
| 90 |
if opts.processes == 1: |
| 91 |
for ilo in ilos: |
| 92 |
scan_ilo(ilo) |
| 93 |
else: |
| 94 |
multiprocessing.Pool(opts.processes).map(scan_ilo, ilos) |
| 95 |
# Finally, clean up stale data (older than 7 days) |
| 96 |
limit = (datetime.datetime.now() - datetime.timedelta(7)).strftime('%Y-%m-%dT%H:%M:%s') |
| 97 |
query = { "constant_score": { "filter": { "range": { "last_updated": { "lte": limit } } } } } |
| 98 |
es.delete_by_query(index='hpilo', doc_type='hpilo', body={'query': query}) |
| 99 |
|
| 100 |
def scan_ilo(args): |
| 101 |
name, login, password, timeout, port, protocol, debug, es_host = args |
| 102 |
print("Scanning %s" % name) |
| 103 |
ilo = hpilo.Ilo(name, login, password, timeout, port, delayed=True) |
| 104 |
ilo.debug = debug |
| 105 |
if protocol == 'http': |
| 106 |
ilo.protocol = hpilo.ILO_HTTP |
| 107 |
elif protocol == 'raw': |
| 108 |
ilo.protocol = hpilo.ILO_RAW |
| 109 |
|
| 110 |
data = {'status': 'ok'} |
| 111 |
try: |
| 112 |
ilo.get_fw_version() |
| 113 |
ilo.get_host_data() |
| 114 |
ilo.get_global_settings() |
| 115 |
ilo.get_network_settings() |
| 116 |
ilo.get_all_user_info() |
| 117 |
data['fw_version'], data['host_data'], data['global_settings'], \ |
| 118 |
data['network_settings'], data['all_user_info'] = ilo.call_delayed() |
| 119 |
|
| 120 |
ilo.delayed = False |
| 121 |
try: |
| 122 |
data['oa_info'] = ilo.get_oa_info() |
| 123 |
except hpilo.IloError: |
| 124 |
data['oa_info'] = None |
| 125 |
data['xmldata'] = ilo.xmldata() |
| 126 |
except hpilo.IloError: |
| 127 |
e = sys.exc_info()[1] |
| 128 |
data['status'] = 'error' |
| 129 |
data['error'] = str(e) |
| 130 |
|
| 131 |
now = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') |
| 132 |
es = elasticsearch.Elasticsearch(es_host) |
| 133 |
try: |
| 134 |
stored = es.get(index='hpilo', doc_type='hpilo', id=name) |
| 135 |
es.update(index='hpilo', doc_type='hpilo', id=name, body={'doc': data}, timestamp=now) |
| 136 |
except elasticsearch.exceptions.NotFoundError: |
| 137 |
es.index(index='hpilo', doc_type='hpilo', id=name, body=data, timestamp=now) |
| 138 |
|
| 139 |
def add_string_property(mapping, key): |
| 140 |
rest = key.split('.', 1) |
| 141 |
mine = rest.pop(0) |
| 142 |
if rest: |
| 143 |
mapping["properties"][mine] = {"properties": {}} |
| 144 |
add_string_property(mapping["properties"][mine], rest[0]) |
| 145 |
else: |
| 146 |
mapping["properties"][mine] = {"type": "string"} |
| 147 |
|
| 148 |
if __name__ == '__main__': |
| 149 |
main() |