def graph_server(environ, start_response):

    start = datetime.datetime.now()

    content = ('Content-Type', 'text/plain')
    remote = environ['REMOTE_HOST']

    __query = parse_qs(environ['QUERY_STRING'])
    try:
        id_graph = int(escape(__query.get('graph', ['-1'])[0]))
    except ValueError:
        id_graph = -1
    try:
        width = float(escape(__query.get('width', ['8'])[0]))
    except ValueError:
        width = 8
    try:
        height = float(escape(__query.get('height', ['5'])[0]))
    except ValueError:
        height = 5
    try:
        scale = float(escape(__query.get('scale', ['1'])[0]))
    except ValueError:
        scale = 1

    show_grid = escape(__query.get('grid', [''])[0]) == 'true'
    show_legend = escape(__query.get('legend', [''])[0]) == 'true'
    invert = escape(__query.get('invert', [''])[0]) == 'true'

    if id_graph == -1:
        body = 'Graph not specified'
    else:
        try:
            api.login()
            title = api.get_graph_title(id_graph)
            if title is None:
                body = 'Graph not found'
                api.logout()
            else:
                print 'Generating graph \'{0}\' for {1}'.format(title, remote)
                content = ('Content-Type', 'image/png')
                graph = Graph()
                api.get_graph(graph, title, id_graph)
                figure = graph.plot_mpl(width, height, invert, show_grid,
                                        show_legend)
                figure.canvas.draw()
                graph.close(figure)
                image_buffer = StringIO.StringIO()
                size = figure.canvas.get_width_height()
                image = Image.fromstring('RGB', size,
                                         figure.canvas.tostring_rgb())
                if scale != 1:
                    scale_size = (int(size[0] * scale), int(size[1] * scale))
                    image = image.resize(scale_size, Image.ANTIALIAS)
                image.save(image_buffer, 'PNG')
                body = image_buffer.getvalue()
                api.logout()
                duration = datetime.datetime.now() - start
                print 'Completed in {0}ms'.format(duration.microseconds / 1000)
        except Zabbix_Error:
            body = 'Zabbix Error'

    headers = [content, ('Content-Length', str(len(body)))]
    start_response('200 OK', headers)
    return [body]
def graph_server(environ, start_response):

    start = datetime.datetime.now()

    content = ('Content-Type', 'text/plain')
    remote = environ['REMOTE_HOST']

    __query = parse_qs(environ['QUERY_STRING'])
    try:
        id_graph = int(escape(__query.get('graph', ['-1'])[0]))
    except ValueError:
        id_graph = -1
    try:
        width = float(escape(__query.get('width', ['8'])[0]))
    except ValueError:
        width = 8
    try:
        height = float(escape(__query.get('height', ['5'])[0]))
    except ValueError:
        height = 5
    try:
        scale = float(escape(__query.get('scale', ['1'])[0]))
    except ValueError:
        scale = 1

    show_grid = escape(__query.get('grid', [''])[0]) == 'true'
    show_legend = escape(__query.get('legend', [''])[0]) == 'true'
    invert = escape(__query.get('invert', [''])[0]) == 'true'

    if id_graph == -1:
        body = 'Graph not specified'
    else:
        try:
            api.login()
            title = api.get_graph_title(id_graph)
            if title is None:
                body = 'Graph not found'
                api.logout()
            else:
                print 'Generating graph \'{0}\' for {1}'.format(title, remote)
                content = ('Content-Type', 'image/png')
                graph = Graph()
                api.get_graph(graph, title, id_graph)
                figure = graph.plot_mpl(width, height, invert, show_grid,
                                        show_legend)
                figure.canvas.draw()
                graph.close(figure)
                image_buffer = StringIO.StringIO()
                size = figure.canvas.get_width_height()
                image = Image.fromstring('RGB', size,
                                         figure.canvas.tostring_rgb())
                if scale != 1:
                    scale_size = (int(size[0] * scale), int(size[1] * scale))
                    image = image.resize(scale_size, Image.ANTIALIAS)
                image.save(image_buffer, 'PNG')
                body = image_buffer.getvalue()
                api.logout()
                duration = datetime.datetime.now() - start
                print 'Completed in {0}ms'.format(duration.microseconds / 1000)
        except Zabbix_Error:
            body = 'Zabbix Error'

    headers = [content, ('Content-Length', str(len(body)))]
    start_response('200 OK', headers)
    return [body]