Skip to content

Instantly share code, notes, and snippets.

@lsgalves
Last active April 29, 2024 21:44
Show Gist options
  • Save lsgalves/782931c8d348d92eb477c99de4e19959 to your computer and use it in GitHub Desktop.
Save lsgalves/782931c8d348d92eb477c99de4e19959 to your computer and use it in GitHub Desktop.
CloudStack primary storage monitoring
#!/usr/bin/env python3
import json
import sys
import cs
def get_usage_percent(storage: dict) -> float:
allocated_space = float(storage['disksizeallocatedgb'].split()[0])
unallocated_space = float(storage['disksizeunallocatedgb'].split()[0])
usage = (allocated_space / (allocated_space + unallocated_space)) * 100
return round(usage, 2)
def get_storages(cs_client: cs.CloudStack) -> list:
storages = []
st_metrics = cs_client.listStoragePoolsMetrics(all=True)
st_metrics = st_metrics['storagepool'] if 'storagepool' in st_metrics else []
for storage in st_metrics:
usage = get_usage_percent(storage)
storages.append({
'{#NAME}': storage['name'],
'{#USAGE}': usage,
})
return storages
def get_storage_usage(cs_client: cs.CloudStack, name: str) -> float:
st_metrics = cs_client.listStoragePoolsMetrics(name=name)
st_metrics = st_metrics['storagepool'] if 'storagepool' in st_metrics else []
if len(st_metrics) == 0:
raise Exception('Storage not found.')
storage = st_metrics[0]
return get_usage_percent(storage)
if __name__ == '__main__':
cs_client = cs.CloudStack(**cs.read_config())
if len(sys.argv) > 1:
storage_name = sys.argv[1]
usage = get_storage_usage(cs_client, storage_name)
print(usage)
else:
storages = get_storages(cs_client)
print(json.dumps(storages))
zabbix_export:
version: '6.4'
templates:
- uuid: 00f78cd947c34ce89eabc07481a0c38c
template: 'Cloudstack Storage'
name: 'Cloudstack Storage'
discovery_rules:
- uuid: 9f2065411bb04398b2a7bfc8836efdd5
name: 'CloudStack storages discovery'
key: cs.storage.discovery
delay: 6h
filter:
conditions:
- macro: '{#NAME}'
value: '{$CLOUDSTACK.LLD.FILTER.STORAGE.MATCHES}'
formulaid: A
- macro: '{#USAGE}'
value: '{$CLOUDSTACK.LLD.FILTER.STORAGE.MATCHES}'
formulaid: B
item_prototypes:
- uuid: ba5b639256cb4eaebfe5d814da59588c
name: 'CloudStack storage {#NAME}: Get usage'
key: 'cs.storage.usage["{#NAME}"]'
delay: 6h
value_type: FLOAT
units: '%'
trigger_prototypes:
- uuid: a280fbd83efe4a88a134de2f98eb1fa0
expression: 'last(/Cloudstack Storage/cs.storage.usage["{#NAME}"])>{$STORAGE.USAGE.CRIT}'
name: 'High storage utilization {#NAME} (more than {$STORAGE.USAGE.CRIT}% used)'
priority: AVERAGE
dependencies:
- name: 'High storage utilization {#NAME} (more than {$STORAGE.USAGE.WARN}% used)'
expression: 'last(/Cloudstack Storage/cs.storage.usage["{#NAME}"])>{$STORAGE.USAGE.WARN}'
- uuid: 66e96450b3b544b48ecac41b7563fa32
expression: 'last(/Cloudstack Storage/cs.storage.usage["{#NAME}"])>{$STORAGE.USAGE.WARN}'
name: 'High storage utilization {#NAME} (more than {$STORAGE.USAGE.WARN}% used)'
priority: WARNING
macros:
- macro: '{$CLOUDSTACK.LLD.FILTER.STORAGE.MATCHES}'
value: '.*'
description: 'Filter of discoverable storages'
- macro: '{$STORAGE.USAGE.CRIT}'
value: '90'
description: 'This macro is used as a threshold in storage utilization trigger.'
- macro: '{$STORAGE.USAGE.WARN}'
value: '80'
description: 'This macro is used as a threshold in storage utilization trigger.'
@lsgalves
Copy link
Author

lsgalves commented Apr 29, 2024

How to use?

  1. Install dependencies:
    pip3 install cs
  2. Add these parameters to Zabbix Agent config:
    UserParameter=cs.storage.discovery,/path/to/check-storage-capacity.py
    UserParameter=cs.storage.usage[*],/path/to/check-storage-capacity.py "$1"
    
  3. Create the discovery rule on template or host in Zabbix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment