#!/usr/bin/python

import web
import requests
import xmltodict
import json
import os
import sys
import datetime
from collections import OrderedDict
from operator import itemgetter
import utils

urls = ('/', 'index',
        '/help', 'help',
        '/download', 'download'
        )

app = web.application(urls, globals())
render = web.template.render('templates/', base="layout")

# Uncomment to turn debug off
web.config.debug = False

# Variables
output = []

# Functions

def calculate_count(data):
    count = {}
    ls = data.values()
    z, nz = 0,0
    for v in ls:
        if v == 0:
            z += 1
        else:
            nz += 1
    count['green'] = z
    count['red'] = nz
    return count


def getMonit():
    output = []
    xmlQuery = "/_status?format=xml"

    with open('{0}/conf/servers.json'.format(os.path.expanduser('.'))) as f:
        cf = json.loads(f.read())

        for site in cf:
            s = cf[site]
            r = requests.get(s['url'] + xmlQuery,
                             auth=(s['user'], s['passwd']))

            allstat = json.loads(json.dumps(xmltodict.parse(r.text)['monit']))

            services = allstat['service']
            status = {}
            server = {}
            checks = OrderedDict()

            for service in services:
                name = service['name']
                status[name] = int(service['status'])
                checks[name] = status[name]

            sorted_checks = OrderedDict()
            sorted_checks = OrderedDict(sorted(checks.iteritems(), key=itemgetter(1), reverse=True))
            count = calculate_count(sorted_checks)
            server = dict(name=site, url=s['url'], result=sorted_checks, s_rate=count)

            output.append(server)
    print(datetime.datetime.now())
    return(output)

# Classes

class monitDashboard(web.application):

    def run(self, port=8080, *middleware):
        func = self.wsgifunc(*middleware)
        return web.httpserver.runsimple(func, ('0.0.0.0', port))


class index(object):

    def GET(self):
        return render.index(output=getMonit(), now=datetime.datetime.now())


class help(object):

    def GET(self):
        return render.help()

# Main
if __name__ == "__main__":
    app = monitDashboard(urls, globals())
    app.run(port=8080)

@@ -1534,16 +1534,32 @@
 items(),
+%0A               
  key=ite
@@ -1686,16 +1686,32 @@
 %5B'url'%5D,
+%0A               
  result=
@@ -1879,17 +1879,16 @@
 ation):%0A
-%0A
     def 
@@ -2044,33 +2044,32 @@
  index(object):%0A
-%0A
     def GET(self
@@ -2169,17 +2169,16 @@
 bject):%0A
-%0A
     def 
@@ -2218,16 +2218,433 @@
 help()%0A%0A
+class download(object):%0A    def GET(self):%0A        filename = 'health_report.xlsx'%0A        output = getMonit()%0A        utils.generate_report_excel(output, filename)%0A        web.header('Content-Disposition',%0A            'attachment; filename=%22health_report.xlsx%22')%0A        web.header('Content-type','application/octet-stream')%0A        web.header('Cache-Control','no-cache')%0A        return open(filename, 'rb').read()%0A%0A
 # Main%0Ai

add route to download report file