113 lines
3.5 KiB
Text
113 lines
3.5 KiB
Text
# Hetzner Cloud usage and quota tracking
|
|
use api.nu *
|
|
|
|
# Get account usage statistics
|
|
export def hetzner_get_usage_stats []: nothing -> record {
|
|
let servers = (hetzner_api_list_servers)
|
|
let volumes = (hetzner_api_list_volumes)
|
|
let floating_ips = (hetzner_api_list_floating_ips)
|
|
let networks = (hetzner_api_list_networks)
|
|
{
|
|
servers: ($servers | length)
|
|
volumes: ($volumes | length)
|
|
floating_ips: ($floating_ips | length)
|
|
networks: ($networks | length)
|
|
timestamp: (now)
|
|
}
|
|
}
|
|
|
|
# Get server resource usage
|
|
export def hetzner_get_server_usage [server_id: string]: nothing -> record {
|
|
let server = (hetzner_api_server_info $server_id)
|
|
{
|
|
server_id: $server.id
|
|
name: $server.name
|
|
status: $server.status
|
|
type: ($server.server_type.name | default "")
|
|
cores: ($server.server_type.cores | default 0)
|
|
memory_gb: ($server.server_type.memory | default 0)
|
|
disk_gb: ($server.server_type.disk | default 0)
|
|
location: ($server.location.name | default "")
|
|
created: ($server.created | default "")
|
|
timestamp: (now)
|
|
}
|
|
}
|
|
|
|
# Calculate total resource usage cost
|
|
export def hetzner_calculate_usage_cost []: nothing -> record {
|
|
let servers = (hetzner_api_list_servers)
|
|
let volumes = (hetzner_api_list_volumes)
|
|
let server_cost = $servers | each {|s|
|
|
match $s.server_type.name {
|
|
"cx11" => 0.0070
|
|
"cx21" => 0.0140
|
|
"cx31" => 0.0280
|
|
"cx41" => 0.0560
|
|
"cx51" => 0.1120
|
|
"cpx11" => 0.0140
|
|
"cpx21" => 0.0300
|
|
"cpx31" => 0.0600
|
|
"cpx41" => 0.1200
|
|
"cpx51" => 0.2400
|
|
"ccx13" => 0.0410
|
|
"ccx23" => 0.0820
|
|
"ccx33" => 0.1640
|
|
"ccx43" => 0.3280
|
|
"ccx53" => 0.6560
|
|
_ => 0.0
|
|
}
|
|
} | math sum
|
|
let monthly_server = $server_cost * 730
|
|
let volume_size = $volumes | each {|v| $v.size | default 0} | math sum
|
|
let monthly_volume = $volume_size * 0.085
|
|
{
|
|
servers_count: ($servers | length)
|
|
monthly_server_cost: $monthly_server
|
|
volumes_size_gb: $volume_size
|
|
monthly_volume_cost: $monthly_volume
|
|
total_monthly_cost: ($monthly_server + $monthly_volume)
|
|
total_monthly_with_tax: (($monthly_server + $monthly_volume) * 1.19)
|
|
currency: "EUR"
|
|
hourly_equivalent: (($monthly_server + $monthly_volume) / 730)
|
|
}
|
|
}
|
|
|
|
# Format usage report
|
|
export def hetzner_format_usage_report [stats: record]: nothing -> string {
|
|
if ("error" in $stats) {
|
|
return "Could not fetch usage statistics"
|
|
}
|
|
|
|
let report = $"
|
|
Hetzner Cloud Usage Statistics
|
|
==============================
|
|
Servers: ($stats.servers)
|
|
Volumes: ($stats.volumes)
|
|
Floating IPs: ($stats.floating_ips)
|
|
Networks: ($stats.networks)
|
|
|
|
Last Updated: ($stats.timestamp)
|
|
"
|
|
|
|
$report
|
|
}
|
|
|
|
# Format cost report
|
|
export def hetzner_format_cost_report [cost: record]: nothing -> string {
|
|
if ("error" in $cost) {
|
|
return "Could not calculate cost"
|
|
}
|
|
|
|
let report = $"
|
|
Hetzner Cloud Estimated Monthly Cost
|
|
=====================================
|
|
Servers: ($cost.servers_count) x €($cost.monthly_server_cost | math round -p 2)/month
|
|
Storage: ($cost.volumes_size_gb)GB x €($cost.monthly_volume_cost | math round -p 2)/month
|
|
Total (net): €($cost.total_monthly_cost | math round -p 2)/month
|
|
Total (with VAT): €($cost.total_monthly_with_tax | math round -p 2)/month
|
|
|
|
Hourly Equivalent: €($cost.hourly_equivalent | math round -p 4)/hour
|
|
"
|
|
|
|
$report
|
|
}
|