diff --git a/README.md b/README.md index cbdfedc..d73f179 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,13 @@ Configuration now lives in the YAML settings file. By default the service reads vctp -settings /path/to/vctp.yml ``` +If you just want to run a single inventory snapshot across all configured vCenters and +exit (no scheduler/server), use: + +```shell +vctp -settings /path/to/vctp.yml -run-inventory-once +``` + ### Database Configuration By default the app uses SQLite and creates/opens `db.sqlite3`. You can opt into PostgreSQL by updating the settings file: diff --git a/internal/tasks/cronstatus.go b/internal/tasks/cronstatus.go index 098b9f2..8220c59 100644 --- a/internal/tasks/cronstatus.go +++ b/internal/tasks/cronstatus.go @@ -9,12 +9,6 @@ import ( "github.com/jmoiron/sqlx" ) -// CronTracker manages re-entry protection and status recording for cron jobs. -type CronTracker struct { - db db.Database - bindType int -} - func NewCronTracker(database db.Database) *CronTracker { return &CronTracker{ db: database, diff --git a/internal/tasks/dailyAggregate.go b/internal/tasks/dailyAggregate.go index caa6298..e718114 100644 --- a/internal/tasks/dailyAggregate.go +++ b/internal/tasks/dailyAggregate.go @@ -307,40 +307,6 @@ func (c *CronTask) aggregateDailySummaryGo(ctx context.Context, dayStart, dayEnd return nil } -type dailyAggKey struct { - Vcenter string - VmId string - VmUuid string - Name string -} - -type dailyAggVal struct { - key dailyAggKey - resourcePool string - datacenter string - cluster string - folder string - isTemplate string - poweredOn string - srmPlaceholder string - creation int64 - firstSeen int64 - lastSeen int64 - lastDisk float64 - lastVcpu int64 - lastRam int64 - sumVcpu int64 - sumRam int64 - sumDisk float64 - samples int64 - tinHits int64 - bronzeHits int64 - silverHits int64 - goldHits int64 - seen map[int64]struct{} - deletion int64 -} - func (c *CronTask) scanHourlyTablesParallel(ctx context.Context, snapshots []report.SnapshotRecord) (map[dailyAggKey]*dailyAggVal, error) { agg := make(map[dailyAggKey]*dailyAggVal, 1024) mu := sync.Mutex{} diff --git a/internal/tasks/inventorySnapshots.go b/internal/tasks/inventorySnapshots.go index b84d66b..fccf7f7 100644 --- a/internal/tasks/inventorySnapshots.go +++ b/internal/tasks/inventorySnapshots.go @@ -23,31 +23,6 @@ import ( "github.com/vmware/govmomi/vim25/types" ) -type InventorySnapshotRow struct { - InventoryId sql.NullInt64 - Name string - Vcenter string - VmId sql.NullString - EventKey sql.NullString - CloudId sql.NullString - CreationTime sql.NullInt64 - DeletionTime sql.NullInt64 - ResourcePool sql.NullString - Datacenter sql.NullString - Cluster sql.NullString - Folder sql.NullString - ProvisionedDisk sql.NullFloat64 - VcpuCount sql.NullInt64 - RamGB sql.NullInt64 - IsTemplate string - PoweredOn string - SrmPlaceholder string - VmUuid sql.NullString - SnapshotTime int64 -} - -type snapshotTotals = db.SnapshotTotals - // RunVcenterSnapshotHourly records hourly inventory snapshots into a daily table. func (c *CronTask) RunVcenterSnapshotHourly(ctx context.Context, logger *slog.Logger) (err error) { jobCtx := ctx @@ -442,11 +417,6 @@ func filterRecordsInRange(records []report.SnapshotRecord, start, end time.Time) return filtered } -type columnDef struct { - Name string - Type string -} - var summaryUnionColumns = []string{ `"InventoryId"`, `"Name"`, `"Vcenter"`, `"VmId"`, `"EventKey"`, `"CloudId"`, `"CreationTime"`, `"DeletionTime"`, `"ResourcePool"`, `"Datacenter"`, `"Cluster"`, `"Folder"`, diff --git a/internal/tasks/monthlyAggregate.go b/internal/tasks/monthlyAggregate.go index db87e42..001f0c7 100644 --- a/internal/tasks/monthlyAggregate.go +++ b/internal/tasks/monthlyAggregate.go @@ -218,43 +218,6 @@ func (c *CronTask) aggregateMonthlySummaryGo(ctx context.Context, monthStart, mo return nil } -type monthlyAggKey struct { - Vcenter string - VmId string - VmUuid string - Name string -} - -type monthlyAggVal struct { - key monthlyAggKey - inventoryId int64 - eventKey string - cloudId string - resourcePool string - datacenter string - cluster string - folder string - isTemplate string - poweredOn string - srmPlaceholder string - provisioned float64 - vcpuCount int64 - ramGB int64 - creation int64 - deletion int64 - lastSnapshot time.Time - - samplesPresent int64 - totalSamples float64 - sumVcpu float64 - sumRam float64 - sumDisk float64 - tinWeighted float64 - bronzeWeighted float64 - silverWeighted float64 - goldWeighted float64 -} - func (c *CronTask) scanDailyTablesParallel(ctx context.Context, snapshots []report.SnapshotRecord) (map[monthlyAggKey]*monthlyAggVal, error) { agg := make(map[monthlyAggKey]*monthlyAggVal, 1024) mu := sync.Mutex{} diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index 1dd807e..dd4c8ad 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -1,17 +1,3 @@ package tasks -import ( - "log/slog" - "vctp/db" - "vctp/internal/settings" - "vctp/internal/vcenter" -) - -// CronTask stores runtime information to be used by tasks -type CronTask struct { - Logger *slog.Logger - Database db.Database - Settings *settings.Settings - VcCreds *vcenter.VcenterLogin - FirstHourlySnapshotCheck bool -} +// Legacy placeholder: type definitions moved to types.go. diff --git a/internal/tasks/types.go b/internal/tasks/types.go new file mode 100644 index 0000000..32b3a99 --- /dev/null +++ b/internal/tasks/types.go @@ -0,0 +1,123 @@ +package tasks + +import ( + "database/sql" + "log/slog" + "time" + + "vctp/db" + "vctp/internal/settings" + "vctp/internal/vcenter" +) + +// CronTask stores runtime information to be used by tasks. +type CronTask struct { + Logger *slog.Logger + Database db.Database + Settings *settings.Settings + VcCreds *vcenter.VcenterLogin + FirstHourlySnapshotCheck bool +} + +// InventorySnapshotRow represents a single VM snapshot row. +type InventorySnapshotRow struct { + InventoryId sql.NullInt64 + Name string + Vcenter string + VmId sql.NullString + EventKey sql.NullString + CloudId sql.NullString + CreationTime sql.NullInt64 + DeletionTime sql.NullInt64 + ResourcePool sql.NullString + Datacenter sql.NullString + Cluster sql.NullString + Folder sql.NullString + ProvisionedDisk sql.NullFloat64 + VcpuCount sql.NullInt64 + RamGB sql.NullInt64 + IsTemplate string + PoweredOn string + SrmPlaceholder string + VmUuid sql.NullString + SnapshotTime int64 +} + +// snapshotTotals aliases DB snapshot totals for convenience. +type snapshotTotals = db.SnapshotTotals + +type dailyAggKey struct { + Vcenter string + VmId string + VmUuid string + Name string +} + +type dailyAggVal struct { + key dailyAggKey + resourcePool string + datacenter string + cluster string + folder string + isTemplate string + poweredOn string + srmPlaceholder string + creation int64 + firstSeen int64 + lastSeen int64 + lastDisk float64 + lastVcpu int64 + lastRam int64 + sumVcpu int64 + sumRam int64 + sumDisk float64 + samples int64 + tinHits int64 + bronzeHits int64 + silverHits int64 + goldHits int64 + seen map[int64]struct{} + deletion int64 +} + +type monthlyAggKey struct { + Vcenter string + VmId string + VmUuid string + Name string +} + +type monthlyAggVal struct { + key monthlyAggKey + inventoryId int64 + eventKey string + cloudId string + resourcePool string + datacenter string + cluster string + folder string + isTemplate string + poweredOn string + srmPlaceholder string + creation int64 + deletion int64 + lastSnapshot time.Time + provisioned float64 + vcpuCount int64 + ramGB int64 + samplesPresent int64 + totalSamples float64 + sumVcpu float64 + sumRam float64 + sumDisk float64 + tinWeighted float64 + bronzeWeighted float64 + silverWeighted float64 + goldWeighted float64 +} + +// CronTracker manages re-entry protection and status recording for cron jobs. +type CronTracker struct { + db db.Database + bindType int +}