Last active
October 20, 2023 17:17
-
-
Save kfatehi/d5d4aa988fdf1ecdee6bf048f72ea3ab to your computer and use it in GitHub Desktop.
Get a Proxmox VM's name via MAC address via API and use it in a telegraf override
This file contains 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
#!/bin/bash | |
PROXMOX_SERVER="pve:8006" | |
USERNAME="root@pam" | |
PASSWORD="<your pve password>" | |
NODE="pve" | |
INTERNAL_BRIDGE="vmbr1" | |
NIC="ens18" # nic name inside the vm to be templatized | |
MAC_ADDRESS="$(ip addr show $NIC | grep -m 1 -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | tr 'a-f' 'A-F' | head -n1)" | |
# Step 1: Login and get ticket and CSRF token | |
login_data=$(curl -k -s -d "username=$USERNAME&password=$PASSWORD" "https://$PROXMOX_SERVER/api2/json/access/ticket") | |
ticket=$(echo $login_data | jq -r '.data.ticket') | |
csrf_token=$(echo $login_data | jq -r '.data.CSRFPreventionToken') | |
# Step 2: List all VMs | |
vm_list=$(curl -k -s -b "PVEAuthCookie=$ticket" "https://$PROXMOX_SERVER/api2/json/cluster/resources?type=vm") | |
# Step 3: Loop through each VM to find the MAC address | |
vm_ids=$(echo $vm_list | jq -r '.data[].vmid') | |
for vm_id in $vm_ids; do | |
vm_config=$(curl -k -s -b "PVEAuthCookie=$ticket" "https://$PROXMOX_SERVER/api2/json/nodes/$NODE/qemu/$vm_id/config" | jq '.data') | |
net_keys=$(echo $vm_config | jq -r 'keys[] as $k | if $k | startswith("net") then $k else empty end') | |
for net_key in $net_keys; do | |
net_val=$(echo $vm_config | jq -r ".$net_key") | |
vm_name=$(echo $vm_config | jq -r ".name") | |
if [[ "$net_val" =~ .*"virtio=$MAC_ADDRESS".* && "$net_val" =~ .*"bridge=$INTERNAL_BRIDGE".* ]]; then | |
echo $vm_name | |
exit 0 | |
fi | |
done | |
done |
This file contains 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
[Service] | |
ExecStart= | |
ExecStart=/usr/src/pvename/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d $TELEGRAF_OPTS |
This file contains 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
#!/bin/sh | |
export HOSTNAME=$(pvename) | |
exec /usr/bin/telegraf $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment