Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-gtools / synnefo / ganeti / progress_monitor.py @ 0827883e

History | View | Annotate | Download (3.4 kB)

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
# Copyright 2011, 2012 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 that collects various monitoring messages from snf-image and
40
forwards them to the rest of the Synnefo infrastructure over AMQP.
41
"""
42

    
43
import os
44
import sys
45
import time
46
import json
47

    
48
from synnefo import settings
49
from synnefo.lib.amqp import AMQPClient
50
from synnefo.lib.utils import split_time
51

    
52
PROGNAME = os.path.basename(sys.argv[0])
53

    
54

    
55
def jsonstream(file):
56
    buf = ""
57
    decoder = json.JSONDecoder()
58
    while True:
59
        new_data = os.read(file.fileno(), 512)
60
        if not len(new_data):
61
            break
62

    
63
        buf += new_data.strip()
64
        while 1:
65
            try:
66
                msg, idx = decoder.raw_decode(buf)
67
            except ValueError:
68
                break
69
            yield msg
70
            buf = buf[idx:].strip()
71

    
72
def main():
73

    
74
    usage = "Usage: %s <instance_name>\n" % PROGNAME
75

    
76
    if len(sys.argv) != 2:
77
        sys.stderr.write(usage)
78
        return 1
79

    
80
    instance_name = sys.argv[1]
81

    
82
    # WARNING: This assumes that instance names
83
    # are of the form prefix-id, and uses prefix to
84
    # determine the routekey for AMPQ
85
    prefix = instance_name.split('-')[0]
86
    routekey = "ganeti.%s.event.progress" % prefix
87
    amqp_client = AMQPClient(hosts=settings.AMQP_HOSTS, confirm_buffer=2)
88
    amqp_client.connect()
89
    amqp_client.exchange_declare(settings.EXCHANGE_GANETI, type='topic')
90

    
91
    for msg in jsonstream(sys.stdin):
92
        msg['event_time'] = split_time(time.time())
93
        msg['instance'] = instance_name
94

    
95
        # and send it over AMQP
96
        amqp_client.basic_publish(exchange=settings.EXCHANGE_GANETI,
97
                                  routing_key=routekey,
98
                                  body=json.dumps(msg))
99

    
100
    amqp_client.close()
101
    return 0
102

    
103
if __name__ == "__main__":
104
    sys.exit(main())
105

    
106
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :