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[time] = bool(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[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. up = len(filter(lambda i: i[1] == True, data.items()))
  79. total = len(data)
  80. uptime = 100.0 * up / total
  81. print "Client: %s" % client
  82. print "Uptime: %3.2f %% " % uptime
  83. print "----------------------------------------------------"
  84.  
  85.  
  86.  
  87. if __name__ == "__main__":
  88.  
  89. parser = OptionParser()
  90. parser.add_option("-c", "--check", dest="check",
  91. help = "Check if clients are up and update database",
  92. action = "store_true", default=False)
  93. parser.add_option("-s", "--stats", dest="stats",
  94. help = "Print stats about collected informations",
  95. action = "store_true", default = False)
  96.  
  97. (options, args) = parser.parse_args()
  98.  
  99. if options.check:
  100. for client in LoadClients():
  101. UpdateClientsData(client, IsAlive(client))
  102.  
  103. if options.stats:
  104. for client in LoadClients():
  105. PrintStats(client)