root / snf-cyclades-gtools / synnefo / ganeti / progress_monitor.py @ c4e55622
History | View | Annotate | Download (7.9 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
# -*- coding: utf-8 -*-
|
3 |
#
|
4 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
5 |
#
|
6 |
# Redistribution and use in source and binary forms, with or
|
7 |
# without modification, are permitted provided that the following
|
8 |
# conditions are met:
|
9 |
#
|
10 |
# 1. Redistributions of source code must retain the above
|
11 |
# copyright notice, this list of conditions and the following
|
12 |
# disclaimer.
|
13 |
#
|
14 |
# 2. Redistributions in binary form must reproduce the above
|
15 |
# copyright notice, this list of conditions and the following
|
16 |
# disclaimer in the documentation and/or other materials
|
17 |
# provided with the distribution.
|
18 |
#
|
19 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
20 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
22 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
23 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
26 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
27 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
28 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
29 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30 |
# POSSIBILITY OF SUCH DAMAGE.
|
31 |
#
|
32 |
# The views and conclusions contained in the software and
|
33 |
# documentation are those of the authors and should not be
|
34 |
# interpreted as representing official policies, either expressed
|
35 |
# or implied, of GRNET S.A.
|
36 |
#
|
37 |
"""Utility to monitor the progress of image deployment
|
38 |
|
39 |
A small utility to monitor the progress of image deployment
|
40 |
by watching the contents of /proc/<pid>/io and producing
|
41 |
notifications of type 'ganeti-create-progress' to the rest
|
42 |
of the Synnefo infrastructure over AMQP.
|
43 |
|
44 |
"""
|
45 |
|
46 |
import os |
47 |
import sys |
48 |
import time |
49 |
import json |
50 |
import prctl |
51 |
import signal |
52 |
import socket |
53 |
|
54 |
from synnefo import settings |
55 |
from synnefo.lib.amqp import AMQPClient |
56 |
from synnefo.lib.utils import split_time |
57 |
|
58 |
|
59 |
def parse_arguments(args): |
60 |
from optparse import OptionParser |
61 |
|
62 |
kw = {} |
63 |
kw['usage'] = "%prog [options] command [args...]" |
64 |
kw['description'] = \
|
65 |
"%prog runs 'command' with the specified arguments, monitoring the " \
|
66 |
"number of bytes read and written by it. 'command' is assumed to be " \
|
67 |
"A program used to install the OS for a Ganeti instance. %prog " \
|
68 |
"periodically issues notifications of type 'ganeti-create-progress' " \
|
69 |
"to the rest of the Synnefo infrastructure over AMQP."
|
70 |
|
71 |
parser = OptionParser(**kw) |
72 |
parser.disable_interspersed_args() |
73 |
parser.add_option("-r", "--read-bytes", |
74 |
action="store", type="int", dest="read_bytes", |
75 |
metavar="BYTES_TO_READ",
|
76 |
help="The expected number of bytes to be read, " \
|
77 |
"used to compute input progress",
|
78 |
default=0)
|
79 |
parser.add_option("-w", "--write-bytes", |
80 |
action="store", type="int", dest="write_bytes", |
81 |
metavar="BYTES_TO_WRITE",
|
82 |
help="The expected number of bytes to be written, " \
|
83 |
"used to compute output progress",
|
84 |
default=0)
|
85 |
parser.add_option("-i", "--instance-name", |
86 |
dest="instance_name",
|
87 |
metavar="GANETI_INSTANCE",
|
88 |
help="The Ganeti instance name to be used in AMQP " \
|
89 |
"notifications")
|
90 |
|
91 |
(opts, args) = parser.parse_args(args) |
92 |
|
93 |
if opts.instance_name is None or (opts.read_bytes == 0 and |
94 |
opts.write_bytes == 0):
|
95 |
sys.stderr.write("Fatal: Options '-i' and at least one of '-r' " \
|
96 |
"or '-w' are mandatory.\n")
|
97 |
parser.print_help() |
98 |
sys.exit(1)
|
99 |
|
100 |
if len(args) == 0: |
101 |
sys.stderr.write("Fatal: You need to specify the command to run.\n")
|
102 |
parser.print_help() |
103 |
sys.exit(1)
|
104 |
|
105 |
return (opts, args)
|
106 |
|
107 |
|
108 |
def report_wait_status(pid, status): |
109 |
if os.WIFEXITED(status):
|
110 |
sys.stderr.write("Child PID = %d exited, status = %d\n" %
|
111 |
(pid, os.WEXITSTATUS(status))) |
112 |
elif os.WIFSIGNALED(status):
|
113 |
sys.stderr.write("Child PID = %d died by signal, signal = %d\n" %
|
114 |
(pid, os.WTERMSIG(status))) |
115 |
elif os.WIFSTOPPED(status):
|
116 |
sys.stderr.write("Child PID = %d stopped by signal, signal = %d\n" %
|
117 |
(pid, os.WSTOPSIG(status))) |
118 |
else:
|
119 |
sys.stderr.write("Internal error: Unhandled case, " \
|
120 |
"PID = %d, status = %d\n" % (pid, status))
|
121 |
sys.exit(1)
|
122 |
sys.stderr.flush() |
123 |
|
124 |
|
125 |
def main(): |
126 |
(opts, args) = parse_arguments(sys.argv[1:])
|
127 |
|
128 |
# WARNING: This assumes that instance names
|
129 |
# are of the form prefix-id, and uses prefix to
|
130 |
# determine the routekey for AMPQ
|
131 |
prefix = opts.instance_name.split('-')[0] |
132 |
routekey = "ganeti.%s.event.progress" % prefix
|
133 |
amqp_client = AMQPClient() |
134 |
amqp_client.connect() |
135 |
|
136 |
pid = os.fork() |
137 |
if pid == 0: |
138 |
# In child process:
|
139 |
|
140 |
# Make sure we die with the parent and are not left behind
|
141 |
# WARNING: This uses the prctl(2) call and is Linux-specific.
|
142 |
prctl.set_pdeathsig(signal.SIGHUP) |
143 |
|
144 |
# exec command specified in arguments,
|
145 |
# searching the $PATH, keeping all environment
|
146 |
os.execvpe(args[0], args, os.environ)
|
147 |
sys.stderr.write("execvpe failed, exiting with non-zero status")
|
148 |
os.exit(1)
|
149 |
|
150 |
# In parent process:
|
151 |
iofname = "/proc/%d/io" % pid
|
152 |
iof = open(iofname, "r", 0) # 0: unbuffered open |
153 |
sys.stderr.write("%s: created child PID = %d, monitoring file %s\n" %
|
154 |
(sys.argv[0], pid, iofname))
|
155 |
|
156 |
while True: |
157 |
# check if the child process is still alive
|
158 |
(wpid, status) = os.waitpid(pid, os.WNOHANG) |
159 |
if wpid == pid:
|
160 |
report_wait_status(pid, status) |
161 |
if (os.WIFEXITED(status) or os.WIFSIGNALED(status)): |
162 |
if not (os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0): |
163 |
return 1 |
164 |
else:
|
165 |
# send a final notification
|
166 |
final_msg = dict(type="ganeti-create-progress", |
167 |
instance=opts.instance_name) |
168 |
final_msg['event_time'] = split_time(time.time())
|
169 |
if opts.read_bytes:
|
170 |
final_msg['rprogress'] = float(100) |
171 |
if opts.write_bytes:
|
172 |
final_msg['wprogress'] = float(100) |
173 |
amqp_client.basic_publish(exchange=settings.EXCHANGE_GANETI, |
174 |
routing_key=routekey, |
175 |
body=json.dumps(final_msg)) |
176 |
return 0 |
177 |
|
178 |
# retrieve the current values of the read/write byte counters
|
179 |
iof.seek(0)
|
180 |
for l in iof.readlines(): |
181 |
if l.startswith("rchar:"): |
182 |
rchar = int(l.split(': ')[1]) |
183 |
if l.startswith("wchar:"): |
184 |
wchar = int(l.split(': ')[1]) |
185 |
|
186 |
# Construct notification of type 'ganeti-create-progress'
|
187 |
msg = dict(type="ganeti-create-progress", |
188 |
instance=opts.instance_name) |
189 |
msg['event_time'] = split_time(time.time())
|
190 |
if opts.read_bytes:
|
191 |
msg['rprogress'] = float("%2.2f" % |
192 |
(rchar * 100.0 / opts.read_bytes))
|
193 |
if opts.write_bytes:
|
194 |
msg['wprogress'] = float("%2.2f" % |
195 |
(wchar * 100.0 / opts.write_bytes))
|
196 |
|
197 |
# and send it over AMQP
|
198 |
amqp_client.basic_publish(exchange=settings.EXCHANGE_GANETI, |
199 |
routing_key=routekey, |
200 |
body=json.dumps(msg)) |
201 |
|
202 |
# Sleep for a while
|
203 |
time.sleep(3)
|
204 |
|
205 |
|
206 |
if __name__ == "__main__": |
207 |
sys.exit(main()) |