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 / 3 / 60 # 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. data = data.items()
  47. data.sort()
  48. return data[-1:][0][1]
  49.  
  50. def LoadClientData(client):
  51. """
  52. Carica i dati relativi a quel determinato client. Questi
  53. sono nella forma di un dizionario con gli oggetti time e
  54. Valori True o False
  55. """
  56. data = {}
  57. try:
  58. f = open(database_directory + client, 'r')
  59. except IOError:
  60. return data
  61. for line in f:
  62. if ":" in line:
  63. (time, alive) = line.split(":")
  64. alive = ("True" in alive)
  65. data[float(time)] = alive
  66. f.close()
  67. return data
  68.  
  69. def DumpClientData(client, data):
  70. """
  71. Salva i dati relativi ad un client, eliminando quelli troppo
  72. vecchi
  73. """
  74. data = dict (filter(lambda i: (i[0] > time.time() - max_time),
  75. data.items()))
  76. f = open(database_directory + client, 'w')
  77. f.write ("\n".join( map(lambda i: ":".join(map(str, i)), data.items())))
  78. f.close()
  79.  
  80. def UpdateClientsData(client, alive):
  81. """
  82. Aggiorna i dati relativi al client inserendo l'informazione
  83. che in questo momento è acceso.
  84. """
  85. data = LoadClientData(client)
  86. data[float(time.time())] = alive
  87. DumpClientData(client, data)
  88.  
  89. def PrintStats(client):
  90. """
  91. Stampa le statistiche sul client prescelto
  92. """
  93. data = LoadClientData(client)
  94. d = data.items()
  95. d.sort()
  96. ss = 0
  97. old_time = d[0][0]
  98.  
  99. # Un piccolo integrale sul tempo della funzione
  100. # up(client)
  101. for (time_entry, up) in d[1:]:
  102. if up:
  103. ss += (time_entry - old_time)
  104. old_time = time_entry
  105. ss = ss / (d[-1:][0][0] - d[0][0])
  106.  
  107. uptime = 100.0 * ss
  108. if IsLastAlive(client):
  109. is_online = "yes"
  110. else:
  111. is_online = "no"
  112. print "Client: %s" % client
  113. print "IsOnline: %s" % is_online
  114. print "Uptime: %3.2f %% " % uptime
  115. print "----------------------------------------------------"
  116.  
  117.  
  118.  
  119. if __name__ == "__main__":
  120.  
  121. parser = OptionParser()
  122. parser.add_option("-c", "--check", dest="check",
  123. help = "Check if clients are up and update database",
  124. action = "store_true", default=False)
  125. parser.add_option("-s", "--stats", dest="stats",
  126. help = "Print stats about collected informations",
  127. action = "store_true", default = False)
  128. parser.add_option("-g", "--group-file", dest="group_file",
  129. help="The dsh group file to use", default='/etc/dsh/group/all')
  130.  
  131. (options, args) = parser.parse_args()
  132. group = os.path.expanduser(options.group_file)
  133.  
  134. # Se non mi chiedi di fare niente non hai capito
  135. # come funziono, molto probabilmente.
  136. if not (options.check or options.stats):
  137. parser.print_help()
  138.  
  139. if options.check:
  140. for client in LoadClients():
  141. UpdateClientsData(client, IsAlive(client))
  142.  
  143. if options.stats:
  144. try:
  145. for client in LoadClients():
  146. PrintStats(client)
  147. except Exception, e:
  148. print "Errore durante l'esecuzione!\n ==> %s" % e