diff --git a/grid.py b/grid.py index 5f8d7fa..11ef913 100644 --- a/grid.py +++ b/grid.py @@ -69,7 +69,7 @@ def phyidgen(slave_name, unit_type): if str(unit_type) == 'lxc': full_list = list(range(2000,2999)) if str(unit_type) == 'kvm': - full_list = list(range(100,150)) + full_list = list(range(101,150)) exclude_list = [] directory = 'db/' for dbfile in os.listdir(directory): diff --git a/novnc.py b/novnc.py index 4502d5a..f0934de 100644 --- a/novnc.py +++ b/novnc.py @@ -19,9 +19,12 @@ def spawn(target, options): a_options += c_option try: - command_line = 'python3 runwebsockify.py ' + a_options + vnctarget + command_line = 'python3 runwebsockify.py -D ' + a_options + vnctarget args = shlex.split(command_line) - p = subprocess.Popen(args) - print('spawned!') + #print(str(args)) + p = subprocess.Popen(args, shell=False) + print('vnc connector API:{} <--> {}:{} spawned !'.format(target['listen_port'], target['target_host'], target['target_port'])) + p.wait() + print('done.') except: raise diff --git a/plugin.py b/plugin.py index 842b6dd..90bd698 100644 --- a/plugin.py +++ b/plugin.py @@ -41,8 +41,8 @@ def create(json): except Exception as e: ioconfig.logger.warning('grid> slave not predefined. I will query for a capable one.') #slave_name = str(grid.query_happiness(region_id, weight)) - slave_name = 'warrior' - #slave_name = 'lexx' + #slave_name = 'warrior' + slave_name = 'lexx' ioconfig.logger.info('{}> slave selected'.format(slave_name)) proxobject = auth(slave_name) real_slave_name = proxobject.cluster.status.get()[0]['name'] @@ -322,10 +322,11 @@ def vmvnc(json): slaveip = ioconfig.parser.get(str(slave_name), 'ipv4') #slaveport = socket['port'] slaveport = ticket['port'] - slave_id = 1 #TODO: fix this vnchost = ioconfig.parser.get('general', 'novnc_host') - listenport = str(int(slaveport) + 1000 + (int(slave_id) * 100)) #TODO: max 100 parallel connections/slave. + listenport = random.randint(7000, 9999) + #listenport = random.randint(7000, 7001) + vnc_target = { 'target_host': slaveip, 'target_port': slaveport, @@ -342,11 +343,11 @@ def vmvnc(json): novnc.spawn(vnc_target, vnc_options) external_url = ioconfig.parser.get('general', 'novnc_url') - prefix = external_url + "?host=" + vnchost + "&port=" + listenport + "&view_only=false&encrypt=1&true_color=1&password=" + prefix = external_url + "?host=" + vnchost + "&port=" + str(listenport) + "&view_only=false&encrypt=1&true_color=1&password=" vnc_url = prefix + ticket['ticket'] time.sleep(3) #wait few seconds for the parallel vncwebsocket - ioconfig.logger.info('{}[{}]> vnc port {} ready'.format(vm_owner, slave_name, listenport)) + ioconfig.logger.info('{}[{}]> vnc port {} ready'.format(vm_owner, slave_name, str(listenport))) #response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] } response = { 'status':'VNC', 'url':vnc_url } #print(vnc_url) diff --git a/proxmaster.service b/proxmaster.service new file mode 100644 index 0000000..4ad33bb --- /dev/null +++ b/proxmaster.service @@ -0,0 +1,19 @@ +[Unit] +Description=Proxmaster +After=network.target +After=systemd-user-sessions.service +After=network-online.target + +[Service] +User=master +Type=forking +ExecStart=/usr/bin/screen -dmS proxmaster /home/master/proxmaster/start.sh +#ExecStop= +TimeoutSec=30 +Restart=on-failure +RestartSec=30 +StartLimitInterval=350 +StartLimitBurst=10 + +[Install] +WantedBy=multi-user.target diff --git a/start.sh b/start.sh index 88b587c..81e6ef7 100755 --- a/start.sh +++ b/start.sh @@ -1,10 +1,10 @@ #!/bin/bash # Log rotation -LOG_DIR=${HOME}/proxmaster/log -LOG_FILE="${LOG_DIR}/proxmaster.log" +DIR=${HOME}/proxmaster +LOG_FILE="${DIR}/log/proxmaster.log" -mkdir -p $LOG_DIR +mkdir -p $DIR/log TIME=`date -u +%s` @@ -14,6 +14,5 @@ else touch ${LOG_FILE} fi -cd ${LOG_DIR}/.. #startuwsgi instance -uwsgi config.ini +uwsgi --logdate --ini config.ini diff --git a/humanjson.sh b/tools/humanjson.sh similarity index 79% rename from humanjson.sh rename to tools/humanjson.sh index e605c4a..c142961 100755 --- a/humanjson.sh +++ b/tools/humanjson.sh @@ -2,7 +2,7 @@ #makes jsons human (and machine) readable -for filename in db/*.json ; do +for filename in ../db/*.json ; do echo $filename; python3 -m json.tool $filename read; diff --git a/tools/netstat.py b/tools/netstat.py new file mode 100644 index 0000000..1bd900e --- /dev/null +++ b/tools/netstat.py @@ -0,0 +1,87 @@ +#!/usr/bin/python + +import pwd +import os +import re +import glob + +PROC_TCP = "/proc/net/tcp" +STATE = { + '01':'ESTABLISHED', + '02':'SYN_SENT', + '03':'SYN_RECV', + '04':'FIN_WAIT1', + '05':'FIN_WAIT2', + '06':'TIME_WAIT', + '07':'CLOSE', + '08':'CLOSE_WAIT', + '09':'LAST_ACK', + '0A':'LISTEN', + '0B':'CLOSING' + } + +def _load(): + ''' Read the table of tcp connections & remove header ''' + with open(PROC_TCP,'r') as f: + content = f.readlines() + content.pop(0) + return content + +def _hex2dec(s): + return str(int(s,16)) + +def _ip(s): + ip = [(_hex2dec(s[6:8])),(_hex2dec(s[4:6])),(_hex2dec(s[2:4])),(_hex2dec(s[0:2]))] + return '.'.join(ip) + +def _remove_empty(array): + return [x for x in array if x !=''] + +def _convert_ip_port(array): + host,port = array.split(':') + return _ip(host),_hex2dec(port) + +def netstat(): + ''' + Function to return a list with status of tcp connections at linux systems + To get pid of all network process running on system, you must run this script + as superuser + ''' + + content=_load() + result = [] + for line in content: + line_array = _remove_empty(line.split(' ')) # Split lines and remove empty spaces. + l_host,l_port = _convert_ip_port(line_array[1]) # Convert ipaddress and port from hex to decimal. + r_host,r_port = _convert_ip_port(line_array[2]) + tcp_id = line_array[0] + state = STATE[line_array[3]] + uid = pwd.getpwuid(int(line_array[7]))[0] # Get user from UID. + inode = line_array[9] # Need the inode to get process pid. + pid = _get_pid_of_inode(inode) # Get pid prom inode. + try: # try read the process name. + exe = os.readlink('/proc/'+pid+'/exe') + except: + exe = None + + nline = [tcp_id, uid, l_host+':'+l_port, r_host+':'+r_port, state, pid, exe] + result.append(nline) + return result + +def _get_pid_of_inode(inode): + ''' + To retrieve the process pid, check every running process and look for one using + the given inode. + ''' + for item in glob.glob('/proc/[0-9]*/fd/[0-9]*'): + try: + if re.search(inode,os.readlink(item)): + return item.split('/')[2] + except: + pass + return None + +if __name__ == '__main__': + for conn in netstat(): + print conn +