return b"".join(chunks).decode()
class Host:
- def __init__(self, name, alias, status, webname):
+ def __init__(self, name, alias, status, webname, vhost):
self.name = name
self.alias = alias
self.webname = webname or alias
+ self.vhost = vhost
self.status = status
self.services = []
@classmethod
- def parse_hosts(cls, payload):
- parsed = [cls.parse(p) for p in json.loads(payload)]
+ def parse_hosts(cls, payload, vhost):
+ parsed = filter(lambda x: x.vhost == vhost, [cls.parse(p) for p in json.loads(payload)])
return {p.name: p for p in parsed}
@classmethod
def parse(cls, payload):
- return cls(payload[0], payload[1], HOST_STATUS[payload[2]], payload[3].get("WEBSTATUS_NAME"))
+ return cls(payload[0], payload[1], HOST_STATUS[payload[2]], payload[3].get("WEBSTATUS_NAME"), payload[3].get("WEBSTATUS_VHOST"))
def __repr__(self):
return "Host {}: {} ({})".format(self.name, self.alias, self.webname)
@classmethod
- def query(cls):
+ def query(cls, vhost):
answer = get_lq("""GET hosts
Filter: groups >= webstatus-hosts
Columns: name alias state custom_variables
OutputFormat: json
""")
- return cls.parse_hosts(answer)
+ return cls.parse_hosts(answer, vhost)
def fill_services(self, services):
self.services = [service for service in services if service.host == self.name]
""")
return cls.parse_groups(answer)
- def fill_services(self, services):
- self.services = [service for service in services if any([group == self.name for group in service.groups])]
+ def fill_services(self, services, hosts):
+ self.services = [service for service in services if any([group == self.name for group in service.groups]) and service.host in hosts]
def __repr__(self):
return "ServiceGroup {}: {}".format(self.name, self.alias)
def __repr__(self):
return "Service {}: {}".format(self.name, self.webname)
-def get_infos():
- hosts = Host.query()
+def get_infos(vhost):
+ hosts = Host.query(vhost)
servicegroups = ServiceGroup.query()
services = Service.query()
for host in hosts:
hosts[host].fill_services(services)
for group in servicegroups:
- servicegroups[group].fill_services(services)
+ servicegroups[group].fill_services(services, hosts)
return (hosts, servicegroups, services)
TEMPLATE='''<?xml version="1.0" encoding="UTF-8"?>
{% endfor %}
{%- endfor %}
+ {%- for group in servicegroups.values() if group.services and group.name != "webstatus-resources" %}
+ {%- if loop.first %}
<h2>Services</h2>
<div id="services">
- {%- for group in servicegroups.values() if group.services and group.name != "webstatus-resources" %}
+ {%- endif %}
<div class="servicegroup">
<h3 class="servicegroup_title">{{ group.alias }}</h3>
- {%- for service in group.services -%}
+ {%- for service in group.services if service.host in hosts -%}
{%- if loop.first %}
<ul class="services">
{% endif %}
{% endif %}
{%- endfor -%}
</div>
- {%- endfor %}
+ {%- if loop.last %}
</div>
+ {% endif %}
+ {%- endfor %}
</body>
</html>
'''
@app.route("/", methods=["GET"])
def get():
- (hosts, servicegroups, services) = get_infos()
+ (hosts, servicegroups, services) = get_infos(request.host)
resp = make_response(render_template_string(TEMPLATE, hosts=hosts, servicegroups=servicegroups))
resp.content_type = "text/html"
return resp