/
vnc_greenlets.py
71 lines (62 loc) · 2.63 KB
/
vnc_greenlets.py
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
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#
import os
import socket
from pysandesh.sandesh_base import *
from pysandesh.sandesh_logger import *
from pysandesh.gen_py.sandesh.ttypes import SandeshLevel
from cfgm_common.uve.greenlets import ttypes as sandesh
import gc
import gevent
import traceback
from greenlet import greenlet
from gevent import Greenlet
class VncGreenlet(Greenlet):
def __init__(self, module_name, handler):
sandesh.GreenletObjectReq.handle_request = \
self.sandesh_greenlet_object_handle_request
Greenlet.__init__(self, self.super_greenlet_handler,
handler, module_name)
self.start()
# end __init_
def super_greenlet_handler(self, greenlet_handler, module_name):
# Create a flag/variable local to the greenlet so that
# it can be identified as a "well known" greenlet.
gevent.getcurrent().greenlet_name = module_name
if callable(greenlet_handler):
greenlet_handler()
@staticmethod
def build_greenlet_sandesh(name, traces, count = 1):
sandesh_greenlet = sandesh.GreenletObject()
sandesh_greenlet.greenlet_name = name
sandesh_greenlet.count = count
sandesh_greenlet.greenlet_traces = traces
return sandesh_greenlet
def sandesh_greenlet_object_handle_request(self, req):
greenlet_resp = sandesh.GreenletObjectListResp(greenlets=[])
anonymous_cnt = 0
for obj in gc.get_objects():
if not obj:
continue
if not isinstance(obj, greenlet):
continue
# If the GC object is an instance of greenlet,
# but does not have the flag/variable 'greenlet_name'
# then it is not well known, hence count it as anonymous.
if not hasattr(obj, 'greenlet_name'):
anonymous_cnt = anonymous_cnt + 1
continue
if (req.greenlet_name is None or
req.greenlet_name == obj.greenlet_name):
sandesh_greenlet = VncGreenlet.build_greenlet_sandesh(
obj.greenlet_name,
''.join(traceback.format_stack(obj.gr_frame)))
greenlet_resp.greenlets.append(sandesh_greenlet)
if anonymous_cnt > 0:
sandesh_greenlet = VncGreenlet.build_greenlet_sandesh(
'Anonymous', 'Not Applicable', anonymous_cnt)
greenlet_resp.greenlets.append(sandesh_greenlet)
greenlet_resp.response(req.context())
#end sandesh_greenlet_object_handle_request
# end class VncGreenlet