A script to work with OCI Cost API (usage-api)

OCI has a great interface to gather Cost and Usage reports, but imagine that you have a tenancy with almost 300 compartments and you need to build a report with the cost by each one?

I know that you can export the reports in PDF format(or CSV), but you will need to do some type of work to aggregate it, so this script will help you, the main goal here is to get the last 7 days of cost from the last Sunday (that’s why the script is called show_costs_week.sh).

As usual, this script uses the oci cli (you need to have it installed and configured before using the script), and to keep the things clear, this is not an official script from Oracle, use it to just estimate the cost!

Example of output:

[opc@XXXXX ~]$ /home/opc/XXX/show_costs_week.sh TENANCY ocid1.tenancy.oc1..XXX us-ashburn-1
-------------------------------------------------------------------------------------------------------------------------------------------------
Profile: TENANCY| Begin Date: 2024-05-26 | End Date: 2024-06-02
-------------------------------------------------------------------------------------------------------------------------------------------------
Compartment:  compB
-------------------------------------------------------------------------------------------------------------------------------------------------
+----------------------------------------+------------------+---------------------------+---------------------------+-----------------+
| CompPath                               | CompartName      | Fim                       | Inicio                    | Valor           |
+----------------------------------------+------------------+---------------------------+---------------------------+-----------------+
| root/compA/compB | compB | 2024-05-27T00:00:00+00:00 | 2024-05-26T00:00:00+00:00 | 23.002692666972 |
| root/compA/compB | compB | 2024-05-30T00:00:00+00:00 | 2024-05-29T00:00:00+00:00 | 22.898028714713 |
| root/compA/compB | compB | 2024-06-02T00:00:00+00:00 | 2024-06-01T00:00:00+00:00 | 46.408353207519 |
| root/compA/compB | compB | 2024-06-01T00:00:00+00:00 | 2024-05-31T00:00:00+00:00 | 22.740664837568 |
| root/compA/compB | compB | 2024-05-29T00:00:00+00:00 | 2024-05-28T00:00:00+00:00 | 20.279208945572 |
| root/compA/compB | compB | 2024-05-31T00:00:00+00:00 | 2024-05-30T00:00:00+00:00 | 22.725654484152 |
| root/compA/compB | compB | 2024-05-28T00:00:00+00:00 | 2024-05-27T00:00:00+00:00 | 23.493054517126 |
+----------------------------------------+------------------+---------------------------+---------------------------+-----------------+
Total for compB compartment: 181.548

Get the script from my GitHub : scripts/oci/cli/show_costs_week.sh at master · adrianotanaka/scripts (github.com)

If you have any problem or suggestion, feel free to get in touch.

chevron_left
chevron_right