viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Monitora se i computer dell'aula studenti sono accesi
  5. # e restituisce delle statistiche a proposito.
  6.  
  7. import subprocess, time, os
  8. from optparse import OptionParser
  9.  
  10. # Qualche variabile globale
  11. database_directory = '/home/robol/client_stats/' # Il traling slash ci serve
  12. max_time = 86400 * 10 # 10 giorni
  13. group = '/etc/dsh/group/all'
  14.  
  15. def LoadClients():
  16. """
  17. Ritorna una lista con tutti i client all'interno del
  18. gruppo group di dsh. Il default è 'all'
  19. """
  20. global group
  21. clients = []
  22. try:
  23. for client in open(group, 'r'):
  24. client = client.strip()
  25. if client.strip != '' and client[0] != '#':
  26. clients.append (client)
  27. except IOError:
  28. raise IOError('Il gruppo dsh \'%s\' non esiste, nessun client trovato!' %
  29. group)
  30. return clients
  31.  
  32. def IsAlive(client):
  33. """
  34. Ritorna True se il client è acceso e risponde ai ping
  35. """
  36. alive = subprocess.call(["fping", client], stdout = subprocess.PIPE,
  37. stderr = subprocess.PIPE)
  38. return (alive == 0)
  39.  
  40. def IsLastAlive(client):
  41. """
  42. Ritorna True se il client era acceso durante l'ultimo check
  43. effettuato.
  44. """
  45. data = LoadClientData(client)
  46. return data.items()[-1:][0][1]
  47.  
  48. def LoadClientData(client):
  49. """
  50. Carica i dati relativi a quel determinato client. Questi
  51. sono nella forma di un dizionario con gli oggetti time e
  52. Valori True o False
  53. """
  54. data = {}
  55. try:
  56. f = open(database_directory + client, 'r')
  57. except IOError:
  58. return data
  59. for line in f:
  60. if ":" in line:
  61. (time, alive) = line.split(":")
  62. alive = ("True" in alive)
  63. data[float(time)] = alive
  64. f.close()
  65. return data
  66.  
  67. def DumpClientData(client, data):
  68. """
  69. Salva i dati relativi ad un client, eliminando quelli troppo
  70. vecchi
  71. """
  72. data = dict (filter(lambda i: (i[0] > time.time() - max_time),
  73. data.items()))
  74. f = open(database_directory + client, 'w')
  75. f.write ("\n".join( map(lambda i: ":".join(map(str, i)), data.items())))
  76. f.close()
  77.  
  78. def UpdateClientsData(client, alive):
  79. """
  80. Aggiorna i dati relativi al client inserendo l'informazione
  81. che in questo momento è acceso.
  82. """
  83. data = LoadClientData(client)
  84. data[float(time.time())] = alive
  85. DumpClientData(client, data)
  86.  
  87. def PrintStats(client):
  88. """
  89. Stampa le statistiche sul client prescelto
  90. """
  91. data = LoadClientData(client)
  92. d = data.items()
  93. d.sort()
  94. ss = 0
  95. old_time = d[0][0]
  96.  
  97. # Un piccolo integrale sul tempo della funzione
  98. # up(client)
  99. for (time_entry, up) in d[1:]:
  100. if up:
  101. ss += (time_entry - old_time)
  102. old_time = time_entry
  103. ss = ss / (d[-1:][0][0] - d[0][0])
  104.  
  105. uptime = 100.0 * ss
  106. if IsLastAlive(client):
  107. is_online = "yes"
  108. else:
  109. is_online = "no"
  110. print "Client: %s" % client
  111. print "IsOnline: %s" % is_online
  112. print "Uptime: %3.2f %% " % uptime
  113. print "----------------------------------------------------"
  114.  
  115.  
  116.  
  117. if __name__ == "__main__":
  118.  
  119. parser = OptionParser()
  120. parser.add_option("-c", "--check", dest="check",
  121. help = "Check if clients are up and update database",
  122. action = "store_true", default=False)
  123. parser.add_option("-s", "--stats", dest="stats",
  124. help = "Print stats about collected informations",
  125. action = "store_true", default = False)
  126. parser.add_option("-g", "--group-file", dest="group_file",
  127. help="The dsh group file to use", default='/etc/dsh/group/all')
  128.  
  129. (options, args) = parser.parse_args()
  130. group = os.path.expanduser(options.group_file)
  131.  
  132. if options.check:
  133. for client in LoadClients():
  134. UpdateClientsData(client, IsAlive(client))
  135.  
  136. if options.stats:
  137. try:
  138. for client in LoadClients():
  139. PrintStats(client)
  140. except Exception, e:
  141. print "Errore durante l'esecuzione!\n ==> %s" % e