X-Git-Url: https://gerrit.akraino.org/r/gitweb?p=ta%2Fmonitoring.git;a=blobdiff_plain;f=src%2Fkeepalivedmonitor.py;fp=src%2Fkeepalivedmonitor.py;h=92e7ad4e54ce747f6f11137c03f9936288041f68;hp=0000000000000000000000000000000000000000;hb=461929b69c0526c3b8c25a11dbbd5d4b301dbac5;hpb=e04550241c9c27272fbcff079222878b5b88bff3 diff --git a/src/keepalivedmonitor.py b/src/keepalivedmonitor.py new file mode 100755 index 0000000..92e7ad4 --- /dev/null +++ b/src/keepalivedmonitor.py @@ -0,0 +1,58 @@ +#! /usr/bin/python + +# Copyright 2019 Nokia + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import socket +import select +import os +import errno +import sys + +if __name__ == '__main__': + host = socket.gethostname() + ip = socket.gethostbyname(host) + port = int(sys.argv[1]) + + print("Starting listening to port %d" % port) + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.setblocking(0) + s.bind((ip, port)) + s.listen(1) + inputs = [s] + while True: + try: + readable, _, _ = select.select(inputs, [], []) + for f in readable: + if f is s: + client, address = s.accept() + client.setblocking(0) + inputs.append(client) + #print("Accepted connection from %r, total inputs %d" % (address, len(inputs))) + else: + try: + result = f.recv() + if not result: + inputs.remove(f) + except Exception as exp: + inputs.remove(f) + except (SystemExit, KeyboardInterrupt): + break + except select.error as ex: + if ex.args[0] == errno.EINTR: + break + + print("Stopping...")