##// END OF EJS Templates
process-management: use safer way to extract data for processes....
marcink -
r2540:370f15d3 default
parent child Browse files
Show More
@@ -1,133 +1,163 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2016-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import logging
22 22
23 23 import psutil
24 24 import signal
25 25 from pyramid.view import view_config
26 26
27 27 from rhodecode.apps._base import BaseAppView
28 28 from rhodecode.apps.admin.navigation import navigation_list
29 29 from rhodecode.lib.auth import (
30 30 LoginRequired, HasPermissionAllDecorator, CSRFRequired)
31 from rhodecode.lib.utils2 import safe_int
31 from rhodecode.lib.utils2 import safe_int, StrictAttributeDict
32 32
33 33 log = logging.getLogger(__name__)
34 34
35 35
36 36 class AdminProcessManagementView(BaseAppView):
37 37 def load_default_context(self):
38 38 c = self._get_local_tmpl_context()
39 return c
39 40
40 return c
41 def _format_proc(self, proc, with_children=False):
42 try:
43 mem = proc.memory_info()
44 proc_formatted = StrictAttributeDict({
45 'pid': proc.pid,
46 'name': proc.name(),
47 'mem_rss': mem.rss,
48 'mem_vms': mem.vms,
49 'cpu_percent': proc.cpu_percent(),
50 'create_time': proc.create_time(),
51 'cmd': ' '.join(proc.cmdline()),
52 })
53
54 if with_children:
55 proc_formatted.update({
56 'children': [self._format_proc(x)
57 for x in proc.children(recursive=True)]
58 })
59 except Exception:
60 log.exception('Failed to load proc')
61 proc_formatted = None
62 return proc_formatted
63
64 def get_processes(self):
65 proc_list = []
66 for p in psutil.process_iter():
67 if 'gunicorn' in p.name():
68 proc = self._format_proc(p, with_children=True)
69 if proc:
70 proc_list.append(proc)
71
72 return proc_list
41 73
42 74 @LoginRequired()
43 75 @HasPermissionAllDecorator('hg.admin')
44 76 @view_config(
45 77 route_name='admin_settings_process_management', request_method='GET',
46 78 renderer='rhodecode:templates/admin/settings/settings.mako')
47 79 def process_management(self):
48 80 _ = self.request.translate
49 81 c = self.load_default_context()
50 82
51 83 c.active = 'process_management'
52 84 c.navlist = navigation_list(self.request)
53 c.gunicorn_processes = (
54 p for p in psutil.process_iter() if 'gunicorn' in p.name())
85 c.gunicorn_processes = self.get_processes()
55 86 return self._get_template_context(c)
56 87
57 88 @LoginRequired()
58 89 @HasPermissionAllDecorator('hg.admin')
59 90 @view_config(
60 91 route_name='admin_settings_process_management_data', request_method='GET',
61 92 renderer='rhodecode:templates/admin/settings/settings_process_management_data.mako')
62 93 def process_management_data(self):
63 94 _ = self.request.translate
64 95 c = self.load_default_context()
65 c.gunicorn_processes = (
66 p for p in psutil.process_iter() if 'gunicorn' in p.name())
96 c.gunicorn_processes = self.get_processes()
67 97 return self._get_template_context(c)
68 98
69 99 @LoginRequired()
70 100 @HasPermissionAllDecorator('hg.admin')
71 101 @CSRFRequired()
72 102 @view_config(
73 103 route_name='admin_settings_process_management_signal',
74 104 request_method='POST', renderer='json_ext')
75 105 def process_management_signal(self):
76 106 pids = self.request.json.get('pids', [])
77 107 result = []
78 108 def on_terminate(proc):
79 109 msg = "process `PID:{}` terminated with exit code {}".format(
80 110 proc.pid, proc.returncode)
81 111 result.append(msg)
82 112
83 113 procs = []
84 114 for pid in pids:
85 115 pid = safe_int(pid)
86 116 if pid:
87 117 try:
88 118 proc = psutil.Process(pid)
89 119 except psutil.NoSuchProcess:
90 120 continue
91 121
92 122 children = proc.children(recursive=True)
93 123 if children:
94 124 print('Wont kill Master Process')
95 125 else:
96 126 procs.append(proc)
97 127
98 128 for p in procs:
99 129 p.terminate()
100 130 gone, alive = psutil.wait_procs(procs, timeout=10, callback=on_terminate)
101 131 for p in alive:
102 132 p.kill()
103 133
104 134 return {'result': result}
105 135
106 136 @LoginRequired()
107 137 @HasPermissionAllDecorator('hg.admin')
108 138 @CSRFRequired()
109 139 @view_config(
110 140 route_name='admin_settings_process_management_master_signal',
111 141 request_method='POST', renderer='json_ext')
112 142 def process_management_master_signal(self):
113 143 pid_data = self.request.json.get('pid_data', {})
114 144 pid = safe_int(pid_data['pid'])
115 145 action = pid_data['action']
116 146 if pid:
117 147 try:
118 148 proc = psutil.Process(pid)
119 149 except psutil.NoSuchProcess:
120 150 return {'result': 'failure_no_such_process'}
121 151
122 152 children = proc.children(recursive=True)
123 153 if children:
124 154 # master process
125 155 if action == '+' and len(children) <= 20:
126 156 proc.send_signal(signal.SIGTTIN)
127 157 elif action == '-' and len(children) >= 2:
128 158 proc.send_signal(signal.SIGTTOU)
129 159 else:
130 160 return {'result': 'failure_wrong_action'}
131 161 return {'result': 'success'}
132 162
133 163 return {'result': 'failure_not_master'}
@@ -1,95 +1,91 b''
1 1
2 2 <table id="procList">
3 3 <%
4 4 def get_name(proc):
5 cmd = ' '.join(proc.cmdline())
6 if 'vcsserver.ini' in cmd:
5 if 'vcsserver.ini' in proc.cmd:
7 6 return 'VCSServer'
8 elif 'rhodecode.ini' in cmd:
7 elif 'rhodecode.ini' in proc.cmd:
9 8 return 'RhodeCode'
10 return proc.name()
9 return proc.name
11 10 %>
12 11 <tr>
13 12 <td colspan="8">
14 13 <span id="processTimeStamp">${h.format_date(h.datetime.now())}</span>
15 14 </td>
16 15 </tr>
17 16 % for proc in c.gunicorn_processes:
18 <% mem = proc.memory_info()%>
19 <% children = proc.children(recursive=True) %>
20 % if children:
21 17
18 % if proc.children:
22 19 <tr>
23 20 <td>
24 21 <code>
25 22 ${proc.pid} - ${get_name(proc)}
26 23 </code>
27 24 </td>
28 25 <td>
29 26 <a href="#showCommand" onclick="$('#pid'+${proc.pid}).toggle();return false"> command </a>
30 27 <code id="pid${proc.pid}" style="display: none">
31 ${' '.join(proc.cmdline())}
28 ${proc.cmd}
32 29 </code>
33 30 </td>
34 31 <td></td>
35 32 <td>
36 RSS:${h.format_byte_size_binary(mem.rss)}
33 RSS:${h.format_byte_size_binary(proc.mem_rss)}
37 34 </td>
38 35 <td>
39 VMS:${h.format_byte_size_binary(mem.vms)}
36 VMS:${h.format_byte_size_binary(proc.mem_vms)}
40 37 </td>
41 38 <td>
42 AGE: ${h.age_component(h.time_to_utcdatetime(proc.create_time()))}
39 AGE: ${h.age_component(h.time_to_utcdatetime(proc.create_time))}
43 40 </td>
44 41 <td>
45 42 MASTER
46 43 % if request.GET.get('dev'):
47 44 | <a href="#addWorker" onclick="addWorker(${proc.pid}); return false">ADD</a> | <a href="#removeWorker" onclick="removeWorker(${proc.pid}); return false">REMOVE</a>
48 45 % endif
49 46 </td>
50 47 </tr>
51 48 <% mem_sum = 0 %>
52 % for proc_child in children:
53 <% mem = proc_child.memory_info()%>
49 % for proc_child in proc.children:
54 50 <tr>
55 51 <td>
56 52 <code>
57 53 | ${proc_child.pid} - ${get_name(proc_child)}
58 54 </code>
59 55 </td>
60 56 <td>
61 57 <a href="#showCommand" onclick="$('#pid'+${proc_child.pid}).toggle();return false"> command </a>
62 58 <code id="pid${proc_child.pid}" style="display: none">
63 ${' '.join(proc_child.cmdline())}
59 ${proc_child.cmd}
64 60 </code>
65 61 </td>
66 62 <td>
67 CPU: ${proc_child.cpu_percent()} %
63 CPU: ${proc_child.cpu_percent} %
68 64 </td>
69 65 <td>
70 RSS:${h.format_byte_size_binary(mem.rss)}
71 <% mem_sum += mem.rss %>
66 RSS:${h.format_byte_size_binary(proc_child.mem_rss)}
67 <% mem_sum += proc_child.mem_rss %>
72 68 </td>
73 69 <td>
74 VMS:${h.format_byte_size_binary(mem.vms)}
70 VMS:${h.format_byte_size_binary(proc_child.mem_vms)}
75 71 </td>
76 72 <td>
77 AGE: ${h.age_component(h.time_to_utcdatetime(proc_child.create_time()))}
73 AGE: ${h.age_component(h.time_to_utcdatetime(proc_child.create_time))}
78 74 </td>
79 75 <td>
80 76 <a href="#restartProcess" onclick="restart(this, ${proc_child.pid});return false">
81 77 restart
82 78 </a>
83 79 </td>
84 80 </tr>
85 81 % endfor
86 82 <tr>
87 <td colspan="2"><code>| total processes: ${len(children)}</code></td>
83 <td colspan="2"><code>| total processes: ${len(proc.children)}</code></td>
88 84 <td></td>
89 85 <td><strong>RSS:${h.format_byte_size_binary(mem_sum)}</strong></td>
90 86 <td></td>
91 87 </tr>
92 88 <tr><td> <code> -- </code> </td></tr>
93 89 % endif
94 90 % endfor
95 91 </table>
General Comments 0
You need to be logged in to leave comments. Login now