forked from jensl/critic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservices.py
152 lines (122 loc) · 5.61 KB
/
services.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
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
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import os
import os.path
import socket
import time
import errno
import page.utils
import htmlutils
import dbutils
import configuration
import textutils
def renderServices(req, db, user):
req.content_type = "text/html; charset=utf-8"
document = htmlutils.Document(req)
document.setTitle("Services")
html = document.html()
head = html.head()
body = html.body()
page.utils.generateHeader(body, db, user, current_page="services")
document.addExternalStylesheet("resource/services.css")
document.addExternalScript("resource/services.js")
document.addInternalScript(user.getJS())
delay = 0.5
connected = False
while not connected and delay <= 10:
connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# This loop is for the case where we just restarted the service manager
# via the /services UI. The client-side script immediately reloads the
# page after restart, which typically leads to us trying to connect to
# the service manager while it's in the process of restarting. So just
# try a couple of times if at first the connection fails.
try:
connection.connect(configuration.services.SERVICEMANAGER["address"])
connected = True
except socket.error, error:
if error[0] in (errno.ENOENT, errno.ECONNREFUSED):
time.sleep(delay)
delay += delay
else: raise
if not connected:
raise page.utils.DisplayMessage, "Service manager not responding!"
connection.send(textutils.json_encode({ "query": "status" }))
connection.shutdown(socket.SHUT_WR)
data = ""
while True:
received = connection.recv(4096)
if not received: break
data += received
result = textutils.json_decode(data)
if result["status"] == "error":
raise page.utils.DisplayMessage, result["error"]
paleyellow = page.utils.PaleYellowTable(body, "Services")
def render(target):
table = target.table("services", cellspacing=0, align="center")
headings = table.tr("headings")
headings.th("name").text("Name")
headings.th("path").text("Path")
headings.th("pid").text("PID")
headings.th("rss").text("RSS")
headings.th("uptime").text("Uptime")
headings.th("commands").text()
table.tr("spacer").td("spacer", colspan=4)
def formatUptime(seconds):
def inner(seconds):
if seconds < 60: return "%d seconds" % seconds
elif seconds < 60 * 60: return "%d minutes" % (seconds / 60)
elif seconds < 60 * 60 * 24: return "%d hours" % (seconds / (60 * 60))
else: return "%d days" % (seconds / (60 * 60 * 24))
return inner(int(seconds)).replace(" ", " ")
def formatRSS(bytes):
if bytes < 1024: return "%d B" % bytes
elif bytes < 1024 ** 2: return "%.1f kB" % (float(bytes) / 1024)
elif bytes < 1024 ** 3: return "%.1f MB" % (float(bytes) / 1024 ** 2)
else: return "%.1f GB" % (float(bytes) / 1024 ** 3)
def getRSS(pid):
try:
for line in open("/proc/%d/status" % pid):
words = line.split()
if words[0] == "VmRSS:":
if words[2].lower() == "kb": unit = 1024
elif words[2].lower() == "mb": unit = 1024 ** 2
else: raise Exception, "unknown unit: %s" % words[2]
return formatRSS(int(words[1]) * unit)
except: pass
return "N/A"
for service_name, service_data in sorted(result["services"].items()):
row = table.tr("service")
row.td("name").text(service_name)
row.td("path").text(service_data["path"])
row.td("pid").text(service_data["pid"] if service_data["pid"] != -1 else "(not running)")
row.td("rss").text(getRSS(service_data["pid"]))
row.td("uptime").innerHTML(formatUptime(service_data["uptime"]))
commands = row.td("commands")
commands.a(href="javascript:void(restartService(%s));" % htmlutils.jsify(service_name)).text("[restart]")
commands.a(href="javascript:void(getServiceLog(%s));" % htmlutils.jsify(service_name)).text("[log]")
for index, pid in enumerate(os.listdir(configuration.paths.WSGI_PIDFILE_DIR)):
startup = float(open(os.path.join(configuration.paths.WSGI_PIDFILE_DIR, pid)).read())
uptime = time.time() - startup
row = table.tr("service")
row.td("name").text("wsgi:%d" % index)
row.td("path").text()
row.td("pid").text(pid)
row.td("rss").text(getRSS(int(pid)))
row.td("uptime").innerHTML(formatUptime(uptime))
commands = row.td("commands")
commands.a(href="javascript:void(restartService('wsgi'));").text("[restart]")
paleyellow.addCentered(render)
return document