xfreerdpctl.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/python
  2. ##Forkato da labcalcoloctl di Elisa Aliverti
  3. ##Autore: Nicolo' Palazzini
  4. ##Ultima modifica: 20/04/2019
  5. from time import time
  6. start = time()
  7. import argparse
  8. #import textwrap
  9. import os
  10. import subprocess, sys
  11. from threading import Thread
  12. #from getpass import getpass
  13. choices = ('status','start','stop')
  14. parser = argparse.ArgumentParser(description='Simple tool to handle xfreerdp on LCM nodes.', usage='xfreerdp {'+','.join(choices)+'} [options]')
  15. ## positional arguments
  16. parser.add_argument( 'cmd', nargs="?", choices=choices, default='status', help='Specify command to execute, default is \'status\'' )
  17. ## optional arguments
  18. parser.add_argument( '-a', '--all', action='store_true', dest='lcm', help='All LCM nodes are considered' )
  19. parser.add_argument( '-n', nargs='+', dest='node', help='Select one or more nodes (at least one)' )
  20. parser.add_argument( '-1', '--lcm1', action='store_true', dest='lcm1', help='LCM1 nodes are considered' )
  21. parser.add_argument( '-2', '--lcm2', action='store_true', dest='lcm2', help='LCM2 nodes are considered' )
  22. parser.add_argument( '-v', '--version', action='version', version='%(prog)s 1.0 beta', help='Print program version' )
  23. args = parser.parse_args()
  24. def print_progressbar(index, num) :
  25. sys.stdout.write('\r ['
  26. + '='*index
  27. + '>'*(1-int(index/num))
  28. + ' '*(num-index-1) + ']')
  29. sys.stdout.flush()
  30. class Host(Thread):
  31. # Constructor
  32. def __init__(self, name, location):
  33. # Fork the thread first thing
  34. Thread.__init__(self)
  35. # Variables initialization
  36. self.hostname = name
  37. self.location = location
  38. self.running = False
  39. self.up = False
  40. # Run method called on Thread start. Check if host is up and if is running a VM
  41. def run(self) :
  42. if self.isup() :
  43. self.up = True
  44. self.running=self.status()
  45. # Ping the host to see if it's up
  46. def isup(self):
  47. # Is the host up?
  48. ping = os.popen("ping -w1 -c1 " + self.hostname, "r")
  49. # print("pinging " self.hostname)
  50. if "0 received" in ping.read():
  51. return False
  52. else:
  53. return True
  54. def sshcommand(self, command):
  55. if self.up:
  56. ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
  57. return ssh
  58. else:
  59. print self.hostname + ' is not up.'
  60. return False
  61. def status(self):
  62. statuscmd = "ps aux | grep 'sleep 300' | grep -v grep"
  63. ssh = self.sshcommand(statuscmd)
  64. result = [ l for l in ssh.stdout.readlines() if 'sleep 300' in l ]
  65. if result == []:
  66. return False
  67. else:
  68. return True
  69. def xstart(self):
  70. if self.up:
  71. if not self.running:
  72. startcmd = "sleep 300" #'startx /usr/bin/xfreerdp /cert-ignore /f /v:tolab.fisica.unimi.it &> /dev/null &'
  73. self.sshcommand(startcmd)
  74. print 'xfreerdp started on ' + self.hostname
  75. else:
  76. print 'xfreerdp is already running on ' + self.hostname
  77. else:
  78. print self.hostname + ' is not up.'
  79. def xstop(self):
  80. if self.up:
  81. if self.running:
  82. stopcmd = "pkill sleep" #pericoloso (testato e funzionante con pkill xfreerdp)
  83. self.sshcommand(stopcmd)
  84. print 'xfreerdp stopped on ' + self.hostname
  85. else:
  86. print 'xfreerdp is not running on ' + self.hostname
  87. else:
  88. print self.hostname + ' is not up.'
  89. Hosts = [
  90. Host('abe', 'LCM1'),
  91. Host('crash', 'LCM1'),
  92. Host('duke', 'LCM1'),
  93. Host('glados', 'LCM1'),
  94. Host('lara', 'LCM1'),
  95. Host('link', 'LCM1'),
  96. Host('king', 'LCM1'),
  97. Host('pang', 'LCM1'),
  98. Host('pong', 'LCM1'),
  99. Host('snake', 'LCM1'),
  100. Host('sonic', 'LCM1'),
  101. Host('spyro', 'LCM1'),
  102. Host('yoshi', 'LCM1'),
  103. Host('actarus', 'LCM2'),
  104. Host('elwood', 'LCM2'),
  105. Host('gex', 'LCM2'),
  106. Host('gin', 'LCM2'),
  107. Host('jake', 'LCM2'),
  108. Host('kirk', 'LCM2'),
  109. Host('martini', 'LCM2'),
  110. Host('picard', 'LCM2'),
  111. Host('q', 'LCM2'),
  112. Host('raziel', 'LCM2'),
  113. Host('sarek', 'LCM2'),
  114. Host('spock', 'LCM2'),
  115. Host('tron', 'LCM2'),
  116. Host('worf', 'LCM2'),
  117. Host('zombie', 'LCM2')
  118. ]
  119. nodes = []
  120. # Filter hostlist according to arguments
  121. if args.lcm:
  122. nodes = Hosts
  123. elif args.lcm1:
  124. nodes = [ host for host in Hosts if host.location == 'LCM1' ]
  125. elif args.lcm2:
  126. nodes = [ host for host in Hosts if host.location == 'LCM2' ]
  127. elif args.node:
  128. nodes = [ j for j in Hosts if j.hostname in args.node ]
  129. for i in nodes :
  130. i.start()
  131. running=[]
  132. down=[]
  133. num=len(nodes)
  134. index=0
  135. print ' Querying ' + str(num) + ' hosts...'
  136. for i in nodes:
  137. # Rejoin them when their work is done
  138. i.join()
  139. if i.running:
  140. running.append(i.hostname)
  141. if not i.up :
  142. down.append(i.hostname)
  143. index += 1
  144. print_progressbar(index, num)
  145. # New line after progress bar
  146. print '\n Done... (%(t).3f s)' % {'t': (time() - start)}
  147. if args.cmd == 'status':
  148. if len(running):
  149. print "xfreerdp is running on:"
  150. for i in running : print '\t',i
  151. else:
  152. print "xfreerdp is not running"
  153. if len(down):
  154. print "Down nodes:"
  155. for i in down : print '\t',i
  156. elif args.cmd == 'start':
  157. for i in nodes: i.xstart()
  158. elif args.cmd == 'stop':
  159. for i in nodes: i.xstop()