Hi,
I have created a Python script to execute requests. It has already been distributed to the host (it needs to run on the host), and
I have placed it in /local/nagios/plugins/azure_file_check2.py
and executed the chmod
command, resulting in 0 Azure_App_Service_File_System_Usage - Used Space: 0.67 GB, Total Space: 50.00 GB, Space Usage: 1.33%
. Now, I am unsure about the next steps as the documentation on CheckMK’s website is confusing. If there’s an alternative option to make it work, I would appreciate the guidance.
Thank you,
#!/usr/bin/env python3
import requests
def get_access_token(tenant_id, client_id, client_secret):
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"client_id": client_id,
"scope": "https://management.azure.com/.default",
"client_secret": client_secret,
"grant_type": "client_credentials"
}
response = requests.post(token_url, headers=headers, data=data)
response.raise_for_status() # Raise an error for failed requests
token = response.json().get("access_token")
return token
def get_file_system_usage(subscription_id, resource_group, app_service_plan_name, access_token):
url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Web/serverFarms/{app_service_plan_name}/usages?api-version=2022-03-01"
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an error for failed requests
data = response.json()
for item in data['value']:
if item['name']['value'] == 'FileSystemStorage':
current_value = item['currentValue']
limit = item['limit']
return current_value, limit
# If FileSystemStorage usage is not found, return None
return None, None
def calculate_space_usage(current_value, limit):
if current_value is not None and limit is not None:
# Convert bytes to gigabytes
used_space_gb = current_value / (xxxx)
total_space_gb = limit / (xxxx)
return used_space_gb, total_space_gb
else:
return None, None
if __name__ == "__main__":
tenant_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
resource_group = "xxxxxxx"
app_service_plan_name = "xxxxxxx"
try:
access_token = get_access_token(tenant_id, client_id, client_secret)
current_value, limit = get_file_system_usage(subscription_id, resource_group, app_service_plan_name, access_token)
used_space_gb, total_space_gb = calculate_space_usage(current_value, limit)
if used_space_gb is not None and total_space_gb is not None:
# Output the result in Checkmk format
print(f"0 Azure_App_Service_File_System_Usage - Used Space: {used_space_gb:.2f} GB, Total Space: {total_space_gb:.2f} GB, Space Usage: {used_space_gb / total_space_gb * 100:.2f}%")
else:
print("3 Azure_App_Service_File_System_Usage - Failed to retrieve file system usage.")
except Exception as e:
print(f"3 Azure_App_Service_File_System_Usage - Error: {e}")
print("3 Azure_App_Service_File_System_Usage - Failed to retrieve file system usage. Please check your Azure configuration.")e or paste code here