Skip to content

Commit

Permalink
chore: reduce cyclomatic complexities and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
tbelda-ems committed Mar 9, 2023
1 parent 7c140b5 commit 007822a
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 87 deletions.
30 changes: 0 additions & 30 deletions internal/ovirtcollector/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ import (
type VcCache struct {
dcs *ovirtsdk.DataCenterSlice
clusters *ovirtsdk.ClusterSlice
gvs *ovirtsdk.GlusterVolumeSlice
sds *ovirtsdk.StorageDomainSlice
hosts *ovirtsdk.HostSlice
vms *ovirtsdk.VmSlice
lastDCUpdate time.Time
lastHoUpdate time.Time
lastGvUpdate time.Time
lastSdUpdate time.Time
lastVmUpdate time.Time
}
Expand Down Expand Up @@ -61,34 +59,6 @@ func (c *OVirtCollector) getDatacentersAndClusters(ctx context.Context) error {
return err
}

func (c *OVirtCollector) getAllDatacentersGlusterVolumes(ctx context.Context) error {
var err error

if time.Since(c.lastGvUpdate) < c.dataDuration {
return nil
}

if err = c.getDatacentersAndClusters(ctx); err != nil {
return err
}

// Get Gluster Volumes
clserv := ovirtsdk.NewClusterService(c.conn, "")
gvsService := clserv.GlusterVolumesService()
gvsResponse, err := gvsService.List().Send()
if err != nil {
return err
}
gvs, ok := gvsResponse.Volumes()
if !ok {
return fmt.Errorf("Could not get gluster volume list or it is empty")
}
c.gvs = gvs
c.lastGvUpdate = time.Now()

return nil
}

func (c *OVirtCollector) getAllDatacentersHosts(ctx context.Context) error {
var err error

Expand Down
120 changes: 120 additions & 0 deletions internal/ovirtcollector/glustervolume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// This file contains ovirtcollector methods to gathers stats about glustervolumes
//
// Author: Tesifonte Belda
// License: The MIT License (MIT)

package ovirtcollector

import (
"context"
"fmt"
"time"

"github.com/influxdata/telegraf"

ovirtsdk "github.com/ovirt/go-ovirt"
)

// CollectGlusterVolumeInfo gathers oVirt glustervolume's info
func (c *OVirtCollector) CollectGlusterVolumeInfo(
ctx context.Context,
acc telegraf.Accumulator,
) error {
var (
status ovirtsdk.GlusterVolumeStatus
gvtype ovirtsdk.GlusterVolumeType
gvs *ovirtsdk.GlusterVolumeSlice
br *ovirtsdk.GlusterBrickSlice
cl *ovirtsdk.Cluster
gvtags = make(map[string]string)
gvfields = make(map[string]interface{})
id, name, dcname string
clname string
t time.Time
briks int
disperse, redundancy int64
replica, stripe int64
ok bool
err error
)

if c.conn == nil {
return fmt.Errorf("Could not get gluster volumes info: %w", ErrorNoClient)
}

if err = c.getDatacentersAndClusters(ctx); err != nil {
return fmt.Errorf("Could not get all gluster volumes entity lists: %w", err)
}
t = time.Now()

for _, cl = range c.clusters.Slice() {
if clname, ok = cl.Name(); !ok {
acc.AddError(fmt.Errorf("Found a cluster without Name, skipping"))
continue
}
if !c.filterClusters.Match(clname) {
continue
}
if gvs, ok = cl.GlusterVolumes(); !ok {
acc.AddError(fmt.Errorf("Cloud not get gluster volumes for cluster %s", clname))
continue
}
dcname = c.clusterDatacenterName(cl)
for _, gv := range gvs.Slice() {
if id, ok = gv.Id(); !ok {
acc.AddError(fmt.Errorf("Found a gluster volume without Id, skipping"))
continue
}
if name, ok = gv.Name(); !ok {
acc.AddError(fmt.Errorf("Found a gluster volume without Name, skipping"))
continue
}
gvtype, _ = gv.VolumeType()
if status, ok = gv.Status(); !ok {
acc.AddError(fmt.Errorf("Cloud not get status for gluster volume %s", name))
continue
}
briks = 0
if br, ok = gv.Bricks(); ok {
briks = len(br.Slice())
}
disperse, _ = gv.DisperseCount()
redundancy, _ = gv.RedundancyCount()
replica, _ = gv.ReplicaCount()
stripe, _ = gv.StripeCount()

gvtags["clustername"] = clname
gvtags["dcname"] = dcname
gvtags["id"] = id
gvtags["name"] = name
gvtags["ovirt-engine"] = c.url.Host
gvtags["type"] = string(gvtype)

gvfields["briks"] = briks
gvfields["disperse_count"] = disperse
gvfields["redundancy_count"] = redundancy
gvfields["replica_count"] = replica
gvfields["status"] = string(status)
gvfields["status_code"] = gvStatusCode(status)
gvfields["stripe_count"] = stripe

acc.AddFields("ovirtstat_glustervolume", gvfields, gvtags, t)
}
}

return err
}

// gvStatusCode converts GlusterVolumeStatus to int16 for easy alerting
func gvStatusCode(status ovirtsdk.GlusterVolumeStatus) int16 {
switch status {
case ovirtsdk.GLUSTERVOLUMESTATUS_UP:
return 0
case ovirtsdk.GLUSTERVOLUMESTATUS_UNKNOWN:
return 1
case ovirtsdk.GLUSTERVOLUMESTATUS_DOWN:
return 2
default:
return 1
}
}
64 changes: 31 additions & 33 deletions internal/ovirtcollector/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (c *OVirtCollector) CollectHostInfo(
if !c.filterHosts.Match(name) {
continue
}
if status, ok = host.Status(); !ok {
acc.AddError(fmt.Errorf("Cloud not get status for host %s", name))
continue
}
htype, _ = host.Type()
clname, dcname = "", ""
if cl, ok = host.Cluster(); ok {
Expand All @@ -70,10 +74,6 @@ func (c *OVirtCollector) CollectHostInfo(
}
dcname = c.clusterDatacenterName(cl)
}
if status, ok = host.Status(); !ok {
acc.AddError(fmt.Errorf("Cloud not get status for host %s", name))
continue
}
cores, sockets, speed, threads = 0, 0, 0, 0
if cpu, ok = host.Cpu(); ok {
if cort, ok = cpu.Topology(); ok {
Expand All @@ -83,9 +83,7 @@ func (c *OVirtCollector) CollectHostInfo(
}
speed, _ = cpu.Speed()
}
if mem, ok = host.Memory(); !ok {
mem = 0
}
mem, _ = host.Memory()
if reinstall, ok = host.ReinstallationRequired(); !ok {
reinstall = false
}
Expand Down Expand Up @@ -123,40 +121,40 @@ func (c *OVirtCollector) CollectHostInfo(

// hostStatusCode converts HostStatus to int16 for easy alerting
func hostStatusCode(status ovirtsdk.HostStatus) int16 {
var code int16
switch status {
case ovirtsdk.HOSTSTATUS_UP:
return 0
case ovirtsdk.HOSTSTATUS_UNASSIGNED:
return 4
case ovirtsdk.HOSTSTATUS_REBOOT:
return 6
code = 0
case ovirtsdk.HOSTSTATUS_MAINTENANCE:
code = 1
case ovirtsdk.HOSTSTATUS_PREPARING_FOR_MAINTENANCE:
return 2
code = 2
case ovirtsdk.HOSTSTATUS_PENDING_APPROVAL:
return 3
case ovirtsdk.HOSTSTATUS_NON_RESPONSIVE:
return 10
case ovirtsdk.HOSTSTATUS_NON_OPERATIONAL:
return 11
case ovirtsdk.HOSTSTATUS_MAINTENANCE:
return 1
case ovirtsdk.HOSTSTATUS_KDUMPING:
return 8
case ovirtsdk.HOSTSTATUS_INSTALLING_OS:
return 5
case ovirtsdk.HOSTSTATUS_INSTALLING:
return 5
code = 3
case ovirtsdk.HOSTSTATUS_UNASSIGNED:
code = 4
case ovirtsdk.HOSTSTATUS_INSTALLING_OS,
ovirtsdk.HOSTSTATUS_INSTALLING:
code = 5
case ovirtsdk.HOSTSTATUS_REBOOT,
ovirtsdk.HOSTSTATUS_INITIALIZING:
code = 6
case ovirtsdk.HOSTSTATUS_INSTALL_FAILED:
return 5
case ovirtsdk.HOSTSTATUS_INITIALIZING:
return 6
code = 7
case ovirtsdk.HOSTSTATUS_KDUMPING:
code = 8
case ovirtsdk.HOSTSTATUS_ERROR:
return 9
code = 9
case ovirtsdk.HOSTSTATUS_NON_RESPONSIVE:
code = 10
case ovirtsdk.HOSTSTATUS_NON_OPERATIONAL:
code = 11
case ovirtsdk.HOSTSTATUS_DOWN:
return 12
code = 12
case ovirtsdk.HOSTSTATUS_CONNECTING:
return 7
code = 7
default:
return 1
code = 2
}
return code
}
46 changes: 22 additions & 24 deletions internal/ovirtcollector/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (c *OVirtCollector) CollectVmsInfo(
if !c.filterVms.Match(name) {
continue
}
if status, ok = vm.Status(); !ok {
acc.AddError(fmt.Errorf("Cloud not get status for VM %s", name))
continue
}
if ho, ok = vm.Host(); ok {
hostname = c.hostName(ho)
if !c.filterHosts.Match(hostname) {
Expand All @@ -74,10 +78,6 @@ func (c *OVirtCollector) CollectVmsInfo(
}
dcname = c.clusterDatacenterName(cl)
}
if status, ok = vm.Status(); !ok {
acc.AddError(fmt.Errorf("Cloud not get status for VM %s", name))
continue
}
cores, sockets, threads = 0, 0, 0
if cpu, ok = vm.Cpu(); ok {
if cort, ok = cpu.Topology(); ok {
Expand All @@ -86,9 +86,7 @@ func (c *OVirtCollector) CollectVmsInfo(
threads, _ = cort.Threads()
}
}
if mem, ok = vm.Memory(); !ok {
mem = 0
}
mem, _ = vm.Memory()
stateless, _ = vm.Stateless()
runOnce, _ = vm.RunOnce()

Expand Down Expand Up @@ -117,38 +115,38 @@ func (c *OVirtCollector) CollectVmsInfo(

// vmStatusCode converts VmStatus to int16 for easy alerting
func vmStatusCode(status ovirtsdk.VmStatus) int16 {
var code int16
switch status {
case ovirtsdk.VMSTATUS_UP:
return 0
code = 0
case ovirtsdk.VMSTATUS_PAUSED:
return 1
code = 1
case ovirtsdk.VMSTATUS_SUSPENDED:
return 2
code = 2
case ovirtsdk.VMSTATUS_POWERING_UP:
return 3
code = 3
case ovirtsdk.VMSTATUS_WAIT_FOR_LAUNCH:
return 4
code = 4
case ovirtsdk.VMSTATUS_SAVING_STATE:
return 5
code = 5
case ovirtsdk.VMSTATUS_MIGRATING:
return 6
code = 6
case ovirtsdk.VMSTATUS_POWERING_DOWN:
return 7
code = 7
case ovirtsdk.VMSTATUS_RESTORING_STATE:
return 9
code = 9
case ovirtsdk.VMSTATUS_REBOOT_IN_PROGRESS:
return 8
case ovirtsdk.VMSTATUS_UNKNOWN:
return 10
code = 8
case ovirtsdk.VMSTATUS_IMAGE_LOCKED:
return 11
code = 11
case ovirtsdk.VMSTATUS_UNASSIGNED:
return 12
code = 12
case ovirtsdk.VMSTATUS_NOT_RESPONDING:
return 13
code = 13
case ovirtsdk.VMSTATUS_DOWN:
return 14
code = 14
default:
return 10
code = 10
}
return code
}

0 comments on commit 007822a

Please sign in to comment.