package proxmox import ( "errors" "fmt" "sort" "strings" ) // PveVersion represents the version information of a PVE (Proxmox Virtual Environment). type PveVersion struct { Version string `json:"version"` // PVE version number. RepoID string `json:"repoid"` // Repository ID of the PVE. Release string `json:"release"` // Release version of the PVE. } // PveNodeStatus represents the status of a PVE node. type PveNodeStatus struct { IP string `json:"ip"` // IP address of the node. Name string `json:"name"` // Name of the node. Online uint8 `json:"online"` // Online status of the node (1 = online, 0 = offline). Local uint8 `json:"local"` // Local status of the node (1 = local, 0 = not local). NodeID int `json:"nodeid"` // Unique identifier for the node. Type string `json:"type"` // Type of the resource (e.g., "node"). Level string `json:"level"` // Subscription level of the node. ID string `json:"id"` // Unique identifier for the node resource. } // PveClusterStatus represents the status of a PVE cluster. type PveClusterStatus struct { Name string `json:"name"` // Name of the cluster. ID string `json:"id"` // Unique identifier for the cluster resource. Type string `json:"type"` // Type of the resource (e.g., "cluster"). Version uint16 `json:"version"` // Version of the cluster. Quorate uint8 `json:"quorate"` // Quorate status of the cluster (1 = quorate, 0 = not quorate). Nodes uint16 `json:"nodes"` // Number of nodes in the cluster. NodeStatuses []PveNodeStatus `json:"-"` // Cluster nodes statuses. } // PveResource represents a generic PVE resource object. type PveResource struct { Type string `mapstructure:"type"` // Type of resource (e.g., "lxc", "qemu", "node", "storage", "sdn"). Node string `mapstructure:"node"` // Node where the resource is located. Status string `mapstructure:"status"` // Status of the resource (e.g., "running", "stopped", "online", "available"). ID string `mapstructure:"id"` // Unique identifier for the resource. } // PveLxcResource represents a PVE LXC container resource. type PveLxcResource struct { PveResource DiskRead uint64 `mapstructure:"diskread"` // Disk read bytes. MaxDisk uint64 `mapstructure:"maxdisk"` // Maximum disk size in bytes. CPU float64 `mapstructure:"cpu"` // CPU usage. NetOut uint64 `mapstructure:"netout"` // Network output bytes. Uptime uint32 `mapstructure:"uptime"` // Uptime in seconds. NetIn uint64 `mapstructure:"netin"` // Network input bytes. MaxCPU uint16 `mapstructure:"maxcpu"` // Maximum number of CPUs. Disk uint64 `mapstructure:"disk"` // Disk usage in bytes. Tags string `mapstructure:"tags"` // Tags associated with the container. VMID uint16 `mapstructure:"vmid"` // Virtual Machine ID. DiskWrite uint64 `mapstructure:"diskwrite"` // Disk write bytes. Name string `mapstructure:"name"` // Name of the container. Template uint8 `mapstructure:"template"` // Template status (0 = not a template, 1 = template). MaxMem uint64 `mapstructure:"maxmem"` // Maximum memory in bytes. ID string `mapstructure:"id"` // Unique identifier for the resource. } // PveQemuResource represents a PVE QEMU virtual machine resource. type PveQemuResource struct { PveResource DiskRead uint64 `mapstructure:"diskread"` // Disk read bytes. MaxDisk uint64 `mapstructure:"maxdisk"` // Maximum disk size in bytes. NetOut uint64 `mapstructure:"netout"` // Network output bytes. Uptime uint32 `mapstructure:"uptime"` // Uptime in seconds. NetIn uint64 `mapstructure:"netin"` // Network input bytes. MaxCPU uint16 `mapstructure:"maxcpu"` // Maximum number of CPUs. Disk uint64 `mapstructure:"disk"` // Disk usage in bytes. Mem uint64 `mapstructure:"mem"` // Memory usage in bytes. Name string `mapstructure:"name"` // Name of the virtual machine. Template uint8 `mapstructure:"template"` // Template status (0 = not a template, 1 = template). MaxMem uint64 `mapstructure:"maxmem"` // Maximum memory in bytes. } // PveNodeResource represents a PVE node resource. type PveNodeResource struct { PveResource MaxMem uint64 `mapstructure:"maxmem"` // Maximum memory in bytes. MaxCPU uint16 `mapstructure:"maxcpu"` // Maximum number of CPUs. Level string `mapstructure:"level"` // Subscription level. CPU float64 `mapstructure:"cpu"` // CPU usage. Uptime uint32 `mapstructure:"uptime"` // Uptime in seconds. Disk uint64 `mapstructure:"disk"` // Disk usage in bytes. Mem uint64 `mapstructure:"mem"` // Memory usage in bytes. CGroupMode uint8 `mapstructure:"cgroup-mode"` // Control group mode. } // PveStorageResource represents a PVE storage resource. type PveStorageResource struct { PveResource Storage string `mapstructure:"storage"` // Name of the storage. MaxDisk uint64 `mapstructure:"maxdisk"` // Maximum disk size in bytes. Shared uint64 `mapstructure:"shared"` // Shared storage status (0 = not shared, 1 = shared). PluginType string `mapstructure:"plugintype"` // Type of storage plugin. Status string `mapstructure:"status"` // Status of the storage (e.g., "available"). Content string `mapstructure:"content"` // Types of content stored (e.g., "rootdir,images"). } // PveSdnResource represents a PVE software-defined network (SDN) resource. type PveSdnResource struct { PveResource SDN string `mapstructure:"sdn"` // Name of the SDN. } // PVE resources. type PveResources struct { CTs []PveLxcResource // LXC container resources. VMs []PveQemuResource // Virtual machine container resources. Nodes []PveNodeResource // Node resources. Storages []PveStorageResource // Storage resources. SDNs []PveSdnResource // Software defiend network resources. } // PveNodeRootfs represents root filesystem stats. type PveNodeRootfs struct { Free uint64 `json:"free"` // Free space in bytes. Total uint64 `json:"total"` // Total space in bytes. Used uint64 `json:"used"` // Used space in bytes. Avail uint64 `json:"avail"` // Available space in bytes. } // PveNodeMemory represents memory stats. type PveNodeMemory struct { Total uint64 `json:"total"` // Total memory in bytes. Free uint64 `json:"free"` // Free memory in bytes. Used uint64 `json:"used"` // Used memory in bytes. } // PveNodeBootInfo represents boot information. type PveNodeBootInfo struct { Mode string `json:"mode"` // Boot mode (e.g., "efi"). Secureboot int `json:"secureboot"` // Secure boot status (0 = disabled, 1 = enabled). } // PveNodeKsm represents Kernel Same-page Merging (KSM) info. type PveNodeKsm struct { Shared int64 `json:"shared"` // Amount of shared memory in bytes. } // PveNodeCurrentKernel represents current kernel info. type PveNodeCurrentKernel struct { Machine string `json:"machine"` // Machine architecture (e.g., "x86_64"). Sysname string `json:"sysname"` // Operating system name (e.g., "Linux"). Release string `json:"release"` // Kernel release version. Version string `json:"version"` // Detailed kernel version info. } // PveNodeSwap represents swap memory stats. type PveNodeSwap struct { Used uint64 `json:"used"` // Used swap space in bytes. Free uint64 `json:"free"` // Free swap space in bytes. Total uint64 `json:"total"` // Total swap space in bytes. } // PveNodeCPUInfo represents CPU information. type PveNodeCPUInfo struct { Flags string `json:"flags"` // CPU flags. Cores int `json:"cores"` // Number of CPU cores. MHz string `json:"mhz"` // CPU frequency in MHz. Model string `json:"model"` // CPU model. Sockets int `json:"sockets"` // Number of CPU sockets. UserHZ float64 `json:"user_hz"` // User HZ value. CPUs int `json:"cpus"` // Number of logical CPUs. HVM string `json:"hvm"` // Hardware virtualization support. } // PveNodeStatusDetail represents detailed status of a PVE node. type PveNodeStatusDetail struct { Rootfs PveNodeRootfs `json:"rootfs"` // Root filesystem stats. Wait float64 `json:"wait"` // CPU wait time. Memory PveNodeMemory `json:"memory"` // Memory stats. BootInfo PveNodeBootInfo `json:"boot-info"` // Boot information. Ksm PveNodeKsm `json:"ksm"` // Kernel Same-page Merging (KSM) info. Kversion string `json:"kversion"` // Kernel version. LoadAvg []string `json:"loadavg,string"` // Load average values. CPUInfo PveNodeCPUInfo `json:"cpuinfo"` // CPU information. PveVersion string `json:"pveversion"` // Proxmox VE version. Uptime uint64 `json:"uptime"` // System uptime in seconds. Idle uint64 `json:"idle"` // Idle time in seconds. CurrentKernel PveNodeCurrentKernel `json:"current-kernel"` // Current kernel information. CPU float64 `json:"cpu"` // CPU usage. Swap PveNodeSwap `json:"swap"` // Swap memory stats. } // PveSubscription represents PVE node subscription info. type PveSubscription struct { NextDueDate string `json:"nextduedate"` // Next due date for subscription (format: YYYY-MM-DD). ProductName string `json:"productname"` // Name of product associated with subscription. ServerID string `json:"serverid"` // Unique identifier of server. Registration string `json:"regdate"` // Registration date of subscription (format: YYYY-MM-DD HH:MM:SS). Sockets uint8 `json:"sockets"` // Number of CPU sockets covered by subscription. CheckTime uint `json:"checktime"` // Unix timestamp of last check time. URL string `json:"url"` // URL with more information about subscription. Level string `json:"level"` // Subscription level (e.g., "c" for community). Key string `json:"key"` // Subscription key. Status string `json:"status"` // Status of subscription (e.g., "active"). } // PVE node disk. type PveDisk struct { WWN string `json:"wwn"` // WWN (World Wide Name) of the drive. Type string `json:"type"` // Type of drive (e.g., ssd, hdd). Model string `json:"model"` // Model of the drive. Serial string `json:"serial"` // Serial number of the drive. Size int64 `json:"size"` // Size of the drive in bytes. Vendor string `json:"vendor"` // Vendor of the drive. OSDID int `json:"osdid"` // OSD (Object Storage Device) ID of the drive. DevPath string `json:"devpath"` // Device path of the drive. OSDIDList []int `json:"osdid-list"` // List of OSD IDs associated with the drive. ByIDLink string `json:"by_id_link"` // Symbolic link to the drive in /dev/disk/by-id directory. GPT int `json:"gpt"` // Indicates whether the drive uses GPT partitioning (1 for true, 0 for false). Health string `json:"health"` // Health status of the drive (e.g., PASSED, FAILED, OK). WearOut interface{} `json:"wearout,omitempty"` // Wear out percentage of the drive. Used string `json:"used,omitempty"` // How the drive is used (optional field). } // PVE node time. type PveNodeTime struct { Time uint64 `json:"time"` // Unix timestamp in UTC. LocalTime uint64 `json:"localtime"` // Unix timestamp in local time. Timezone string `json:"timezone"` // Timezone information. } // PVE storage information. type PveStorage struct { Shared int `json:"shared"` // Indicates if the storage is shared between multiple nodes. Enabled int `json:"enabled"` // Indicates if the storage is enabled. Storage string `json:"storage"` // Name of the storage. Total int64 `json:"total"` // Total storage capacity in bytes. Content string `json:"content"` // Type of content stored (e.g., backup, ISO, vztmpl). Avail int64 `json:"avail"` // Available storage capacity in bytes. Active int `json:"active"` // Indicates if the storage is active. Used int64 `json:"used"` // Used storage capacity in bytes. UsedFraction float64 `json:"used_fraction"` // Fraction of used storage. Type string `json:"type"` // Type of storage (e.g., pbs, zfspool, rbd, lvm). } // PveLXCStatus represents the status information of a PVE LXC container. type PveLxcStatus struct { Status string `json:"status"` // Status of the LXC container (e.g., "running", "stopped"). DiskRead uint64 `json:"diskread"` // Amount of disk read by the LXC container. DiskWrite uint64 `json:"diskwrite"` // Amount of disk write by the LXC container. VMID string `json:"vmid"` // Virtual Machine ID of the LXC container. MaxSwap uint64 `json:"maxswap"` // Maximum swap space allocated for the LXC container. PID uint32 `json:"pid"` // Process ID of the LXC container. Disk uint64 `json:"disk"` // Disk usage of the LXC container. Type string `json:"type"` // Type of the container (e.g., "lxc"). MaxMem uint64 `json:"maxmem"` // Maximum memory allocated for the LXC container. Name string `json:"name"` // Name of the LXC container. NetIn uint64 `json:"netin"` // Network incoming traffic of the LXC container. CPU float64 `json:"cpu"` // CPU usage of the LXC container. Uptime uint32 `json:"uptime"` // Uptime of the LXC container in seconds. NetOut uint64 `json:"netout"` // Network outgoing traffic of the LXC container. Ha PveHaStatus `json:"ha"` // High Availability configuration for the LXC container. CPUs int `json:"cpus"` // Number of CPUs allocated for the LXC container. Swap uint64 `json:"swap"` // Swap usage of the LXC container. Mem uint64 `json:"mem"` // Memory usage of the LXC container. MaxDisk uint64 `json:"maxdisk"` // Maximum disk space allocated for the LXC container. Template int `json:"template"` // Template status (0 = not a template, 1 = template). } // PveQemuStatus represents the status of a Proxmox virtual machine. type PveQemuStatus struct { BalloonMin uint64 `json:"balloon_min"` // Minimum balloon memory in bytes. Pid int `json:"pid"` // Process ID. Disk uint64 `json:"disk"` // Disk usage in bytes. Status string `json:"status"` // Status of the virtual machine (e.g., "running"). DiskRead uint64 `json:"diskread"` // Disk read bytes. Tags string `json:"tags"` // Tags for the container. VMID int `json:"vmid"` // VM ID. DiskWrite uint64 `json:"diskwrite"` // Disk write bytes. CPUs int `json:"cpus"` // Number of CPUs. Shares int `json:"shares"` // CPU shares. MaxDisk uint64 `json:"maxdisk"` // Maximum disk size in bytes. Mem uint64 `json:"mem"` // Memory usage in bytes. Uptime int `json:"uptime"` // Uptime in seconds. NetOut uint64 `json:"netout"` // Network out bytes. Name string `json:"name"` // Name of the virtual machine. NetIn uint64 `json:"netin"` // Network in bytes. CPU float64 `json:"cpu"` // CPU usage. MaxMem uint64 `json:"maxmem"` // Maximum memory in bytes. Template int `json:"template"` // Template status (0 = not a template, 1 = template). } // Ha represents the High Availability configuration for a PVE resource. type PveHaStatus struct { Managed int `json:"managed"` // Whether HA is managed for the resource. } // PveBalloonInfo represents the balloon memory information. type PveBalloonInfo struct { LastUpdate int64 `json:"last_update"` // Last update time in Unix timestamp. MemSwappedIn uint64 `json:"mem_swapped_in"` // Memory swapped in, in bytes. TotalMem uint64 `json:"total_mem"` // Total memory in bytes. FreeMem uint64 `json:"free_mem"` // Free memory in bytes. MemSwappedOut uint64 `json:"mem_swapped_out"` // Memory swapped out, in bytes. MajorPageFaults uint64 `json:"major_page_faults"` // Number of major page faults. Actual uint64 `json:"actual"` // Actual memory in bytes. MaxMem uint64 `json:"max_mem"` // Maximum memory in bytes. MinorPageFaults uint64 `json:"minor_page_faults"` // Number of minor page faults. } // PveNic represents network interface controller statistics. type PveNic struct { NetOut uint64 `json:"netout"` // Network out bytes. NetIn uint64 `json:"netin"` // Network in bytes. } // PveBlockStat represents block device statistics. type PveBlockStat struct { ZoneAppendMerged uint64 `json:"zone_append_merged"` // Number of zone append merged operations. RdTotalTimeNs uint64 `json:"rd_total_time_ns"` // Total read time in nanoseconds. UnmapTotalTimeNs uint64 `json:"unmap_total_time_ns"` // Total unmap time in nanoseconds. RdMerged uint64 `json:"rd_merged"` // Number of read merged operations. TimedStats []interface{} `json:"timed_stats"` // Timed statistics (not detailed in sample). FlushOperations uint64 `json:"flush_operations"` // Number of flush operations. ZoneAppendOperations uint64 `json:"zone_append_operations"` // Number of zone append operations. RdOperations uint64 `json:"rd_operations"` // Number of read operations. FailedWrOperations uint64 `json:"failed_wr_operations"` // Number of failed write operations. FailedUnmapOperations uint64 `json:"failed_unmap_operations"` // Number of failed unmap operations. AccountInvalid bool `json:"account_invalid"` // Indicates if the account is invalid. WrTotalTimeNs uint64 `json:"wr_total_time_ns"` // Total write time in nanoseconds. InvalidUnmapOperations uint64 `json:"invalid_unmap_operations"` // Number of invalid unmap operations. WrMerged uint64 `json:"wr_merged"` // Number of write merged operations. AccountFailed bool `json:"account_failed"` // Indicates if the account failed. InvalidZoneAppendOperations uint64 `json:"invalid_zone_append_operations"` // Number of invalid zone append operations. WrHighestOffset uint64 `json:"wr_highest_offset"` // Highest write offset. WrOperations uint64 `json:"wr_operations"` // Number of write operations. UnmapMerged uint64 `json:"unmap_merged"` // Number of unmap merged operations. InvalidFlushOperations uint64 `json:"invalid_flush_operations"` // Number of invalid flush operations. WrBytes uint64 `json:"wr_bytes"` // Write bytes. UnmapBytes uint64 `json:"unmap_bytes"` // Unmap bytes. InvalidRdOperations uint64 `json:"invalid_rd_operations"` // Number of invalid read operations. ZoneAppendTotalTimeNs uint64 `json:"zone_append_total_time_ns"` // Total zone append time in nanoseconds. IdleTimeNs uint64 `json:"idle_time_ns"` // Idle time in nanoseconds. FailedZoneAppendOperations uint64 `json:"failed_zone_append_operations"` // Number of failed zone append operations. UnmapOperations uint64 `json:"unmap_operations"` // Number of unmap operations. FailedFlushOperations uint64 `json:"failed_flush_operations"` // Number of failed flush operations. ZoneAppendBytes uint64 `json:"zone_append_bytes"` // Zone append bytes. RdBytes uint64 `json:"rd_bytes"` // Read bytes. FailedRdOperations uint64 `json:"failed_rd_operations"` // Number of failed read operations. FlushTotalTimeNs uint64 `json:"flush_total_time_ns"` // Total flush time in nanoseconds. InvalidWrOperations uint64 `json:"invalid_wr_operations"` // Number of invalid write operations. } // PveProxmoxSupport represents Proxmox support features and versions. type PveProxmoxSupport struct { BackupMaxWorkers bool `json:"backup-max-workers"` // Indicates if backup max workers is supported. PbsMasterkey bool `json:"pbs-masterkey"` // Indicates if PBS master key is supported. PbsDirtyBitmapSavevm bool `json:"pbs-dirty-bitmap-savevm"` // Indicates if PBS dirty bitmap save VM is supported. PbsDirtyBitmapMigration bool `json:"pbs-dirty-bitmap-migration"` // Indicates if PBS dirty bitmap migration is supported. PbsDirtyBitmap bool `json:"pbs-dirty-bitmap"` // Indicates if PBS dirty bitmap is supported. QueryBitmapInfo bool `json:"query-bitmap-info"` // Indicates if querying bitmap info is supported. PbsLibraryVersion string `json:"pbs-library-version"` // PBS library version. } // PveQemuStatusDetail represents the detailed status of a Proxmox QEMU virtual machine. type PveQemuStatusDetail struct { VMID int `json:"vmid"` // VM ID. DiskWrite uint64 `json:"diskwrite"` // Disk write bytes. Status string `json:"status"` // Status of the VM (e.g., "running"). RunningQemu string `json:"running-qemu"` // Running QEMU version. RunningMachine string `json:"running-machine"` // Running machine type. ProxmoxSupport PveProxmoxSupport `json:"proxmox-support"` // Proxmox support features and versions. Clipboard interface{} `json:"clipboard"` // Clipboard status (could be null). BalloonMin uint64 `json:"balloon_min"` // Minimum balloon memory in bytes. Agent int `json:"agent"` // Agent status. Name string `json:"name"` // Name of the VM. NetIn uint64 `json:"netin"` // Network in bytes. Ha PveHaStatus `json:"ha"` // High availability configuration. BalloonInfo PveBalloonInfo `json:"ballooninfo"` // Balloon memory information. MaxDisk uint64 `json:"maxdisk"` // Maximum disk size in bytes. Tags string `json:"tags"` // Tags for the VM. DiskRead uint64 `json:"diskread"` // Disk read bytes. Nics map[string]PveNic `json:"nics"` // Network interface controllers statistics. BlockStat map[string]PveBlockStat `json:"blockstat"` // Block device statistics. QmpStatus string `json:"qmpstatus"` // QMP status of the VM. Disk uint64 `json:"disk"` // Disk usage in bytes. Pid int `json:"pid"` // Process ID. Balloon uint64 `json:"balloon"` // Balloon memory in bytes. MaxMem uint64 `json:"maxmem"` // Maximum memory in bytes. CPU float64 `json:"cpu"` // CPU usage. NetOut uint64 `json:"netout"` // Network out bytes. Uptime int `json:"uptime"` // Uptime in seconds. FreeMem uint64 `json:"freemem"` // Free memory in bytes. Shares int `json:"shares"` // CPU shares. CPUs int `json:"cpus"` // Number of CPUs. Mem uint64 `json:"mem"` // Memory usage in bytes. Template int `json:"template"` // Template status (0 = not a template, 1 = template). } // FindNode searches for a node by its name in the PVE resources. // Returns a pointer to the PveNodeResource if found, otherwise returns an error. func (r *PveResources) FindNode(nodeName string) (*PveNodeResource, error) { for i := range r.Nodes { if r.Nodes[i].Node == nodeName { return &r.Nodes[i], nil } } return nil, errors.New("Unable to find node '" + nodeName + "' in node resources.") } // FindNodeSDN searches for SDN resources associated with a specific node name in the PVE resources. // Returns a slice of PveSdnResource pointers containing all matching SDN resources. func (r *PveResources) FindNodeSDN(nodeName string) *[]PveSdnResource { var sdns []PveSdnResource for _, sdn := range r.SDNs { if sdn.Node == nodeName { sdns = append(sdns, sdn) } } return &sdns } // GetStatusNumeric returns the numeric status of an SDN resource. // Returns 1 if the status is "ok", otherwise returns 0. func (r *PveSdnResource) GetStatusNumeric() float64 { if r.Status == "ok" { return 1 } return 0 } // GetActiveNumeric returns the numeric state of a subscription. // Returns 1 if the subscription status is "active", otherwise returns 0. func (r *PveSubscription) GetActiveNumeric() float64 { if r.Status == "active" { return 1 } return 0 } // GetSmartPassedState returns the numeric health state of a disk. // Returns 1 if the disk health status is "OK" or "PASSED", otherwise returns 0. func (r *PveDisk) GetSmartPassedState() float64 { switch r.Health { case "OK", "PASSED": return 1 default: return 0 } } // GetStatusNumeric returns a numeric representation of the LXC container's status. // If the container status is "running", it returns 1. Otherwise, it returns 0. func (r *PveLxcStatus) GetStatusNumeric() float64 { if r.Status == "running" { return 1 } return 0 } // GetStatusNumeric returns a numeric representation of the virtual machine status. // If the virtual machine status is "running", it returns 1. Otherwise, it returns 0. func (r *PveQemuStatus) GetStatusNumeric() float64 { if r.Status == "running" { return 1 } return 0 } // IsRunning checks if LXC container is running. // If the LXC container status is "running", it returns true. Otherwise, it returns false. func (r *PveLxcStatus) IsRunning() bool { if r.Status == "running" { return true } return false } // IsRunning checks if virtual machine is running. // If the virtual machine status is "running", it returns true. Otherwise, it returns false. func (r *PveQemuStatus) IsRunning() bool { if r.Status == "running" { return true } return false } // GetClusterName returns name of cluster for labeling purposes. // If there is no cluster then return standalone node with node name. Otherwise, it returns cluster name. func (r *PveClusterStatus) GetClusterName() string { if r.Name == "" { return fmt.Sprintf("Standalone node - %s", r.NodeStatuses[0].Name) } return r.Name } // GetContentSorted returns sorted storage content. // This is crucial for labeling because PVE API returns these walues randomly sorted. func (r *PveStorage) GetContentSorted() string { parts := strings.Split(r.Content, ",") sort.Strings(parts) return strings.Join(parts, ",") }