| 1 | //go:build cgo |
| 2 | |
| 3 | package jmx |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 11 | "github.com/netdata/netdata/go/plugins/pkg/matcher" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/framework" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/modules/websphere/common" |
| 14 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/modules/websphere/jmx/contexts" |
| 15 | jmxproto "github.com/netdata/netdata/go/plugins/plugin/ibm.d/protocols/websphere/jmx" |
| 16 | ) |
| 17 | |
| 18 | func defaultConfig() Config { |
| 19 | return Config{ |
| 20 | Config: framework.Config{ |
| 21 | UpdateEvery: int(defaultUpdateEvery / time.Second), |
| 22 | ObsoletionIterations: 60, |
| 23 | }, |
| 24 | Vnode: "", |
| 25 | |
| 26 | JMXURL: "", |
| 27 | JMXUsername: "", |
| 28 | JMXPassword: "", |
| 29 | JMXClasspath: "", |
| 30 | JavaExecPath: "", |
| 31 | |
| 32 | JMXTimeout: confopt.Duration(defaultScrapeTimeout), |
| 33 | InitTimeout: confopt.Duration(defaultInitTimeout), |
| 34 | ShutdownDelay: confopt.Duration(defaultShutdownDelay), |
| 35 | |
| 36 | ClusterName: "", |
| 37 | CellName: "", |
| 38 | NodeName: "", |
| 39 | ServerName: "", |
| 40 | ServerType: "", |
| 41 | CustomLabels: map[string]string{}, |
| 42 | |
| 43 | CollectJVMMetrics: confopt.AutoBoolEnabled, |
| 44 | CollectThreadPoolMetrics: confopt.AutoBoolEnabled, |
| 45 | CollectJDBCMetrics: confopt.AutoBoolEnabled, |
| 46 | CollectJCAMetrics: confopt.AutoBoolEnabled, |
| 47 | CollectJMSMetrics: confopt.AutoBoolEnabled, |
| 48 | CollectWebAppMetrics: confopt.AutoBoolEnabled, |
| 49 | CollectSessionMetrics: confopt.AutoBoolEnabled, |
| 50 | CollectTransactionMetrics: confopt.AutoBoolEnabled, |
| 51 | CollectClusterMetrics: confopt.AutoBoolEnabled, |
| 52 | CollectServletMetrics: confopt.AutoBoolEnabled, |
| 53 | CollectEJBMetrics: confopt.AutoBoolEnabled, |
| 54 | CollectJDBCAdvanced: confopt.AutoBoolDisabled, |
| 55 | |
| 56 | MaxThreadPools: 50, |
| 57 | MaxJDBCPools: 50, |
| 58 | MaxJCAPools: 50, |
| 59 | MaxJMSDestinations: 50, |
| 60 | MaxApplications: 100, |
| 61 | MaxServlets: 50, |
| 62 | MaxEJBs: 50, |
| 63 | |
| 64 | CollectPoolsMatching: "", |
| 65 | CollectJMSMatching: "", |
| 66 | CollectAppsMatching: "", |
| 67 | CollectServletsMatching: "", |
| 68 | CollectEJBsMatching: "", |
| 69 | |
| 70 | MaxRetries: 3, |
| 71 | RetryBackoffMultiplier: 2.0, |
| 72 | CircuitBreakerThreshold: 5, |
| 73 | HelperRestartMax: 3, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Init initialises the collector. |
| 78 | func (c *Collector) Init(context.Context) error { |
| 79 | c.SetImpl(c) |
| 80 | c.RegisterContexts(contexts.GetAllContexts()...) |
| 81 | |
| 82 | cfg := &c.Config |
| 83 | |
| 84 | if cfg.JMXURL == "" { |
| 85 | return fmt.Errorf("jmx_url is required") |
| 86 | } |
| 87 | |
| 88 | if cfg.UpdateEvery <= 0 { |
| 89 | c.Config.UpdateEvery = int(defaultUpdateEvery / time.Second) |
| 90 | } |
| 91 | |
| 92 | if cfg.JMXTimeout <= 0 { |
| 93 | c.Config.JMXTimeout = confopt.Duration(defaultScrapeTimeout) |
| 94 | } |
| 95 | if cfg.InitTimeout <= 0 { |
| 96 | c.Config.InitTimeout = confopt.Duration(defaultInitTimeout) |
| 97 | } |
| 98 | if cfg.ShutdownDelay <= 0 { |
| 99 | c.Config.ShutdownDelay = confopt.Duration(defaultShutdownDelay) |
| 100 | } |
| 101 | |
| 102 | clientCfg := jmxproto.Config{ |
| 103 | JMXURL: cfg.JMXURL, |
| 104 | JMXUsername: cfg.JMXUsername, |
| 105 | JMXPassword: cfg.JMXPassword, |
| 106 | JMXClasspath: cfg.JMXClasspath, |
| 107 | JavaExecPath: cfg.JavaExecPath, |
| 108 | InitTimeout: cfg.InitTimeout.Duration(), |
| 109 | CommandTimeout: cfg.JMXTimeout.Duration(), |
| 110 | ShutdownDelay: cfg.ShutdownDelay.Duration(), |
| 111 | } |
| 112 | |
| 113 | client, err := jmxproto.NewClient(clientCfg, c) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | if err := client.Start(context.Background()); err != nil { |
| 118 | client.Shutdown() |
| 119 | return err |
| 120 | } |
| 121 | c.client = client |
| 122 | |
| 123 | if cfg.CollectPoolsMatching != "" { |
| 124 | sel, err := matcher.NewSimplePatternsMatcher(cfg.CollectPoolsMatching) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | c.poolSelector = sel |
| 129 | } |
| 130 | |
| 131 | if cfg.CollectJMSMatching != "" { |
| 132 | sel, err := matcher.NewSimplePatternsMatcher(cfg.CollectJMSMatching) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| 136 | c.jmsSelector = sel |
| 137 | } |
| 138 | |
| 139 | if cfg.CollectAppsMatching != "" { |
| 140 | sel, err := matcher.NewSimplePatternsMatcher(cfg.CollectAppsMatching) |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | c.appSelector = sel |
| 145 | } |
| 146 | |
| 147 | c.identity = common.Identity{ |
| 148 | Cluster: cfg.ClusterName, |
| 149 | Cell: cfg.CellName, |
| 150 | Node: cfg.NodeName, |
| 151 | Server: cfg.ServerName, |
| 152 | } |
| 153 | labels := c.identity.Labels() |
| 154 | if cfg.ServerType != "" { |
| 155 | labels["server_type"] = cfg.ServerType |
| 156 | } |
| 157 | for k, v := range cfg.CustomLabels { |
| 158 | if k == "" || v == "" { |
| 159 | continue |
| 160 | } |
| 161 | labels[k] = v |
| 162 | } |
| 163 | c.SetGlobalLabels(labels) |
| 164 | |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | // Cleanup releases collector resources. |
| 169 | func (c *Collector) Cleanup(context.Context) { |
| 170 | if c.client != nil { |
| 171 | c.client.Shutdown() |
| 172 | } |
| 173 | } |