Compare commits
1
Commits
master
..
f48cec73f3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f48cec73f3 |
No files matched your search
@@ -1 +0,0 @@
|
|||||||
result
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
# pve-local-exporter
|
|
||||||
|
|
||||||
Proxmox VE local metrics exporter for Prometheus, written in Go. Collects VM and storage metrics directly from the host (no PVE API) by reading /proc, /sys, /etc/pve, and running `qm monitor`.
|
|
||||||
|
|
||||||
Reimplementation of the Python exporter at `../pvemon`.
|
|
||||||
|
|
||||||
## Build & Test
|
|
||||||
|
|
||||||
```sh
|
|
||||||
nix develop # shell with go + gopls
|
|
||||||
nix develop -c go test ./... # run tests (from src/)
|
|
||||||
nix build # build static binary
|
|
||||||
nix flake check # evaluate + check flake outputs
|
|
||||||
```
|
|
||||||
|
|
||||||
Go source lives in `src/`. The Nix build uses `buildGoModule` with `CGO_ENABLED=0`.
|
|
||||||
|
|
||||||
After changing go.mod dependencies, run `go mod tidy` then update `vendorHash` in `default.nix` (set to a bogus hash, run `nix build`, copy the correct hash from the error).
|
|
||||||
|
|
||||||
## Architecture
|
|
||||||
|
|
||||||
All I/O is behind interfaces for testability. No test touches the real filesystem or runs real commands.
|
|
||||||
|
|
||||||
```
|
|
||||||
src/
|
|
||||||
main.go HTTP server, signal handling, log setup
|
|
||||||
internal/
|
|
||||||
config/ CLI flag parsing -> Config struct
|
|
||||||
cache/ TTLCache[K,V] (with jitter), MtimeCache[V] (file change detection)
|
|
||||||
procfs/ /proc parsing: process discovery, CPU, IO, memory, threads, ctx switches
|
|
||||||
sysfs/ /sys reading: NIC stats, block device sizes
|
|
||||||
qmmonitor/ qm monitor pipe I/O + TTL cache, network/block output parsers
|
|
||||||
pveconfig/ /etc/pve parsers: user.cfg (pools), storage.cfg (storage defs)
|
|
||||||
storage/ statvfs wrapper, zpool list parser
|
|
||||||
collector/ Prometheus Collector wiring all packages together
|
|
||||||
```
|
|
||||||
|
|
||||||
### Key interfaces (defined alongside their real implementations)
|
|
||||||
|
|
||||||
- `procfs.ProcReader` -- process discovery and /proc/{pid}/* parsing
|
|
||||||
- `sysfs.SysReader` -- /sys/class/net stats, /sys/block sizes
|
|
||||||
- `qmmonitor.QMMonitor` -- `RunCommand(vmid, cmd)`, `InvalidateCache(vmid, cmd)`
|
|
||||||
- `storage.StatFS` -- `Statfs(path)` for dir/nfs/cephfs sizing
|
|
||||||
- `collector.CommandRunner` -- `Run(name, args...)` for zpool list
|
|
||||||
- `collector.FileReaderIface` -- `ReadFile(path)` for config files
|
|
||||||
|
|
||||||
### Concurrency
|
|
||||||
|
|
||||||
Bounded goroutine pool (16 workers) for parallel NIC + disk collection per VM, matching the Python ThreadPoolExecutor(max_workers=16).
|
|
||||||
|
|
||||||
## Conventions
|
|
||||||
|
|
||||||
- Indent nix files with tabs
|
|
||||||
- No emoji in code or text
|
|
||||||
- Tests use fixture data / mock interfaces, never real /proc or /sys
|
|
||||||
- Parser functions are exported and pure (take string, return parsed struct) so tests are straightforward
|
|
||||||
- `storage.cfg` parser: section headers start at column 0, properties are indented (tab or space)
|
|
||||||
- `user.cfg` pool format: `pool:name:comment:vmlist` (colon-separated, vmlist is comma-separated VM IDs)
|
|
||||||
- Memory extended label keys are lowercase without trailing colon (e.g., `vmrss`, `vmpeak`)
|
|
||||||
|
|
||||||
## CLI Flags
|
|
||||||
|
|
||||||
| Flag | Default | Description |
|
|
||||||
|------|---------|-------------|
|
|
||||||
| `--port` | 9116 | HTTP listen port |
|
|
||||||
| `--host` | 0.0.0.0 | Bind address |
|
|
||||||
| `--collect-running-vms` | true | Collect KVM VM metrics |
|
|
||||||
| `--collect-storage` | true | Collect storage pool metrics |
|
|
||||||
| `--metrics-prefix` | pve | Prefix for all metric names |
|
|
||||||
| `--loglevel` | INFO | DEBUG, INFO, WARNING, ERROR |
|
|
||||||
| `--qm-terminal-timeout` | 10s | qm monitor command timeout |
|
|
||||||
| `--qm-max-ttl` | 600s | TTL cache for qm monitor data |
|
|
||||||
| `--qm-rand` | 60s | Jitter range for cache expiry |
|
|
||||||
| `--qm-monitor-defer-close` | true | Defer closing unresponsive qm sessions |
|
|
||||||
| `--version` | | Print version and exit |
|
|
||||||
@@ -1,117 +1,2 @@
|
|||||||
# pve-local-exporter
|
# pve_local_exporter
|
||||||
|
|
||||||
Prometheus exporter for Proxmox VE that collects VM and storage metrics directly from the host without using the PVE API. Written in Go.
|
|
||||||
|
|
||||||
Metrics are gathered by reading `/proc`, `/sys`, `/etc/pve`, and running `qm monitor` commands.
|
|
||||||
|
|
||||||
> **Disclaimer:** This is a heavily vibe-coded rewrite of [pvemon](https://github.com/illustris/pvemon) for better maintainability and easier distribution. This disclaimer will remain up until the codebase has been reviewed and validated.
|
|
||||||
|
|
||||||
## Building
|
|
||||||
|
|
||||||
Requires [Nix](https://nixos.org/):
|
|
||||||
|
|
||||||
```sh
|
|
||||||
nix build # build static binary
|
|
||||||
nix flake check # evaluate flake + run go vet
|
|
||||||
```
|
|
||||||
|
|
||||||
The output binary is statically linked (`CGO_ENABLED=0`).
|
|
||||||
|
|
||||||
## Development
|
|
||||||
|
|
||||||
```sh
|
|
||||||
nix develop # shell with go + gopls
|
|
||||||
nix develop -c go test ./... -race # run tests with race detector
|
|
||||||
nix develop -c go vet ./... # static analysis
|
|
||||||
```
|
|
||||||
|
|
||||||
Go source lives in `src/`. After changing `go.mod` dependencies, run `go mod tidy` then update `vendorHash` in `default.nix` (set to a bogus hash, run `nix build`, copy the correct hash from the error).
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```sh
|
|
||||||
pve_local_exporter [flags]
|
|
||||||
```
|
|
||||||
|
|
||||||
### Flags
|
|
||||||
|
|
||||||
| Flag | Default | Description |
|
|
||||||
|------|---------|-------------|
|
|
||||||
| `--port` | 9116 | HTTP listen port |
|
|
||||||
| `--host` | 0.0.0.0 | Bind address |
|
|
||||||
| `--collect-running-vms` | true | Collect KVM VM metrics |
|
|
||||||
| `--collect-storage` | true | Collect storage pool metrics |
|
|
||||||
| `--metrics-prefix` | pve | Prefix for all metric names |
|
|
||||||
| `--loglevel` | INFO | DEBUG, INFO, WARNING, ERROR |
|
|
||||||
| `--qm-terminal-timeout` | 10s | qm monitor command timeout |
|
|
||||||
| `--qm-max-ttl` | 600s | TTL cache for qm monitor data |
|
|
||||||
| `--qm-rand` | 60s | Jitter range for cache expiry |
|
|
||||||
| `--qm-monitor-defer-close` | true | Defer closing unresponsive qm sessions |
|
|
||||||
| `--version` | | Print version and exit |
|
|
||||||
|
|
||||||
## Exported metrics
|
|
||||||
|
|
||||||
All metric names are prefixed with the configured `--metrics-prefix` (default `pve`).
|
|
||||||
|
|
||||||
### Per-VM metrics
|
|
||||||
|
|
||||||
| Metric | Type | Labels | Description |
|
|
||||||
|--------|------|--------|-------------|
|
|
||||||
| `_kvm_info` | gauge | id, name, cpu, pid, pool, pool_levels, pool1, pool2, pool3 | VM info (value 1) |
|
|
||||||
| `_kvm_cpu_seconds_total` | counter | id, mode | KVM CPU time (mode: user, system, iowait) |
|
|
||||||
| `_kvm_vcores` | gauge | id | vCores allocated |
|
|
||||||
| `_kvm_maxmem_bytes` | gauge | id | Maximum memory in bytes |
|
|
||||||
| `_kvm_memory_percent` | gauge | id | Memory percent of host |
|
|
||||||
| `_kvm_memory_extended` | gauge | id, type | Extended memory info from /proc status (vmrss, vmpeak, etc.) |
|
|
||||||
| `_kvm_threads` | gauge | id | Threads used |
|
|
||||||
| `_kvm_ctx_switches_total` | counter | id, type | Context switches (type: voluntary, involuntary) |
|
|
||||||
| `_kvm_io_read_count_total` | counter | id | Read system calls by KVM process |
|
|
||||||
| `_kvm_io_read_bytes_total` | counter | id | Bytes read from disk by KVM process |
|
|
||||||
| `_kvm_io_read_chars_total` | counter | id | Bytes read including buffers by KVM process |
|
|
||||||
| `_kvm_io_write_count_total` | counter | id | Write system calls by KVM process |
|
|
||||||
| `_kvm_io_write_bytes_total` | counter | id | Bytes written to disk by KVM process |
|
|
||||||
| `_kvm_io_write_chars_total` | counter | id | Bytes written including buffers by KVM process |
|
|
||||||
| `_kvm_nic_info` | gauge | id, ifname, netdev, queues, type, model, macaddr | NIC info (value 1) |
|
|
||||||
| `_kvm_nic_queues` | gauge | id, ifname | NIC queue count |
|
|
||||||
| `_kvm_nic_{stat}_total` | counter | id, ifname | Per-NIC sysfs counters (rx_bytes, tx_bytes, rx_packets, etc.) |
|
|
||||||
| `_kvm_disk_info` | gauge | id, disk_name, block_id, disk_path, disk_type, vol_name, pool, pool_name, cluster_id, vg_name, device, attached_to, cache_mode, detect_zeroes, read_only | Disk info (value 1) |
|
|
||||||
| `_kvm_disk_size_bytes` | gauge | id, disk_name | Disk size in bytes |
|
|
||||||
|
|
||||||
### Storage metrics
|
|
||||||
|
|
||||||
| Metric | Type | Labels | Description |
|
|
||||||
|--------|------|--------|-------------|
|
|
||||||
| `_node_storage_info` | gauge | (dynamic, varies by storage config) | Storage pool info (value 1) |
|
|
||||||
| `_node_storage_size_bytes` | gauge | name, type | Storage total size in bytes |
|
|
||||||
| `_node_storage_free_bytes` | gauge | name, type | Storage free space in bytes |
|
|
||||||
|
|
||||||
### Operational metrics
|
|
||||||
|
|
||||||
| Metric | Type | Labels | Description |
|
|
||||||
|--------|------|--------|-------------|
|
|
||||||
| `_scrape_duration_seconds` | gauge | | Duration of metrics collection |
|
|
||||||
| `_exporter_build_info` | gauge | version | Build information (value 1) |
|
|
||||||
|
|
||||||
## Architecture
|
|
||||||
|
|
||||||
All I/O is behind interfaces for testability. No test touches the real filesystem or runs real commands.
|
|
||||||
|
|
||||||
```
|
|
||||||
src/
|
|
||||||
main.go HTTP server, signal handling
|
|
||||||
internal/
|
|
||||||
config/ CLI flag parsing
|
|
||||||
cache/ TTLCache (with jitter), MtimeCache (file change detection)
|
|
||||||
procfs/ /proc parsing: process discovery, CPU, IO, memory, threads
|
|
||||||
sysfs/ /sys reading: NIC stats, block device sizes
|
|
||||||
qmmonitor/ qm monitor pipe I/O + TTL cache, network/block parsers
|
|
||||||
pveconfig/ /etc/pve parsers: pools (user.cfg), storage (storage.cfg)
|
|
||||||
storage/ statvfs wrapper, zpool list parser
|
|
||||||
collector/ Prometheus Collector wiring all packages together
|
|
||||||
```
|
|
||||||
|
|
||||||
Concurrent NIC and disk collection uses a bounded goroutine pool (16 workers).
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
See [LICENSE](LICENSE) if present.
|
|
||||||
-17
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
lib,
|
|
||||||
buildGoModule,
|
|
||||||
...
|
|
||||||
}:
|
|
||||||
|
|
||||||
buildGoModule rec {
|
|
||||||
pname = "pve-local-exporter";
|
|
||||||
version = "0.1.0";
|
|
||||||
src = ./src;
|
|
||||||
vendorHash = "sha256-MLB7y7shnOhxW8K2R6+d9E63wGEhlErnv+1MYOJO3Hw=";
|
|
||||||
ldflags = [
|
|
||||||
"-X=main.version=${version}"
|
|
||||||
];
|
|
||||||
env.CGO_ENABLED = 0;
|
|
||||||
meta.mainProgram = "pve_local_exporter";
|
|
||||||
}
|
|
||||||
Generated
-27
@@ -1,27 +0,0 @@
|
|||||||
{
|
|
||||||
"nodes": {
|
|
||||||
"nixpkgs": {
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1772963539,
|
|
||||||
"narHash": "sha256-9jVDGZnvCckTGdYT53d/EfznygLskyLQXYwJLKMPsZs=",
|
|
||||||
"owner": "NixOS",
|
|
||||||
"repo": "nixpkgs",
|
|
||||||
"rev": "9dcb002ca1690658be4a04645215baea8b95f31d",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "NixOS",
|
|
||||||
"ref": "nixos-unstable",
|
|
||||||
"repo": "nixpkgs",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"root": {
|
|
||||||
"inputs": {
|
|
||||||
"nixpkgs": "nixpkgs"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"root": "root",
|
|
||||||
"version": 7
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
{
|
|
||||||
description = "Proxmox VE local metrics exporter for Prometheus";
|
|
||||||
|
|
||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
|
||||||
|
|
||||||
outputs = { self, nixpkgs }: let
|
|
||||||
forAllSystems = nixpkgs.lib.genAttrs [
|
|
||||||
"x86_64-linux"
|
|
||||||
"aarch64-linux"
|
|
||||||
"riscv64-linux"
|
|
||||||
];
|
|
||||||
in {
|
|
||||||
packages = forAllSystems (system: let
|
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
|
||||||
in rec {
|
|
||||||
pve-local-exporter = pkgs.callPackage ./. {};
|
|
||||||
default = pve-local-exporter;
|
|
||||||
});
|
|
||||||
|
|
||||||
checks = forAllSystems (system: let
|
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
|
||||||
pkg = self.packages.${system}.pve-local-exporter;
|
|
||||||
in {
|
|
||||||
vet = pkgs.runCommand "go-vet" {
|
|
||||||
nativeBuildInputs = [ pkgs.go ];
|
|
||||||
inherit (pkg) src goModules;
|
|
||||||
CGO_ENABLED = 0;
|
|
||||||
} ''
|
|
||||||
export HOME=$TMPDIR
|
|
||||||
export GOPATH=$TMPDIR/go
|
|
||||||
workdir=$TMPDIR/src
|
|
||||||
mkdir -p $workdir
|
|
||||||
cp -r $src/* $workdir/
|
|
||||||
chmod -R u+w $workdir
|
|
||||||
ln -s $goModules $workdir/vendor
|
|
||||||
cd $workdir
|
|
||||||
go vet -mod=vendor ./...
|
|
||||||
touch $out
|
|
||||||
'';
|
|
||||||
});
|
|
||||||
|
|
||||||
devShells = forAllSystems (system: let
|
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
|
||||||
in {
|
|
||||||
default = pkgs.mkShell {
|
|
||||||
buildInputs = [ pkgs.go pkgs.gopls ];
|
|
||||||
};
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
-19
@@ -1,19 +0,0 @@
|
|||||||
module pve_local_exporter
|
|
||||||
|
|
||||||
go 1.25.7
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/creack/pty v1.1.24
|
|
||||||
github.com/prometheus/client_golang v1.22.0
|
|
||||||
github.com/prometheus/client_model v0.6.1
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
|
||||||
github.com/prometheus/common v0.62.0 // indirect
|
|
||||||
github.com/prometheus/procfs v0.15.1 // indirect
|
|
||||||
golang.org/x/sys v0.30.0 // indirect
|
|
||||||
google.golang.org/protobuf v1.36.5 // indirect
|
|
||||||
)
|
|
||||||
-34
@@ -1,34 +0,0 @@
|
|||||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
|
||||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
|
||||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
|
||||||
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
|
||||||
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
|
||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
|
||||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
|
||||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
|
||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
|
||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/prometheus/client_golang v1.22.0 h1:rb93p9lokFEsctTys46VnV1kLCDpVZ0a/Y92Vm0Zc6Q=
|
|
||||||
github.com/prometheus/client_golang v1.22.0/go.mod h1:R7ljNsLXhuQXYZYtw6GAE9AZg8Y7vEW5scdCXrWRXC0=
|
|
||||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
|
||||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
|
||||||
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
|
|
||||||
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
|
|
||||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
|
||||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
|
||||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
|
||||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
||||||
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
|
||||||
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
Vendored
-61
@@ -1,61 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MtimeCache caches a value and invalidates when a file's mtime changes.
|
|
||||||
type MtimeCache[V any] struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
value V
|
|
||||||
lastMtime time.Time
|
|
||||||
valid bool
|
|
||||||
stat func(path string) (time.Time, error)
|
|
||||||
path string
|
|
||||||
}
|
|
||||||
|
|
||||||
// StatFunc returns the mtime of a file. Allows injection for testing.
|
|
||||||
type StatFunc func(path string) (time.Time, error)
|
|
||||||
|
|
||||||
func NewMtimeCache[V any](path string, stat StatFunc) *MtimeCache[V] {
|
|
||||||
return &MtimeCache[V]{
|
|
||||||
path: path,
|
|
||||||
stat: stat,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the cached value if the file mtime hasn't changed.
|
|
||||||
// Returns (value, true) on cache hit, (zero, false) on miss.
|
|
||||||
func (c *MtimeCache[V]) Get() (V, bool) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
if !c.valid {
|
|
||||||
var zero V
|
|
||||||
return zero, false
|
|
||||||
}
|
|
||||||
mtime, err := c.stat(c.path)
|
|
||||||
if err != nil {
|
|
||||||
var zero V
|
|
||||||
return zero, false
|
|
||||||
}
|
|
||||||
if !mtime.Equal(c.lastMtime) {
|
|
||||||
var zero V
|
|
||||||
c.valid = false
|
|
||||||
return zero, false
|
|
||||||
}
|
|
||||||
return c.value, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set stores the value with the current file mtime.
|
|
||||||
func (c *MtimeCache[V]) Set(value V) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
mtime, err := c.stat(c.path)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.value = value
|
|
||||||
c.lastMtime = mtime
|
|
||||||
c.valid = true
|
|
||||||
}
|
|
||||||
-57
@@ -1,57 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMtimeCache_HitAndMiss(t *testing.T) {
|
|
||||||
mtime := time.Now()
|
|
||||||
stat := func(path string) (time.Time, error) { return mtime, nil }
|
|
||||||
|
|
||||||
c := NewMtimeCache[string]("/test", stat)
|
|
||||||
|
|
||||||
// Miss before any Set
|
|
||||||
_, ok := c.Get()
|
|
||||||
if ok {
|
|
||||||
t.Fatal("expected miss before Set")
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Set("hello")
|
|
||||||
v, ok := c.Get()
|
|
||||||
if !ok || v != "hello" {
|
|
||||||
t.Fatalf("expected hit with 'hello', got %q ok=%v", v, ok)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simulate file change
|
|
||||||
mtime = mtime.Add(time.Second)
|
|
||||||
_, ok = c.Get()
|
|
||||||
if ok {
|
|
||||||
t.Fatal("expected miss after mtime change")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set again with new mtime
|
|
||||||
c.Set("world")
|
|
||||||
v, ok = c.Get()
|
|
||||||
if !ok || v != "world" {
|
|
||||||
t.Fatalf("expected hit with 'world', got %q ok=%v", v, ok)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMtimeCache_Concurrent(t *testing.T) {
|
|
||||||
mtime := time.Now()
|
|
||||||
stat := func(path string) (time.Time, error) { return mtime, nil }
|
|
||||||
c := NewMtimeCache[int]("/test", stat)
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for i := range 100 {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(n int) {
|
|
||||||
defer wg.Done()
|
|
||||||
c.Set(n)
|
|
||||||
c.Get()
|
|
||||||
}(i)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
Vendored
-62
@@ -1,62 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/rand/v2"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TTLCache is a generic TTL cache with random jitter to prevent thundering herd.
|
|
||||||
type TTLCache[K comparable, V any] struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
entries map[K]ttlEntry[V]
|
|
||||||
maxTTL time.Duration
|
|
||||||
rand time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
type ttlEntry[V any] struct {
|
|
||||||
value V
|
|
||||||
expiresAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTTLCache[K comparable, V any](maxTTL, randRange time.Duration) *TTLCache[K, V] {
|
|
||||||
return &TTLCache[K, V]{
|
|
||||||
entries: make(map[K]ttlEntry[V]),
|
|
||||||
maxTTL: maxTTL,
|
|
||||||
rand: randRange,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *TTLCache[K, V]) jitteredTTL() time.Duration {
|
|
||||||
jitter := time.Duration(rand.Float64()*2*float64(c.rand)) - c.rand
|
|
||||||
return c.maxTTL + jitter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *TTLCache[K, V]) Get(key K) (V, bool) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
e, ok := c.entries[key]
|
|
||||||
if !ok || time.Now().After(e.expiresAt) {
|
|
||||||
if ok {
|
|
||||||
delete(c.entries, key)
|
|
||||||
}
|
|
||||||
var zero V
|
|
||||||
return zero, false
|
|
||||||
}
|
|
||||||
return e.value, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *TTLCache[K, V]) Set(key K, value V) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
c.entries[key] = ttlEntry[V]{
|
|
||||||
value: value,
|
|
||||||
expiresAt: time.Now().Add(c.jitteredTTL()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *TTLCache[K, V]) Invalidate(key K) {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
delete(c.entries, key)
|
|
||||||
}
|
|
||||||
Vendored
-73
@@ -1,73 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestTTLCache_SetGet(t *testing.T) {
|
|
||||||
c := NewTTLCache[string, int](time.Hour, 0)
|
|
||||||
c.Set("a", 42)
|
|
||||||
v, ok := c.Get("a")
|
|
||||||
if !ok || v != 42 {
|
|
||||||
t.Fatalf("expected 42, got %d (ok=%v)", v, ok)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTTLCache_Miss(t *testing.T) {
|
|
||||||
c := NewTTLCache[string, int](time.Hour, 0)
|
|
||||||
_, ok := c.Get("missing")
|
|
||||||
if ok {
|
|
||||||
t.Fatal("expected miss")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTTLCache_Expiry(t *testing.T) {
|
|
||||||
c := NewTTLCache[string, int](time.Millisecond, 0)
|
|
||||||
c.Set("a", 1)
|
|
||||||
time.Sleep(5 * time.Millisecond)
|
|
||||||
_, ok := c.Get("a")
|
|
||||||
if ok {
|
|
||||||
t.Fatal("expected expired entry")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTTLCache_Invalidate(t *testing.T) {
|
|
||||||
c := NewTTLCache[string, int](time.Hour, 0)
|
|
||||||
c.Set("a", 1)
|
|
||||||
c.Invalidate("a")
|
|
||||||
_, ok := c.Get("a")
|
|
||||||
if ok {
|
|
||||||
t.Fatal("expected invalidated entry")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTTLCache_Concurrent(t *testing.T) {
|
|
||||||
c := NewTTLCache[string, int](time.Hour, 0)
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for i := range 100 {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(n int) {
|
|
||||||
defer wg.Done()
|
|
||||||
key := "key"
|
|
||||||
c.Set(key, n)
|
|
||||||
c.Get(key)
|
|
||||||
c.Invalidate(key)
|
|
||||||
c.Set(key, n+1)
|
|
||||||
c.Get(key)
|
|
||||||
}(i)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTTLCache_JitterRange(t *testing.T) {
|
|
||||||
c := NewTTLCache[string, int](time.Second, 500*time.Millisecond)
|
|
||||||
// jitteredTTL should be in [500ms, 1500ms]
|
|
||||||
for range 100 {
|
|
||||||
d := c.jitteredTTL()
|
|
||||||
if d < 500*time.Millisecond || d > 1500*time.Millisecond {
|
|
||||||
t.Fatalf("jitter out of range: %v", d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,521 +0,0 @@
|
|||||||
package collector
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"slices"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/cache"
|
|
||||||
"pve_local_exporter/internal/config"
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
"pve_local_exporter/internal/procfs"
|
|
||||||
"pve_local_exporter/internal/pveconfig"
|
|
||||||
"pve_local_exporter/internal/qmmonitor"
|
|
||||||
"pve_local_exporter/internal/storage"
|
|
||||||
"pve_local_exporter/internal/sysfs"
|
|
||||||
)
|
|
||||||
|
|
||||||
const maxWorkers = 16
|
|
||||||
|
|
||||||
// PVECollector implements prometheus.Collector.
|
|
||||||
type PVECollector struct {
|
|
||||||
cfg config.Config
|
|
||||||
proc procfs.ProcReader
|
|
||||||
sys sysfs.SysReader
|
|
||||||
qm qmmonitor.QMMonitor
|
|
||||||
statFS storage.StatFS
|
|
||||||
cmdRunner CommandRunner
|
|
||||||
fileReader FileReaderIface
|
|
||||||
|
|
||||||
poolCache *cache.MtimeCache[poolData]
|
|
||||||
storageCache *cache.MtimeCache[[]pveconfig.StorageEntry]
|
|
||||||
|
|
||||||
prefix string
|
|
||||||
|
|
||||||
// Pre-allocated metric descriptors for fixed-label metrics.
|
|
||||||
descCPU *prometheus.Desc
|
|
||||||
descVcores *prometheus.Desc
|
|
||||||
descMaxmem *prometheus.Desc
|
|
||||||
descMemPct *prometheus.Desc
|
|
||||||
descMemExt *prometheus.Desc
|
|
||||||
descThreads *prometheus.Desc
|
|
||||||
descCtxSwitches *prometheus.Desc
|
|
||||||
descNicInfo *prometheus.Desc
|
|
||||||
descNicQueues *prometheus.Desc
|
|
||||||
descDiskInfo *prometheus.Desc
|
|
||||||
descDiskSize *prometheus.Desc
|
|
||||||
descStorageSize *prometheus.Desc
|
|
||||||
descStorageFree *prometheus.Desc
|
|
||||||
|
|
||||||
// IO counter descriptors (counters).
|
|
||||||
descIOReadCount *prometheus.Desc
|
|
||||||
descIOReadBytes *prometheus.Desc
|
|
||||||
descIOReadChars *prometheus.Desc
|
|
||||||
descIOWriteCount *prometheus.Desc
|
|
||||||
descIOWriteBytes *prometheus.Desc
|
|
||||||
descIOWriteChars *prometheus.Desc
|
|
||||||
|
|
||||||
// Operational metrics.
|
|
||||||
descScrapeDuration *prometheus.Desc
|
|
||||||
descBuildInfo *prometheus.Desc
|
|
||||||
}
|
|
||||||
|
|
||||||
type poolData struct {
|
|
||||||
vmPoolMap map[string]string
|
|
||||||
pools map[string]pveconfig.PoolInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
// CommandRunner for executing shell commands.
|
|
||||||
type CommandRunner interface {
|
|
||||||
Run(name string, args ...string) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FileReaderIface for reading files.
|
|
||||||
type FileReaderIface interface {
|
|
||||||
ReadFile(path string) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RealCommandRunner executes real commands.
|
|
||||||
type RealCommandRunner struct{}
|
|
||||||
|
|
||||||
func (RealCommandRunner) Run(name string, args ...string) (string, error) {
|
|
||||||
out, err := exec.Command(name, args...).Output()
|
|
||||||
return string(out), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// RealFileReader reads real files.
|
|
||||||
type RealFileReader struct{}
|
|
||||||
|
|
||||||
func (RealFileReader) ReadFile(path string) (string, error) {
|
|
||||||
data, err := os.ReadFile(path)
|
|
||||||
return string(data), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func fileMtime(path string) (time.Time, error) {
|
|
||||||
info, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
return time.Time{}, err
|
|
||||||
}
|
|
||||||
return info.ModTime(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a PVECollector with real I/O implementations.
|
|
||||||
func New(cfg config.Config) *PVECollector {
|
|
||||||
return NewWithDeps(cfg,
|
|
||||||
procfs.NewRealProcReader(),
|
|
||||||
sysfs.NewRealSysReader(),
|
|
||||||
qmmonitor.NewRealQMMonitor(cfg.QMTerminalTimeout, cfg.QMMaxTTL, cfg.QMRand, cfg.QMMonitorDeferClose),
|
|
||||||
storage.RealStatFS{},
|
|
||||||
RealCommandRunner{},
|
|
||||||
RealFileReader{},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewWithDeps creates a PVECollector with injected dependencies (for testing).
|
|
||||||
func NewWithDeps(cfg config.Config, proc procfs.ProcReader, sys sysfs.SysReader,
|
|
||||||
qm qmmonitor.QMMonitor, statFS storage.StatFS, cmd CommandRunner, fr FileReaderIface) *PVECollector {
|
|
||||||
|
|
||||||
p := cfg.MetricsPrefix
|
|
||||||
c := &PVECollector{
|
|
||||||
cfg: cfg,
|
|
||||||
proc: proc,
|
|
||||||
sys: sys,
|
|
||||||
qm: qm,
|
|
||||||
statFS: statFS,
|
|
||||||
cmdRunner: cmd,
|
|
||||||
fileReader: fr,
|
|
||||||
prefix: p,
|
|
||||||
|
|
||||||
descCPU: prometheus.NewDesc(p+"_kvm_cpu_seconds_total", "KVM CPU time", []string{"id", "mode"}, nil),
|
|
||||||
descVcores: prometheus.NewDesc(p+"_kvm_vcores", "vCores allocated", []string{"id"}, nil),
|
|
||||||
descMaxmem: prometheus.NewDesc(p+"_kvm_maxmem_bytes", "Maximum memory bytes", []string{"id"}, nil),
|
|
||||||
descMemPct: prometheus.NewDesc(p+"_kvm_memory_percent", "Memory percent of host", []string{"id"}, nil),
|
|
||||||
descMemExt: prometheus.NewDesc(p+"_kvm_memory_extended", "Extended memory info", []string{"id", "type"}, nil),
|
|
||||||
descThreads: prometheus.NewDesc(p+"_kvm_threads", "Threads used", []string{"id"}, nil),
|
|
||||||
descCtxSwitches: prometheus.NewDesc(p+"_kvm_ctx_switches_total", "Context switches", []string{"id", "type"}, nil),
|
|
||||||
descNicInfo: prometheus.NewDesc(p+"_kvm_nic_info", "NIC info", []string{"id", "ifname", "netdev", "queues", "type", "model", "macaddr"}, nil),
|
|
||||||
descNicQueues: prometheus.NewDesc(p+"_kvm_nic_queues", "NIC queue count", []string{"id", "ifname"}, nil),
|
|
||||||
descDiskInfo: prometheus.NewDesc(p+"_kvm_disk_info", "Disk info", []string{
|
|
||||||
"id", "disk_name", "block_id", "disk_path", "disk_type",
|
|
||||||
"vol_name", "pool", "pool_name", "cluster_id", "vg_name",
|
|
||||||
"device", "attached_to", "cache_mode", "detect_zeroes", "read_only",
|
|
||||||
}, nil),
|
|
||||||
descDiskSize: prometheus.NewDesc(p+"_kvm_disk_size_bytes", "Disk size bytes", []string{"id", "disk_name"}, nil),
|
|
||||||
descStorageSize: prometheus.NewDesc(p+"_node_storage_size_bytes", "Storage total size", []string{"name", "type"}, nil),
|
|
||||||
descStorageFree: prometheus.NewDesc(p+"_node_storage_free_bytes", "Storage free space", []string{"name", "type"}, nil),
|
|
||||||
|
|
||||||
descIOReadCount: prometheus.NewDesc(p+"_kvm_io_read_count_total", "Read system calls by KVM process", []string{"id"}, nil),
|
|
||||||
descIOReadBytes: prometheus.NewDesc(p+"_kvm_io_read_bytes_total", "Bytes read from disk by KVM process", []string{"id"}, nil),
|
|
||||||
descIOReadChars: prometheus.NewDesc(p+"_kvm_io_read_chars_total", "Bytes read including buffers by KVM process", []string{"id"}, nil),
|
|
||||||
descIOWriteCount: prometheus.NewDesc(p+"_kvm_io_write_count_total", "Write system calls by KVM process", []string{"id"}, nil),
|
|
||||||
descIOWriteBytes: prometheus.NewDesc(p+"_kvm_io_write_bytes_total", "Bytes written to disk by KVM process", []string{"id"}, nil),
|
|
||||||
descIOWriteChars: prometheus.NewDesc(p+"_kvm_io_write_chars_total", "Bytes written including buffers by KVM process", []string{"id"}, nil),
|
|
||||||
|
|
||||||
descScrapeDuration: prometheus.NewDesc(p+"_scrape_duration_seconds", "Duration of metrics collection", nil, nil),
|
|
||||||
descBuildInfo: prometheus.NewDesc(p+"_exporter_build_info", "Build information", []string{"version"}, nil),
|
|
||||||
}
|
|
||||||
c.poolCache = cache.NewMtimeCache[poolData]("/etc/pve/user.cfg", fileMtime)
|
|
||||||
c.storageCache = cache.NewMtimeCache[[]pveconfig.StorageEntry]("/etc/pve/storage.cfg", fileMtime)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) Describe(ch chan<- *prometheus.Desc) {
|
|
||||||
// Dynamic metrics - use empty desc to signal unchecked collector
|
|
||||||
ch <- c.descCPU
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) Collect(ch chan<- prometheus.Metric) {
|
|
||||||
start := time.Now()
|
|
||||||
|
|
||||||
if c.cfg.CollectRunningVMs {
|
|
||||||
c.collectVMs(ch)
|
|
||||||
}
|
|
||||||
if c.cfg.CollectStorage {
|
|
||||||
c.collectStorage(ch)
|
|
||||||
}
|
|
||||||
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descScrapeDuration, prometheus.GaugeValue, time.Since(start).Seconds())
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descBuildInfo, prometheus.GaugeValue, 1, c.cfg.Version)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) collectVMs(ch chan<- prometheus.Metric) {
|
|
||||||
procs, err := c.proc.DiscoverQEMUProcesses()
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("discover QEMU processes", "err", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logging.Trace("collectVMs", "vm_count", len(procs))
|
|
||||||
|
|
||||||
// Load pool info
|
|
||||||
vmPoolMap, pools := c.getPoolInfo()
|
|
||||||
logging.Trace("pool info loaded", "vm_pool_map_size", len(vmPoolMap), "pools_count", len(pools))
|
|
||||||
|
|
||||||
for _, proc := range procs {
|
|
||||||
c.collectVMMetrics(ch, proc, vmPoolMap, pools)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parallel NIC + disk collection with bounded worker pool
|
|
||||||
sem := make(chan struct{}, maxWorkers)
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
for _, proc := range procs {
|
|
||||||
proc := proc
|
|
||||||
wg.Add(2)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
sem <- struct{}{}
|
|
||||||
defer func() { <-sem; wg.Done() }()
|
|
||||||
c.collectNICMetrics(ch, proc)
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
sem <- struct{}{}
|
|
||||||
defer func() { <-sem; wg.Done() }()
|
|
||||||
c.collectDiskMetrics(ch, proc)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) collectVMMetrics(ch chan<- prometheus.Metric, proc procfs.QEMUProcess,
|
|
||||||
vmPoolMap map[string]string, pools map[string]pveconfig.PoolInfo) {
|
|
||||||
|
|
||||||
id := proc.VMID
|
|
||||||
|
|
||||||
// CPU times
|
|
||||||
if cpu, err := c.proc.GetCPUTimes(proc.PID); err == nil {
|
|
||||||
for _, m := range []struct {
|
|
||||||
mode string
|
|
||||||
val float64
|
|
||||||
}{
|
|
||||||
{"user", cpu.User},
|
|
||||||
{"system", cpu.System},
|
|
||||||
{"iowait", cpu.IOWait},
|
|
||||||
} {
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descCPU, prometheus.CounterValue, m.val, id, m.mode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Vcores
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descVcores, prometheus.GaugeValue, float64(proc.Vcores), id)
|
|
||||||
|
|
||||||
// MaxMem (kB to bytes)
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descMaxmem, prometheus.GaugeValue, float64(proc.MaxMem*1024), id)
|
|
||||||
|
|
||||||
// Status (threads, memory, context switches) -- single /proc/{pid}/status read
|
|
||||||
if status, err := c.proc.GetStatus(proc.PID); err == nil {
|
|
||||||
// Memory percent
|
|
||||||
if memPct, err := c.proc.GetMemoryPercent(proc.PID, status.VmRSS); err == nil {
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descMemPct, prometheus.GaugeValue, memPct, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Memory extended
|
|
||||||
for key, val := range status.MemoryExtended {
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descMemExt, prometheus.GaugeValue, float64(val), id, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Threads
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descThreads, prometheus.GaugeValue, float64(status.Threads), id)
|
|
||||||
|
|
||||||
// Context switches
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descCtxSwitches, prometheus.CounterValue, float64(status.CtxSwitches.Voluntary), id, "voluntary")
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descCtxSwitches, prometheus.CounterValue, float64(status.CtxSwitches.Involuntary), id, "involuntary")
|
|
||||||
}
|
|
||||||
|
|
||||||
// IO counters
|
|
||||||
if io, err := c.proc.GetIOCounters(proc.PID); err == nil {
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descIOReadCount, prometheus.CounterValue, float64(io.ReadSyscalls), id)
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descIOReadBytes, prometheus.CounterValue, float64(io.ReadBytes), id)
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descIOReadChars, prometheus.CounterValue, float64(io.ReadChars), id)
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descIOWriteCount, prometheus.CounterValue, float64(io.WriteSyscalls), id)
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descIOWriteBytes, prometheus.CounterValue, float64(io.WriteBytes), id)
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descIOWriteChars, prometheus.CounterValue, float64(io.WriteChars), id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// VM info metric
|
|
||||||
poolName := vmPoolMap[id]
|
|
||||||
poolInfo := pools[poolName]
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
prometheus.NewDesc(c.prefix+"_kvm_info", "VM info", []string{
|
|
||||||
"id", "name", "cpu", "pid", "pool", "pool_levels", "pool1", "pool2", "pool3",
|
|
||||||
}, nil),
|
|
||||||
prometheus.GaugeValue, 1,
|
|
||||||
id, proc.Name, proc.CPU, strconv.Itoa(proc.PID),
|
|
||||||
poolName, strconv.Itoa(poolInfo.LevelCount),
|
|
||||||
poolInfo.Level1, poolInfo.Level2, poolInfo.Level3,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) collectNICMetrics(ch chan<- prometheus.Metric, proc procfs.QEMUProcess) {
|
|
||||||
id := proc.VMID
|
|
||||||
|
|
||||||
raw, err := c.qm.RunCommand(id, "info network")
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("qm info network", "vmid", id, "err", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logging.Trace("qm info network response", "vmid", id, "raw_len", len(raw))
|
|
||||||
|
|
||||||
nics := qmmonitor.ParseNetworkInfo(raw)
|
|
||||||
logging.Trace("parsed NICs", "vmid", id, "nic_count", len(nics))
|
|
||||||
for _, nic := range nics {
|
|
||||||
// NIC info metric
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descNicInfo, prometheus.GaugeValue, 1,
|
|
||||||
id, nic.Ifname, nic.Netdev, strconv.Itoa(nic.Queues),
|
|
||||||
nic.Type, nic.Model, nic.Macaddr,
|
|
||||||
)
|
|
||||||
|
|
||||||
// NIC queues
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descNicQueues, prometheus.GaugeValue, float64(nic.Queues), id, nic.Ifname)
|
|
||||||
|
|
||||||
// NIC stats from sysfs
|
|
||||||
stats, err := c.sys.ReadInterfaceStats(nic.Ifname)
|
|
||||||
if err != nil {
|
|
||||||
slog.Debug("read interface stats", "ifname", nic.Ifname, "err", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for statName, val := range stats {
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
|
||||||
prometheus.NewDesc(c.prefix+"_kvm_nic_"+statName+"_total", fmt.Sprintf("NIC statistic %s", statName), []string{"id", "ifname"}, nil),
|
|
||||||
prometheus.CounterValue, float64(val), id, nic.Ifname,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) collectDiskMetrics(ch chan<- prometheus.Metric, proc procfs.QEMUProcess) {
|
|
||||||
id := proc.VMID
|
|
||||||
|
|
||||||
raw, err := c.qm.RunCommand(id, "info block")
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("qm info block", "vmid", id, "err", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logging.Trace("qm info block response", "vmid", id, "raw_len", len(raw))
|
|
||||||
|
|
||||||
disks := qmmonitor.ParseBlockInfo(raw)
|
|
||||||
logging.Trace("parsed disks", "vmid", id, "disk_count", len(disks))
|
|
||||||
for diskName, disk := range disks {
|
|
||||||
// Try to get device symlink target for zvol/rbd/lvm
|
|
||||||
if disk.DiskType == "zvol" || disk.DiskType == "rbd" || disk.DiskType == "lvm" {
|
|
||||||
target, err := sysfs.GetDeviceSymlinkTarget(disk.DiskPath)
|
|
||||||
if err == nil {
|
|
||||||
disk.Labels["device"] = filepath.Base(target)
|
|
||||||
} else {
|
|
||||||
slog.Debug("resolve device symlink", "path", disk.DiskPath, "err", err)
|
|
||||||
// Retry with cache invalidation
|
|
||||||
c.qm.InvalidateCache(id, "info block")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disk size
|
|
||||||
var diskSize int64
|
|
||||||
switch disk.DiskType {
|
|
||||||
case "qcow2":
|
|
||||||
// File-backed: use file size
|
|
||||||
if fi, err := os.Stat(disk.DiskPath); err == nil {
|
|
||||||
diskSize = fi.Size()
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// Block device
|
|
||||||
size, err := c.sys.GetBlockDeviceSize(disk.DiskPath)
|
|
||||||
if err == nil {
|
|
||||||
diskSize = size
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Trace("disk metric", "vmid", id, "disk", diskName, "type", disk.DiskType,
|
|
||||||
"path", disk.DiskPath, "size", diskSize, "device", disk.Labels["device"])
|
|
||||||
|
|
||||||
if diskSize > 0 {
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descDiskSize, prometheus.GaugeValue, float64(diskSize), id, diskName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disk info metric with fixed label set
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descDiskInfo, prometheus.GaugeValue, 1,
|
|
||||||
id,
|
|
||||||
diskName,
|
|
||||||
disk.BlockID,
|
|
||||||
disk.DiskPath,
|
|
||||||
disk.DiskType,
|
|
||||||
disk.Labels["vol_name"],
|
|
||||||
disk.Labels["pool"],
|
|
||||||
disk.Labels["pool_name"],
|
|
||||||
disk.Labels["cluster_id"],
|
|
||||||
disk.Labels["vg_name"],
|
|
||||||
disk.Labels["device"],
|
|
||||||
disk.Labels["attached_to"],
|
|
||||||
disk.Labels["cache_mode"],
|
|
||||||
disk.Labels["detect_zeroes"],
|
|
||||||
disk.Labels["read_only"],
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) collectStorage(ch chan<- prometheus.Metric) {
|
|
||||||
entries := c.getStorageEntries()
|
|
||||||
logging.Trace("collectStorage", "entries_count", len(entries))
|
|
||||||
|
|
||||||
// Compute superset of property keys across all entries
|
|
||||||
keySet := make(map[string]struct{})
|
|
||||||
for _, entry := range entries {
|
|
||||||
for k := range entry.Properties {
|
|
||||||
keySet[k] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
allKeys := sortedKeySet(keySet)
|
|
||||||
|
|
||||||
// Create descriptor once with fixed labels for this scrape
|
|
||||||
storageInfoDesc := prometheus.NewDesc(
|
|
||||||
c.prefix+"_node_storage_info", "Storage info", allKeys, nil,
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, entry := range entries {
|
|
||||||
storageType := entry.Properties["type"]
|
|
||||||
storageName := entry.Properties["name"]
|
|
||||||
logging.Trace("storage entry", "name", storageName, "type", storageType)
|
|
||||||
|
|
||||||
// Info metric with consistent labels
|
|
||||||
vals := make([]string, len(allKeys))
|
|
||||||
for i, k := range allKeys {
|
|
||||||
vals[i] = entry.Properties[k] // "" if missing
|
|
||||||
}
|
|
||||||
ch <- prometheus.MustNewConstMetric(storageInfoDesc, prometheus.GaugeValue, 1, vals...)
|
|
||||||
|
|
||||||
// Size metrics
|
|
||||||
var size storage.StorageSize
|
|
||||||
var err error
|
|
||||||
|
|
||||||
switch storageType {
|
|
||||||
case "dir", "nfs", "cephfs":
|
|
||||||
path := entry.Properties["path"]
|
|
||||||
if path == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
size, err = storage.GetDirStorageSize(c.statFS, path)
|
|
||||||
case "zfspool":
|
|
||||||
pool := entry.Properties["pool"]
|
|
||||||
if pool == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// Extract base pool name (before any /)
|
|
||||||
poolName := strings.Split(pool, "/")[0]
|
|
||||||
out, runErr := c.cmdRunner.Run("zpool", "list", "-p", poolName)
|
|
||||||
if runErr != nil {
|
|
||||||
slog.Warn("zpool list", "pool", poolName, "err", runErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
size, err = storage.GetZPoolSize(out)
|
|
||||||
default:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("storage size", "name", storageName, "err", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descStorageSize, prometheus.GaugeValue, float64(size.Total), storageName, storageType)
|
|
||||||
ch <- prometheus.MustNewConstMetric(c.descStorageFree, prometheus.GaugeValue, float64(size.Free), storageName, storageType)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) getPoolInfo() (map[string]string, map[string]pveconfig.PoolInfo) {
|
|
||||||
if data, ok := c.poolCache.Get(); ok {
|
|
||||||
return data.vmPoolMap, data.pools
|
|
||||||
}
|
|
||||||
|
|
||||||
content, err := c.fileReader.ReadFile("/etc/pve/user.cfg")
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("read user.cfg", "err", err)
|
|
||||||
return make(map[string]string), make(map[string]pveconfig.PoolInfo)
|
|
||||||
}
|
|
||||||
|
|
||||||
vmPoolMap, pools := pveconfig.ParsePoolConfig(content)
|
|
||||||
c.poolCache.Set(poolData{vmPoolMap: vmPoolMap, pools: pools})
|
|
||||||
return vmPoolMap, pools
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *PVECollector) getStorageEntries() []pveconfig.StorageEntry {
|
|
||||||
if data, ok := c.storageCache.Get(); ok {
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
content, err := c.fileReader.ReadFile("/etc/pve/storage.cfg")
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("read storage.cfg", "err", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
entries := pveconfig.ParseStorageConfig(content)
|
|
||||||
c.storageCache.Set(entries)
|
|
||||||
return entries
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortedKeys(m map[string]string) []string {
|
|
||||||
keys := make([]string, 0, len(m))
|
|
||||||
for k := range m {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
slices.Sort(keys)
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortedKeySet(m map[string]struct{}) []string {
|
|
||||||
keys := make([]string, 0, len(m))
|
|
||||||
for k := range m {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
slices.Sort(keys)
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,564 +0,0 @@
|
|||||||
package collector
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/config"
|
|
||||||
"pve_local_exporter/internal/procfs"
|
|
||||||
"pve_local_exporter/internal/storage"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Mock implementations
|
|
||||||
|
|
||||||
type mockProcReader struct {
|
|
||||||
procs []procfs.QEMUProcess
|
|
||||||
procsErr error
|
|
||||||
cpuTimes map[int]procfs.CPUTimes
|
|
||||||
ioCount map[int]procfs.IOCounters
|
|
||||||
status map[int]procfs.StatusInfo
|
|
||||||
memPct map[int]float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockProcReader) DiscoverQEMUProcesses() ([]procfs.QEMUProcess, error) {
|
|
||||||
return m.procs, m.procsErr
|
|
||||||
}
|
|
||||||
func (m *mockProcReader) GetCPUTimes(pid int) (procfs.CPUTimes, error) {
|
|
||||||
return m.cpuTimes[pid], nil
|
|
||||||
}
|
|
||||||
func (m *mockProcReader) GetIOCounters(pid int) (procfs.IOCounters, error) {
|
|
||||||
return m.ioCount[pid], nil
|
|
||||||
}
|
|
||||||
func (m *mockProcReader) GetStatus(pid int) (procfs.StatusInfo, error) {
|
|
||||||
return m.status[pid], nil
|
|
||||||
}
|
|
||||||
func (m *mockProcReader) GetMemoryPercent(pid int, rssBytes int64) (float64, error) {
|
|
||||||
return m.memPct[pid], nil
|
|
||||||
}
|
|
||||||
func (m *mockProcReader) VMConfigExists(vmid string) bool { return true }
|
|
||||||
|
|
||||||
type mockSysReader struct {
|
|
||||||
ifStats map[string]map[string]int64
|
|
||||||
blockSize map[string]int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockSysReader) ReadInterfaceStats(ifname string) (map[string]int64, error) {
|
|
||||||
return m.ifStats[ifname], nil
|
|
||||||
}
|
|
||||||
func (m *mockSysReader) GetBlockDeviceSize(devPath string) (int64, error) {
|
|
||||||
return m.blockSize[devPath], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type mockQMMonitor struct {
|
|
||||||
responses map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockQMMonitor) RunCommand(vmid, cmd string) (string, error) {
|
|
||||||
return m.responses[vmid+":"+cmd], nil
|
|
||||||
}
|
|
||||||
func (m *mockQMMonitor) InvalidateCache(vmid, cmd string) {}
|
|
||||||
|
|
||||||
type mockStatFS struct {
|
|
||||||
sizes map[string]storage.StorageSize
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockStatFS) Statfs(path string) (storage.StorageSize, error) {
|
|
||||||
return m.sizes[path], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type mockCmdRunner struct {
|
|
||||||
outputs map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockCmdRunner) Run(name string, args ...string) (string, error) {
|
|
||||||
key := name + " " + strings.Join(args, " ")
|
|
||||||
return m.outputs[key], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type mockFileReader struct {
|
|
||||||
files map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockFileReader) ReadFile(path string) (string, error) {
|
|
||||||
return m.files[path], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// metricValue extracts the numeric value from a dto.Metric, whether it is a Gauge or Counter.
|
|
||||||
func metricValue(m *dto.Metric) float64 {
|
|
||||||
if m.Gauge != nil {
|
|
||||||
return m.Gauge.GetValue()
|
|
||||||
}
|
|
||||||
if m.Counter != nil {
|
|
||||||
return m.Counter.GetValue()
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// collectMetrics collects all metrics from a collector into a map keyed by metric name.
|
|
||||||
func collectMetrics(c prometheus.Collector) map[string][]*dto.Metric {
|
|
||||||
ch := make(chan prometheus.Metric, 200)
|
|
||||||
go func() {
|
|
||||||
c.Collect(ch)
|
|
||||||
close(ch)
|
|
||||||
}()
|
|
||||||
|
|
||||||
result := make(map[string][]*dto.Metric)
|
|
||||||
for m := range ch {
|
|
||||||
d := &dto.Metric{}
|
|
||||||
m.Write(d)
|
|
||||||
desc := m.Desc()
|
|
||||||
// Extract fqName from desc string
|
|
||||||
name := desc.String()
|
|
||||||
// Format: Desc{fqName: "name", ...}
|
|
||||||
if idx := strings.Index(name, "fqName: \""); idx >= 0 {
|
|
||||||
name = name[idx+9:]
|
|
||||||
if end := strings.Index(name, "\""); end >= 0 {
|
|
||||||
name = name[:end]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result[name] = append(result[name], d)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func findMetricWithLabels(metrics []*dto.Metric, labels map[string]string) *dto.Metric {
|
|
||||||
for _, m := range metrics {
|
|
||||||
match := true
|
|
||||||
for wantName, wantVal := range labels {
|
|
||||||
found := false
|
|
||||||
for _, l := range m.Label {
|
|
||||||
if l.GetName() == wantName && l.GetValue() == wantVal {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
match = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if match {
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCollector_BasicVMMetrics(t *testing.T) {
|
|
||||||
cfg := config.Config{
|
|
||||||
CollectRunningVMs: true,
|
|
||||||
CollectStorage: false,
|
|
||||||
MetricsPrefix: "pve",
|
|
||||||
}
|
|
||||||
|
|
||||||
proc := &mockProcReader{
|
|
||||||
procs: []procfs.QEMUProcess{
|
|
||||||
{PID: 1234, VMID: "100", Name: "testvm", CPU: "host", Vcores: 4, MaxMem: 4194304},
|
|
||||||
},
|
|
||||||
cpuTimes: map[int]procfs.CPUTimes{
|
|
||||||
1234: {User: 5.0, System: 2.0, IOWait: 0.5},
|
|
||||||
},
|
|
||||||
ioCount: map[int]procfs.IOCounters{
|
|
||||||
1234: {ReadChars: 1000, WriteChars: 2000, ReadSyscalls: 10, WriteSyscalls: 20, ReadBytes: 500, WriteBytes: 1000},
|
|
||||||
},
|
|
||||||
status: map[int]procfs.StatusInfo{
|
|
||||||
1234: {
|
|
||||||
Threads: 50,
|
|
||||||
VmRSS: 1048576,
|
|
||||||
MemoryExtended: procfs.MemoryExtended{"vmrss": 1048576, "vmpeak": 2097152},
|
|
||||||
CtxSwitches: procfs.CtxSwitches{Voluntary: 100, Involuntary: 10},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
memPct: map[int]float64{1234: 25.5},
|
|
||||||
}
|
|
||||||
|
|
||||||
sys := &mockSysReader{
|
|
||||||
ifStats: map[string]map[string]int64{},
|
|
||||||
blockSize: map[string]int64{},
|
|
||||||
}
|
|
||||||
|
|
||||||
qm := &mockQMMonitor{responses: map[string]string{
|
|
||||||
"100:info network": "",
|
|
||||||
"100:info block": "",
|
|
||||||
}}
|
|
||||||
|
|
||||||
fr := &mockFileReader{files: map[string]string{
|
|
||||||
"/etc/pve/user.cfg": "pool:prod:Production:100\n",
|
|
||||||
}}
|
|
||||||
|
|
||||||
c := NewWithDeps(cfg, proc, sys, qm, &mockStatFS{}, &mockCmdRunner{}, fr)
|
|
||||||
metrics := collectMetrics(c)
|
|
||||||
|
|
||||||
// Check CPU metrics (counter)
|
|
||||||
cpuMetrics := metrics["pve_kvm_cpu_seconds_total"]
|
|
||||||
if len(cpuMetrics) != 3 {
|
|
||||||
t.Fatalf("expected 3 cpu metrics, got %d", len(cpuMetrics))
|
|
||||||
}
|
|
||||||
m := findMetricWithLabels(cpuMetrics, map[string]string{"mode": "user"})
|
|
||||||
if m == nil || metricValue(m) != 5.0 {
|
|
||||||
t.Errorf("cpu user = %v", m)
|
|
||||||
}
|
|
||||||
m = findMetricWithLabels(cpuMetrics, map[string]string{"mode": "system"})
|
|
||||||
if m == nil || metricValue(m) != 2.0 {
|
|
||||||
t.Errorf("cpu system = %v", m)
|
|
||||||
}
|
|
||||||
m = findMetricWithLabels(cpuMetrics, map[string]string{"mode": "iowait"})
|
|
||||||
if m == nil || metricValue(m) != 0.5 {
|
|
||||||
t.Errorf("cpu iowait = %v", m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check vcores
|
|
||||||
vcoreMetrics := metrics["pve_kvm_vcores"]
|
|
||||||
if len(vcoreMetrics) != 1 || metricValue(vcoreMetrics[0]) != 4 {
|
|
||||||
t.Errorf("vcores = %v", vcoreMetrics)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check threads
|
|
||||||
threadMetrics := metrics["pve_kvm_threads"]
|
|
||||||
if len(threadMetrics) != 1 || metricValue(threadMetrics[0]) != 50 {
|
|
||||||
t.Errorf("threads = %v", threadMetrics)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check memory percent
|
|
||||||
memPctMetrics := metrics["pve_kvm_memory_percent"]
|
|
||||||
if len(memPctMetrics) != 1 || metricValue(memPctMetrics[0]) != 25.5 {
|
|
||||||
t.Errorf("memory_percent = %v", memPctMetrics)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check maxmem (renamed with _bytes)
|
|
||||||
maxmemMetrics := metrics["pve_kvm_maxmem_bytes"]
|
|
||||||
if len(maxmemMetrics) != 1 || metricValue(maxmemMetrics[0]) != float64(4194304*1024) {
|
|
||||||
t.Errorf("maxmem_bytes = %v", maxmemMetrics)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check IO (counters, renamed with _total)
|
|
||||||
if m := metrics["pve_kvm_io_read_count_total"]; len(m) != 1 || metricValue(m[0]) != 10 {
|
|
||||||
t.Errorf("io_read_count_total = %v", m)
|
|
||||||
}
|
|
||||||
if m := metrics["pve_kvm_io_write_bytes_total"]; len(m) != 1 || metricValue(m[0]) != 1000 {
|
|
||||||
t.Errorf("io_write_bytes_total = %v", m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check context switches (counter, renamed with _total)
|
|
||||||
csMetrics := metrics["pve_kvm_ctx_switches_total"]
|
|
||||||
if len(csMetrics) != 2 {
|
|
||||||
t.Fatalf("expected 2 ctx_switches_total metrics, got %d", len(csMetrics))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check VM info metric
|
|
||||||
infoMetrics := metrics["pve_kvm_info"]
|
|
||||||
if len(infoMetrics) != 1 {
|
|
||||||
t.Fatalf("expected 1 kvm info metric, got %d", len(infoMetrics))
|
|
||||||
}
|
|
||||||
m = findMetricWithLabels(infoMetrics, map[string]string{"id": "100", "name": "testvm", "pool": "prod"})
|
|
||||||
if m == nil {
|
|
||||||
t.Error("kvm info metric not found with expected labels")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check scrape duration exists
|
|
||||||
if sd := metrics["pve_scrape_duration_seconds"]; len(sd) != 1 {
|
|
||||||
t.Errorf("expected 1 scrape_duration_seconds, got %d", len(sd))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check build info exists
|
|
||||||
if bi := metrics["pve_exporter_build_info"]; len(bi) != 1 {
|
|
||||||
t.Errorf("expected 1 build_info, got %d", len(bi))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCollector_StorageMetrics(t *testing.T) {
|
|
||||||
cfg := config.Config{
|
|
||||||
CollectRunningVMs: false,
|
|
||||||
CollectStorage: true,
|
|
||||||
MetricsPrefix: "pve",
|
|
||||||
}
|
|
||||||
|
|
||||||
fr := &mockFileReader{files: map[string]string{
|
|
||||||
"/etc/pve/storage.cfg": `dir: local
|
|
||||||
path /var/lib/vz
|
|
||||||
content iso,vztmpl,backup
|
|
||||||
`,
|
|
||||||
"/etc/pve/user.cfg": "",
|
|
||||||
}}
|
|
||||||
|
|
||||||
statFS := &mockStatFS{sizes: map[string]storage.StorageSize{
|
|
||||||
"/var/lib/vz": {Total: 1000000000, Free: 500000000},
|
|
||||||
}}
|
|
||||||
|
|
||||||
c := NewWithDeps(cfg, &mockProcReader{}, &mockSysReader{}, &mockQMMonitor{responses: map[string]string{}},
|
|
||||||
statFS, &mockCmdRunner{}, fr)
|
|
||||||
|
|
||||||
metrics := collectMetrics(c)
|
|
||||||
|
|
||||||
// Check storage size (renamed with _bytes)
|
|
||||||
sizeMetrics := metrics["pve_node_storage_size_bytes"]
|
|
||||||
if len(sizeMetrics) != 1 || metricValue(sizeMetrics[0]) != 1e9 {
|
|
||||||
t.Errorf("storage_size_bytes = %v", sizeMetrics)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check storage free (renamed with _bytes)
|
|
||||||
freeMetrics := metrics["pve_node_storage_free_bytes"]
|
|
||||||
if len(freeMetrics) != 1 || metricValue(freeMetrics[0]) != 5e8 {
|
|
||||||
t.Errorf("storage_free_bytes = %v", freeMetrics)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check storage info
|
|
||||||
infoMetrics := metrics["pve_node_storage_info"]
|
|
||||||
if len(infoMetrics) != 1 {
|
|
||||||
t.Fatalf("expected 1 storage info metric, got %d", len(infoMetrics))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCollector_NICMetrics(t *testing.T) {
|
|
||||||
cfg := config.Config{
|
|
||||||
CollectRunningVMs: true,
|
|
||||||
CollectStorage: false,
|
|
||||||
MetricsPrefix: "pve",
|
|
||||||
}
|
|
||||||
|
|
||||||
proc := &mockProcReader{
|
|
||||||
procs: []procfs.QEMUProcess{
|
|
||||||
{PID: 1, VMID: "100", Name: "vm", Vcores: 1, MaxMem: 1024},
|
|
||||||
},
|
|
||||||
cpuTimes: map[int]procfs.CPUTimes{1: {}},
|
|
||||||
ioCount: map[int]procfs.IOCounters{1: {}},
|
|
||||||
status: map[int]procfs.StatusInfo{
|
|
||||||
1: {Threads: 1, MemoryExtended: procfs.MemoryExtended{}},
|
|
||||||
},
|
|
||||||
memPct: map[int]float64{1: 0},
|
|
||||||
}
|
|
||||||
|
|
||||||
sys := &mockSysReader{
|
|
||||||
ifStats: map[string]map[string]int64{
|
|
||||||
"tap100i0": {"rx_bytes": 1000, "tx_bytes": 2000},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
qm := &mockQMMonitor{responses: map[string]string{
|
|
||||||
"100:info network": "net0: index=0,type=tap,ifname=tap100i0,model=virtio-net-pci,macaddr=AA:BB:CC:DD:EE:FF",
|
|
||||||
"100:info block": "",
|
|
||||||
}}
|
|
||||||
|
|
||||||
fr := &mockFileReader{files: map[string]string{"/etc/pve/user.cfg": ""}}
|
|
||||||
c := NewWithDeps(cfg, proc, sys, qm, &mockStatFS{}, &mockCmdRunner{}, fr)
|
|
||||||
metrics := collectMetrics(c)
|
|
||||||
|
|
||||||
// NIC info
|
|
||||||
nicInfo := metrics["pve_kvm_nic_info"]
|
|
||||||
if len(nicInfo) != 1 {
|
|
||||||
t.Fatalf("expected 1 nic info, got %d", len(nicInfo))
|
|
||||||
}
|
|
||||||
|
|
||||||
// NIC stats (counter, renamed with _total)
|
|
||||||
rxBytes := metrics["pve_kvm_nic_rx_bytes_total"]
|
|
||||||
if len(rxBytes) != 1 || metricValue(rxBytes[0]) != 1000 {
|
|
||||||
t.Errorf("rx_bytes_total = %v", rxBytes)
|
|
||||||
}
|
|
||||||
txBytes := metrics["pve_kvm_nic_tx_bytes_total"]
|
|
||||||
if len(txBytes) != 1 || metricValue(txBytes[0]) != 2000 {
|
|
||||||
t.Errorf("tx_bytes_total = %v", txBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// mockFileReaderErr returns an error for a specific path.
|
|
||||||
type mockFileReaderErr struct {
|
|
||||||
files map[string]string
|
|
||||||
errPath string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockFileReaderErr) ReadFile(path string) (string, error) {
|
|
||||||
if path == m.errPath {
|
|
||||||
return "", fmt.Errorf("read error: %s", path)
|
|
||||||
}
|
|
||||||
return m.files[path], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCollector_PoolReadError(t *testing.T) {
|
|
||||||
cfg := config.Config{
|
|
||||||
CollectRunningVMs: true,
|
|
||||||
CollectStorage: false,
|
|
||||||
MetricsPrefix: "pve",
|
|
||||||
}
|
|
||||||
|
|
||||||
proc := &mockProcReader{
|
|
||||||
procs: []procfs.QEMUProcess{
|
|
||||||
{PID: 1, VMID: "100", Name: "vm", Vcores: 1, MaxMem: 1024},
|
|
||||||
},
|
|
||||||
cpuTimes: map[int]procfs.CPUTimes{1: {}},
|
|
||||||
ioCount: map[int]procfs.IOCounters{1: {}},
|
|
||||||
status: map[int]procfs.StatusInfo{
|
|
||||||
1: {Threads: 1, MemoryExtended: procfs.MemoryExtended{}},
|
|
||||||
},
|
|
||||||
memPct: map[int]float64{1: 0},
|
|
||||||
}
|
|
||||||
|
|
||||||
fr := &mockFileReaderErr{
|
|
||||||
files: map[string]string{},
|
|
||||||
errPath: "/etc/pve/user.cfg",
|
|
||||||
}
|
|
||||||
|
|
||||||
c := NewWithDeps(cfg, proc, &mockSysReader{}, &mockQMMonitor{responses: map[string]string{
|
|
||||||
"100:info network": "",
|
|
||||||
"100:info block": "",
|
|
||||||
}}, &mockStatFS{}, &mockCmdRunner{}, fr)
|
|
||||||
|
|
||||||
metrics := collectMetrics(c)
|
|
||||||
|
|
||||||
// Should still produce VM info with empty pool
|
|
||||||
infoMetrics := metrics["pve_kvm_info"]
|
|
||||||
if len(infoMetrics) != 1 {
|
|
||||||
t.Fatalf("expected 1 kvm info metric, got %d", len(infoMetrics))
|
|
||||||
}
|
|
||||||
m := findMetricWithLabels(infoMetrics, map[string]string{"pool": ""})
|
|
||||||
if m == nil {
|
|
||||||
t.Error("expected empty pool label when user.cfg unreadable")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCollector_ProcessDiscoveryError(t *testing.T) {
|
|
||||||
cfg := config.Config{
|
|
||||||
CollectRunningVMs: true,
|
|
||||||
CollectStorage: false,
|
|
||||||
MetricsPrefix: "pve",
|
|
||||||
}
|
|
||||||
|
|
||||||
proc := &mockProcReader{
|
|
||||||
procsErr: fmt.Errorf("permission denied"),
|
|
||||||
}
|
|
||||||
|
|
||||||
fr := &mockFileReader{files: map[string]string{"/etc/pve/user.cfg": ""}}
|
|
||||||
c := NewWithDeps(cfg, proc, &mockSysReader{}, &mockQMMonitor{responses: map[string]string{}},
|
|
||||||
&mockStatFS{}, &mockCmdRunner{}, fr)
|
|
||||||
|
|
||||||
metrics := collectMetrics(c)
|
|
||||||
|
|
||||||
// No VM metrics should be emitted, but scrape_duration + build_info are always present
|
|
||||||
expectedNames := map[string]bool{
|
|
||||||
"pve_scrape_duration_seconds": true,
|
|
||||||
"pve_exporter_build_info": true,
|
|
||||||
}
|
|
||||||
for name := range metrics {
|
|
||||||
if !expectedNames[name] {
|
|
||||||
t.Errorf("unexpected metric %q on discovery error", name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(metrics) != 2 {
|
|
||||||
t.Errorf("expected 2 metrics (scrape_duration + build_info) on discovery error, got %d", len(metrics))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCollector_BuildInfo(t *testing.T) {
|
|
||||||
cfg := config.Config{
|
|
||||||
CollectRunningVMs: false,
|
|
||||||
CollectStorage: false,
|
|
||||||
MetricsPrefix: "pve",
|
|
||||||
Version: "1.2.3",
|
|
||||||
}
|
|
||||||
|
|
||||||
c := NewWithDeps(cfg, &mockProcReader{}, &mockSysReader{}, &mockQMMonitor{responses: map[string]string{}},
|
|
||||||
&mockStatFS{}, &mockCmdRunner{}, &mockFileReader{files: map[string]string{}})
|
|
||||||
|
|
||||||
metrics := collectMetrics(c)
|
|
||||||
|
|
||||||
bi := metrics["pve_exporter_build_info"]
|
|
||||||
if len(bi) != 1 {
|
|
||||||
t.Fatalf("expected 1 build_info metric, got %d", len(bi))
|
|
||||||
}
|
|
||||||
if metricValue(bi[0]) != 1 {
|
|
||||||
t.Errorf("build_info value = %v, want 1", metricValue(bi[0]))
|
|
||||||
}
|
|
||||||
m := findMetricWithLabels(bi, map[string]string{"version": "1.2.3"})
|
|
||||||
if m == nil {
|
|
||||||
t.Error("build_info missing version label")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCollector_DiskInfoMetrics(t *testing.T) {
|
|
||||||
cfg := config.Config{
|
|
||||||
CollectRunningVMs: true,
|
|
||||||
CollectStorage: false,
|
|
||||||
MetricsPrefix: "pve",
|
|
||||||
}
|
|
||||||
|
|
||||||
proc := &mockProcReader{
|
|
||||||
procs: []procfs.QEMUProcess{
|
|
||||||
{PID: 1, VMID: "100", Name: "vm", Vcores: 1, MaxMem: 1024},
|
|
||||||
},
|
|
||||||
cpuTimes: map[int]procfs.CPUTimes{1: {}},
|
|
||||||
ioCount: map[int]procfs.IOCounters{1: {}},
|
|
||||||
status: map[int]procfs.StatusInfo{
|
|
||||||
1: {Threads: 1, MemoryExtended: procfs.MemoryExtended{}},
|
|
||||||
},
|
|
||||||
memPct: map[int]float64{1: 0},
|
|
||||||
}
|
|
||||||
|
|
||||||
blockOutput := `drive-scsi0 (#block100): /dev/zvol/rpool/data/vm-100-disk-0 (raw, read-write)
|
|
||||||
Attached to: /machine/peripheral/virtioscsi0/virtio-backend
|
|
||||||
Cache mode: writeback, direct
|
|
||||||
Detect zeroes: on
|
|
||||||
drive-scsi1 (#block101): /mnt/storage/images/100/vm-100-disk-1.qcow2 (qcow2, read-only)
|
|
||||||
Attached to: /machine/peripheral/virtioscsi0/virtio-backend
|
|
||||||
`
|
|
||||||
|
|
||||||
sys := &mockSysReader{
|
|
||||||
blockSize: map[string]int64{
|
|
||||||
"/dev/zvol/rpool/data/vm-100-disk-0": 10737418240,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
qm := &mockQMMonitor{responses: map[string]string{
|
|
||||||
"100:info network": "",
|
|
||||||
"100:info block": blockOutput,
|
|
||||||
}}
|
|
||||||
|
|
||||||
fr := &mockFileReader{files: map[string]string{"/etc/pve/user.cfg": ""}}
|
|
||||||
c := NewWithDeps(cfg, proc, sys, qm, &mockStatFS{}, &mockCmdRunner{}, fr)
|
|
||||||
metrics := collectMetrics(c)
|
|
||||||
|
|
||||||
diskInfo := metrics["pve_kvm_disk_info"]
|
|
||||||
if len(diskInfo) != 2 {
|
|
||||||
t.Fatalf("expected 2 disk info metrics, got %d", len(diskInfo))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check zvol disk
|
|
||||||
m := findMetricWithLabels(diskInfo, map[string]string{
|
|
||||||
"id": "100",
|
|
||||||
"disk_name": "scsi0",
|
|
||||||
"disk_type": "zvol",
|
|
||||||
"cache_mode": "writeback, direct",
|
|
||||||
"detect_zeroes": "on",
|
|
||||||
"read_only": "",
|
|
||||||
"vol_name": "vm-100-disk-0",
|
|
||||||
"pool": "rpool/data",
|
|
||||||
})
|
|
||||||
if m == nil {
|
|
||||||
t.Error("zvol disk info metric not found with expected labels")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check qcow2 disk (read-only, no cache_mode)
|
|
||||||
m = findMetricWithLabels(diskInfo, map[string]string{
|
|
||||||
"id": "100",
|
|
||||||
"disk_name": "scsi1",
|
|
||||||
"disk_type": "qcow2",
|
|
||||||
"read_only": "true",
|
|
||||||
"cache_mode": "",
|
|
||||||
"vol_name": "vm-100-disk-1",
|
|
||||||
})
|
|
||||||
if m == nil {
|
|
||||||
t.Error("qcow2 disk info metric not found with expected labels")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify disk size for zvol
|
|
||||||
diskSize := metrics["pve_kvm_disk_size_bytes"]
|
|
||||||
if len(diskSize) < 1 {
|
|
||||||
t.Fatal("expected at least 1 disk size metric")
|
|
||||||
}
|
|
||||||
m = findMetricWithLabels(diskSize, map[string]string{"disk_name": "scsi0"})
|
|
||||||
if m == nil || metricValue(m) != 10737418240 {
|
|
||||||
t.Errorf("disk size for scsi0 = %v", m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
Port int
|
|
||||||
Host string
|
|
||||||
CollectRunningVMs bool
|
|
||||||
CollectStorage bool
|
|
||||||
MetricsPrefix string
|
|
||||||
LogLevel string
|
|
||||||
QMTerminalTimeout time.Duration
|
|
||||||
QMMaxTTL time.Duration
|
|
||||||
QMRand time.Duration
|
|
||||||
QMMonitorDeferClose bool
|
|
||||||
ShowVersion bool
|
|
||||||
Version string
|
|
||||||
}
|
|
||||||
|
|
||||||
func Parse() Config {
|
|
||||||
c := Config{}
|
|
||||||
flag.IntVar(&c.Port, "port", 9116, "HTTP server listen port")
|
|
||||||
flag.StringVar(&c.Host, "host", "0.0.0.0", "HTTP server bind address")
|
|
||||||
flag.BoolVar(&c.CollectRunningVMs, "collect-running-vms", true, "collect KVM VM metrics")
|
|
||||||
flag.BoolVar(&c.CollectStorage, "collect-storage", true, "collect storage pool metrics")
|
|
||||||
flag.StringVar(&c.MetricsPrefix, "metrics-prefix", "pve", "metric name prefix")
|
|
||||||
flag.StringVar(&c.LogLevel, "loglevel", "INFO", "log level (DEBUG, INFO, WARNING, ERROR)")
|
|
||||||
flag.DurationVar(&c.QMTerminalTimeout, "qm-terminal-timeout", 10*time.Second, "qm monitor command timeout")
|
|
||||||
flag.DurationVar(&c.QMMaxTTL, "qm-max-ttl", 600*time.Second, "cache TTL for qm monitor data")
|
|
||||||
flag.DurationVar(&c.QMRand, "qm-rand", 60*time.Second, "randomness for qm cache expiry")
|
|
||||||
flag.BoolVar(&c.QMMonitorDeferClose, "qm-monitor-defer-close", true, "defer closing unresponsive qm sessions")
|
|
||||||
flag.BoolVar(&c.ShowVersion, "version", false, "print the version and exit")
|
|
||||||
flag.Parse()
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package logging
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"log/slog"
|
|
||||||
)
|
|
||||||
|
|
||||||
// LevelTrace is a custom log level below Debug for very detailed diagnostic output.
|
|
||||||
const LevelTrace = slog.Level(-8)
|
|
||||||
|
|
||||||
// Trace logs a message at TRACE level using the default logger.
|
|
||||||
func Trace(msg string, args ...any) {
|
|
||||||
slog.Default().Log(context.Background(), LevelTrace, msg, args...)
|
|
||||||
}
|
|
||||||
@@ -1,402 +0,0 @@
|
|||||||
package procfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
const clkTck = 100 // sysconf(_SC_CLK_TCK) on Linux
|
|
||||||
|
|
||||||
// QEMUProcess holds info discovered from /proc for a QEMU VM.
|
|
||||||
type QEMUProcess struct {
|
|
||||||
PID int
|
|
||||||
VMID string
|
|
||||||
Name string
|
|
||||||
CPU string
|
|
||||||
Vcores int
|
|
||||||
MaxMem int64 // in kB (parsed from cmdline)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CPUTimes holds parsed CPU times from /proc/{pid}/stat.
|
|
||||||
type CPUTimes struct {
|
|
||||||
User float64
|
|
||||||
System float64
|
|
||||||
IOWait float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// IOCounters holds parsed I/O counters from /proc/{pid}/io.
|
|
||||||
type IOCounters struct {
|
|
||||||
ReadChars uint64
|
|
||||||
WriteChars uint64
|
|
||||||
ReadSyscalls uint64
|
|
||||||
WriteSyscalls uint64
|
|
||||||
ReadBytes uint64
|
|
||||||
WriteBytes uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// CtxSwitches holds context switch counts from /proc/{pid}/status.
|
|
||||||
type CtxSwitches struct {
|
|
||||||
Voluntary uint64
|
|
||||||
Involuntary uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// MemoryExtended holds memory info from /proc/{pid}/status (values in bytes).
|
|
||||||
type MemoryExtended map[string]int64
|
|
||||||
|
|
||||||
// StatusInfo holds all fields parsed from /proc/{pid}/status in a single read.
|
|
||||||
type StatusInfo struct {
|
|
||||||
Threads int
|
|
||||||
VmRSS int64 // bytes
|
|
||||||
MemoryExtended MemoryExtended
|
|
||||||
CtxSwitches CtxSwitches
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProcReader abstracts /proc access for testability.
|
|
||||||
type ProcReader interface {
|
|
||||||
DiscoverQEMUProcesses() ([]QEMUProcess, error)
|
|
||||||
GetCPUTimes(pid int) (CPUTimes, error)
|
|
||||||
GetIOCounters(pid int) (IOCounters, error)
|
|
||||||
GetStatus(pid int) (StatusInfo, error)
|
|
||||||
GetMemoryPercent(pid int, rssBytes int64) (float64, error)
|
|
||||||
VMConfigExists(vmid string) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// RealProcReader reads from the actual /proc filesystem.
|
|
||||||
type RealProcReader struct {
|
|
||||||
ProcPath string // default "/proc"
|
|
||||||
PVECfgPath string // default "/etc/pve/qemu-server"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRealProcReader() *RealProcReader {
|
|
||||||
return &RealProcReader{
|
|
||||||
ProcPath: "/proc",
|
|
||||||
PVECfgPath: "/etc/pve/qemu-server",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RealProcReader) DiscoverQEMUProcesses() ([]QEMUProcess, error) {
|
|
||||||
entries, err := os.ReadDir(r.ProcPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
numericPIDs := 0
|
|
||||||
var procs []QEMUProcess
|
|
||||||
for _, e := range entries {
|
|
||||||
if !e.IsDir() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
pid, err := strconv.Atoi(e.Name())
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
numericPIDs++
|
|
||||||
|
|
||||||
exe, err := os.Readlink(filepath.Join(r.ProcPath, e.Name(), "exe"))
|
|
||||||
if err != nil {
|
|
||||||
logging.Trace("proc readlink failed", "pid", pid, "err", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if exe != "/usr/bin/qemu-system-x86_64" && exe != "/usr/bin/qemu-system-x86_64 (deleted)" {
|
|
||||||
logging.Trace("proc exe skip", "pid", pid, "exe", exe)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
cmdlineBytes, err := os.ReadFile(filepath.Join(r.ProcPath, e.Name(), "cmdline"))
|
|
||||||
if err != nil {
|
|
||||||
logging.Trace("proc cmdline read failed", "pid", pid, "err", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
cmdline := ParseCmdline(cmdlineBytes)
|
|
||||||
|
|
||||||
vmid := FlagValue(cmdline, "-id")
|
|
||||||
if vmid == "" {
|
|
||||||
logging.Trace("proc no -id flag", "pid", pid)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !r.VMConfigExists(vmid) {
|
|
||||||
logging.Trace("proc no config", "pid", pid, "vmid", vmid)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
proc := QEMUProcess{
|
|
||||||
PID: pid,
|
|
||||||
VMID: vmid,
|
|
||||||
Name: FlagValueBase(cmdline, "-name"),
|
|
||||||
CPU: FlagValueBase(cmdline, "-cpu"),
|
|
||||||
}
|
|
||||||
proc.Vcores = ParseVcores(cmdline)
|
|
||||||
proc.MaxMem = ParseMem(cmdline)
|
|
||||||
logging.Trace("proc discovered VM", "pid", pid, "vmid", vmid, "name", proc.Name)
|
|
||||||
procs = append(procs, proc)
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Trace("proc scan complete", "numeric_pids", numericPIDs, "qemu_count", len(procs))
|
|
||||||
if len(procs) == 0 {
|
|
||||||
slog.Warn("no QEMU processes discovered", "numeric_pids", numericPIDs, "proc_path", r.ProcPath, "pve_cfg_path", r.PVECfgPath)
|
|
||||||
}
|
|
||||||
return procs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RealProcReader) VMConfigExists(vmid string) bool {
|
|
||||||
_, err := os.Stat(filepath.Join(r.PVECfgPath, vmid+".conf"))
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RealProcReader) GetCPUTimes(pid int) (CPUTimes, error) {
|
|
||||||
data, err := os.ReadFile(filepath.Join(r.ProcPath, strconv.Itoa(pid), "stat"))
|
|
||||||
if err != nil {
|
|
||||||
return CPUTimes{}, err
|
|
||||||
}
|
|
||||||
return ParseStat(string(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RealProcReader) GetIOCounters(pid int) (IOCounters, error) {
|
|
||||||
data, err := os.ReadFile(filepath.Join(r.ProcPath, strconv.Itoa(pid), "io"))
|
|
||||||
if err != nil {
|
|
||||||
return IOCounters{}, err
|
|
||||||
}
|
|
||||||
return ParseIO(string(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RealProcReader) GetStatus(pid int) (StatusInfo, error) {
|
|
||||||
data, err := os.ReadFile(filepath.Join(r.ProcPath, strconv.Itoa(pid), "status"))
|
|
||||||
if err != nil {
|
|
||||||
return StatusInfo{}, err
|
|
||||||
}
|
|
||||||
return ParseStatus(string(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RealProcReader) GetMemoryPercent(pid int, rssBytes int64) (float64, error) {
|
|
||||||
meminfoData, err := os.ReadFile(filepath.Join(r.ProcPath, "meminfo"))
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
totalMem := int64(0)
|
|
||||||
for _, line := range strings.Split(string(meminfoData), "\n") {
|
|
||||||
if strings.HasPrefix(line, "MemTotal:") {
|
|
||||||
parts := strings.Fields(line)
|
|
||||||
if len(parts) >= 2 {
|
|
||||||
totalMem, _ = strconv.ParseInt(parts[1], 10, 64)
|
|
||||||
totalMem *= 1024 // kB to bytes
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if totalMem == 0 {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return float64(rssBytes) / float64(totalMem) * 100.0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseCmdline splits a null-byte separated /proc/{pid}/cmdline.
|
|
||||||
func ParseCmdline(data []byte) []string {
|
|
||||||
s := string(data)
|
|
||||||
if len(s) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// Remove trailing null byte if present
|
|
||||||
s = strings.TrimRight(s, "\x00")
|
|
||||||
return strings.Split(s, "\x00")
|
|
||||||
}
|
|
||||||
|
|
||||||
// FlagValue returns the value after a flag in cmdline args.
|
|
||||||
func FlagValue(cmdline []string, flag string) string {
|
|
||||||
for i, arg := range cmdline {
|
|
||||||
if arg == flag && i+1 < len(cmdline) {
|
|
||||||
return cmdline[i+1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// FlagValueBase returns the value after a flag, taking only the part
|
|
||||||
// before the first comma. QEMU flags like -name and -cpu use
|
|
||||||
// comma-separated sub-options (e.g. "-name myvm,debug-threads=on",
|
|
||||||
// "-cpu host,+kvm_pv_eoi") where only the first element is the
|
|
||||||
// primary value.
|
|
||||||
func FlagValueBase(cmdline []string, flag string) string {
|
|
||||||
v := FlagValue(cmdline, flag)
|
|
||||||
if i := strings.IndexByte(v, ','); i >= 0 {
|
|
||||||
return v[:i]
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseVcores extracts vCPU count from -smp flag.
|
|
||||||
// -smp can be just a number or key=value pairs like "4,sockets=1,cores=4,maxcpus=4"
|
|
||||||
func ParseVcores(cmdline []string) int {
|
|
||||||
smp := FlagValue(cmdline, "-smp")
|
|
||||||
if smp == "" {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
// Try simple numeric
|
|
||||||
parts := strings.Split(smp, ",")
|
|
||||||
n, err := strconv.Atoi(parts[0])
|
|
||||||
if err == nil {
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
// Try key=value format
|
|
||||||
for _, p := range parts {
|
|
||||||
kv := strings.SplitN(p, "=", 2)
|
|
||||||
if len(kv) == 2 && kv[0] == "cpus" {
|
|
||||||
n, _ = strconv.Atoi(kv[1])
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseMem extracts max memory in kB from cmdline.
|
|
||||||
// Simple: -m 1024 -> 1024*1024 kB
|
|
||||||
// Hotplug: -m size=4096M,slots=255,maxmem=524288M -> parse size= key
|
|
||||||
// NUMA: memory-backend-ram...size=NM -> sum * 1024 kB
|
|
||||||
func ParseMem(cmdline []string) int64 {
|
|
||||||
mVal := FlagValue(cmdline, "-m")
|
|
||||||
if mVal == "" {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
// Simple numeric case
|
|
||||||
if n, err := strconv.ParseInt(mVal, 10, 64); err == nil {
|
|
||||||
return n * 1024 // MB to kB
|
|
||||||
}
|
|
||||||
// Hotplug/NUMA: -m size=NM,slots=N,maxmem=NM
|
|
||||||
// Parse the size= key from the -m value itself
|
|
||||||
for _, part := range strings.Split(mVal, ",") {
|
|
||||||
if strings.HasPrefix(part, "size=") {
|
|
||||||
sizeStr := strings.TrimPrefix(part, "size=")
|
|
||||||
if strings.HasSuffix(sizeStr, "M") {
|
|
||||||
sizeStr = strings.TrimSuffix(sizeStr, "M")
|
|
||||||
if n, err := strconv.ParseInt(sizeStr, 10, 64); err == nil {
|
|
||||||
return n * 1024 // MB to kB
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// NUMA fallback: sum memory-backend-ram object sizes
|
|
||||||
var total int64
|
|
||||||
for _, arg := range cmdline {
|
|
||||||
if strings.Contains(arg, "memory-backend-ram") {
|
|
||||||
for _, part := range strings.Split(arg, ",") {
|
|
||||||
if strings.HasPrefix(part, "size=") {
|
|
||||||
sizeStr := strings.TrimPrefix(part, "size=")
|
|
||||||
if strings.HasSuffix(sizeStr, "M") {
|
|
||||||
sizeStr = strings.TrimSuffix(sizeStr, "M")
|
|
||||||
if n, err := strconv.ParseInt(sizeStr, 10, 64); err == nil {
|
|
||||||
total += n * 1024 // MB to kB
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return total
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseStat extracts CPU times from /proc/{pid}/stat.
|
|
||||||
// Fields: (1-indexed) 14=utime, 15=stime, 42=delayacct_blkio_ticks
|
|
||||||
func ParseStat(data string) (CPUTimes, error) {
|
|
||||||
// Find the closing paren of comm field to handle spaces in process names
|
|
||||||
closeIdx := strings.LastIndex(data, ")")
|
|
||||||
if closeIdx < 0 {
|
|
||||||
return CPUTimes{}, fmt.Errorf("malformed stat: no closing paren")
|
|
||||||
}
|
|
||||||
// Fields after ") " are 1-indexed starting at field 3
|
|
||||||
rest := data[closeIdx+2:]
|
|
||||||
fields := strings.Fields(rest)
|
|
||||||
// field 14 (utime) is at index 14-3=11, field 15 (stime) at 12, field 42 at 39
|
|
||||||
if len(fields) < 40 {
|
|
||||||
return CPUTimes{}, fmt.Errorf("not enough fields in stat: %d", len(fields))
|
|
||||||
}
|
|
||||||
utime, _ := strconv.ParseUint(fields[11], 10, 64)
|
|
||||||
stime, _ := strconv.ParseUint(fields[12], 10, 64)
|
|
||||||
blkio, _ := strconv.ParseUint(fields[39], 10, 64)
|
|
||||||
return CPUTimes{
|
|
||||||
User: float64(utime) / clkTck,
|
|
||||||
System: float64(stime) / clkTck,
|
|
||||||
IOWait: float64(blkio) / clkTck,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseIO parses /proc/{pid}/io.
|
|
||||||
func ParseIO(data string) (IOCounters, error) {
|
|
||||||
var io IOCounters
|
|
||||||
for _, line := range strings.Split(data, "\n") {
|
|
||||||
parts := strings.SplitN(line, ": ", 2)
|
|
||||||
if len(parts) != 2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val, err := strconv.ParseUint(strings.TrimSpace(parts[1]), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
switch parts[0] {
|
|
||||||
case "rchar":
|
|
||||||
io.ReadChars = val
|
|
||||||
case "wchar":
|
|
||||||
io.WriteChars = val
|
|
||||||
case "syscr":
|
|
||||||
io.ReadSyscalls = val
|
|
||||||
case "syscw":
|
|
||||||
io.WriteSyscalls = val
|
|
||||||
case "read_bytes":
|
|
||||||
io.ReadBytes = val
|
|
||||||
case "write_bytes":
|
|
||||||
io.WriteBytes = val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return io, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseStatus parses /proc/{pid}/status in one pass, extracting threads, VmRSS,
|
|
||||||
// memory extended fields, and context switches.
|
|
||||||
func ParseStatus(data string) (StatusInfo, error) {
|
|
||||||
var info StatusInfo
|
|
||||||
info.MemoryExtended = make(MemoryExtended)
|
|
||||||
foundThreads := false
|
|
||||||
|
|
||||||
for _, line := range strings.Split(data, "\n") {
|
|
||||||
parts := strings.Fields(line)
|
|
||||||
if len(parts) < 2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case parts[0] == "Threads:":
|
|
||||||
n, err := strconv.Atoi(parts[1])
|
|
||||||
if err != nil {
|
|
||||||
return StatusInfo{}, fmt.Errorf("parse Threads: %w", err)
|
|
||||||
}
|
|
||||||
info.Threads = n
|
|
||||||
foundThreads = true
|
|
||||||
case parts[0] == "voluntary_ctxt_switches:":
|
|
||||||
info.CtxSwitches.Voluntary, _ = strconv.ParseUint(parts[1], 10, 64)
|
|
||||||
case parts[0] == "nonvoluntary_ctxt_switches:":
|
|
||||||
info.CtxSwitches.Involuntary, _ = strconv.ParseUint(parts[1], 10, 64)
|
|
||||||
case strings.HasPrefix(line, "Vm") || strings.HasPrefix(line, "Rss") || strings.HasPrefix(line, "Hugetlb"):
|
|
||||||
key := strings.ToLower(strings.TrimSuffix(parts[0], ":"))
|
|
||||||
val, err := strconv.ParseInt(parts[1], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if len(parts) >= 3 && parts[2] == "kB" {
|
|
||||||
val *= 1024
|
|
||||||
}
|
|
||||||
info.MemoryExtended[key] = val
|
|
||||||
if key == "vmrss" {
|
|
||||||
info.VmRSS = val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !foundThreads {
|
|
||||||
return StatusInfo{}, fmt.Errorf("Threads field not found")
|
|
||||||
}
|
|
||||||
return info, nil
|
|
||||||
}
|
|
||||||
@@ -1,378 +0,0 @@
|
|||||||
package procfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestParseCmdline(t *testing.T) {
|
|
||||||
data := []byte("/usr/bin/qemu-system-x86_64\x00-id\x00100\x00-name\x00myvm\x00-cpu\x00host\x00-smp\x004\x00-m\x002048\x00")
|
|
||||||
args := ParseCmdline(data)
|
|
||||||
if len(args) != 11 {
|
|
||||||
t.Fatalf("expected 11 args, got %d: %v", len(args), args)
|
|
||||||
}
|
|
||||||
if args[0] != "/usr/bin/qemu-system-x86_64" {
|
|
||||||
t.Fatalf("unexpected first arg: %s", args[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFlagValue(t *testing.T) {
|
|
||||||
cmdline := []string{"/usr/bin/qemu-system-x86_64", "-id", "100", "-name", "myvm", "-cpu", "host"}
|
|
||||||
tests := []struct {
|
|
||||||
flag, want string
|
|
||||||
}{
|
|
||||||
{"-id", "100"},
|
|
||||||
{"-name", "myvm"},
|
|
||||||
{"-cpu", "host"},
|
|
||||||
{"-missing", ""},
|
|
||||||
}
|
|
||||||
for _, tc := range tests {
|
|
||||||
got := FlagValue(cmdline, tc.flag)
|
|
||||||
if got != tc.want {
|
|
||||||
t.Errorf("FlagValue(%q) = %q, want %q", tc.flag, got, tc.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFlagValueBase(t *testing.T) {
|
|
||||||
cmdline := []string{"-name", "myvm,debug-threads=on", "-cpu", "host,+kvm_pv_eoi,+kvm_pv_unhalt", "-id", "100"}
|
|
||||||
tests := []struct {
|
|
||||||
flag, want string
|
|
||||||
}{
|
|
||||||
{"-name", "myvm"},
|
|
||||||
{"-cpu", "host"},
|
|
||||||
{"-id", "100"},
|
|
||||||
{"-missing", ""},
|
|
||||||
}
|
|
||||||
for _, tc := range tests {
|
|
||||||
got := FlagValueBase(cmdline, tc.flag)
|
|
||||||
if got != tc.want {
|
|
||||||
t.Errorf("FlagValueBase(%q) = %q, want %q", tc.flag, got, tc.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseVcores(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
cmdline []string
|
|
||||||
want int
|
|
||||||
}{
|
|
||||||
{"simple", []string{"-smp", "4"}, 4},
|
|
||||||
{"with_opts", []string{"-smp", "4,sockets=1,cores=4,maxcpus=4"}, 4},
|
|
||||||
{"missing", []string{"-m", "1024"}, 0},
|
|
||||||
}
|
|
||||||
for _, tc := range tests {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
got := ParseVcores(tc.cmdline)
|
|
||||||
if got != tc.want {
|
|
||||||
t.Errorf("got %d, want %d", got, tc.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseMem(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
cmdline []string
|
|
||||||
want int64
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
"simple",
|
|
||||||
[]string{"-m", "1024"},
|
|
||||||
1024 * 1024, // kB
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"numa",
|
|
||||||
[]string{
|
|
||||||
"-m", "size=4096M,slots=255,maxmem=524288M",
|
|
||||||
"-object", "memory-backend-ram,id=ram-node0,size=2048M",
|
|
||||||
"-object", "memory-backend-ram,id=ram-node1,size=2048M",
|
|
||||||
},
|
|
||||||
4096 * 1024, // 2048+2048 MB in kB
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"hotplug without numa",
|
|
||||||
[]string{"-m", "size=131072M,slots=255,maxmem=524288M"},
|
|
||||||
131072 * 1024,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"large vm simple",
|
|
||||||
[]string{
|
|
||||||
"/usr/bin/kvm", "-id", "106",
|
|
||||||
"-name", "vm-106,debug-threads=on",
|
|
||||||
"-smp", "32,sockets=1,cores=32,maxcpus=32",
|
|
||||||
"-cpu", "host,+kvm_pv_eoi,+kvm_pv_unhalt",
|
|
||||||
"-m", "131072",
|
|
||||||
"-object", "iothread,id=iothread-virtio0",
|
|
||||||
"-machine", "type=q35+pve1",
|
|
||||||
},
|
|
||||||
131072 * 1024,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"missing",
|
|
||||||
[]string{"-smp", "4"},
|
|
||||||
0,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tc := range tests {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
got := ParseMem(tc.cmdline)
|
|
||||||
if got != tc.want {
|
|
||||||
t.Errorf("got %d, want %d", got, tc.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseStat(t *testing.T) {
|
|
||||||
// Realistic /proc/{pid}/stat with QEMU process name containing spaces
|
|
||||||
// Fields after ')': state(3) ppid(4) pgrp(5) session(6) tty_nr(7) tpgid(8) flags(9)
|
|
||||||
// minflt(10) cminflt(11) majflt(12) cmajflt(13) utime(14) stime(15)
|
|
||||||
// cutime(16) cstime(17) priority(18) nice(19) num_threads(20) itrealvalue(21)
|
|
||||||
// starttime(22) vsize(23) rss(24) rsslim(25) startcode(26) endcode(27) startstack(28)
|
|
||||||
// kstkesp(29) kstkeip(30) signal(31) blocked(32) sigignore(33) sigcatch(34) wchan(35)
|
|
||||||
// nswap(36) cnswap(37) exit_signal(38) processor(39) rt_priority(40) policy(41)
|
|
||||||
// delayacct_blkio_ticks(42)
|
|
||||||
stat := `12345 (qemu-system-x86) S 1 12345 12345 0 -1 4194304 1000 0 0 0 500 200 0 0 20 0 50 0 100 1000000 500 18446744073709551615 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42`
|
|
||||||
cpu, err := ParseStat(stat)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if cpu.User != 5.0 { // 500/100
|
|
||||||
t.Errorf("User = %f, want 5.0", cpu.User)
|
|
||||||
}
|
|
||||||
if cpu.System != 2.0 { // 200/100
|
|
||||||
t.Errorf("System = %f, want 2.0", cpu.System)
|
|
||||||
}
|
|
||||||
if cpu.IOWait != 0.42 { // 42/100
|
|
||||||
t.Errorf("IOWait = %f, want 0.42", cpu.IOWait)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseIO(t *testing.T) {
|
|
||||||
data := `rchar: 123456
|
|
||||||
wchar: 789012
|
|
||||||
syscr: 100
|
|
||||||
syscw: 200
|
|
||||||
read_bytes: 4096
|
|
||||||
write_bytes: 8192
|
|
||||||
cancelled_write_bytes: 0
|
|
||||||
`
|
|
||||||
io, err := ParseIO(data)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if io.ReadChars != 123456 {
|
|
||||||
t.Errorf("ReadChars = %d", io.ReadChars)
|
|
||||||
}
|
|
||||||
if io.WriteChars != 789012 {
|
|
||||||
t.Errorf("WriteChars = %d", io.WriteChars)
|
|
||||||
}
|
|
||||||
if io.ReadSyscalls != 100 {
|
|
||||||
t.Errorf("ReadSyscalls = %d", io.ReadSyscalls)
|
|
||||||
}
|
|
||||||
if io.WriteSyscalls != 200 {
|
|
||||||
t.Errorf("WriteSyscalls = %d", io.WriteSyscalls)
|
|
||||||
}
|
|
||||||
if io.ReadBytes != 4096 {
|
|
||||||
t.Errorf("ReadBytes = %d", io.ReadBytes)
|
|
||||||
}
|
|
||||||
if io.WriteBytes != 8192 {
|
|
||||||
t.Errorf("WriteBytes = %d", io.WriteBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseStatus(t *testing.T) {
|
|
||||||
data := `Name: qemu-system-x86
|
|
||||||
Threads: 50
|
|
||||||
VmPeak: 1000 kB
|
|
||||||
VmRSS: 500 kB
|
|
||||||
VmData: 200 kB
|
|
||||||
RssAnon: 100 kB
|
|
||||||
HugetlbPages: 0 kB
|
|
||||||
voluntary_ctxt_switches: 1234
|
|
||||||
nonvoluntary_ctxt_switches: 56
|
|
||||||
`
|
|
||||||
info, err := ParseStatus(data)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Threads
|
|
||||||
if info.Threads != 50 {
|
|
||||||
t.Errorf("Threads = %d, want 50", info.Threads)
|
|
||||||
}
|
|
||||||
|
|
||||||
// VmRSS
|
|
||||||
if info.VmRSS != 500*1024 {
|
|
||||||
t.Errorf("VmRSS = %d, want %d", info.VmRSS, 500*1024)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Memory extended
|
|
||||||
if info.MemoryExtended["vmpeak"] != 1000*1024 {
|
|
||||||
t.Errorf("VmPeak = %d", info.MemoryExtended["vmpeak"])
|
|
||||||
}
|
|
||||||
if info.MemoryExtended["vmrss"] != 500*1024 {
|
|
||||||
t.Errorf("VmRSS = %d", info.MemoryExtended["vmrss"])
|
|
||||||
}
|
|
||||||
if info.MemoryExtended["vmdata"] != 200*1024 {
|
|
||||||
t.Errorf("VmData = %d", info.MemoryExtended["vmdata"])
|
|
||||||
}
|
|
||||||
if info.MemoryExtended["rssanon"] != 100*1024 {
|
|
||||||
t.Errorf("RssAnon = %d", info.MemoryExtended["rssanon"])
|
|
||||||
}
|
|
||||||
if info.MemoryExtended["hugetlbpages"] != 0 {
|
|
||||||
t.Errorf("HugetlbPages = %d", info.MemoryExtended["hugetlbpages"])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Context switches
|
|
||||||
if info.CtxSwitches.Voluntary != 1234 {
|
|
||||||
t.Errorf("Voluntary = %d", info.CtxSwitches.Voluntary)
|
|
||||||
}
|
|
||||||
if info.CtxSwitches.Involuntary != 56 {
|
|
||||||
t.Errorf("Involuntary = %d", info.CtxSwitches.Involuntary)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseStatus_NoThreads(t *testing.T) {
|
|
||||||
data := `Name: qemu
|
|
||||||
VmRSS: 100 kB
|
|
||||||
`
|
|
||||||
_, err := ParseStatus(data)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error for missing Threads")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseStat_Malformed(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
data string
|
|
||||||
}{
|
|
||||||
{"no_closing_paren", "12345 (qemu S 1 12345"},
|
|
||||||
{"truncated", "12345 (qemu) S 1 2 3"},
|
|
||||||
{"empty", ""},
|
|
||||||
}
|
|
||||||
for _, tc := range tests {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
_, err := ParseStat(tc.data)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error for malformed stat")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseIO_Empty(t *testing.T) {
|
|
||||||
io, err := ParseIO("")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if io.ReadChars != 0 || io.WriteChars != 0 {
|
|
||||||
t.Errorf("expected zero counters, got %+v", io)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseIO_MalformedLines(t *testing.T) {
|
|
||||||
data := "rchar: notanumber\nbadline\nwchar: 100\n"
|
|
||||||
io, err := ParseIO(data)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if io.ReadChars != 0 {
|
|
||||||
t.Errorf("ReadChars = %d, want 0 (parse failure)", io.ReadChars)
|
|
||||||
}
|
|
||||||
if io.WriteChars != 100 {
|
|
||||||
t.Errorf("WriteChars = %d, want 100", io.WriteChars)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestDiscoverQEMUProcesses_DeletedExe verifies that QEMU processes whose
|
|
||||||
// /proc/{pid}/exe has a " (deleted)" suffix (common after package upgrades)
|
|
||||||
// are still discovered.
|
|
||||||
func TestDiscoverQEMUProcesses_DeletedExe(t *testing.T) {
|
|
||||||
// Build a fake /proc tree with two "QEMU" PIDs:
|
|
||||||
// 1000 -> normal exe
|
|
||||||
// 1001 -> exe with " (deleted)" suffix
|
|
||||||
tmpDir := t.TempDir()
|
|
||||||
procDir := filepath.Join(tmpDir, "proc")
|
|
||||||
pveCfgDir := filepath.Join(tmpDir, "pve")
|
|
||||||
|
|
||||||
cmdline100 := "/usr/bin/qemu-system-x86_64\x00-id\x00100\x00-name\x00vm100,debug-threads=on\x00-cpu\x00host,+kvm_pv_eoi,+kvm_pv_unhalt\x00-smp\x004\x00-m\x002048\x00"
|
|
||||||
cmdline101 := "/usr/bin/qemu-system-x86_64\x00-id\x00101\x00-name\x00vm101\x00-cpu\x00host\x00-smp\x002\x00-m\x001024\x00"
|
|
||||||
|
|
||||||
for _, tc := range []struct {
|
|
||||||
pid, vmid, exe, cmdline string
|
|
||||||
}{
|
|
||||||
{"1000", "100", "/usr/bin/qemu-system-x86_64", cmdline100},
|
|
||||||
{"1001", "101", "/usr/bin/qemu-system-x86_64 (deleted)", cmdline101},
|
|
||||||
} {
|
|
||||||
pidDir := filepath.Join(procDir, tc.pid)
|
|
||||||
if err := os.MkdirAll(pidDir, 0o755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// Create a real file as the symlink target, then symlink "exe" -> that file.
|
|
||||||
// os.Readlink returns the target path, which is what DiscoverQEMUProcesses reads.
|
|
||||||
target := filepath.Join(tmpDir, "bin-"+tc.pid)
|
|
||||||
if err := os.WriteFile(target, nil, 0o755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// We can't make Readlink return an arbitrary string with a real symlink,
|
|
||||||
// so instead we write the exe path to a regular file and override the
|
|
||||||
// readlink behavior. But DiscoverQEMUProcesses uses os.Readlink...
|
|
||||||
// The trick: symlink to the exact path string. On Linux, symlink targets
|
|
||||||
// don't need to exist -- Readlink returns the raw target.
|
|
||||||
if err := os.Symlink(tc.exe, filepath.Join(pidDir, "exe")); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath.Join(pidDir, "cmdline"), []byte(tc.cmdline), 0o644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
// Create VM config so VMConfigExists returns true
|
|
||||||
if err := os.MkdirAll(pveCfgDir, 0o755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := os.WriteFile(filepath.Join(pveCfgDir, tc.vmid+".conf"), nil, 0o644); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
r := &RealProcReader{
|
|
||||||
ProcPath: procDir,
|
|
||||||
PVECfgPath: pveCfgDir,
|
|
||||||
}
|
|
||||||
procs, err := r.DiscoverQEMUProcesses()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(procs) != 2 {
|
|
||||||
t.Fatalf("expected 2 procs, got %d", len(procs))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Collect discovered VMIDs
|
|
||||||
vmids := map[string]bool{}
|
|
||||||
for _, p := range procs {
|
|
||||||
vmids[p.VMID] = true
|
|
||||||
}
|
|
||||||
if !vmids["100"] {
|
|
||||||
t.Error("VM 100 (normal exe) not discovered")
|
|
||||||
}
|
|
||||||
if !vmids["101"] {
|
|
||||||
t.Error("VM 101 (deleted exe) not discovered")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify comma-separated options are stripped from name and cpu
|
|
||||||
for _, p := range procs {
|
|
||||||
if p.VMID == "100" {
|
|
||||||
if p.Name != "vm100" {
|
|
||||||
t.Errorf("VM 100 name = %q, want %q", p.Name, "vm100")
|
|
||||||
}
|
|
||||||
if p.CPU != "host" {
|
|
||||||
t.Errorf("VM 100 cpu = %q, want %q", p.CPU, "host")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
package pveconfig
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// PoolInfo holds parsed pool hierarchy info.
|
|
||||||
type PoolInfo struct {
|
|
||||||
LevelCount int
|
|
||||||
Level1 string
|
|
||||||
Level2 string
|
|
||||||
Level3 string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParsePoolConfig parses /etc/pve/user.cfg for pool definitions.
|
|
||||||
// Returns (vm_pool_map, pools).
|
|
||||||
// vm_pool_map: vmid -> pool_name
|
|
||||||
// pools: pool_name -> PoolInfo
|
|
||||||
func ParsePoolConfig(data string) (map[string]string, map[string]PoolInfo) {
|
|
||||||
vmPoolMap := make(map[string]string)
|
|
||||||
pools := make(map[string]PoolInfo)
|
|
||||||
|
|
||||||
for _, line := range strings.Split(data, "\n") {
|
|
||||||
if !strings.HasPrefix(line, "pool:") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
parts := strings.Split(strings.TrimSpace(line), ":")
|
|
||||||
if len(parts) < 2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
poolName := parts[1]
|
|
||||||
|
|
||||||
poolParts := strings.Split(poolName, "/")
|
|
||||||
info := PoolInfo{
|
|
||||||
LevelCount: len(poolParts),
|
|
||||||
}
|
|
||||||
if len(poolParts) > 0 {
|
|
||||||
info.Level1 = poolParts[0]
|
|
||||||
}
|
|
||||||
if len(poolParts) > 1 {
|
|
||||||
info.Level2 = poolParts[1]
|
|
||||||
}
|
|
||||||
if len(poolParts) > 2 {
|
|
||||||
info.Level3 = poolParts[2]
|
|
||||||
}
|
|
||||||
pools[poolName] = info
|
|
||||||
|
|
||||||
// VM list is in parts[3] if it exists
|
|
||||||
if len(parts) > 3 && parts[3] != "" {
|
|
||||||
for _, vmid := range strings.Split(parts[3], ",") {
|
|
||||||
vmid = strings.TrimSpace(vmid)
|
|
||||||
if vmid != "" {
|
|
||||||
vmPoolMap[vmid] = poolName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return vmPoolMap, pools
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
package pveconfig
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestParsePoolConfig_Basic(t *testing.T) {
|
|
||||||
data := `user:root@pam:1:0:::root@pam:
|
|
||||||
pool:production:Some comment:100,200,300
|
|
||||||
pool:staging:Staging env:400
|
|
||||||
pool:production/tier1:Tier 1:500,600
|
|
||||||
`
|
|
||||||
vmMap, pools := ParsePoolConfig(data)
|
|
||||||
|
|
||||||
// Check VM mappings
|
|
||||||
if vmMap["100"] != "production" {
|
|
||||||
t.Errorf("VM 100 pool = %q, want production", vmMap["100"])
|
|
||||||
}
|
|
||||||
if vmMap["200"] != "production" {
|
|
||||||
t.Errorf("VM 200 pool = %q", vmMap["200"])
|
|
||||||
}
|
|
||||||
if vmMap["400"] != "staging" {
|
|
||||||
t.Errorf("VM 400 pool = %q", vmMap["400"])
|
|
||||||
}
|
|
||||||
if vmMap["500"] != "production/tier1" {
|
|
||||||
t.Errorf("VM 500 pool = %q", vmMap["500"])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check pool info
|
|
||||||
prod := pools["production"]
|
|
||||||
if prod.LevelCount != 1 || prod.Level1 != "production" {
|
|
||||||
t.Errorf("production pool = %+v", prod)
|
|
||||||
}
|
|
||||||
|
|
||||||
tier1 := pools["production/tier1"]
|
|
||||||
if tier1.LevelCount != 2 || tier1.Level1 != "production" || tier1.Level2 != "tier1" {
|
|
||||||
t.Errorf("production/tier1 pool = %+v", tier1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParsePoolConfig_NoVMs(t *testing.T) {
|
|
||||||
data := `pool:empty:No VMs:
|
|
||||||
`
|
|
||||||
vmMap, pools := ParsePoolConfig(data)
|
|
||||||
if len(vmMap) != 0 {
|
|
||||||
t.Errorf("expected no VM mappings, got %d", len(vmMap))
|
|
||||||
}
|
|
||||||
if _, ok := pools["empty"]; !ok {
|
|
||||||
t.Error("expected 'empty' pool to exist")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParsePoolConfig_ThreeLevels(t *testing.T) {
|
|
||||||
data := `pool:a/b/c::deep:100
|
|
||||||
`
|
|
||||||
_, pools := ParsePoolConfig(data)
|
|
||||||
p := pools["a/b/c"]
|
|
||||||
if p.LevelCount != 3 || p.Level1 != "a" || p.Level2 != "b" || p.Level3 != "c" {
|
|
||||||
t.Errorf("pool = %+v", p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
package pveconfig
|
|
||||||
|
|
||||||
import (
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
// StorageEntry holds a parsed storage definition from storage.cfg.
|
|
||||||
type StorageEntry struct {
|
|
||||||
Properties map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
var sanitizeRe = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
|
||||||
|
|
||||||
// SanitizeKey replaces non-alphanumeric/underscore chars with underscore.
|
|
||||||
func SanitizeKey(key string) string {
|
|
||||||
return sanitizeRe.ReplaceAllString(key, "_")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseStorageConfig parses /etc/pve/storage.cfg content.
|
|
||||||
// Returns a list of storage entries, each with sanitized key-value properties.
|
|
||||||
func ParseStorageConfig(data string) []StorageEntry {
|
|
||||||
var result []StorageEntry
|
|
||||||
var current *StorageEntry
|
|
||||||
|
|
||||||
for _, rawLine := range strings.Split(data, "\n") {
|
|
||||||
trimmed := strings.TrimSpace(rawLine)
|
|
||||||
|
|
||||||
if trimmed == "" || strings.HasPrefix(trimmed, "#") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Section headers start at column 0 (no leading whitespace)
|
|
||||||
isIndented := len(rawLine) > 0 && (rawLine[0] == '\t' || rawLine[0] == ' ')
|
|
||||||
|
|
||||||
if !isIndented && strings.Contains(trimmed, ":") {
|
|
||||||
colonIdx := strings.Index(trimmed, ":")
|
|
||||||
sectionType := trimmed[:colonIdx]
|
|
||||||
sectionName := strings.TrimSpace(trimmed[colonIdx+1:])
|
|
||||||
|
|
||||||
if current != nil {
|
|
||||||
result = append(result, *current)
|
|
||||||
}
|
|
||||||
current = &StorageEntry{
|
|
||||||
Properties: map[string]string{
|
|
||||||
"type": SanitizeKey(sectionType),
|
|
||||||
"name": SanitizeKey(sectionName),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
logging.Trace("storage.cfg section", "type", sectionType, "name", sectionName)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Key-value property line
|
|
||||||
if current != nil {
|
|
||||||
parts := strings.SplitN(trimmed, " ", 2)
|
|
||||||
key := SanitizeKey(strings.TrimSpace(parts[0]))
|
|
||||||
if len(parts) > 1 {
|
|
||||||
current.Properties[key] = strings.TrimSpace(parts[1])
|
|
||||||
} else {
|
|
||||||
current.Properties[key] = "true"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if current != nil {
|
|
||||||
result = append(result, *current)
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Trace("ParseStorageConfig complete", "entries", len(result))
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
package pveconfig
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSanitizeKey(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
in, want string
|
|
||||||
}{
|
|
||||||
{"simple", "simple"},
|
|
||||||
{"with-dash", "with_dash"},
|
|
||||||
{"with.dot", "with_dot"},
|
|
||||||
{"with space", "with_space"},
|
|
||||||
{"key123", "key123"},
|
|
||||||
}
|
|
||||||
for _, tc := range tests {
|
|
||||||
got := SanitizeKey(tc.in)
|
|
||||||
if got != tc.want {
|
|
||||||
t.Errorf("SanitizeKey(%q) = %q, want %q", tc.in, got, tc.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseStorageConfig_Basic(t *testing.T) {
|
|
||||||
data := `dir: local
|
|
||||||
path /var/lib/vz
|
|
||||||
content iso,vztmpl,backup
|
|
||||||
maxfiles 3
|
|
||||||
|
|
||||||
zfspool: local-zfs
|
|
||||||
pool rpool/data
|
|
||||||
content images,rootdir
|
|
||||||
sparse 1
|
|
||||||
|
|
||||||
nfs: nas-backup
|
|
||||||
export /mnt/backup
|
|
||||||
path /mnt/pve/nas-backup
|
|
||||||
server 10.0.0.1
|
|
||||||
content backup
|
|
||||||
`
|
|
||||||
entries := ParseStorageConfig(data)
|
|
||||||
if len(entries) != 3 {
|
|
||||||
t.Fatalf("expected 3 entries, got %d", len(entries))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check dir entry
|
|
||||||
e := entries[0]
|
|
||||||
if e.Properties["type"] != "dir" {
|
|
||||||
t.Errorf("type = %q", e.Properties["type"])
|
|
||||||
}
|
|
||||||
if e.Properties["name"] != "local" {
|
|
||||||
t.Errorf("name = %q", e.Properties["name"])
|
|
||||||
}
|
|
||||||
if e.Properties["path"] != "/var/lib/vz" {
|
|
||||||
t.Errorf("path = %q", e.Properties["path"])
|
|
||||||
}
|
|
||||||
if e.Properties["content"] != "iso,vztmpl,backup" {
|
|
||||||
t.Errorf("content = %q", e.Properties["content"])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check zfspool entry
|
|
||||||
e = entries[1]
|
|
||||||
if e.Properties["type"] != "zfspool" {
|
|
||||||
t.Errorf("type = %q", e.Properties["type"])
|
|
||||||
}
|
|
||||||
if e.Properties["name"] != "local_zfs" {
|
|
||||||
t.Errorf("name = %q, want local_zfs", e.Properties["name"])
|
|
||||||
}
|
|
||||||
if e.Properties["pool"] != "rpool/data" {
|
|
||||||
t.Errorf("pool = %q", e.Properties["pool"])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check nfs entry
|
|
||||||
e = entries[2]
|
|
||||||
if e.Properties["type"] != "nfs" {
|
|
||||||
t.Errorf("type = %q", e.Properties["type"])
|
|
||||||
}
|
|
||||||
if e.Properties["server"] != "10.0.0.1" {
|
|
||||||
t.Errorf("server = %q", e.Properties["server"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseStorageConfig_Comments(t *testing.T) {
|
|
||||||
data := `# This is a comment
|
|
||||||
dir: local
|
|
||||||
path /var/lib/vz
|
|
||||||
# inline comment
|
|
||||||
content iso
|
|
||||||
`
|
|
||||||
entries := ParseStorageConfig(data)
|
|
||||||
if len(entries) != 1 {
|
|
||||||
t.Fatalf("expected 1 entry, got %d", len(entries))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseStorageConfig_BooleanValue(t *testing.T) {
|
|
||||||
data := `zfspool: tank
|
|
||||||
pool rpool/data
|
|
||||||
sparse
|
|
||||||
`
|
|
||||||
entries := ParseStorageConfig(data)
|
|
||||||
if entries[0].Properties["sparse"] != "true" {
|
|
||||||
t.Errorf("sparse = %q, want 'true'", entries[0].Properties["sparse"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,216 +0,0 @@
|
|||||||
package qmmonitor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DiskInfo holds parsed block device info from "info block".
|
|
||||||
type DiskInfo struct {
|
|
||||||
DiskName string
|
|
||||||
BlockID string
|
|
||||||
DiskPath string
|
|
||||||
DiskType string
|
|
||||||
Labels map[string]string // additional labels: vol_name, pool, device, etc.
|
|
||||||
}
|
|
||||||
|
|
||||||
// blockHeaderRe matches block device headers in both old and new QEMU formats:
|
|
||||||
// Old: "disk_name (#blockN): /path/to/disk (type, mode)"
|
|
||||||
// New: "disk_name: /path/to/disk (type, mode)"
|
|
||||||
var blockHeaderRe = regexp.MustCompile(`^(\w+)(?:\s+\(#block(\d+)\))?: (.+) \(([\w, -]+)\)$`)
|
|
||||||
|
|
||||||
// lvmRe matches: /dev/{vg_name}/vm-{N}-disk-{N}
|
|
||||||
var lvmRe = regexp.MustCompile(`^/dev/([^/]+)/(vm-\d+-disk-\d+)$`)
|
|
||||||
|
|
||||||
// driveStartRe matches "drive-" at the start of a line.
|
|
||||||
// This avoids splitting on "drive-" embedded inside JSON values
|
|
||||||
// (e.g., "throttle-group": "throttle-drive-virtio0").
|
|
||||||
var driveStartRe = regexp.MustCompile(`(?m)^drive-`)
|
|
||||||
|
|
||||||
// ParseBlockInfo parses "info block" output from qm monitor.
|
|
||||||
// Returns map of disk_name -> DiskInfo. Skips efidisk entries.
|
|
||||||
func ParseBlockInfo(raw string) map[string]DiskInfo {
|
|
||||||
result := make(map[string]DiskInfo)
|
|
||||||
|
|
||||||
// Find all "drive-" at start of a line and extract each block section
|
|
||||||
indices := driveStartRe.FindAllStringIndex(raw, -1)
|
|
||||||
if len(indices) == 0 {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := make([]string, len(indices))
|
|
||||||
for i, idx := range indices {
|
|
||||||
start := idx[0] + len("drive-")
|
|
||||||
end := len(raw)
|
|
||||||
if i+1 < len(indices) {
|
|
||||||
end = indices[i+1][0]
|
|
||||||
}
|
|
||||||
parts[i] = raw[start:end]
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, part := range parts {
|
|
||||||
lines := strings.Split(strings.TrimSpace(part), "\n")
|
|
||||||
if len(lines) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
match := blockHeaderRe.FindStringSubmatch(strings.TrimSpace(lines[0]))
|
|
||||||
if match == nil {
|
|
||||||
logging.Trace("block header no match", "line", lines[0])
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
diskName := match[1]
|
|
||||||
blockID := match[2]
|
|
||||||
diskPath := match[3]
|
|
||||||
diskTypeAndMode := match[4]
|
|
||||||
modeParts := strings.Split(diskTypeAndMode, ", ")
|
|
||||||
diskType := modeParts[0]
|
|
||||||
readOnly := false
|
|
||||||
for _, p := range modeParts[1:] {
|
|
||||||
if p == "read-only" {
|
|
||||||
readOnly = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip EFI disks
|
|
||||||
if strings.Contains(diskName, "efidisk") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle json: paths
|
|
||||||
if strings.HasPrefix(diskPath, "json:") {
|
|
||||||
resolved, err := HandleJSONPath(diskPath)
|
|
||||||
if err != nil {
|
|
||||||
logging.Trace("block json path error", "disk", diskName, "err", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
logging.Trace("block json resolved", "disk", diskName, "resolved", resolved)
|
|
||||||
diskPath = resolved
|
|
||||||
}
|
|
||||||
|
|
||||||
info := DiskInfo{
|
|
||||||
DiskName: diskName,
|
|
||||||
BlockID: blockID,
|
|
||||||
DiskPath: diskPath,
|
|
||||||
DiskType: diskType,
|
|
||||||
Labels: make(map[string]string),
|
|
||||||
}
|
|
||||||
|
|
||||||
if readOnly {
|
|
||||||
info.Labels["read_only"] = "true"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Detect disk type from path
|
|
||||||
classifyDisk(&info)
|
|
||||||
logging.Trace("block classified", "disk", diskName, "type", info.DiskType, "path", diskPath)
|
|
||||||
|
|
||||||
// Parse additional info from remaining lines
|
|
||||||
for _, line := range lines[1:] {
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
if strings.HasPrefix(line, "Attached to:") {
|
|
||||||
// Extract device ID, e.g. "Attached to: /machine/peripheral/virtio0/virtio-backend"
|
|
||||||
val := strings.TrimSpace(strings.TrimPrefix(line, "Attached to:"))
|
|
||||||
// Extract short device name from QOM path
|
|
||||||
if strings.Contains(val, "/") {
|
|
||||||
qomParts := strings.Split(val, "/")
|
|
||||||
if len(qomParts) > 3 {
|
|
||||||
val = qomParts[3]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
info.Labels["attached_to"] = val
|
|
||||||
} else if strings.HasPrefix(line, "Cache mode:") {
|
|
||||||
val := strings.TrimSpace(strings.TrimPrefix(line, "Cache mode:"))
|
|
||||||
info.Labels["cache_mode"] = val
|
|
||||||
} else if strings.HasPrefix(line, "Detect zeroes:") {
|
|
||||||
val := strings.TrimSpace(strings.TrimPrefix(line, "Detect zeroes:"))
|
|
||||||
info.Labels["detect_zeroes"] = val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Trace("block parsed", "disk", diskName, "labels", info.Labels)
|
|
||||||
result[diskName] = info
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Trace("ParseBlockInfo complete", "disk_count", len(result))
|
|
||||||
if len(result) == 0 && raw != "" {
|
|
||||||
slog.Debug("ParseBlockInfo found no disks", "rawLen", len(raw))
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// classifyDisk sets DiskType and extra labels based on the disk path.
|
|
||||||
func classifyDisk(info *DiskInfo) {
|
|
||||||
path := info.DiskPath
|
|
||||||
|
|
||||||
if info.DiskType == "qcow2" {
|
|
||||||
// Extract volume name: filename without extension
|
|
||||||
parts := strings.Split(path, "/")
|
|
||||||
filename := parts[len(parts)-1]
|
|
||||||
dotIdx := strings.Index(filename, ".")
|
|
||||||
if dotIdx > 0 {
|
|
||||||
info.Labels["vol_name"] = filename[:dotIdx]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(path, "/dev/zvol/") {
|
|
||||||
info.DiskType = "zvol"
|
|
||||||
// /dev/zvol/pool_name/vol_name
|
|
||||||
trimmed := strings.TrimPrefix(path, "/dev/zvol/")
|
|
||||||
parts := strings.Split(trimmed, "/")
|
|
||||||
if len(parts) >= 2 {
|
|
||||||
info.Labels["pool"] = strings.Join(parts[:len(parts)-1], "/")
|
|
||||||
info.Labels["vol_name"] = parts[len(parts)-1]
|
|
||||||
}
|
|
||||||
} else if strings.HasPrefix(path, "/dev/rbd-pve/") {
|
|
||||||
info.DiskType = "rbd"
|
|
||||||
// /dev/rbd-pve/cluster_id/pool/vol_name
|
|
||||||
parts := strings.Split(path, "/")
|
|
||||||
if len(parts) >= 5 {
|
|
||||||
info.Labels["cluster_id"] = parts[len(parts)-3]
|
|
||||||
info.Labels["pool"] = parts[len(parts)-2]
|
|
||||||
info.Labels["pool_name"] = parts[len(parts)-2]
|
|
||||||
info.Labels["vol_name"] = parts[len(parts)-1]
|
|
||||||
}
|
|
||||||
} else if m := lvmRe.FindStringSubmatch(path); m != nil {
|
|
||||||
info.DiskType = "lvm"
|
|
||||||
info.Labels["vg_name"] = m[1]
|
|
||||||
info.Labels["vol_name"] = m[2]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleJSONPath resolves a "json:{...}" disk path by searching for
|
|
||||||
// a driver == "host_device" entry and extracting its filename.
|
|
||||||
func HandleJSONPath(path string) (string, error) {
|
|
||||||
jsonStr := strings.TrimPrefix(path, "json:")
|
|
||||||
var data map[string]any
|
|
||||||
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
|
|
||||||
return "", fmt.Errorf("parse json path: %w", err)
|
|
||||||
}
|
|
||||||
if result := searchHostDevice(data); result != "" {
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
return "", fmt.Errorf("no host_device driver found in json path")
|
|
||||||
}
|
|
||||||
|
|
||||||
func searchHostDevice(data map[string]any) string {
|
|
||||||
driver, _ := data["driver"].(string)
|
|
||||||
if driver == "host_device" {
|
|
||||||
if filename, ok := data["filename"].(string); ok {
|
|
||||||
return filename
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, v := range data {
|
|
||||||
if sub, ok := v.(map[string]any); ok {
|
|
||||||
if result := searchHostDevice(sub); result != "" {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
@@ -1,325 +0,0 @@
|
|||||||
package qmmonitor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestParseBlockInfo_Qcow2(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block100): /mnt/storage/images/100/vm-100-disk-0.qcow2 (qcow2, read-write)
|
|
||||||
Attached to: /machine/peripheral/virtioscsi0/virtio-backend
|
|
||||||
Cache mode: writeback, direct
|
|
||||||
Detect zeroes: on
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
if len(disks) != 1 {
|
|
||||||
t.Fatalf("expected 1 disk, got %d", len(disks))
|
|
||||||
}
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.DiskType != "qcow2" {
|
|
||||||
t.Errorf("type = %q", d.DiskType)
|
|
||||||
}
|
|
||||||
if d.BlockID != "100" {
|
|
||||||
t.Errorf("block_id = %q", d.BlockID)
|
|
||||||
}
|
|
||||||
if d.Labels["vol_name"] != "vm-100-disk-0" {
|
|
||||||
t.Errorf("vol_name = %q", d.Labels["vol_name"])
|
|
||||||
}
|
|
||||||
if d.Labels["detect_zeroes"] != "on" {
|
|
||||||
t.Errorf("detect_zeroes = %q", d.Labels["detect_zeroes"])
|
|
||||||
}
|
|
||||||
if d.Labels["cache_mode"] != "writeback, direct" {
|
|
||||||
t.Errorf("cache_mode = %q, want %q", d.Labels["cache_mode"], "writeback, direct")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_Zvol(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block200): /dev/zvol/rpool/data/vm-200-disk-0 (raw, read-write)
|
|
||||||
Attached to: /machine/peripheral/virtioscsi0/virtio-backend
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.DiskType != "zvol" {
|
|
||||||
t.Errorf("type = %q", d.DiskType)
|
|
||||||
}
|
|
||||||
if d.Labels["pool"] != "rpool/data" {
|
|
||||||
t.Errorf("pool = %q", d.Labels["pool"])
|
|
||||||
}
|
|
||||||
if d.Labels["vol_name"] != "vm-200-disk-0" {
|
|
||||||
t.Errorf("vol_name = %q", d.Labels["vol_name"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_RBD(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block300): /dev/rbd-pve/ceph1/pool1/vm-300-disk-0 (raw, read-write)
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.DiskType != "rbd" {
|
|
||||||
t.Errorf("type = %q", d.DiskType)
|
|
||||||
}
|
|
||||||
if d.Labels["cluster_id"] != "ceph1" {
|
|
||||||
t.Errorf("cluster_id = %q", d.Labels["cluster_id"])
|
|
||||||
}
|
|
||||||
if d.Labels["pool"] != "pool1" {
|
|
||||||
t.Errorf("pool = %q", d.Labels["pool"])
|
|
||||||
}
|
|
||||||
if d.Labels["vol_name"] != "vm-300-disk-0" {
|
|
||||||
t.Errorf("vol_name = %q", d.Labels["vol_name"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_LVM(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block400): /dev/myvg/vm-400-disk-0 (raw, read-write)
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.DiskType != "lvm" {
|
|
||||||
t.Errorf("type = %q", d.DiskType)
|
|
||||||
}
|
|
||||||
if d.Labels["vg_name"] != "myvg" {
|
|
||||||
t.Errorf("vg_name = %q", d.Labels["vg_name"])
|
|
||||||
}
|
|
||||||
if d.Labels["vol_name"] != "vm-400-disk-0" {
|
|
||||||
t.Errorf("vol_name = %q", d.Labels["vol_name"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_SkipsEFI(t *testing.T) {
|
|
||||||
raw := `drive-efidisk0 (#block500): /dev/zvol/rpool/data/vm-500-disk-1 (raw, read-write)
|
|
||||||
drive-scsi0 (#block501): /dev/zvol/rpool/data/vm-500-disk-0 (raw, read-write)
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
if len(disks) != 1 {
|
|
||||||
t.Fatalf("expected 1 disk (efidisk skipped), got %d", len(disks))
|
|
||||||
}
|
|
||||||
if _, ok := disks["efidisk0"]; ok {
|
|
||||||
t.Error("efidisk0 should be skipped")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleJSONPath(t *testing.T) {
|
|
||||||
jsonPath := `json:{"driver":"raw","file":{"driver":"host_device","filename":"/dev/zvol/rpool/data/vm-100-disk-0"}}`
|
|
||||||
result, err := HandleJSONPath(jsonPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if result != "/dev/zvol/rpool/data/vm-100-disk-0" {
|
|
||||||
t.Errorf("got %q", result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleJSONPath_Nested(t *testing.T) {
|
|
||||||
jsonPath := `json:{"driver":"raw","file":{"driver":"copy-on-read","file":{"driver":"host_device","filename":"/dev/rbd-pve/ceph/pool/vm-200-disk-0"}}}`
|
|
||||||
result, err := HandleJSONPath(jsonPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if result != "/dev/rbd-pve/ceph/pool/vm-200-disk-0" {
|
|
||||||
t.Errorf("got %q", result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleJSONPath_NoHostDevice(t *testing.T) {
|
|
||||||
jsonPath := `json:{"driver":"raw","file":{"driver":"file","filename":"/tmp/test.img"}}`
|
|
||||||
_, err := HandleJSONPath(jsonPath)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error for missing host_device")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_ReadOnly(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block100): /dev/zvol/rpool/data/vm-100-disk-0 (raw, read-only)
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.Labels["read_only"] != "true" {
|
|
||||||
t.Errorf("expected read_only=true, got %q", d.Labels["read_only"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_ReadWrite(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block100): /dev/zvol/rpool/data/vm-100-disk-0 (raw, read-write)
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if _, ok := d.Labels["read_only"]; ok {
|
|
||||||
t.Error("read_only label should not be set for read-write disks")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_MalformedHeader(t *testing.T) {
|
|
||||||
raw := `drive-scsi0: this is not a valid header
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
if len(disks) != 0 {
|
|
||||||
t.Fatalf("expected 0 disks for malformed header, got %d", len(disks))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_Empty(t *testing.T) {
|
|
||||||
disks := ParseBlockInfo("")
|
|
||||||
if len(disks) != 0 {
|
|
||||||
t.Fatalf("expected 0 disks for empty input, got %d", len(disks))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_JSONError(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block100): json:{invalid json} (raw, read-write)
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
if len(disks) != 0 {
|
|
||||||
t.Fatalf("expected 0 disks for invalid JSON path, got %d", len(disks))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_Throttle(t *testing.T) {
|
|
||||||
// PVE 9.x / newer QEMU format: no (#blockN)
|
|
||||||
raw := `drive-scsi0: json:{"driver":"raw","file":{"driver":"host_device","filename":"/dev/zvol/rpool/data/vm-100-disk-0"}} (throttle, read-write)
|
|
||||||
Attached to: /machine/peripheral/virtioscsi0/virtio-backend
|
|
||||||
Cache mode: writeback, direct
|
|
||||||
Detect zeroes: unmap
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
if len(disks) != 1 {
|
|
||||||
t.Fatalf("expected 1 disk, got %d", len(disks))
|
|
||||||
}
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.BlockID != "" {
|
|
||||||
t.Errorf("block_id = %q, want empty", d.BlockID)
|
|
||||||
}
|
|
||||||
if d.DiskType != "zvol" {
|
|
||||||
t.Errorf("type = %q, want zvol", d.DiskType)
|
|
||||||
}
|
|
||||||
if d.DiskPath != "/dev/zvol/rpool/data/vm-100-disk-0" {
|
|
||||||
t.Errorf("path = %q", d.DiskPath)
|
|
||||||
}
|
|
||||||
if d.Labels["pool"] != "rpool/data" {
|
|
||||||
t.Errorf("pool = %q", d.Labels["pool"])
|
|
||||||
}
|
|
||||||
if d.Labels["vol_name"] != "vm-100-disk-0" {
|
|
||||||
t.Errorf("vol_name = %q", d.Labels["vol_name"])
|
|
||||||
}
|
|
||||||
if d.Labels["detect_zeroes"] != "unmap" {
|
|
||||||
t.Errorf("detect_zeroes = %q, want unmap", d.Labels["detect_zeroes"])
|
|
||||||
}
|
|
||||||
if d.Labels["cache_mode"] != "writeback, direct" {
|
|
||||||
t.Errorf("cache_mode = %q", d.Labels["cache_mode"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_DetectZeroesUnmap(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block100): /dev/zvol/rpool/data/vm-100-disk-0 (raw, read-write)
|
|
||||||
Detect zeroes: unmap
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.Labels["detect_zeroes"] != "unmap" {
|
|
||||||
t.Errorf("detect_zeroes = %q, want unmap", d.Labels["detect_zeroes"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_AttachedToVirtio(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block100): /dev/zvol/rpool/data/vm-100-disk-0 (raw, read-write)
|
|
||||||
Attached to: /machine/peripheral/virtio0/virtio-backend
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.Labels["attached_to"] != "virtio0" {
|
|
||||||
t.Errorf("attached_to = %q, want %q", d.Labels["attached_to"], "virtio0")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_AttachedToVirtioScsi(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block100): /dev/zvol/rpool/data/vm-100-disk-0 (raw, read-write)
|
|
||||||
Attached to: /machine/peripheral/virtioscsi0/virtio-backend
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["scsi0"]
|
|
||||||
if d.Labels["attached_to"] != "virtioscsi0" {
|
|
||||||
t.Errorf("attached_to = %q, want %q", d.Labels["attached_to"], "virtioscsi0")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_AttachedToBare(t *testing.T) {
|
|
||||||
raw := `drive-ide2 (#block100): /path/to/disk.iso (raw, read-only)
|
|
||||||
Attached to: ide2
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
d := disks["ide2"]
|
|
||||||
if d.Labels["attached_to"] != "ide2" {
|
|
||||||
t.Errorf("attached_to = %q, want %q", d.Labels["attached_to"], "ide2")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_MultiDisk(t *testing.T) {
|
|
||||||
raw := `drive-scsi0 (#block100): /dev/zvol/rpool/data/vm-100-disk-0 (raw, read-write)
|
|
||||||
Attached to: /machine/peripheral/virtioscsi0/virtio-backend
|
|
||||||
drive-scsi1 (#block101): /mnt/storage/images/100/vm-100-disk-1.qcow2 (qcow2, read-write)
|
|
||||||
Attached to: /machine/peripheral/virtioscsi0/virtio-backend
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
if len(disks) != 2 {
|
|
||||||
t.Fatalf("expected 2 disks, got %d", len(disks))
|
|
||||||
}
|
|
||||||
if disks["scsi0"].DiskType != "zvol" {
|
|
||||||
t.Errorf("scsi0 type = %q", disks["scsi0"].DiskType)
|
|
||||||
}
|
|
||||||
if disks["scsi1"].DiskType != "qcow2" {
|
|
||||||
t.Errorf("scsi1 type = %q", disks["scsi1"].DiskType)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_ThrottleJSONWithDriveInValue(t *testing.T) {
|
|
||||||
// PVE 9.x format: throttle JSON contains "throttle-drive-*" in values,
|
|
||||||
// which previously caused strings.Split("drive-") to break mid-JSON.
|
|
||||||
raw := `drive-ide2: json:{"throttle-group": "throttle-drive-ide2", "driver": "throttle", "file": {"driver": "raw", "file": {"driver": "host_device", "filename": "/dev/zvol/rpool/data/vm-100-cloudinit"}}} (throttle, read-only)
|
|
||||||
Attached to: ide2
|
|
||||||
Removable device: locked, tray closed
|
|
||||||
Cache mode: writeback
|
|
||||||
|
|
||||||
drive-virtio0: json:{"throttle-group": "throttle-drive-virtio0", "driver": "throttle", "file": {"driver": "raw", "file": {"driver": "host_device", "filename": "/dev/rbd-pve/00000000-0000-0000-0000-000000000001/pool1/vm-100-disk-0"}}} (throttle)
|
|
||||||
Attached to: /machine/peripheral/virtio0/virtio-backend
|
|
||||||
Cache mode: writeback
|
|
||||||
Detect zeroes: on`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
// ide2 is cloudinit (not efidisk), so it should be included
|
|
||||||
if len(disks) != 2 {
|
|
||||||
t.Fatalf("expected 2 disks, got %d", len(disks))
|
|
||||||
}
|
|
||||||
d := disks["virtio0"]
|
|
||||||
if d.DiskType != "rbd" {
|
|
||||||
t.Errorf("type = %q, want rbd", d.DiskType)
|
|
||||||
}
|
|
||||||
if d.DiskPath != "/dev/rbd-pve/00000000-0000-0000-0000-000000000001/pool1/vm-100-disk-0" {
|
|
||||||
t.Errorf("path = %q", d.DiskPath)
|
|
||||||
}
|
|
||||||
if d.Labels["attached_to"] != "virtio0" {
|
|
||||||
t.Errorf("attached_to = %q", d.Labels["attached_to"])
|
|
||||||
}
|
|
||||||
if d.Labels["detect_zeroes"] != "on" {
|
|
||||||
t.Errorf("detect_zeroes = %q", d.Labels["detect_zeroes"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseBlockInfo_OldFormatNoMode(t *testing.T) {
|
|
||||||
// Old PVE format with (#blockN) but no read-write/read-only suffix
|
|
||||||
raw := `drive-virtio0 (#block109): /dev/zvol/data/vm-100-disk-0 (raw)
|
|
||||||
Attached to: /machine/peripheral/virtio0/virtio-backend
|
|
||||||
Cache mode: writeback, direct
|
|
||||||
Detect zeroes: on
|
|
||||||
`
|
|
||||||
disks := ParseBlockInfo(raw)
|
|
||||||
if len(disks) != 1 {
|
|
||||||
t.Fatalf("expected 1 disk, got %d", len(disks))
|
|
||||||
}
|
|
||||||
d := disks["virtio0"]
|
|
||||||
if d.BlockID != "109" {
|
|
||||||
t.Errorf("block_id = %q, want 109", d.BlockID)
|
|
||||||
}
|
|
||||||
if d.DiskType != "zvol" {
|
|
||||||
t.Errorf("type = %q, want zvol", d.DiskType)
|
|
||||||
}
|
|
||||||
if d.Labels["cache_mode"] != "writeback, direct" {
|
|
||||||
t.Errorf("cache_mode = %q", d.Labels["cache_mode"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
package qmmonitor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NICInfo holds parsed network interface info from "info network".
|
|
||||||
type NICInfo struct {
|
|
||||||
Netdev string
|
|
||||||
Queues int
|
|
||||||
Type string
|
|
||||||
Model string
|
|
||||||
Macaddr string
|
|
||||||
Ifname string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseNetworkInfo parses the output of "info network" from qm monitor.
|
|
||||||
// Format: "net0: index=0,type=tap,ifname=tap100i0,model=virtio-net-pci,macaddr=AA:BB:CC:DD:EE:FF"
|
|
||||||
// Multiqueue lines: " \ net0: index=1,type=tap,ifname=tap100i0"
|
|
||||||
// For multiqueue, same netdev appears multiple times with increasing index; queues = max(index)+1.
|
|
||||||
func ParseNetworkInfo(raw string) []NICInfo {
|
|
||||||
nicsMap := make(map[string]map[string]string)
|
|
||||||
|
|
||||||
lines := strings.Split(raw, "\n")
|
|
||||||
for _, line := range lines {
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
// Strip leading "\ " from continuation lines
|
|
||||||
line = strings.TrimPrefix(line, "\\ ")
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
if line == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
colonIdx := strings.Index(line, ": ")
|
|
||||||
if colonIdx < 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
netdev := line[:colonIdx]
|
|
||||||
if !strings.HasPrefix(netdev, "net") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg := line[colonIdx+2:]
|
|
||||||
if _, ok := nicsMap[netdev]; !ok {
|
|
||||||
nicsMap[netdev] = make(map[string]string)
|
|
||||||
}
|
|
||||||
for _, pair := range strings.Split(cfg, ",") {
|
|
||||||
pair = strings.TrimSpace(pair)
|
|
||||||
if pair == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
eqIdx := strings.Index(pair, "=")
|
|
||||||
if eqIdx < 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
key := pair[:eqIdx]
|
|
||||||
value := pair[eqIdx+1:]
|
|
||||||
// Overwrite so last index wins (for multiqueue)
|
|
||||||
nicsMap[netdev][key] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var result []NICInfo
|
|
||||||
for netdev, cfg := range nicsMap {
|
|
||||||
idx, _ := strconv.Atoi(cfg["index"])
|
|
||||||
nic := NICInfo{
|
|
||||||
Netdev: netdev,
|
|
||||||
Queues: idx + 1,
|
|
||||||
Type: cfg["type"],
|
|
||||||
Model: cfg["model"],
|
|
||||||
Macaddr: cfg["macaddr"],
|
|
||||||
Ifname: cfg["ifname"],
|
|
||||||
}
|
|
||||||
logging.Trace("parsed NIC", "netdev", netdev, "ifname", nic.Ifname, "queues", nic.Queues, "model", nic.Model)
|
|
||||||
result = append(result, nic)
|
|
||||||
}
|
|
||||||
logging.Trace("ParseNetworkInfo complete", "nic_count", len(result))
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
package qmmonitor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestParseNetworkInfo_Single(t *testing.T) {
|
|
||||||
raw := `net0: index=0,type=tap,ifname=tap100i0,script=/var/lib/qemu-server/pve-bridge,downscript=/var/lib/qemu-server/pve-bridgedown,model=virtio-net-pci,macaddr=AA:BB:CC:DD:EE:FF`
|
|
||||||
|
|
||||||
nics := ParseNetworkInfo(raw)
|
|
||||||
if len(nics) != 1 {
|
|
||||||
t.Fatalf("expected 1 NIC, got %d", len(nics))
|
|
||||||
}
|
|
||||||
nic := nics[0]
|
|
||||||
if nic.Netdev != "net0" {
|
|
||||||
t.Errorf("netdev = %q", nic.Netdev)
|
|
||||||
}
|
|
||||||
if nic.Queues != 1 {
|
|
||||||
t.Errorf("queues = %d", nic.Queues)
|
|
||||||
}
|
|
||||||
if nic.Type != "tap" {
|
|
||||||
t.Errorf("type = %q", nic.Type)
|
|
||||||
}
|
|
||||||
if nic.Model != "virtio-net-pci" {
|
|
||||||
t.Errorf("model = %q", nic.Model)
|
|
||||||
}
|
|
||||||
if nic.Macaddr != "AA:BB:CC:DD:EE:FF" {
|
|
||||||
t.Errorf("macaddr = %q", nic.Macaddr)
|
|
||||||
}
|
|
||||||
if nic.Ifname != "tap100i0" {
|
|
||||||
t.Errorf("ifname = %q", nic.Ifname)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseNetworkInfo_Multiqueue(t *testing.T) {
|
|
||||||
raw := `net0: index=0,type=tap,ifname=tap100i0,model=virtio-net-pci,macaddr=AA:BB:CC:DD:EE:FF
|
|
||||||
\ net0: index=1,type=tap,ifname=tap100i0
|
|
||||||
\ net0: index=2,type=tap,ifname=tap100i0
|
|
||||||
\ net0: index=3,type=tap,ifname=tap100i0
|
|
||||||
net1: index=0,type=tap,ifname=tap100i1,model=virtio-net-pci,macaddr=11:22:33:44:55:66`
|
|
||||||
|
|
||||||
nics := ParseNetworkInfo(raw)
|
|
||||||
if len(nics) != 2 {
|
|
||||||
t.Fatalf("expected 2 NICs, got %d", len(nics))
|
|
||||||
}
|
|
||||||
|
|
||||||
byName := map[string]NICInfo{}
|
|
||||||
for _, n := range nics {
|
|
||||||
byName[n.Netdev] = n
|
|
||||||
}
|
|
||||||
|
|
||||||
if byName["net0"].Queues != 4 {
|
|
||||||
t.Errorf("net0 queues = %d, want 4", byName["net0"].Queues)
|
|
||||||
}
|
|
||||||
if byName["net1"].Queues != 1 {
|
|
||||||
t.Errorf("net1 queues = %d, want 1", byName["net1"].Queues)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseNetworkInfo_Empty(t *testing.T) {
|
|
||||||
nics := ParseNetworkInfo("")
|
|
||||||
if len(nics) != 0 {
|
|
||||||
t.Fatalf("expected 0 NICs, got %d", len(nics))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseNetworkInfo_MalformedLine(t *testing.T) {
|
|
||||||
// Lines without colon-space or without "net" prefix should be skipped
|
|
||||||
raw := "this is garbage\nnotnet0: index=0,type=tap\nno-colon-here\n"
|
|
||||||
nics := ParseNetworkInfo(raw)
|
|
||||||
if len(nics) != 0 {
|
|
||||||
t.Fatalf("expected 0 NICs for malformed input, got %d", len(nics))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseNetworkInfo_MissingFields(t *testing.T) {
|
|
||||||
// NIC with minimal fields
|
|
||||||
raw := "net0: index=0"
|
|
||||||
nics := ParseNetworkInfo(raw)
|
|
||||||
if len(nics) != 1 {
|
|
||||||
t.Fatalf("expected 1 NIC, got %d", len(nics))
|
|
||||||
}
|
|
||||||
if nics[0].Queues != 1 {
|
|
||||||
t.Errorf("queues = %d, want 1", nics[0].Queues)
|
|
||||||
}
|
|
||||||
if nics[0].Model != "" {
|
|
||||||
t.Errorf("model = %q, want empty", nics[0].Model)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,220 +0,0 @@
|
|||||||
package qmmonitor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/creack/pty"
|
|
||||||
"pve_local_exporter/internal/cache"
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
var errTimeout = errors.New("timeout waiting for qm monitor")
|
|
||||||
|
|
||||||
// QMMonitor runs commands against qm monitor and caches results.
|
|
||||||
type QMMonitor interface {
|
|
||||||
RunCommand(vmid, cmd string) (string, error)
|
|
||||||
InvalidateCache(vmid, cmd string)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RealQMMonitor spawns `qm monitor` on a PTY via creack/pty.
|
|
||||||
type RealQMMonitor struct {
|
|
||||||
timeout time.Duration
|
|
||||||
deferClose bool
|
|
||||||
cache *cache.TTLCache[string, string]
|
|
||||||
|
|
||||||
mu sync.Mutex
|
|
||||||
deferredProcs []deferredProc
|
|
||||||
}
|
|
||||||
|
|
||||||
type deferredProc struct {
|
|
||||||
cmd *exec.Cmd
|
|
||||||
timestamp time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRealQMMonitor(timeout, maxTTL, randRange time.Duration, deferClose bool) *RealQMMonitor {
|
|
||||||
return &RealQMMonitor{
|
|
||||||
timeout: timeout,
|
|
||||||
deferClose: deferClose,
|
|
||||||
cache: cache.NewTTLCache[string, string](maxTTL, randRange),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cacheKey(vmid, cmd string) string {
|
|
||||||
return vmid + "\x00" + cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *RealQMMonitor) InvalidateCache(vmid, cmd string) {
|
|
||||||
m.cache.Invalidate(cacheKey(vmid, cmd))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *RealQMMonitor) RunCommand(vmid, cmd string) (string, error) {
|
|
||||||
key := cacheKey(vmid, cmd)
|
|
||||||
if v, ok := m.cache.Get(key); ok {
|
|
||||||
slog.Debug("qm cache hit", "vmid", vmid, "cmd", cmd)
|
|
||||||
return v, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := m.execQMMonitor(vmid, cmd)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
m.cache.Set(key, result)
|
|
||||||
m.cleanupDeferred()
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *RealQMMonitor) execQMMonitor(vmid, cmd string) (string, error) {
|
|
||||||
slog.Debug("qm monitor exec", "vmid", vmid, "cmd", cmd)
|
|
||||||
start := time.Now()
|
|
||||||
|
|
||||||
logging.Trace("qm pty spawn start", "vmid", vmid)
|
|
||||||
qmCmd := exec.Command("qm", "monitor", vmid)
|
|
||||||
qmCmd.Env = append(os.Environ(), "TERM=dumb")
|
|
||||||
|
|
||||||
ptmx, err := pty.Start(qmCmd)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("start qm monitor: %w", err)
|
|
||||||
}
|
|
||||||
logging.Trace("qm pty spawn success", "vmid", vmid, "pid", qmCmd.Process.Pid)
|
|
||||||
|
|
||||||
reader := bufio.NewReader(ptmx)
|
|
||||||
|
|
||||||
// Wait for initial "qm>" prompt
|
|
||||||
deadline := time.Now().Add(m.timeout)
|
|
||||||
_, err = readUntilMarker(reader, "qm>", deadline)
|
|
||||||
if err != nil {
|
|
||||||
slog.Debug("qm monitor initial prompt failed", "vmid", vmid, "err", err)
|
|
||||||
m.killOrDefer(qmCmd, ptmx)
|
|
||||||
return "", fmt.Errorf("initial prompt: %w", err)
|
|
||||||
}
|
|
||||||
logging.Trace("qm initial prompt received", "vmid", vmid)
|
|
||||||
|
|
||||||
// Send command
|
|
||||||
logging.Trace("qm send command", "vmid", vmid, "cmd", cmd)
|
|
||||||
fmt.Fprintf(ptmx, "%s\n", cmd)
|
|
||||||
|
|
||||||
// Read response until next "qm>" prompt
|
|
||||||
deadline = time.Now().Add(m.timeout)
|
|
||||||
raw, err := readUntilMarker(reader, "qm>", deadline)
|
|
||||||
if err != nil {
|
|
||||||
slog.Debug("qm monitor response failed", "vmid", vmid, "cmd", cmd, "err", err)
|
|
||||||
m.killOrDefer(qmCmd, ptmx)
|
|
||||||
return "", fmt.Errorf("read response: %w", err)
|
|
||||||
}
|
|
||||||
logging.Trace("qm raw response", "vmid", vmid, "raw_len", len(raw))
|
|
||||||
|
|
||||||
response := parseQMResponse(raw)
|
|
||||||
|
|
||||||
// Close cleanly: closing ptmx sends SIGHUP to child
|
|
||||||
ptmx.Close()
|
|
||||||
if err := qmCmd.Wait(); err != nil {
|
|
||||||
slog.Debug("qm monitor wait error", "vmid", vmid, "err", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Debug("qm monitor done", "vmid", vmid, "cmd", cmd,
|
|
||||||
"duration", time.Since(start), "responseLen", len(response))
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *RealQMMonitor) killOrDefer(cmd *exec.Cmd, closer io.Closer) {
|
|
||||||
closer.Close()
|
|
||||||
if m.deferClose {
|
|
||||||
m.mu.Lock()
|
|
||||||
m.deferredProcs = append(m.deferredProcs, deferredProc{cmd: cmd, timestamp: time.Now()})
|
|
||||||
m.mu.Unlock()
|
|
||||||
slog.Warn("deferred closing qm monitor process", "pid", cmd.Process.Pid)
|
|
||||||
} else {
|
|
||||||
cmd.Process.Kill()
|
|
||||||
cmd.Wait()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *RealQMMonitor) cleanupDeferred() {
|
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
|
|
||||||
var still []deferredProc
|
|
||||||
for _, dp := range m.deferredProcs {
|
|
||||||
if time.Since(dp.timestamp) > 10*time.Second {
|
|
||||||
if err := dp.cmd.Process.Kill(); err != nil {
|
|
||||||
still = append(still, dp)
|
|
||||||
} else {
|
|
||||||
dp.cmd.Wait()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
still = append(still, dp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.deferredProcs = still
|
|
||||||
}
|
|
||||||
|
|
||||||
// readUntilMarker reads from r byte-by-byte until the buffer ends with marker
|
|
||||||
// or the deadline expires. Returns everything read before the marker.
|
|
||||||
// Uses a goroutine for reads so the deadline is enforced even when ReadByte blocks.
|
|
||||||
func readUntilMarker(r *bufio.Reader, marker string, deadline time.Time) (string, error) {
|
|
||||||
type result struct {
|
|
||||||
data string
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
ch := make(chan result, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
var buf []byte
|
|
||||||
markerBytes := []byte(marker)
|
|
||||||
for {
|
|
||||||
b, err := r.ReadByte()
|
|
||||||
if err != nil {
|
|
||||||
ch <- result{"", err}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
buf = append(buf, b)
|
|
||||||
if len(buf) >= len(markerBytes) &&
|
|
||||||
string(buf[len(buf)-len(markerBytes):]) == marker {
|
|
||||||
// Return everything before the marker
|
|
||||||
ch <- result{string(buf[:len(buf)-len(markerBytes)]), nil}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
remaining := time.Until(deadline)
|
|
||||||
if remaining <= 0 {
|
|
||||||
remaining = time.Millisecond
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case res := <-ch:
|
|
||||||
return res.data, res.err
|
|
||||||
case <-time.After(remaining):
|
|
||||||
return "", errTimeout
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseQMResponse takes the raw output before the "qm>" marker from a command
|
|
||||||
// response, skips the command echo (first line), and trims \r characters.
|
|
||||||
func parseQMResponse(raw string) string {
|
|
||||||
lines := strings.Split(raw, "\n")
|
|
||||||
// Skip the command echo (first line)
|
|
||||||
if len(lines) > 0 {
|
|
||||||
lines = lines[1:]
|
|
||||||
}
|
|
||||||
var out []string
|
|
||||||
for _, line := range lines {
|
|
||||||
cleaned := strings.TrimRight(line, "\r")
|
|
||||||
out = append(out, cleaned)
|
|
||||||
}
|
|
||||||
// Trim trailing empty lines
|
|
||||||
for len(out) > 0 && out[len(out)-1] == "" {
|
|
||||||
out = out[:len(out)-1]
|
|
||||||
}
|
|
||||||
return strings.Join(out, "\n")
|
|
||||||
}
|
|
||||||
@@ -1,143 +0,0 @@
|
|||||||
package qmmonitor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"io"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/cache"
|
|
||||||
)
|
|
||||||
|
|
||||||
// mockQMMonitor is a test double for QMMonitor.
|
|
||||||
type mockQMMonitor struct {
|
|
||||||
responses map[string]string
|
|
||||||
cache *cache.TTLCache[string, string]
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMockQMMonitor(responses map[string]string) *mockQMMonitor {
|
|
||||||
return &mockQMMonitor{
|
|
||||||
responses: responses,
|
|
||||||
cache: cache.NewTTLCache[string, string](time.Hour, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockQMMonitor) RunCommand(vmid, cmd string) (string, error) {
|
|
||||||
key := cacheKey(vmid, cmd)
|
|
||||||
if v, ok := m.cache.Get(key); ok {
|
|
||||||
return v, nil
|
|
||||||
}
|
|
||||||
resp := m.responses[vmid+":"+cmd]
|
|
||||||
m.cache.Set(key, resp)
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockQMMonitor) InvalidateCache(vmid, cmd string) {
|
|
||||||
m.cache.Invalidate(cacheKey(vmid, cmd))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMockQMMonitor_CacheHit(t *testing.T) {
|
|
||||||
mon := newMockQMMonitor(map[string]string{
|
|
||||||
"100:info network": "some output",
|
|
||||||
})
|
|
||||||
r1, _ := mon.RunCommand("100", "info network")
|
|
||||||
r2, _ := mon.RunCommand("100", "info network")
|
|
||||||
if r1 != r2 {
|
|
||||||
t.Errorf("cache miss: %q != %q", r1, r2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMockQMMonitor_Invalidate(t *testing.T) {
|
|
||||||
mon := newMockQMMonitor(map[string]string{
|
|
||||||
"100:info network": "some output",
|
|
||||||
})
|
|
||||||
mon.RunCommand("100", "info network")
|
|
||||||
mon.InvalidateCache("100", "info network")
|
|
||||||
// After invalidation, it fetches again (same mock response)
|
|
||||||
r, _ := mon.RunCommand("100", "info network")
|
|
||||||
if r != "some output" {
|
|
||||||
t.Errorf("unexpected: %q", r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadUntilMarker_Success(t *testing.T) {
|
|
||||||
pr, pw := io.Pipe()
|
|
||||||
defer pr.Close()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
pw.Write([]byte("banner\nqm> "))
|
|
||||||
pw.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
reader := bufio.NewReader(pr)
|
|
||||||
deadline := time.Now().Add(5 * time.Second)
|
|
||||||
got, err := readUntilMarker(reader, "qm>", deadline)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
if got != "banner\n" {
|
|
||||||
t.Errorf("got %q, want %q", got, "banner\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadUntilMarker_Timeout(t *testing.T) {
|
|
||||||
pr, pw := io.Pipe()
|
|
||||||
defer pr.Close()
|
|
||||||
defer pw.Close()
|
|
||||||
|
|
||||||
// Write nothing -- should timeout
|
|
||||||
reader := bufio.NewReader(pr)
|
|
||||||
deadline := time.Now().Add(50 * time.Millisecond)
|
|
||||||
start := time.Now()
|
|
||||||
_, err := readUntilMarker(reader, "qm>", deadline)
|
|
||||||
elapsed := time.Since(start)
|
|
||||||
|
|
||||||
if err != errTimeout {
|
|
||||||
t.Fatalf("expected errTimeout, got: %v", err)
|
|
||||||
}
|
|
||||||
if elapsed > 500*time.Millisecond {
|
|
||||||
t.Errorf("timeout took too long: %v", elapsed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadUntilMarker_EOF(t *testing.T) {
|
|
||||||
pr, pw := io.Pipe()
|
|
||||||
defer pr.Close()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
pw.Write([]byte("partial data"))
|
|
||||||
pw.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
reader := bufio.NewReader(pr)
|
|
||||||
deadline := time.Now().Add(5 * time.Second)
|
|
||||||
_, err := readUntilMarker(reader, "qm>", deadline)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error on EOF before marker")
|
|
||||||
}
|
|
||||||
if err == errTimeout {
|
|
||||||
t.Fatal("expected EOF error, not timeout")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseQMResponse(t *testing.T) {
|
|
||||||
// Simulate: command echo + response lines with \r + trailing newline
|
|
||||||
raw := "info network\r\n" +
|
|
||||||
"net0: index=0,type=tap,ifname=tap100i0\r\n" +
|
|
||||||
"net1: index=1,type=tap,ifname=tap100i1\r\n"
|
|
||||||
|
|
||||||
got := parseQMResponse(raw)
|
|
||||||
want := "net0: index=0,type=tap,ifname=tap100i0\n" +
|
|
||||||
"net1: index=1,type=tap,ifname=tap100i1"
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("parseQMResponse:\ngot: %q\nwant: %q", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseQMResponse_Empty(t *testing.T) {
|
|
||||||
// Command echo only, no response data
|
|
||||||
got := parseQMResponse("info version\r\n")
|
|
||||||
if got != "" {
|
|
||||||
t.Errorf("expected empty, got %q", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
// StorageSize holds the total and free bytes of a storage pool.
|
|
||||||
type StorageSize struct {
|
|
||||||
Total int64
|
|
||||||
Free int64
|
|
||||||
}
|
|
||||||
|
|
||||||
// StatFS abstracts the statfs syscall for testability.
|
|
||||||
type StatFS interface {
|
|
||||||
Statfs(path string) (StorageSize, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RealStatFS uses the real syscall.
|
|
||||||
type RealStatFS struct{}
|
|
||||||
|
|
||||||
func (RealStatFS) Statfs(path string) (StorageSize, error) {
|
|
||||||
var stat syscall.Statfs_t
|
|
||||||
if err := syscall.Statfs(path, &stat); err != nil {
|
|
||||||
return StorageSize{}, fmt.Errorf("statfs %s: %w", path, err)
|
|
||||||
}
|
|
||||||
return StorageSize{
|
|
||||||
Total: int64(stat.Frsize) * int64(stat.Blocks),
|
|
||||||
Free: int64(stat.Frsize) * int64(stat.Bavail),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDirStorageSize returns size info for dir/nfs/cephfs storage using statfs.
|
|
||||||
func GetDirStorageSize(fs StatFS, path string) (StorageSize, error) {
|
|
||||||
return fs.Statfs(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetZPoolSize parses `zpool list -p {poolName}` output for size and free.
|
|
||||||
func GetZPoolSize(output string) (StorageSize, error) {
|
|
||||||
lines := strings.Split(strings.TrimSpace(output), "\n")
|
|
||||||
if len(lines) < 2 {
|
|
||||||
return StorageSize{}, fmt.Errorf("unexpected zpool output: %q", output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Header: NAME SIZE ALLOC FREE ...
|
|
||||||
// Data: pool 1234 567 890 ...
|
|
||||||
fields := strings.Fields(lines[1])
|
|
||||||
if len(fields) < 4 {
|
|
||||||
return StorageSize{}, fmt.Errorf("not enough fields in zpool output: %q", lines[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Trace("zpool fields", "name", fields[0], "size", fields[1], "alloc", fields[2], "free", fields[3])
|
|
||||||
|
|
||||||
total, err := strconv.ParseInt(fields[1], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return StorageSize{}, fmt.Errorf("parse total: %w", err)
|
|
||||||
}
|
|
||||||
free, err := strconv.ParseInt(fields[3], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return StorageSize{}, fmt.Errorf("parse free: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Trace("zpool parsed", "total", total, "free", free)
|
|
||||||
return StorageSize{Total: total, Free: free}, nil
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type mockStatFS struct {
|
|
||||||
sizes map[string]StorageSize
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m mockStatFS) Statfs(path string) (StorageSize, error) {
|
|
||||||
if s, ok := m.sizes[path]; ok {
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
return StorageSize{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetDirStorageSize(t *testing.T) {
|
|
||||||
fs := mockStatFS{sizes: map[string]StorageSize{
|
|
||||||
"/var/lib/vz": {Total: 1000000, Free: 500000},
|
|
||||||
}}
|
|
||||||
s, err := GetDirStorageSize(fs, "/var/lib/vz")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if s.Total != 1000000 {
|
|
||||||
t.Errorf("total = %d", s.Total)
|
|
||||||
}
|
|
||||||
if s.Free != 500000 {
|
|
||||||
t.Errorf("free = %d", s.Free)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetZPoolSize(t *testing.T) {
|
|
||||||
output := `NAME SIZE ALLOC FREE CKPOINT EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT
|
|
||||||
rpool 1073741824 536870912 536870912 - - 10% 50% 1.00x ONLINE -
|
|
||||||
`
|
|
||||||
s, err := GetZPoolSize(output)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if s.Total != 1073741824 {
|
|
||||||
t.Errorf("total = %d", s.Total)
|
|
||||||
}
|
|
||||||
if s.Free != 536870912 {
|
|
||||||
t.Errorf("free = %d", s.Free)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetZPoolSize_BadOutput(t *testing.T) {
|
|
||||||
_, err := GetZPoolSize("bad")
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
package sysfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SysReader abstracts /sys access for testability.
|
|
||||||
type SysReader interface {
|
|
||||||
ReadInterfaceStats(ifname string) (map[string]int64, error)
|
|
||||||
GetBlockDeviceSize(devPath string) (int64, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RealSysReader reads from the actual /sys filesystem.
|
|
||||||
type RealSysReader struct {
|
|
||||||
SysPath string // default "/sys"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRealSysReader() *RealSysReader {
|
|
||||||
return &RealSysReader{SysPath: "/sys"}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadInterfaceStats reads all statistics files from /sys/class/net/{ifname}/statistics/.
|
|
||||||
func (r *RealSysReader) ReadInterfaceStats(ifname string) (map[string]int64, error) {
|
|
||||||
dir := filepath.Join(r.SysPath, "class", "net", ifname, "statistics")
|
|
||||||
entries, err := os.ReadDir(dir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
stats := make(map[string]int64)
|
|
||||||
for _, e := range entries {
|
|
||||||
if e.IsDir() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
data, err := os.ReadFile(filepath.Join(dir, e.Name()))
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
stats[e.Name()] = val
|
|
||||||
}
|
|
||||||
logging.Trace("interface stats", "ifname", ifname, "stat_count", len(stats))
|
|
||||||
return stats, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBlockDeviceSize returns the size in bytes of a block device.
|
|
||||||
// For symlinks (e.g., /dev/zvol/...), resolves to the real device first.
|
|
||||||
// Reads size from /sys/block/{dev}/size (in 512-byte sectors).
|
|
||||||
func (r *RealSysReader) GetBlockDeviceSize(devPath string) (int64, error) {
|
|
||||||
// Resolve symlinks
|
|
||||||
resolved, err := filepath.EvalSymlinks(devPath)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("resolve symlink %s: %w", devPath, err)
|
|
||||||
}
|
|
||||||
logging.Trace("block device resolved", "path", devPath, "resolved", resolved)
|
|
||||||
|
|
||||||
// Extract device name from /dev/XXX
|
|
||||||
devName := filepath.Base(resolved)
|
|
||||||
|
|
||||||
// Try /sys/block/{devName}/size
|
|
||||||
sizeFile := filepath.Join(r.SysPath, "block", devName, "size")
|
|
||||||
data, err := os.ReadFile(sizeFile)
|
|
||||||
if err != nil {
|
|
||||||
// For partition devices like dm-0, try without partition suffix
|
|
||||||
return 0, fmt.Errorf("read size %s: %w", sizeFile, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
sectors, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("parse size: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return sectors * 512, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDeviceSymlinkTarget resolves a device symlink and returns the target path.
|
|
||||||
func GetDeviceSymlinkTarget(devPath string) (string, error) {
|
|
||||||
resolved, err := filepath.EvalSymlinks(devPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
logging.Trace("device symlink resolved", "path", devPath, "target", resolved)
|
|
||||||
return resolved, nil
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
package sysfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestReadInterfaceStats(t *testing.T) {
|
|
||||||
// Create temp sysfs-like structure
|
|
||||||
tmpDir := t.TempDir()
|
|
||||||
statsDir := filepath.Join(tmpDir, "class", "net", "tap100i0", "statistics")
|
|
||||||
os.MkdirAll(statsDir, 0755)
|
|
||||||
|
|
||||||
os.WriteFile(filepath.Join(statsDir, "rx_bytes"), []byte("123456\n"), 0644)
|
|
||||||
os.WriteFile(filepath.Join(statsDir, "tx_bytes"), []byte("789012\n"), 0644)
|
|
||||||
os.WriteFile(filepath.Join(statsDir, "rx_packets"), []byte("100\n"), 0644)
|
|
||||||
|
|
||||||
reader := &RealSysReader{SysPath: tmpDir}
|
|
||||||
stats, err := reader.ReadInterfaceStats("tap100i0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if stats["rx_bytes"] != 123456 {
|
|
||||||
t.Errorf("rx_bytes = %d", stats["rx_bytes"])
|
|
||||||
}
|
|
||||||
if stats["tx_bytes"] != 789012 {
|
|
||||||
t.Errorf("tx_bytes = %d", stats["tx_bytes"])
|
|
||||||
}
|
|
||||||
if stats["rx_packets"] != 100 {
|
|
||||||
t.Errorf("rx_packets = %d", stats["rx_packets"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadInterfaceStats_NotFound(t *testing.T) {
|
|
||||||
reader := &RealSysReader{SysPath: t.TempDir()}
|
|
||||||
_, err := reader.ReadInterfaceStats("nonexistent")
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error for nonexistent interface")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetBlockDeviceSize(t *testing.T) {
|
|
||||||
tmpDir := t.TempDir()
|
|
||||||
|
|
||||||
// Create /sys/block/dm-0/size
|
|
||||||
blockDir := filepath.Join(tmpDir, "block", "dm-0")
|
|
||||||
os.MkdirAll(blockDir, 0755)
|
|
||||||
// 1GB = 2097152 sectors of 512 bytes
|
|
||||||
os.WriteFile(filepath.Join(blockDir, "size"), []byte("2097152\n"), 0644)
|
|
||||||
|
|
||||||
// Create a "device" symlink that points to dm-0
|
|
||||||
devDir := filepath.Join(tmpDir, "dev")
|
|
||||||
os.MkdirAll(devDir, 0755)
|
|
||||||
os.Symlink(filepath.Join(devDir, "dm-0"), filepath.Join(devDir, "mydev"))
|
|
||||||
// Create the actual "device" file so symlink resolves
|
|
||||||
os.WriteFile(filepath.Join(devDir, "dm-0"), []byte{}, 0644)
|
|
||||||
|
|
||||||
reader := &RealSysReader{SysPath: tmpDir}
|
|
||||||
size, err := reader.GetBlockDeviceSize(filepath.Join(devDir, "dm-0"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
expected := int64(2097152 * 512)
|
|
||||||
if size != expected {
|
|
||||||
t.Errorf("size = %d, want %d", size, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-81
@@ -1,81 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
|
|
||||||
"pve_local_exporter/internal/collector"
|
|
||||||
"pve_local_exporter/internal/config"
|
|
||||||
"pve_local_exporter/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
var version string
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
cfg := config.Parse()
|
|
||||||
cfg.Version = version
|
|
||||||
|
|
||||||
if cfg.ShowVersion {
|
|
||||||
fmt.Println(version)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
level := slog.LevelInfo
|
|
||||||
switch strings.ToUpper(cfg.LogLevel) {
|
|
||||||
case "TRACE":
|
|
||||||
level = logging.LevelTrace
|
|
||||||
case "DEBUG":
|
|
||||||
level = slog.LevelDebug
|
|
||||||
case "WARNING", "WARN":
|
|
||||||
level = slog.LevelWarn
|
|
||||||
case "ERROR", "CRITICAL":
|
|
||||||
level = slog.LevelError
|
|
||||||
}
|
|
||||||
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
|
|
||||||
Level: level,
|
|
||||||
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
|
|
||||||
if a.Key == slog.LevelKey {
|
|
||||||
lvl := a.Value.Any().(slog.Level)
|
|
||||||
if lvl == logging.LevelTrace {
|
|
||||||
a.Value = slog.StringValue("TRACE")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return a
|
|
||||||
},
|
|
||||||
})))
|
|
||||||
|
|
||||||
reg := prometheus.NewRegistry()
|
|
||||||
c := collector.New(cfg)
|
|
||||||
reg.MustRegister(c)
|
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
|
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Write([]byte(`<html><body><a href="/metrics">Metrics</a></body></html>`))
|
|
||||||
})
|
|
||||||
|
|
||||||
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
|
|
||||||
server := &http.Server{Addr: addr, Handler: mux}
|
|
||||||
|
|
||||||
sigCh := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
go func() {
|
|
||||||
<-sigCh
|
|
||||||
slog.Info("shutting down")
|
|
||||||
server.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
slog.Info("listening", "addr", addr)
|
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user