From f940101ae940604b56da1e3c263736a93adc3d3d Mon Sep 17 00:00:00 2001 From: Zader Zheng Date: Fri, 17 Apr 2015 14:01:13 +0800 Subject: substitute TCP socket for UNIX socket --- lab_monitor_client.py | 8 +++++--- lab_monitor_server.py | 16 +++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) 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(" Date: Fri, 17 Apr 2015 23:09:17 +0800 Subject: kill a AF_UNIX left behind --- lab_monitor_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab_monitor_server.py b/lab_monitor_server.py index 6a11da6..4f1439b 100644 --- a/lab_monitor_server.py +++ b/lab_monitor_server.py @@ -137,7 +137,7 @@ def cmd_shutdown(): global is_exiting, cmd_socket, local_socket_address, cmd is_exiting.set(); cmd_socket.close() - socket(AF_UNIX).connect(local_socket_address) + socket(AF_INET).connect(local_socket_address) cmd.join() class AJAXHandler(RequestHandler): -- cgit v1.2.3-70-g09d2 From ec7869e614cd4a83175313583e080b8093a6c31b Mon Sep 17 00:00:00 2001 From: Zader Zheng Date: Fri, 17 Apr 2015 23:14:25 +0800 Subject: add settings support on client --- client_settings.example.sh | 3 +++ monitor_daemon.sh | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 client_settings.example.sh 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/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 -- cgit v1.2.3-70-g09d2 From 71adb4d3beb9496a4d61f028f5429e3117b77f34 Mon Sep 17 00:00:00 2001 From: Zader Zheng Date: Fri, 17 Apr 2015 23:24:15 +0800 Subject: add requirements for Python project and venv ignore --- .gitignore | 2 ++ requirements.txt | 4 ++++ 2 files changed, 6 insertions(+) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index a1e76d0..1c15a9b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ .*.swp .*.swo *.socket +venv/ + 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 -- cgit v1.2.3-70-g09d2 From a710f90d7de0efff19e0fd9ba2794ef276341eb0 Mon Sep 17 00:00:00 2001 From: Zader Zheng Date: Fri, 17 Apr 2015 23:27:29 +0800 Subject: exclude client_settings.sh in .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1c15a9b..2e05134 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ .*.swp .*.swo *.socket +client_settings.sh venv/ - -- cgit v1.2.3-70-g09d2 From 42daf5aa3be3a659c94c9214ae63820c33876e32 Mon Sep 17 00:00:00 2001 From: Zader Zheng Date: Fri, 17 Apr 2015 23:29:58 +0800 Subject: refactor lab_monitor_server a little --- lab_monitor_server.py | 64 +++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/lab_monitor_server.py b/lab_monitor_server.py index 4f1439b..50f9430 100644 --- a/lab_monitor_server.py +++ b/lab_monitor_server.py @@ -9,20 +9,6 @@ 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 -SOCKET_PORT = 2334 -HOST = '' - -lock = RWLock() -stat_res = {} -is_exiting = threading.Event() -reclaimed_ids = [] -id_cnt = 0 - class ActionError(Exception): pass @@ -82,12 +68,6 @@ 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 cmd_socket = socket.socket(AF_INET, SOCK_STREAM) @@ -129,10 +109,6 @@ 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 is_exiting.set(); @@ -151,11 +127,35 @@ 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__': + logging.basicConfig(level=logging.DEBUG, + format='[%(levelname)-7s] (%(threadName)-10s) %(message)s',) + MAX_LENGTH = 65536 + MAX_RECORDS = 32 + PORT = 2333 + SOCKET_PORT = 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(PORT) + IOLoop.current().start() + except KeyboardInterrupt: + cmd_shutdown() -- cgit v1.2.3-70-g09d2 From 1c3bb4acd175ec2ef13fa1edcf2fc7d765a0df45 Mon Sep 17 00:00:00 2001 From: Zader Zheng Date: Sat, 18 Apr 2015 00:00:29 +0800 Subject: don't need to find old socket file now --- lab_monitor_server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab_monitor_server.py b/lab_monitor_server.py index 50f9430..1c664cd 100644 --- a/lab_monitor_server.py +++ b/lab_monitor_server.py @@ -110,10 +110,10 @@ def command_server(): conn.close() 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_INET).connect(local_socket_address) + socket.socket(AF_INET, SOCK_STREAM).connect((HOST, SOCKET_PORT)) cmd.join() class AJAXHandler(RequestHandler): -- cgit v1.2.3-70-g09d2 From 2a8bf2735b96e6c0d4b749c1672c3e549735de3c Mon Sep 17 00:00:00 2001 From: Zader Zheng Date: Sat, 18 Apr 2015 00:07:09 +0800 Subject: add setting support for server --- .gitignore | 1 + lab_monitor_server.py | 11 ++++++++--- server_settings.example.cfg | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 server_settings.example.cfg diff --git a/.gitignore b/.gitignore index 2e05134..f6e17d4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ .*.swo *.socket client_settings.sh +server_settings.cfg venv/ diff --git a/lab_monitor_server.py b/lab_monitor_server.py index 1c664cd..91bdbb9 100644 --- a/lab_monitor_server.py +++ b/lab_monitor_server.py @@ -8,6 +8,7 @@ from tornado import gen from tornado.ioloop import IOLoop from tornado.web import RequestHandler, Application, url, StaticFileHandler from rwlock import RWLock +from ConfigParser import SafeConfigParser class ActionError(Exception): pass @@ -129,12 +130,14 @@ class AJAXHandler(RequestHandler): yield gen.Task(grab_lock, self) 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 - PORT = 2333 - SOCKET_PORT = 2334 + HTTP_PORT = config.get('HTTP', 'port') or 2333 + SOCKET_PORT = config.get('socket', 'port') or 2334 HOST = '' lock = RWLock() @@ -151,11 +154,13 @@ if __name__ == '__main__': 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(PORT) + app.listen(HTTP_PORT) IOLoop.current().start() except KeyboardInterrupt: cmd_shutdown() + 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 -- cgit v1.2.3-70-g09d2