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
  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.  
  14. def LoadClients(group = 'all'):
  15. """
  16. Ritorna una lista con tutti i client all'interno del
  17. gruppo group di dsh. Il default è 'all'
  18. """
  19. clients = []
  20. for client in open('/etc/dsh/group/' + group, 'r'):
  21. client = client.strip()
  22. if client.strip != '' and client[0] != '#':
  23. clients.append (client)
  24. return clients
  25.  
  26. def IsAlive(client):
  27. """
  28. Ritorna True se il client è acceso e risponde ai ping
  29. """
  30. alive = subprocess.call(["fping", client], stdout = subprocess.PIPE,
  31. stderr = subprocess.PIPE)
  32. return (alive == 0)
  33.  
  34. def LoadClientData(client):
  35. """
  36. Carica i dati relativi a quel determinato client. Questi
  37. sono nella forma di un dizionario con gli oggetti time e
  38. Valori True o False
  39. """
  40. data = {}
  41. try:
  42. f = open(database_directory + client, 'r')
  43. except IOError:
  44. return data
  45. for line in f:
  46. if ":" in line:
  47. (time, alive) = line.split(":")
  48. alive = ("True" in alive)
  49. data[float(time)] = alive
  50. f.close()
  51. return data
  52.  
  53. def DumpClientData(client, data):
  54. """
  55. Salva i dati relativi ad un client, eliminando quelli troppo
  56. vecchi
  57. """
  58. data = dict (filter(lambda i: (i[0] > time.time() - max_time),
  59. data.items()))
  60. f = open(database_directory + client, 'w')
  61. f.write ("\n".join( map(lambda i: ":".join(map(str, i)), data.items())))
  62. f.close()
  63.  
  64. def UpdateClientsData(client, alive):
  65. """
  66. Aggiorna i dati relativi al client inserendo l'informazione
  67. che in questo momento è acceso.
  68. """
  69. data = LoadClientData(client)
  70. data[float(time.time())] = alive
  71. DumpClientData(client, data)
  72.  
  73. def PrintStats(client):
  74. """
  75. Stampa le statistiche sul client prescelto
  76. """
  77. data = LoadClientData(client)
  78. d = data.items()
  79. d.sort()
  80. ss = 0
  81. old_time = d[0][0]
  82.  
  83. # Un piccolo integrale sul tempo della funzione
  84. # up(client)
  85. for (time_entry, up) in d[1:]:
  86. if up:
  87. ss += (time_entry - old_time)
  88. old_time = time_entry
  89. ss = ss / (d[-1:][0][0] - d[0][0])
  90.  
  91. uptime = 100.0 * ss
  92. print "Client: %s" % client
  93. print "Uptime: %3.2f %% " % uptime
  94. print "----------------------------------------------------"
  95.  
  96.  
  97.  
  98. if __name__ == "__main__":
  99.  
  100. parser = OptionParser()
  101. parser.add_option("-c", "--check", dest="check",
  102. help = "Check if clients are up and update database",
  103. action = "store_true", default=False)
  104. parser.add_option("-s", "--stats", dest="stats",
  105. help = "Print stats about collected informations",
  106. action = "store_true", default = False)
  107.  
  108. (options, args) = parser.parse_args()
  109.  
  110. if options.check:
  111. for client in LoadClients():
  112. UpdateClientsData(client, IsAlive(client))
  113.  
  114. if options.stats:
  115. for client in LoadClients():
  116. PrintStats(client)