Created
October 21, 2022 09:21
-
-
Save yujiterada/02df0a5cf150f5ce3cba9db8a009f2c7 to your computer and use it in GitHub Desktop.
Obtains license status for all Meraki organizations attached to the account. Compatible with PDL and Co-term.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from datetime import datetime | |
import meraki | |
MERAKI_API_KEY = os.environ.get('MERAKI_API_KEY', None) | |
def device_needs_license(device): | |
now = datetime.now() | |
license_expiration_date = datetime.strptime(device['licenseExpirationDate'], '%Y-%m-%dT%H:%M:%SZ') | |
if license_expiration_date > now: | |
return (False, license_expiration_date) | |
else: | |
return (True, license_expiration_date) | |
if MERAKI_API_KEY: | |
# Initialize Meraki API Client | |
dashboard = meraki.DashboardAPI(api_key=MERAKI_API_KEY, suppress_logging=True, output_log=False) | |
organizations = dashboard.organizations.getOrganizations() | |
for org in organizations: | |
license_model = org['licensing']['model'] | |
print(org['name']) | |
print(' Model: {}'.format(license_model)) | |
if license_model == 'per-device': | |
number_of_expired_devices = 0 | |
earliest_expiration_date = None | |
is_expired = False | |
devices = dashboard.organizations.getOrganizationInventoryDevices(organizationId=org['id'], total_pages='all') | |
for device in devices: | |
if device['networkId'] != None and device['licenseExpirationDate'] != None: | |
result, expiration_date = device_needs_license(device) | |
if not earliest_expiration_date: | |
earliest_expiration_date = expiration_date | |
if earliest_expiration_date > expiration_date: | |
earliest_expiration_date = expiration_date | |
if result: | |
number_of_expired_devices += 1 | |
is_expired = True | |
if is_expired: | |
print(' Status: License Expired for {} device(s)'.format(number_of_expired_devices)) | |
else: | |
print(' Status: OK') | |
if earliest_expiration_date: | |
print(' Expiration Date: {}'.format(earliest_expiration_date.strftime('%b %-d, %Y UTC'))) | |
else: | |
print(' Expiration Date: N/A') | |
elif license_model == 'co-term': | |
license_overview = dashboard.organizations.getOrganizationLicensesOverview(organizationId=org['id']) | |
print(' Status: {}'.format(license_overview['status'])) | |
print(' Expiration Date: {}'.format(license_overview['expirationDate'])) | |
else: | |
print('Unknown license model') | |
print() | |
''' | |
SAMPLE OUTPUT: | |
Test | |
Model: per-device | |
Status: License Expired for 1 device(s) | |
Expiration Date: Jul 13, 2022 UTC | |
Sydney SE Pod 2 | |
Model: co-term | |
Status: OK | |
Expiration Date: Dec 15, 2022 UTC | |
Sydney Sales | |
Model: per-device | |
Status: License Expired for 1 device(s) | |
Expiration Date: May 20, 2020 UTC | |
Test corp | |
Model: co-term | |
Status: OK | |
Expiration Date: N/A | |
Serverless Guest Wi-Fi prod | |
Model: co-term | |
Status: OK | |
Expiration Date: N/A | |
Sydney SE Pod 1 | |
Model: co-term | |
Status: License Expired | |
Expiration Date: Sep 13, 2022 UTC | |
Sydney SE Lab | |
Model: co-term | |
Status: License Expired | |
Expiration Date: Aug 5, 2022 UTC | |
Serverless Guest Wi-Fi dev | |
Model: co-term | |
Status: OK | |
Expiration Date: N/A | |
The Experts Lab CORE | |
Model: per-device | |
Status: License Expired for 2 device(s) | |
Expiration Date: Jun 3, 2020 UTC | |
Yuji Terada Personal | |
Model: co-term | |
Status: License Required | |
Expiration Date: Feb 25, 2029 UTC | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment