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]
def main(argv=None):

    print 'zabbix_getgraph\n'

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    try:
        parser = argparse.ArgumentParser(description='Get Zabbix graphs')
        parser.add_argument('url', type=str, help='Zabbix JSON URL')
        parser.add_argument('graph_id', type=int, help='Graph ID')
        parser.add_argument('-u', type=str, default='guest', metavar='User',
                            help='User')
        parser.add_argument('-p', type=str, default='', metavar='Password',
                            help='Password')
        parser.add_argument('-o', type=str, default='graph.png',
                            help='Output image')
        parser.add_argument('-x', type=int, default=8, help='Image width)')
        parser.add_argument('-y', type=int, default=5, help='Image height)')
        parser.add_argument('-g', action="store_true", help='Show grid)')
        parser.add_argument('-l', action="store_true", help='Show legend)')
        parser.add_argument('-b', action="store_true",
                            help='Invert backround)')
        parser.add_argument('-w', action="store_true",
                            help='Show image in window')

        args = parser.parse_args()
        url = args.url
        user = args.u
        password = args.p
        id_graph = args.graph_id
        filename = args.o
        width = args.x
        height = args.y
        show_grid = args.g
        show_legend = args.l
        invert = args.b
        show_window = args.w

        api = Zabbix_Json(url, user, password)
        try:
            api.login()
            title = api.get_graph_title(id_graph)
            if title is None:
                sys.stderr.write('Graph not found\n')
                return 1
            if show_window:
                print 'Generating graph \'{0}\''.format(title)
            else:
                print 'Generating graph \'{0}\' as {1}'.format(title, filename)

            graph = Graph()
            api.get_graph(graph, title, id_graph)
            api.logout()
            figure = graph.plot_mpl(width, height, invert, show_grid,
                                      show_legend)
            if show_window:
                pylab.show()
            else:
                try:
                    figure.savefig(filename)
                except ValueError, error:
                    sys.stderr.write(error.message + "\n")
                    return 1

            print 'Completed'
            return 0

        except Zabbix_Error:
            return 2
Exemplo n.º 4
0
def main(argv=None):

    print 'zabbix_getgraph\n'

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    try:
        parser = argparse.ArgumentParser(description='Get Zabbix graphs')
        parser.add_argument('url', type=str, help='Zabbix JSON URL')
        parser.add_argument('graph_id', type=int, help='Graph ID')
        parser.add_argument('-u',
                            type=str,
                            default='guest',
                            metavar='User',
                            help='User')
        parser.add_argument('-p',
                            type=str,
                            default='',
                            metavar='Password',
                            help='Password')
        parser.add_argument('-o',
                            type=str,
                            default='graph.png',
                            help='Output image')
        parser.add_argument('-x', type=int, default=8, help='Image width)')
        parser.add_argument('-y', type=int, default=5, help='Image height)')
        parser.add_argument('-g', action="store_true", help='Show grid)')
        parser.add_argument('-l', action="store_true", help='Show legend)')
        parser.add_argument('-b',
                            action="store_true",
                            help='Invert backround)')
        parser.add_argument('-w',
                            action="store_true",
                            help='Show image in window')

        args = parser.parse_args()
        url = args.url
        user = args.u
        password = args.p
        id_graph = args.graph_id
        filename = args.o
        width = args.x
        height = args.y
        show_grid = args.g
        show_legend = args.l
        invert = args.b
        show_window = args.w

        api = Zabbix_Json(url, user, password)
        try:
            api.login()
            title = api.get_graph_title(id_graph)
            if title is None:
                sys.stderr.write('Graph not found\n')
                return 1
            if show_window:
                print 'Generating graph \'{0}\''.format(title)
            else:
                print 'Generating graph \'{0}\' as {1}'.format(title, filename)

            graph = Graph()
            api.get_graph(graph, title, id_graph)
            api.logout()
            figure = graph.plot_mpl(width, height, invert, show_grid,
                                    show_legend)
            if show_window:
                pylab.show()
            else:
                try:
                    figure.savefig(filename)
                except ValueError, error:
                    sys.stderr.write(error.message + "\n")
                    return 1

            print 'Completed'
            return 0

        except Zabbix_Error:
            return 2