commit
This commit is contained in:
28
.drone.yml
Normal file
28
.drone.yml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: default
|
||||||
|
|
||||||
|
# Docs at https://docs.drone.io/pipeline/exec/overview/
|
||||||
|
# Also see https://github.com/harness/drone-cli/blob/master/.drone.yml
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: build
|
||||||
|
image: golang
|
||||||
|
commands:
|
||||||
|
- sh ./.drone.sh
|
||||||
|
|
||||||
|
- name: dell-deploy
|
||||||
|
# # https://github.com/cschlosser/drone-ftps/blob/master/README.md
|
||||||
|
image: cschlosser/drone-ftps
|
||||||
|
environment:
|
||||||
|
FTP_USERNAME:
|
||||||
|
from_secret: FTP_USERNAME
|
||||||
|
FTP_PASSWORD:
|
||||||
|
from_secret: FTP_PASSWORD
|
||||||
|
PLUGIN_HOSTNAME: ftp.emc.com:21
|
||||||
|
PLUGIN_SECURE: false
|
||||||
|
PLUGIN_VERIFY: false
|
||||||
|
PLUGIN_CHMOD: false
|
||||||
|
#PLUGIN_DEBUG: false
|
||||||
|
PLUGIN_INCLUDE: ^vm-metrics$,^vm-metrics_checksum.txt$
|
||||||
|
PLUGIN_EXCLUDE: ^\.git/$
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
vm-metrics
|
60
log.txt
Normal file
60
log.txt
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
2023/08/30 17:24:19 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:24:19 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:24:19 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:28:39 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:28:39 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:28:39 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:29:38 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:29:38 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:29:38 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:30:22 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:30:22 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:30:22 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:30:50 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:30:50 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:30:50 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:32:26 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:32:26 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:32:26 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:35:17 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:35:17 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:35:17 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:36:08 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:36:08 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:36:08 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:37:12 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:37:12 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:37:12 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 17:37:55 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 17:37:55 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 17:37:55 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:24:24 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:24:24 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:24:24 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:26:23 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:26:23 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:26:23 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:26:42 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:26:42 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:26:42 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:29:38 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:29:38 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:29:38 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:32:26 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:32:26 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:32:26 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:37:35 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:37:35 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:37:35 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:38:37 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:38:37 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:38:37 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:39:04 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:39:04 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:39:04 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:39:32 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:39:32 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:39:32 Connecting to vCenter https://vc.lab.local/sdk
|
||||||
|
2023/08/30 19:39:50 Starting execution. Built on from sha1
|
||||||
|
2023/08/30 19:39:50 Setting timezone to 'Australia/Sydney'
|
||||||
|
2023/08/30 19:39:50 Connecting to vCenter https://vc.lab.local/sdk
|
255
main.go
Normal file
255
main.go
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
_ "time/tzdata"
|
||||||
|
|
||||||
|
"github.com/vmware/govmomi"
|
||||||
|
"github.com/vmware/govmomi/object"
|
||||||
|
"github.com/vmware/govmomi/performance"
|
||||||
|
"github.com/vmware/govmomi/view"
|
||||||
|
"github.com/vmware/govmomi/vim25"
|
||||||
|
"github.com/vmware/govmomi/vim25/mo"
|
||||||
|
"github.com/vmware/govmomi/vim25/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
c *govmomi.Client
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
location *time.Location
|
||||||
|
sha1ver string // sha1 revision used to build the program
|
||||||
|
buildTime string // when the executable was built
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
func findPerfCounter(perfManager *vim25.PerformanceManager, counterName string) (*vim25.PerfCounterInfo, error) {
|
||||||
|
perfCounters, err := perfManager.QueryPerfCounter(context.Background(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, counter := range perfCounters {
|
||||||
|
if counter.NameInfo.GetElement().Key == counterName {
|
||||||
|
return &counter, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("Performance counter '%s' not found", counterName)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
func findVMByName(ctx context.Context, client *vim25.Client, vmName string) ([]mo.VirtualMachine, error) {
|
||||||
|
m := view.NewManager(client)
|
||||||
|
|
||||||
|
vms, err := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer vms.Destroy(ctx)
|
||||||
|
|
||||||
|
var matchingVMs []mo.VirtualMachine
|
||||||
|
|
||||||
|
err = vms.Retrieve(ctx, []string{"VirtualMachine"}, []string{"name"}, &matchingVMs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []mo.VirtualMachine
|
||||||
|
|
||||||
|
for _, vm := range matchingVMs {
|
||||||
|
if vm.Name == vmName {
|
||||||
|
result = append(result, vm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMetricIds(counters []types.PerfCounterInfo) []types.PerfMetricId {
|
||||||
|
var metricIds []types.PerfMetricId
|
||||||
|
for _, counter := range counters {
|
||||||
|
var metricId types.PerfMetricId
|
||||||
|
metricId.CounterId = counter.Key
|
||||||
|
metricIds = append(metricIds, metricId)
|
||||||
|
}
|
||||||
|
return metricIds
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSpecificVMMetrics(ctx context.Context, c *vim25.Client, vmName string) {
|
||||||
|
//perfManager := c.ServiceContent.PerfManager
|
||||||
|
perfManager := performance.NewManager(c)
|
||||||
|
var perfCounterInfos []types.PerfCounterInfo
|
||||||
|
startTime := time.Now().Add(-time.Hour)
|
||||||
|
timeNow := time.Now()
|
||||||
|
|
||||||
|
// Find the performance counters for CPU Entitlement, CPU Consumed, and CPU Demand
|
||||||
|
counterNames := []string{"cpu.entitlement.latest", "cpu.usage.average", "cpu.demand.average"}
|
||||||
|
|
||||||
|
// Retrieve counters name list
|
||||||
|
counters, err := perfManager.CounterInfoByName(ctx)
|
||||||
|
if err != nil {
|
||||||
|
//return err
|
||||||
|
fmt.Printf("Error getting counter info : '%s'\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Found '%d' counters\n", len(counters))
|
||||||
|
|
||||||
|
for _, counter := range counters {
|
||||||
|
for _, counterName := range counterNames {
|
||||||
|
if counter.Name() == counterName {
|
||||||
|
fmt.Printf("Found counter matching name '%s' : '%v'\n", counterName, counter.PerDeviceLevel)
|
||||||
|
perfCounterInfos = append(perfCounterInfos, *counter)
|
||||||
|
} else {
|
||||||
|
//fmt.Printf("Ignoring '%s' : '%s'\n", counter.StatsType, counter.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Ended search with %d counter infos\n", len(perfCounterInfos))
|
||||||
|
|
||||||
|
// Find the VM by its name
|
||||||
|
vmMatches, err := findVMByName(ctx, c, vmName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error finding VM:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, vm := range vmMatches {
|
||||||
|
// Create PerfQuerySpec as per https://github.com/vmware/govmomi/blob/main/performance/example_test.go
|
||||||
|
spec := types.PerfQuerySpec{
|
||||||
|
MaxSample: 1800,
|
||||||
|
MetricId: getMetricIds(perfCounterInfos),
|
||||||
|
IntervalId: 20,
|
||||||
|
StartTime: &startTime, // 1 hour ago
|
||||||
|
EndTime: &timeNow,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query metrics
|
||||||
|
sample, err := perfManager.SampleByName(ctx, spec, counterNames, []types.ManagedObjectReference{vm.Reference()})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error getting SampleByName : '%s'\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := perfManager.ToMetricSeries(ctx, sample)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error getting ToMetricSeries : '%s'\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Retrieved %d metric series\n", len(result))
|
||||||
|
|
||||||
|
// Read result
|
||||||
|
for _, metric := range result {
|
||||||
|
vm := object.NewVirtualMachine(c, metric.Entity)
|
||||||
|
name, err := vm.ObjectName(ctx)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error getting vm name : '%s'\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Vm %s has %d metric values\n", name, len(metric.Value))
|
||||||
|
|
||||||
|
for _, v := range metric.Value {
|
||||||
|
|
||||||
|
counter := counters[v.Name]
|
||||||
|
units := counter.UnitInfo.GetElementDescription().Label
|
||||||
|
|
||||||
|
instance := v.Instance
|
||||||
|
if instance == "" {
|
||||||
|
instance = "-"
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(v.Value) != 0 {
|
||||||
|
fmt.Printf("%s\t%s\t%s\t%s : %v\n", name, instance, v.Name, units, v.ValueCSV())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Create a query specification
|
||||||
|
query := perfManager.QueryPerf(ctx, &vim25.QueryPerf{
|
||||||
|
Entity: []vim25.ManagedObjectReference{vm.Reference()},
|
||||||
|
MaxSample: 1,
|
||||||
|
MetricId: getMetricIds(perfCounterInfos),
|
||||||
|
IntervalId: 20, // 20-second interval
|
||||||
|
StartTime: time.Now().Add(-time.Hour), // 1 hour ago
|
||||||
|
EndTime: time.Now(),
|
||||||
|
})
|
||||||
|
|
||||||
|
// Process the query result
|
||||||
|
for _, perf := range query {
|
||||||
|
fmt.Println("VM:", vmName)
|
||||||
|
for i, value := range perf.Value {
|
||||||
|
counterInfo := perfCounterInfos[i]
|
||||||
|
fmt.Printf("%s: %v\n", counterInfo.NameInfo.GetElement().Key, value.Value[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Command line flags
|
||||||
|
vURL := flag.String("url", "", "The URL of a vCenter server, eg https://server.domain.example/sdk")
|
||||||
|
vUser := flag.String("user", "", "The username to use when connecting to vCenter")
|
||||||
|
vPass := flag.String("password", "", "The password to use when connecting to vCenter")
|
||||||
|
vTZ := flag.String("tz", "Australia/Sydney", "The timezone to use when converting vCenter UTC times")
|
||||||
|
vInsecure := flag.Bool("insecure", true, "Allow insecure connections to vCenter")
|
||||||
|
vmName := flag.String("vmname", "example-vm", "The vm to query metrics")
|
||||||
|
//begin := flag.Duration("b", time.Hour, "Begin time") // default BeginTime is 1h ago
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
// Print logs to file
|
||||||
|
f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error opening file: %v", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
log.SetOutput(f)
|
||||||
|
|
||||||
|
log.Printf("Starting execution. Built on %s from sha1 %s\n", buildTime, sha1ver)
|
||||||
|
|
||||||
|
// So we can convert vCenter UTC to our local timezone
|
||||||
|
log.Printf("Setting timezone to '%s'\n", *vTZ)
|
||||||
|
location, err = time.LoadLocation(*vTZ)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Error setting timezone to %s : %s\n", *vTZ, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := url.Parse(*vURL)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Error parsing url %s : %s\n", *vURL, err)
|
||||||
|
os.Exit(1)
|
||||||
|
} else {
|
||||||
|
if !strings.HasSuffix(u.Path, "/sdk") {
|
||||||
|
u.Path, _ = url.JoinPath(u.Path, "/sdk")
|
||||||
|
log.Printf("Updated vCenter URL to '%v'\n", u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Connecting to vCenter %s\n", u)
|
||||||
|
u.User = url.UserPassword(*vUser, *vPass)
|
||||||
|
|
||||||
|
ctx, cancel = context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// Login to vcenter
|
||||||
|
c, err = govmomi.NewClient(ctx, u, *vInsecure)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Logging in error: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer c.Logout(ctx)
|
||||||
|
|
||||||
|
getSpecificVMMetrics(ctx, c.Client, *vmName)
|
||||||
|
}
|
Reference in New Issue
Block a user