|
| 1 | +"""Get details for a hardware device.""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +import SoftLayer |
| 7 | +from SoftLayer.CLI import environment |
| 8 | +from SoftLayer.CLI import formatting |
| 9 | +from SoftLayer.CLI import helpers |
| 10 | +from SoftLayer import utils |
| 11 | + |
| 12 | + |
| 13 | +@click.command() |
| 14 | +@click.argument('identifier') |
| 15 | +@click.option('--start_date', '-s', type=click.STRING, required=True, |
| 16 | + help="Start Date YYYY-MM-DD, YYYY-MM-DDTHH:mm:ss,") |
| 17 | +@click.option('--end_date', '-e', type=click.STRING, required=True, |
| 18 | + help="End Date YYYY-MM-DD, YYYY-MM-DDTHH:mm:ss") |
| 19 | +@click.option('--summary_period', '-p', type=click.INT, default=3600, show_default=True, |
| 20 | + help="300, 600, 1800, 3600, 43200 or 86400 seconds") |
| 21 | +@click.option('--quite_summary', '-q', is_flag=True, default=False, show_default=True, |
| 22 | + help="Only show the summary table") |
| 23 | +@environment.pass_env |
| 24 | +def cli(env, identifier, start_date, end_date, summary_period, quite_summary): |
| 25 | + """Bandwidth data over date range. Bandwidth is listed in GB |
| 26 | +
|
| 27 | + Using just a date might get you times off by 1 hour, use T00:01 to get just the specific days data |
| 28 | + Timezones can also be included with the YYYY-MM-DDTHH:mm:ss.00000-HH:mm format. |
| 29 | +
|
| 30 | + Due to some rounding and date alignment details, results here might be slightly different than |
| 31 | + results in the control portal. |
| 32 | +
|
| 33 | + Example:: |
| 34 | +
|
| 35 | + slcli hw bandwidth 1234 -s 2019-05-01T00:01 -e 2019-05-02T00:00:01.00000-12:00 |
| 36 | + """ |
| 37 | + vsi = SoftLayer.VSManager(env.client) |
| 38 | + vsi_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS') |
| 39 | + data = vsi.get_bandwidth_data(vsi_id, start_date, end_date, None, summary_period) |
| 40 | + |
| 41 | + title = "Bandwidth Report: %s - %s" % (start_date, end_date) |
| 42 | + table, sum_table = create_bandwidth_table(data, summary_period, title) |
| 43 | + |
| 44 | + env.fout(sum_table) |
| 45 | + if not quite_summary: |
| 46 | + env.fout(table) |
| 47 | + |
| 48 | + |
| 49 | +def create_bandwidth_table(data, summary_period, title="Bandwidth Report"): |
| 50 | + """Create 2 tables, bandwidth and sumamry. Used here and in hw bandwidth command""" |
| 51 | + |
| 52 | + formatted_data = {} |
| 53 | + for point in data: |
| 54 | + key = utils.clean_time(point['dateTime']) |
| 55 | + data_type = point['type'] |
| 56 | + # conversion from byte to megabyte |
| 57 | + value = round(float(point['counter']) / 2 ** 20, 4) |
| 58 | + if formatted_data.get(key) is None: |
| 59 | + formatted_data[key] = {} |
| 60 | + formatted_data[key][data_type] = float(value) |
| 61 | + |
| 62 | + table = formatting.Table(['Date', 'Pub In', 'Pub Out', 'Pri In', 'Pri Out'], title=title) |
| 63 | + |
| 64 | + sum_table = formatting.Table(['Type', 'Sum GB', 'Average MBps', 'Max GB', 'Max Date'], title="Summary") |
| 65 | + |
| 66 | + # Required to specify keyName because getBandwidthTotals returns other counter types for some reason. |
| 67 | + bw_totals = [ |
| 68 | + {'keyName': 'publicIn_net_octet', 'sum': 0.0, 'max': 0, 'name': 'Pub In'}, |
| 69 | + {'keyName': 'publicOut_net_octet', 'sum': 0.0, 'max': 0, 'name': 'Pub Out'}, |
| 70 | + {'keyName': 'privateIn_net_octet', 'sum': 0.0, 'max': 0, 'name': 'Pri In'}, |
| 71 | + {'keyName': 'privateOut_net_octet', 'sum': 0.0, 'max': 0, 'name': 'Pri Out'}, |
| 72 | + ] |
| 73 | + |
| 74 | + for point in formatted_data: |
| 75 | + new_row = [point] |
| 76 | + for bw_type in bw_totals: |
| 77 | + counter = formatted_data[point].get(bw_type['keyName'], 0) |
| 78 | + new_row.append(mb_to_gb(counter)) |
| 79 | + bw_type['sum'] = bw_type['sum'] + counter |
| 80 | + if counter > bw_type['max']: |
| 81 | + bw_type['max'] = counter |
| 82 | + bw_type['maxDate'] = point |
| 83 | + table.add_row(new_row) |
| 84 | + |
| 85 | + for bw_type in bw_totals: |
| 86 | + total = bw_type.get('sum', 0.0) |
| 87 | + average = 0 |
| 88 | + if total > 0: |
| 89 | + average = round(total / len(formatted_data) / summary_period, 4) |
| 90 | + sum_table.add_row([ |
| 91 | + bw_type.get('name'), |
| 92 | + mb_to_gb(total), |
| 93 | + average, |
| 94 | + mb_to_gb(bw_type.get('max')), |
| 95 | + bw_type.get('maxDate') |
| 96 | + ]) |
| 97 | + |
| 98 | + return table, sum_table |
| 99 | + |
| 100 | + |
| 101 | +def mb_to_gb(mbytes): |
| 102 | + """Converts a MegaByte int to GigaByte. mbytes/2^10""" |
| 103 | + return round(mbytes / 2 ** 10, 4) |
0 commit comments