diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | client_settings.example.sh | 3 | ||||
-rw-r--r-- | lab_monitor_client.py | 8 | ||||
-rw-r--r-- | lab_monitor_server.py | 85 | ||||
-rwxr-xr-x | monitor_daemon.sh | 7 | ||||
-rw-r--r-- | requirements.txt | 4 | ||||
-rw-r--r-- | server_settings.example.cfg | 4 |
7 files changed, 69 insertions, 45 deletions
@@ -3,3 +3,6 @@ .*.swp .*.swo *.socket +client_settings.sh +server_settings.cfg +venv/ diff --git a/client_settings.example.sh b/client_settings.example.sh new file mode 100644 index 0000000..59bd00f --- /dev/null +++ b/client_settings.example.sh @@ -0,0 +1,3 @@ +#!/bin/bash +export SOCKET_HOST=127.0.0.1 +export SOCKET_PORT=2334 diff --git a/lab_monitor_client.py b/lab_monitor_client.py index 194b927..b61170c 100644 --- a/lab_monitor_client.py +++ b/lab_monitor_client.py @@ -1,4 +1,4 @@ -from socket import socket, AF_UNIX +from socket import socket, AF_INET, SOCK_STREAM from struct import pack, unpack from sys import stdout, stdin import argparse @@ -10,6 +10,8 @@ parser.add_argument('--clear') parser.add_argument('--drop') parser.add_argument('--alter') parser.add_argument('--type') +parser.add_argument('--host', default='127.0.0.1') +parser.add_argument('--port', default='2334', type=int) parser.add_argument('col', nargs='*') args = parser.parse_args() @@ -42,8 +44,8 @@ if __name__ == '__main__': print('please specify an action') exit(1) - s = socket(AF_UNIX) - s.connect("./lab_monitor.socket") + s = socket(AF_INET, SOCK_STREAM) + s.connect((args.host, args.port)) mesg = json.dumps(cmd) mesg = pack("<i", len(mesg)) + mesg s.send(mesg) diff --git a/lab_monitor_server.py b/lab_monitor_server.py index 4207eb9..91bdbb9 100644 --- a/lab_monitor_server.py +++ b/lab_monitor_server.py @@ -1,25 +1,14 @@ import time, json, os, re import logging, threading -from socket import socket, AF_UNIX, SHUT_WR, error as SocketError +from socket import socket, AF_INET, SOCK_STREAM, SHUT_WR, error as SocketError +import socket from struct import pack, unpack from time import sleep from tornado import gen from tornado.ioloop import IOLoop from tornado.web import RequestHandler, Application, url, StaticFileHandler from rwlock import RWLock - -logging.basicConfig(level=logging.DEBUG, - format='[%(levelname)-7s] (%(threadName)-10s) %(message)s',) -MAX_LENGTH = 65536 -MAX_RECORDS = 32 -PORT = 2333 -local_socket_address = "./lab_monitor.socket" - -lock = RWLock() -stat_res = {} -is_exiting = threading.Event() -reclaimed_ids = [] -id_cnt = 0 +from ConfigParser import SafeConfigParser class ActionError(Exception): pass @@ -80,21 +69,10 @@ def clear_records(mesg): stat_res[mid]["records"] = [] return "" -action_map = {"create": add_monitor, - "drop": del_monitor, - "add": add_record, - "clear": clear_records, - "alter": alter_records} - def command_server(): global c, cmd_socket, is_exiting - try: - os.unlink(local_socket_address) - except OSError: - if os.path.exists(local_socket_address): - raise - cmd_socket = socket(AF_UNIX) - cmd_socket.bind(local_socket_address) + cmd_socket = socket.socket(AF_INET, SOCK_STREAM) + cmd_socket.bind((HOST, SOCKET_PORT)) cmd_socket.listen(5) while not is_exiting.isSet(): logging.debug("accepting") @@ -132,16 +110,13 @@ def command_server(): finally: conn.close() -cmd = threading.Thread(target=command_server, name="local") -cmd.setDaemon(True) -cmd.start() - def cmd_shutdown(): - global is_exiting, cmd_socket, local_socket_address, cmd + global is_exiting, cmd_socket, cmd is_exiting.set(); cmd_socket.close() - socket(AF_UNIX).connect(local_socket_address) + socket.socket(AF_INET, SOCK_STREAM).connect((HOST, SOCKET_PORT)) cmd.join() + class AJAXHandler(RequestHandler): @gen.coroutine def get(self): @@ -153,11 +128,39 @@ class AJAXHandler(RequestHandler): lock.release() callback() yield gen.Task(grab_lock, self) -try: - app = Application([url(r"/ajax", AJAXHandler), - url(r'/()', StaticFileHandler, {'path': "./static/index.html"}), - url(r'/(.*)', StaticFileHandler, {'path': "./static/"})]) - app.listen(PORT) - IOLoop.current().start() -except KeyboardInterrupt: - cmd_shutdown() + +if __name__ == '__main__': + config = SafeConfigParser() + config.read('server_settings.cfg') + logging.basicConfig(level=logging.DEBUG, + format='[%(levelname)-7s] (%(threadName)-10s) %(message)s',) + MAX_LENGTH = 65536 + MAX_RECORDS = 32 + HTTP_PORT = config.get('HTTP', 'port') or 2333 + SOCKET_PORT = config.get('socket', 'port') or 2334 + HOST = '' + + lock = RWLock() + stat_res = {} + is_exiting = threading.Event() + reclaimed_ids = [] + id_cnt = 0 + action_map = {"create": add_monitor, + "drop": del_monitor, + "add": add_record, + "clear": clear_records, + "alter": alter_records} + + cmd = threading.Thread(target=command_server, name="local") + cmd.setDaemon(True) + cmd.start() + + try: + app = Application([url(r"/ajax", AJAXHandler), + url(r'/()', StaticFileHandler, {'path': "./static/index.html"}), + url(r'/(.*)', StaticFileHandler, {'path': "./static/"})]) + app.listen(HTTP_PORT) + IOLoop.current().start() + except KeyboardInterrupt: + cmd_shutdown() + diff --git a/monitor_daemon.sh b/monitor_daemon.sh index 7d976ee..e18720f 100755 --- a/monitor_daemon.sh +++ b/monitor_daemon.sh @@ -4,7 +4,12 @@ # echo "you must specify the triggered command" # exit 1 #fi -CLIENT="python lab_monitor_client.py" +if [[ -e client_settings.sh ]]; then + source client_settings.sh + SOCKET_SPEC="--host $SOCKET_HOST --port $SOCKET_PORT" +fi +echo "Reporting to $SOCKET_SPEC" +CLIENT="python lab_monitor_client.py $SOCKET_SPEC" function on_exit { $CLIENT --drop "$JID" exit 0 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..107b034 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +backports.ssl-match-hostname==3.4.0.2 +certifi==14.5.14 +psutil==2.2.1 +tornado==4.1 diff --git a/server_settings.example.cfg b/server_settings.example.cfg new file mode 100644 index 0000000..387b054 --- /dev/null +++ b/server_settings.example.cfg @@ -0,0 +1,4 @@ +[HTTP] + port: 2333 +[socket] + port: 2334 |