| 1 | package mq |
| 2 | |
| 3 | import ( |
| 4 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/modules/mq/contexts" |
| 5 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/protocols/pcf" |
| 6 | ) |
| 7 | |
| 8 | // collectSysTopics collects resource metrics using IBM MQ's resource monitoring system |
| 9 | func (c *Collector) collectSysTopics() error { |
| 10 | if !c.Config.CollectSysTopics { |
| 11 | // $SYS topic collection is disabled |
| 12 | return nil |
| 13 | } |
| 14 | |
| 15 | c.Debugf("Collecting metrics from $SYS topics using resource monitoring") |
| 16 | |
| 17 | // Check if resource monitoring is supported |
| 18 | if !c.client.IsResourceMonitoringSupported() { |
| 19 | c.Infof("Resource monitoring not supported on this queue manager (requires MQ V9+ on distributed platforms)") |
| 20 | // Disable future collection attempts |
| 21 | c.Config.CollectSysTopics = false |
| 22 | return nil |
| 23 | } |
| 24 | |
| 25 | if c.client.GetResourceStatus() == pcf.ResourceStatusFailed { |
| 26 | // Previous attempts already determined this isn't available; skip spamming logs |
| 27 | return nil |
| 28 | } |
| 29 | |
| 30 | // Enable resource monitoring on first use |
| 31 | if err := c.client.EnableResourceMonitoring(); err != nil { |
| 32 | c.warnOnce("resource_monitoring_enable", "Failed to enable resource monitoring: %v", err) |
| 33 | if c.client.GetResourceStatus() == pcf.ResourceStatusFailed { |
| 34 | // Stop trying in future iterations |
| 35 | c.Config.CollectSysTopics = false |
| 36 | } |
| 37 | return nil // Don't fail the entire collection |
| 38 | } |
| 39 | c.clearWarnOnce("resource_monitoring_enable") |
| 40 | |
| 41 | // Get resource publications |
| 42 | result, err := c.client.GetResourcePublications() |
| 43 | if err != nil { |
| 44 | c.warnOnce("resource_monitoring_publications", "Failed to get resource publications: %v", err) |
| 45 | return nil // Don't fail the entire collection |
| 46 | } |
| 47 | c.clearWarnOnce("resource_monitoring_publications") |
| 48 | |
| 49 | c.Debugf("Retrieved %d resource publications", result.Stats.Discovery.AvailableItems) |
| 50 | |
| 51 | // Process CPU metrics |
| 52 | if result.UserCPUPercent.IsCollected() || result.SystemCPUPercent.IsCollected() { |
| 53 | values := contexts.QueueManagerResourcesCPUUsageValues{} |
| 54 | |
| 55 | if result.UserCPUPercent.IsCollected() { |
| 56 | // Convert percentage (0-100) to basis points (0-10000) for precision |
| 57 | values.User = result.UserCPUPercent.Int64() * 100 |
| 58 | } |
| 59 | if result.SystemCPUPercent.IsCollected() { |
| 60 | // Convert percentage (0-100) to basis points (0-10000) for precision |
| 61 | values.System = result.SystemCPUPercent.Int64() * 100 |
| 62 | } |
| 63 | |
| 64 | contexts.QueueManagerResources.CPUUsage.SetUpdateEvery(c.State, contexts.EmptyLabels{}, c.GetEffectiveSysTopicInterval()) |
| 65 | contexts.QueueManagerResources.CPUUsage.Set(c.State, contexts.EmptyLabels{}, values) |
| 66 | } |
| 67 | |
| 68 | // Process memory metrics |
| 69 | if result.MemoryUsedMB.IsCollected() { |
| 70 | values := contexts.QueueManagerResourcesMemoryUsageValues{ |
| 71 | // Convert MB to bytes |
| 72 | Total: result.MemoryUsedMB.Int64() * 1024 * 1024, |
| 73 | } |
| 74 | contexts.QueueManagerResources.MemoryUsage.SetUpdateEvery(c.State, contexts.EmptyLabels{}, c.GetEffectiveSysTopicInterval()) |
| 75 | contexts.QueueManagerResources.MemoryUsage.Set(c.State, contexts.EmptyLabels{}, values) |
| 76 | } |
| 77 | |
| 78 | // Process log utilization metrics |
| 79 | if result.LogUsedBytes.IsCollected() && result.LogMaxBytes.IsCollected() { |
| 80 | used := result.LogUsedBytes.Int64() |
| 81 | max := result.LogMaxBytes.Int64() |
| 82 | |
| 83 | // Calculate utilization percentage if both values are available |
| 84 | if max > 0 { |
| 85 | // Convert to 0.01% units (basis points) as expected by the context |
| 86 | utilPercent := (used * 10000) / max |
| 87 | contexts.QueueManagerResources.LogUtilization.SetUpdateEvery(c.State, contexts.EmptyLabels{}, c.GetEffectiveSysTopicInterval()) |
| 88 | contexts.QueueManagerResources.LogUtilization.Set(c.State, contexts.EmptyLabels{}, |
| 89 | contexts.QueueManagerResourcesLogUtilizationValues{ |
| 90 | Used: utilPercent, |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | // Also set log file size |
| 95 | contexts.QueueManagerResources.LogFileSize.SetUpdateEvery(c.State, contexts.EmptyLabels{}, c.GetEffectiveSysTopicInterval()) |
| 96 | contexts.QueueManagerResources.LogFileSize.Set(c.State, contexts.EmptyLabels{}, |
| 97 | contexts.QueueManagerResourcesLogFileSizeValues{ |
| 98 | Size: max, |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | return nil |
| 103 | } |