-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop_responder.py
62 lines (52 loc) · 1.68 KB
/
stop_responder.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
#!/usr/bin/python3
# -*- coding: utf-8-*-
# Stop ZMQ responder.
import argparse
import datetime
import json
import socket
import zmq
import pmon
def datetime_converter(o):
"""
Converter for JSON output
:param o: value to convert to a string
:return: string representation of the value
"""
if isinstance(o, datetime.datetime):
return o.__str__()
def create_message(msg, msg_type):
"""
General notification factory.
:param msg: the text content of the message
:param msg_type: the type-indicator
:return: dictionary with message data
"""
return {'created': datetime.datetime.now(),
'msg.type': msg_type,
'msg': msg,
'from': socket.gethostname()}
if __name__ == '__main__':
# Define arguments and read commandline
parser = argparse.ArgumentParser(description="Simple process monitor.")
parser.add_argument('--conf',
type=str,
default='pmon.ini',
help="the configuration file in INI-format")
args = parser.parse_args()
# init and establish connection, send the message
pmon.init(args.conf)
context = zmq.Context(1)
try:
sckt = context.socket(zmq.REQ)
try:
sckt.setsockopt(zmq.LINGER, 100)
connection_str = "tcp://127.0.0.1:{0}".format(pmon.CFG['pmon']['zmq.port'])
sckt.connect(connection_str)
sckt.send_string(json.dumps(create_message('stop', 'INTERNAL'),
default=datetime_converter),
zmq.DONTWAIT)
finally:
sckt.close()
finally:
context.term()