-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathazure_function.py
73 lines (59 loc) · 3.09 KB
/
azure_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
from azure.mgmt.compute import ComputeManagementClient
from datetime import datetime, timedelta
def get_azure_metrics(subscription_id, resource_group, vm_name, metric_name='NetworkIn'):
"""Fetch Azure metrics using Azure Monitor."""
credential = DefaultAzureCredential()
monitor_client = MonitorManagementClient(credential, subscription_id)
# Time range for metrics
end_time = datetime.utcnow()
start_time = end_time - timedelta(hours=1)
metrics = monitor_client.metrics.list(
resource_uri=f"/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Compute/virtualMachines/{vm_name}",
timespan=f"{start_time.isoformat()}/{end_time.isoformat()}",
metricnames=metric_name
)
network_in = 0
for metric in metrics.value:
if metric.name.value == metric_name:
for timeseries in metric.timeseries:
for data in timeseries.data:
network_in += data.average or 0
return network_in
def scale_vm(subscription_id, resource_group, vm_name, scale_up=True):
"""Scale Azure VM instance."""
credential = DefaultAzureCredential()
compute_client = ComputeManagementClient(credential, subscription_id)
vm = compute_client.virtual_machines.get(resource_group, vm_name)
# Scaling logic (In this case, we simulate by deallocating and starting the VM)
if scale_up:
compute_client.virtual_machines.start(resource_group, vm_name)
return f"Scaling Up: Starting VM {vm_name}"
else:
compute_client.virtual_machines.deallocate(resource_group, vm_name)
return f"Scaling Down: Stopping VM {vm_name}"
def azure_function(req):
"""Azure Function to scale based on Azure metrics."""
# Fetch environment variables
subscription_id = os.getenv('AZURE_SUBSCRIPTION_ID')
resource_group = os.getenv('AZURE_RESOURCE_GROUP')
vm_name = os.getenv('AZURE_VM_NAME')
metric_name = os.getenv('AZURE_METRIC_NAME', 'NetworkIn')
scale_up_threshold = int(os.getenv('AZURE_SCALE_UP_THRESHOLD', 50))
scale_down_threshold = int(os.getenv('AZURE_SCALE_DOWN_THRESHOLD', 10))
if not subscription_id or not resource_group or not vm_name:
return json.dumps({'status': 'error', 'message': 'Missing environment variables.'})
# Get metrics from Azure Monitor
metric_value = get_azure_metrics(subscription_id, resource_group, vm_name, metric_name)
# Scaling decision based on thresholds
if metric_value > scale_up_threshold:
scale_message = scale_vm(subscription_id, resource_group, vm_name, scale_up=True)
elif metric_value < scale_down_threshold:
scale_message = scale_vm(subscription_id, resource_group, vm_name, scale_up=False)
else:
scale_message = f"No scaling required. Current metric value: {metric_value}"
print(f"Metric value: {metric_value}, Scaling action: {scale_message}")
return json.dumps({'metric_value': metric_value, 'message': scale_message})