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]
#!/usr/bin/python # -*- coding: utf-8 -*- # # Monitora se i computer dell'aula studenti sono accesi # e restituisce delle statistiche a proposito. import subprocess, time, os from optparse import OptionParser # Qualche variabile globale database_directory = '/home/robol/client_stats/' # Il traling slash ci serve max_time = 86400 * 10 # 10 giorni group = '/etc/dsh/group/all' def LoadClients(): """ Ritorna una lista con tutti i client all'interno del gruppo group di dsh. Il default è 'all' """ global group clients = [] try: for client in open(group, 'r'): client = client.strip() if client.strip != '' and client[0] != '#': clients.append (client) except IOError: raise IOError('Il gruppo dsh \'%s\' non esiste, nessun client trovato!' % group) return clients def IsAlive(client): """ Ritorna True se il client è acceso e risponde ai ping """ alive = subprocess.call(["fping", client], stdout = subprocess.PIPE, stderr = subprocess.PIPE) return (alive == 0) def LoadClientData(client): """ Carica i dati relativi a quel determinato client. Questi sono nella forma di un dizionario con gli oggetti time e Valori True o False """ data = {} try: f = open(database_directory + client, 'r') except IOError: return data for line in f: if ":" in line: (time, alive) = line.split(":") alive = ("True" in alive) data[float(time)] = alive f.close() return data def DumpClientData(client, data): """ Salva i dati relativi ad un client, eliminando quelli troppo vecchi """ data = dict (filter(lambda i: (i[0] > time.time() - max_time), data.items())) f = open(database_directory + client, 'w') f.write ("\n".join( map(lambda i: ":".join(map(str, i)), data.items()))) f.close() def UpdateClientsData(client, alive): """ Aggiorna i dati relativi al client inserendo l'informazione che in questo momento è acceso. """ data = LoadClientData(client) data[float(time.time())] = alive DumpClientData(client, data) def PrintStats(client): """ Stampa le statistiche sul client prescelto """ data = LoadClientData(client) d = data.items() d.sort() ss = 0 old_time = d[0][0] # Un piccolo integrale sul tempo della funzione # up(client) for (time_entry, up) in d[1:]: if up: ss += (time_entry - old_time) old_time = time_entry ss = ss / (d[-1:][0][0] - d[0][0]) uptime = 100.0 * ss if IsAlive(client): is_online = "no" else: is_online = "yes" print "Client: %s" % client print "IsOnline: %s" % is_online print "Uptime: %3.2f %% " % uptime print "----------------------------------------------------" if __name__ == "__main__": parser = OptionParser() parser.add_option("-c", "--check", dest="check", help = "Check if clients are up and update database", action = "store_true", default=False) parser.add_option("-s", "--stats", dest="stats", help = "Print stats about collected informations", action = "store_true", default = False) parser.add_option("-g", "--group-file", dest="group_file", help="The dsh group file to use", default='/etc/dsh/group/all') (options, args) = parser.parse_args() group = os.path.expand_user(options.group_file) if options.check: for client in LoadClients(): UpdateClientsData(client, IsAlive(client)) if options.stats: try: for client in LoadClients(): PrintStats(client) except Exception, e: print "Errore durante l'esecuzione!\n ==> %s" % e