Nav

CSV Save Start Script

Run the start script just once when you have it configured. You should see a success message in the admin shell, and your Tomcat’s catalina.out log file should show that it is now collecting metric data for the metrics you listed, on each of the servers in the Tcat server group matching the ID you configured.

As soon as you execute this script, it adds a group membership event listener that makes any servers added to the group also begin monitoring these metrics, and any server you remove from the group will have the metric monitors removed as well. You can run the start script as many times as you like – it only refreshes the listener and monitors on each subsequent re-run, whether or not any refresh is needed. You should schedule this start script to run on Tcat startup so that the event listener runs after Tcat console server JVM restarts.


       
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import com.mulesoft.common.server.GroupMembershipEvent
import com.mulesoft.common.agent.jmx.JmxCollectorInfo
import com.mulesoft.common.remoting.RemoteContext
import com.mulesoft.common.agent.jmx.JmxService
import org.mule.galaxy.util.SecurityUtils
import org.mule.galaxy.ee.common.dto.ChartDefinition
import org.mule.galaxy.ee.common.charts.ChartDefinitionTypes
import org.mule.galaxy.DuplicateItemException
import org.mule.galaxy.ee.common.dto.ServerGroup
import org.mule.galaxy.event.EventManager
import org.mule.galaxy.event.annotation.Async
import org.mule.galaxy.event.annotation.BindToEvent
import org.mule.galaxy.event.annotation.OnEvent
import org.mule.galaxy.NotFoundException
 
import org.springframework.context.ApplicationContext
 
@BindToEvent("com.mulesoft.common.server.GroupMembershipEvent")
public class DevGroup10sMetricsMonitorListener {
 
    // The below variables are for the user to configure, to set what is to
    // be monitored on which server group.
    def groupId = "401e81bf-b792-49fd-9006-eff59cd29387"
    def objectNames = [ "Catalina:type=GlobalRequestProcessor,name=http-8080",
                        "Catalina:type=ThreadPool,name=http-8080"
                      ]
    def attributeNames = [ "requestCount", "currentThreadsBusy" ]
    def trackDeltas = [ false, false ]
    def period = 10
 
    // The below variables are more programmatic, and shouldn't need changing.
    def applicationContext
    def serverManager
    def eventManager
    def chartService
 
    public DevGroup10sMetricsMonitorListener(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext
    }
 
    public void install() {
        serverManager = applicationContext.getBean("serverManager")
        eventManager = applicationContext.getBean("eventManager")
        chartService = applicationContext.getBean("chartService")
 
        // Get the group by ID so that if it gets renamed we can still track it.
        try {
            ServerGroup group = serverManager.getServerGroup(groupId)
        } catch (NotFoundException e) {
            println e
            return
        }
 
        // Remove the previous listener, if any.
        eventManager.listeners.each {
            if (it.class.simpleName == "DevGroup10sMetricsMonitorListener") {
                eventManager.removeListener(it)
            }
        }
        // Add this event listener.
        eventManager.addListener(this)
 
        // Add monitors for all servers that are already in this group.
        def results = serverManager.getServersForGroup(groupId, 0, -1, null)
        def servers = results.getData()
        servers.each { server ->
            // Only talk to the server if it is reachable by the console..
            if (serverManager.isServerUp(server.id)) {
                def metricCount = -1;
                objectNames.each { objectName ->
                    metricCount++
                    def attributeName = attributeNames[metricCount]
                    def trackDelta = trackDeltas[metricCount]
                    addMonitor(server.id, objectName, attributeName, trackDelta)
                }
            }
        }
    }
 
    @Async
    @OnEvent
    void onEvent(com.mulesoft.common.server.GroupMembershipEvent e) {
        if (e.groupId == this.groupId) {
            def metricCount = -1;
            objectNames.each { objectName ->
                metricCount++
                def attributeName = attributeNames[metricCount]
                def trackDelta = trackDeltas[metricCount]
                if (e.added == true) {
                    addMonitor(e.serverId, objectName, attributeName, trackDelta)
                }
            }
        }
    }
 
    void addMonitor(String serverId, String objectName,
                    String attributeName, boolean trackDelta) {
        // Start monitoring the JMX attribute in this server's agent.
        def d = new ChartDefinition()
        def jmxCollectorInfoId = groupId + "|" + period + "|" + serverId + "|" + objectName + "|" + attributeName + "|" + trackDelta
        d.setName(jmxCollectorInfoId)
        d.setPortletId("csvsave")
        d.getServerTypeDestination().setId(serverId)
        d.getServerTypeDestination().setGroup(false)
        d.setUnit(ChartDefinition.Unit.NOUNIT)
        d.setUserId(SecurityUtils.SYSTEM_USER.getId())
        d.setType(ChartDefinitionTypes.JMX_ATTRIBUTE_CHART.getId())
        Map<String, String> config = d.getConfiguration()
        config.put(ChartDefinition.JMX_OBJECT_NAME, objectName)
        config.put(ChartDefinition.JMX_ATTRIBUTE, attributeName)
        config.put(ChartDefinition.JMX_PERIOD, period as String)
        config.put(ChartDefinition.JMX_TRACK_DELTA, trackDelta as String)
 
        try {
            chartService.save(d)
            println "Started monitoring ${jmxCollectorInfoId}"
        } catch (DuplicateItemException e) {
            // This metric is already being monitored.
            println "Already being monitored: ${jmxCollectorInfoId}"
        }
    }
}
 
new DevGroup10sMetricsMonitorListener(applicationContext).install()
"Success!"