root / snf-image-host / host-monitor.py @ master
History | View | Annotate | Download (2 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
# Copyright (C) 2012 GRNET S.A.
|
4 |
#
|
5 |
# This program is free software; you can redistribute it and/or modify
|
6 |
# it under the terms of the GNU General Public License as published by
|
7 |
# the Free Software Foundation; either version 2 of the License, or
|
8 |
# (at your option) any later version.
|
9 |
#
|
10 |
# This program is distributed in the hope that it will be useful, but
|
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
# General Public License for more details.
|
14 |
#
|
15 |
# You should have received a copy of the GNU General Public License
|
16 |
# along with this program; if not, write to the Free Software
|
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
18 |
# 02110-1301, USA.
|
19 |
|
20 |
"""Utility that generates monitoring messages for snf-image.
|
21 |
|
22 |
This utility given a message type as option and the message body as input will
|
23 |
print a monitoring message to stdout.
|
24 |
"""
|
25 |
|
26 |
import sys |
27 |
import os |
28 |
import json |
29 |
import time |
30 |
|
31 |
MSG_TYPE_ERROR = "image-error"
|
32 |
MSG_TYPE_INFO = "image-info"
|
33 |
|
34 |
PROTOCOL = { |
35 |
"error": (MSG_TYPE_ERROR, "messages"), |
36 |
"stderr": (MSG_TYPE_ERROR, "stderr"), |
37 |
"info": (MSG_TYPE_INFO, "messages") |
38 |
} |
39 |
|
40 |
PROGNAME = os.path.basename(sys.argv[0])
|
41 |
|
42 |
if __name__ == "__main__": |
43 |
usage = "Usage: %s <msg-type>\n" % PROGNAME
|
44 |
|
45 |
if len(sys.argv) != 2: |
46 |
sys.stderr.write(usage) |
47 |
sys.exit(1)
|
48 |
|
49 |
msg_type = sys.argv[1]
|
50 |
|
51 |
if msg_type not in PROTOCOL.keys(): |
52 |
sys.stderr.write("Unknown message type: %s\n" % msg_type)
|
53 |
sys.exit(1)
|
54 |
|
55 |
msg = {} |
56 |
msg['type'] = PROTOCOL[msg_type][0] |
57 |
|
58 |
lines = [] |
59 |
if msg_type == 'stderr': |
60 |
msg['stderr'] = sys.stdin.read()
|
61 |
else:
|
62 |
while True: |
63 |
line = sys.stdin.readline() |
64 |
|
65 |
if not line: |
66 |
break
|
67 |
|
68 |
lines.append(line.strip()) |
69 |
msg[PROTOCOL[msg_type][1]] = lines
|
70 |
|
71 |
msg['timestamp'] = time.time()
|
72 |
sys.stdout.write("%s\n" % json.dumps(msg))
|
73 |
|
74 |
sys.exit(0)
|
75 |
|
76 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|