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