]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/private/monitoring/status/app.py
Add Eban monitoring
[perso/Immae/Config/Nix.git] / modules / private / monitoring / status / app.py
index b1d419cc1cc88909a265dca4d293a1337a235b9e..ff928914c6d681f15769ea81dd2cfbd95b59b662 100755 (executable)
@@ -55,33 +55,34 @@ def get_lq(request):
     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]
@@ -110,8 +111,8 @@ OutputFormat: json
 """)
         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)
@@ -158,15 +159,15 @@ OutputFormat: json
     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"?>
@@ -254,12 +255,14 @@ 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 %}
@@ -281,8 +284,10 @@ TEMPLATE='''<?xml version="1.0" encoding="UTF-8"?>
                 {% endif %}
             {%- endfor -%}
             </div>
-        {%- endfor %}
+        {%- if loop.last %}
         </div>
+        {% endif %}
+        {%- endfor %}
     </body>
 </html>
 '''
@@ -307,7 +312,7 @@ def live():
 
 @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