Nav

CSV Save Stop Script

Simply removing servers from the monitored Tcat server group should be enough for the Start script’s event listener to stop monitoring the metrics on the server that is being removed. But, if you would like to stop monitoring the metrics on all servers in the group, run this Stop script. It removes all monitors for the listed metrics from every server in the monitored Tcat server group. If the CSV save writer script is still running, the metric values it logs no longer contains changing values, but the CSV save writer script continues to log into the CSV files until you reconfigure the scheduler to stop running the CSV save writer script.


       
    
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
import org.mule.galaxy.ee.common.dto.ServerGroup
import org.mule.galaxy.util.SecurityUtils
import org.mule.galaxy.impl.jcr.JcrUtil
 
import org.springframework.context.ApplicationContext
import org.springmodules.jcr.JcrCallback
import java.util.concurrent.Executors
import java.util.concurrent.ExecutionException
import java.util.concurrent.Callable
import java.util.concurrent.TimeUnit
 
// 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
 
// You probably shouldn't change variables below.
 
def c = applicationContext
// Copy into a local var, referenced in closure.
def logger = log
def serverManager = c.getBean("serverManager")
def chartService = c.getBean("chartService")
def sf = c.getBean("sessionFactory")
 
def Callable deleteMonitorsCallable = {
    SecurityUtils.doPrivileged({
        JcrUtil.doInTransaction(sf, { session ->
 
            def deleteMonitors = { serverId, charts ->
                // Stop monitoring the JMX attribute in this server's agent.
                try {
                    for (int i = 0; i < objectNames.size(); i++) {
                        def objectName = objectNames[i]
                        def attributeName = attributeNames[i]
                        def trackDelta = trackDeltas[i]
                        def jmxCollectorInfoId = groupId + "|" + period + "|" + serverId + "|" + objectName + "|" + attributeName + "|" + trackDelta
                        charts.each { chart ->
                            if (chart.name == jmxCollectorInfoId) {
                                chartService.delete(chart.id)
                                println "Stopped monitoring ${jmxCollectorInfoId}"
                            }
                        }
                    }
                } catch (Exception e) {
                    println e
                }
            }
 
            // Delete monitors for all servers in this group.
            def results = serverManager.getServersForGroup(groupId, 0, -1, null)
            def servers = results.getData()
 
            def charts = chartService.getPortletCharts("csvsave")
 
            servers.each { server ->
                if (serverManager.isServerUp(server.id)) {
                    deleteMonitors(server.id, charts)
                } else {
                    println "Server ${server.id} was unreachable."
                }
            }
        } as JcrCallback)
    })
} as Callable
def executor = Executors.newSingleThreadExecutor()
try {
    executor.submit(deleteMonitorsCallable).get(60, TimeUnit.SECONDS)
} catch (ExecutionException e) {
    throw e?.cause?.cause
}
 
"Success!"