Python bigsuds Performance Graphs

Problem this snippet solves:

This script generates BIG-IP performance graphs.

How to use this snippet:

usage: stats.py [-h] -s SYSTEM -u USERNAME

Code :

#!/usr/bin/env python

'''
----------------------------------------------------------------------------
 The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
 Software Development Kit for iControl"; you may not use this file except in
 compliance with the License. The License is included in the iControl
 Software Development Kit.

 Software distributed under the License is distributed on an "AS IS"
 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 the License for the specific language governing rights and limitations
 under the License.

 The Original Code is iControl Code and related documentation
 distributed by F5.

 The Initial Developer of the Original Code is F5 Networks,
 Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2004 F5 Networks,
 Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.

 Alternatively, the contents of this file may be used under the terms
 of the GNU General Public License (the "GPL"), in which case the
 provisions of GPL are applicable instead of those above.  If you wish
 to allow use of your version of this file only under the terms of the
 GPL and not to allow others to use your version of this file under the
 License, indicate your decision by deleting the provisions above and
 replace them with the notice and other provisions required by the GPL.
 If you do not delete the provisions above, a recipient may use your
 version of this file under either the License or the GPL.
----------------------------------------------------------------------------

Required Modules:
    numpy
    matplotlib
    dateutil
    pyparsing
    suds
    bigsuds
    csv
    time
    base64
    argparse
    getpass
'''
__author__ = 'rahm'


def graph_memory(csvdata):
    timestamp, total_mem, os_used, tmm_alloc, tmm_used, swap_used = np.genfromtxt(csvdata, delimiter=',',
                                                           skip_header=2, skip_footer=2, unpack=True,
                                                           converters= {0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})
    fig = plt.figure()
    plt.plot_date(x=timestamp, y=total_mem, fmt='-')
    plt.plot(timestamp, total_mem, 'k-', label="Total Memory")
    plt.plot(timestamp, tmm_used, 'r-', label="TMM Used")
    plt.plot(timestamp, tmm_alloc, 'c-', label="Tmm Allocated")
    plt.plot(timestamp, os_used, 'g-', label="OS Used")
    plt.plot(timestamp, swap_used, 'b-', label="OS Used Swap")
    plt.legend(title="Context", loc='upper left', shadow=True)
    plt.title('Global Memory')
    plt.ylabel('GB')
    plt.show()

def graph_cpu(csvdata):
    timestamp, cpu_util = np.genfromtxt(csvdata, delimiter=',',
                                                           skip_header=2, skip_footer=2, unpack=True,
                                                           converters= {0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})
    fig = plt.figure()
    plt.plot_date(x=timestamp, y=cpu_util, fmt='-')
    plt.plot(timestamp, cpu_util, 'r-', label="CPU")
    plt.title('CPU Utilization')
    plt.ylabel('%')
    plt.show()

def graph_active_connections(csvdata):
    timestamp, conns = np.genfromtxt(csvdata, delimiter=',',
                                                           skip_header=2, skip_footer=2, unpack=True,
                                                           converters= {0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})
    fig = plt.figure()
    plt.plot_date(x=timestamp, y=conns, fmt='-')
    plt.plot(timestamp, conns, 'r-', label="conns")
    plt.title('Active Connections')
    plt.ylabel('conns')
    plt.show()

def graph_new_connections(csvdata):
    timestamp, client_conns, server_conns = np.genfromtxt(csvdata, delimiter=',',
                                                           skip_header=2, skip_footer=2, unpack=True,
                                                           converters= {0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})
    fig = plt.figure()
    plt.plot_date(x=timestamp, y=client_conns, fmt='-')
    plt.plot(timestamp, client_conns, 'g-', label="client")
    plt.plot(timestamp, server_conns, 'r-', label="server")
    plt.legend(title="Context", loc='best', shadow=True)
    plt.title('New Connections')
    plt.ylabel('conns/s')
    plt.show()

def graph_throughput(csvdata):
    timestamp, client, server, compression = np.genfromtxt(csvdata, delimiter=',',
                                                           skip_header=2, skip_footer=2, unpack=True,
                                                           converters= {0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})
    fig = plt.figure()
    plt.plot_date(x=timestamp, y=client, fmt='-')
    plt.plot(timestamp, client, 'g-', label="client")
    plt.plot(timestamp, server, 'r-', label="server")
    plt.plot(timestamp, compression, 'c-', label="compression")
    plt.legend(title="Context", loc='upper center', ncol=3, shadow=True)
    plt.title('Global Throughput')
    plt.ylabel('Mb/s')
    plt.show()

def graph_http_requests(csvdata):
    timestamp, requests = np.genfromtxt(csvdata, delimiter=',',
                                                           skip_header=2, skip_footer=2, unpack=True,
                                                           converters= {0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})
    fig = plt.figure()
    plt.plot_date(x=timestamp, y=requests, fmt='-')
    plt.plot(timestamp, requests, 'r-', label="requests")
    plt.title('HTTP Requests')
    plt.ylabel('req/s')
    plt.show()

def graph_ramcache_util(csvdata):
    timestamp, hit_rate, byte_rate, eviction_rate = np.genfromtxt(csvdata, delimiter=',',
                                                           skip_header=2, skip_footer=2, unpack=True,
                                                           converters= {0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})
    fig = plt.figure()
    plt.plot_date(x=timestamp, y=hit_rate, fmt='-')
    plt.plot(timestamp, hit_rate, 'k-', label="Hit Rate")
    plt.plot(timestamp, byte_rate, 'c-', label="Byte Rate")
    plt.plot(timestamp, eviction_rate, 'r-', label="Evict Rate")
    plt.legend(title="Context", loc='best', shadow=True)
    plt.title('Ramcache')
    plt.ylabel('%')
    plt.show()

def graph_active_ssl(csvdata):
    timestamp, client_ssl, server_ssl = np.genfromtxt(csvdata, delimiter=',',
                                                           skip_header=2, skip_footer=2, unpack=True,
                                                           converters= {0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})
    fig = plt.figure()
    plt.plot_date(x=timestamp, y=client_ssl, fmt='-')
    plt.plot(timestamp, client_ssl, 'g-', label="client")
    plt.plot(timestamp, server_ssl, 'r-', label="server")
    plt.legend(title="Context", loc='best', shadow=True)
    plt.title('Active SSL Connections')
    plt.ylabel('conns')
    plt.show()


if __name__ == "__main__":

    import bigsuds as pc
    import getpass
    import argparse
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    import numpy as np
    import csv, time, base64

    parser = argparse.ArgumentParser()

    parser.add_argument("-s",  "--system", required=True)
    parser.add_argument("-u", "--username", required=True)

    args = vars(parser.parse_args())

    print "%s, enter your " % args['username'],
    upass = getpass.getpass()

    b = pc.BIGIP(args['system'], args['username'], upass)
    stats = b.System.Statistics

    graphs = stats.get_performance_graph_list()
    graph_obj_name = []
    count = 1
    print "\n\nGraph Options. Not all options supported at this time, please select only 1, 2, 3, 4, 5, 7, 8, or 12.\n\n"
    for x in graphs:
        graph_obj_name.append(x['graph_name'])
        print "%d> %s, %s" % (count, x['graph_description'], x['graph_name'])
        count +=1

    num_select = int(raw_input("\n\nPlease select the number for the desired graph data: "))
    print "\n\n"

    graphstats = stats.get_performance_graph_csv_statistics(
        [{'object_name': graph_obj_name[num_select - 1],
          'start_time': 0,
          'end_time': 0,
          'interval': 0,
          'maximum_rows': 0}])

    statsFile = "%s_rawStats.csv" % graph_obj_name[num_select-1]
    f = open(statsFile, "w")
    f.write(base64.b64decode(graphstats[0]['statistic_data']))
    f.close()

    outputFile = "%s_modStats.csv" % graph_obj_name[num_select-1]
    with open(statsFile) as infile, open(outputFile, "w") as outfile:
        r = csv.reader(infile)
        w = csv.writer(outfile)
        w.writerow(next(r))
        for row in r:
            fl = float(row[0])
            row[0] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(fl))
            w.writerow(row)

    if num_select == 1:
        graph_memory(outputFile)
    elif num_select == 2:
        graph_cpu(outputFile)
    elif num_select == 3:
        graph_active_connections(outputFile)
    elif num_select == 4:
        graph_new_connections(outputFile)
    elif num_select == 5:
        graph_throughput(outputFile)
    elif num_select == 7:
        graph_http_requests(outputFile)
    elif num_select == 8:
        graph_ramcache_util(outputFile)
    elif num_select == 12:
        graph_active_ssl(outputFile)
    else:
        print "graph function not yet defined, please select 1, 2, 3, 4, 5, 7, 8, or 12."
Published Mar 09, 2015
Version 1.0

Was this article helpful?