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