summaryrefslogtreecommitdiff
path: root/py/demo/Ice/metrics/Metrics.py
blob: db3a00512600719c8fad5549edb4a743da6fd638 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# **********************************************************************
#
# Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************

import sys, traceback, Ice, IceMX

#
# Formatting information for metrics maps. The tupple defines the
# field name, title, alignment ('<' for left alignement and '>' for
# right aligment) and width.
#
maps = {
    "Connection" : [
        ("id", "Connections", '<', 35), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("receivedBytes", "RxBytes", '>', 10),
        ("sentBytes", "TxBytes", '>', 10),
        ("averageLifetime", "Avg (s)", '>', 8) 
    ],
    "Invocation" : [
        ("id", "Invocations", '<', 39), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("retry", "Rtrs", '>', 5),
        ("", "Sz", '>', 5),
        ("", "RepSz", '>', 5),
        ("averageLifetime", "Avg (ms)", '>', 8) 
    ],
    "Dispatch" : [
        ("id", "Dispatch", '<', 40), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("size", "Sz", '>', 5),
        ("replySize", "RepSz", '>', 5),
        ("averageLifetime", "Avg (ms)", '>', 8) 
    ],
    "Thread" : [ 
        ("id", "Threads", '<', 25), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("inUseForIO", "IO", '>', 6),
        ("inUseForUser", "User", '>', 6),
        ("inUseForOther", "Other", '>', 6),
        ("averageLifetime", "Avg (s)", '>', 8) 
    ],
    "ConnectionEstablishment" : [ 
        ("id", "Connection Establishments", '<', 30), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("averageLifetime", "Avg (ms)", '>', 8) 
    ],
    "EndpointLookup" : [
        ("id", "Endpoint Lookups", '<', 30), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("averageLifetime", "Avg (ms)",'>', 8) 
    ],
    "Remote" : [
        ("id", "Invocations", '>', 39), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("", "", '>', 5),
        ("size", "Sz", '>', 5),
        ("replySize", "RepSz", '>', 5),
        ("averageLifetime", "Avg (ms)", '>', 8)
    ],
    "Session" : [
        ("id", "Sessions", '<', 15), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("forwardedClient", "FdCli", '>', 5),
        ("forwardedServer", "FdSrv", '>', 5),
        ("queuedClient", "QdCli", '>', 5),
        ("queuedServer", "QdSrv", '>', 5),
        ("overriddenClient", "OdCli", '>', 5),
        ("overriddenServer", "OdSrv", '>', 5),
        ("routingTableSize", "RT Sz", '>', 5),
        ("averageLifetime", "Avg (s)", '>', 8)
    ],
    "Topic" : [
        ("id", "Topics", '<', 30), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("published", "Published", '>', 9),
        ("forwarded", "Forwarded", '>', 9),
        ("averageLifetime", "Avg (s)", '>', 7)
    ],
    "Subscriber" : [
        ("id", "Subscribers", '<', 32), 
        ("current", "#", '>', 3),
        ("total", "Total", '>', 5),
        ("queued", "Queued", '>', 6),
        ("outstanding", "Pending", '>', 7),
        ("delivered", "Delivered", '>', 9),
        ("averageLifetime", "Avg (s)", '>', 8)
    ],
}

def metricsTitle():
    # Returns a lambda which returns the title of the field.
    return lambda mapName, field, title, align, width: ("{0:" + align + str(width) + "}").format(title)

def metricsSeparator(fill):
    # Returns a lambda which returns a separator of the size of the
    # field and using the specified fill character.
    return lambda mapName, field, title, align, width: ("{0:" + fill + align + str(width) + "}").format("")

def metricsField(metrics):
    def getField(mapName, field, title, width):
        if field == "":
            return ""
        elif field == "averageLifetime":
            #
            # Compute the average lifetime.
            #
            if metrics.total <= metrics.current:
                return "0.000"

            avg = metrics.totalLifetime / 1000.0 / (metrics.total - metrics.current)
            if title.find("(s)") > 0: # Display in seconds
                avg /= 1000.0
            return "%.3f" % avg

        else:
            v = str(metrics.__dict__[field])
            if len(v) > width:
                v = v[0:width - 3] + '...' # Truncates the field if it's too large.
            return v

    # Return a lambda which returns the formated metric value for the
    # given metrics object.
    return lambda mapName, field, title, align, width: \
        ("{0:" + align + str(width) + "}").format(getField(mapName, field, title, width))

def printMetrics(mapName, getValue, sep = '|', prefix = " "):
    #
    # This method prints a metrics, a line in the table. 
    #
    # It concatenates all the metric fields according to the format
    # described in the `maps' table defined above. The value of each
    # column is obtained by calling the provided getValue function.
    #
    fieldStr = [ prefix + sep ]
    for (field, title, align, width) in maps[mapName]:
        fieldStr.append(getValue(mapName, field, title, align, width) + sep)
    print("".join(fieldStr))

def printMetricsMap(admin, viewName, mapName, map):

    #
    # Print the table header.
    # 
    printMetrics(mapName, metricsSeparator('='), '+')
    printMetrics(mapName, metricsTitle())
    printMetrics(mapName, metricsSeparator('='), '+')

    #
    # Print the metrics map.
    #
    # For the invocation metrics map we also print the "Remote"
    # submaps embedded with each invocation metrics.
    #
    if mapName != "Invocation":
        # Print each metrics from the map
        for o in map:
            printMetrics(mapName, metricsField(o))
    else:
        first = True
        for o in map:
            if not first:
                printMetrics(mapName, metricsSeparator('-'), '+')
            printMetrics(mapName, metricsField(o))
            for so in o.remotes:
                printMetrics("Remote", metricsField(so))

    #
    # Print the table footer.
    #
    printMetrics(mapName, metricsSeparator('='), '+')

    #
    # Print metrics failure if any after the table.
    #
    failures = admin.getMapMetricsFailures(viewName, mapName)
    if len(failures) > 0:
        print("")
        print(" Failures:")
        for k in failures:
            for (exception, count) in k.failures.items():
                if len(k.id) > 0:
                    print("  - " + str(count) + ' ' + exception + " for `" + k.id + "'")
                else:
                    print("  - " + str(count) + ' ' + exception)
    print("")

def printMetricsView(admin, viewName, viewAndRefreshTime):

    (view, refreshTime) = viewAndRefreshTime

    #
    # Print each metrics map from the metrics view.
    #
    print("View: " + viewName)
    print("")
    for mapName, map in view.items():
        if mapName in maps and len(map) > 0:
            printMetricsMap(admin, viewName, mapName, map)
    print("")

class Client(Ice.Application):

    def usage(self):
        print("usage: " + sys.argv[0] + " dump | enable | disable [<view-name> [<map-name>]]" + """
To connect to the Ice administrative facility of an Ice process, you
should specify its endpoint(s) and instance name with the
`InstanceName' and `Endpoints' properties. For example:
    
 $ ./Metrics.py --Endpoints="tcp -p 12345 -h localhost" --InstanceName=Server dump

Commands:

  dump    Dump all the IceMX metrics views configured for the 
          process or if a specific view or map is specified,
          print only this view or map.

  enable  Enable all the IceMX metrics views configured for 
          the process or the provided view or map if specified.

  disable Disable all the IceMX metrics views configured for 
          the process or the provided view or map if specified.
""")

    def run(self, args):

        props = self.communicator().getProperties()
        args = props.parseCommandLineOptions("", args);

        if len(args) < 2 or len(args) > 6:
            self.usage()
            return 2

        command = args[1]
        viewName = None
        mapName = None
        if len(args) > 2:
            viewName = args[2]
            if len(args) > 3:
                mapName = args[3]

        try:
            #
            # Create the proxy for the Metrics admin facet.
            #
            proxyStr = "%s/admin -f Metrics:%s" % (props.getProperty("InstanceName"), props.getProperty("Endpoints"))
            metrics = IceMX.MetricsAdminPrx.checkedCast(self.communicator().stringToProxy(proxyStr));
            if not metrics:
                print(sys.argv[0] + ": invalid proxy `" + proxyStr + "'")
                return 1

            if command == "dump":
                (views, disabledViews) = metrics.getMetricsViewNames()
                if not viewName:
                    # Print all the enabled metrics views.
                    for v in views:
                        printMetricsView(metrics, v, metrics.getMetricsView(v))
                else:
                    # Ensure the view exists
                    if not viewName in views and not viewName in disabledViews:
                        print("unknown view `" + viewName + "', available views:")
                        print("enabled = " + str(views) + ", disabled = " + str(disabledViews))
                        return 0

                    # Ensure the view is enabled
                    if viewName in disabledViews:
                        print("view `" + viewName + "' is disabled")
                        return 0
                    
                    # Retrieve the metrics view and print it.
                    (view, refresh) = metrics.getMetricsView(viewName);                
                    if mapName:
                        if not mapName in view:
                            print("no map `" + mapName + "' in `" + viewName + "' view, available maps:")
                            print(str(view.keys()))
                            return 0
                        printMetricsMap(metrics, viewName, mapName, view[mapName])
                    else:
                        printMetricsView(metrics, viewName, (view, refresh))

            elif command == "enable":
                metrics.enableMetricsView(viewName)
            elif command == "disable":
                metrics.disableMetricsView(viewName)
            else:
                print("unknown command `" + command + "'")
                self.usage()
                return 2

        except Ice.ObjectNotExistException as ex:
            print("failed to get metrics from `%s':\n(the admin object doesn't exist, " + 
                  "are you sure to use the correct instance name?)") % (proxyStr)
            return 1
        except Ice.Exception as ex:
            print("failed to get metrics from `%s':\n%s") % (proxyStr, ex)
            return 1

        return 0

app = Client()

sys.exit(app.main(sys.argv))