#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; static char get_status(int ret, int nbout, int nberr){ if (ret){ return 'E' ; } else if (nberr){ return 'W' ; } return 'O' ; } int main (int argc, char *argv[]) { glocal int ret = -1; glocal SSTRINGS logs ; glocal bool quiet = false ; glocal.ret = (argc,argv); setproginfo ("ninac-last",VERSION ,"Print the last tasks executed by ninac, including those still running."); setarg ('l',"log","Log file to read",glocal.logs,false); setarg ('q',"quiet","Quiet. Do not print header lines.",glocal.quiet,false); glocal vector mp; // Taches finis glocal map notend; // Taches non finis glocal map start; glocal time_t now = time(NULL); // Lit le log pour trouver les taches qui ont terminé if (glocal.logs.size() == 0){ glocal.logs.add(new SSTRING("/var/log/ninac/ninac.log.1")) ; glocal.logs.add(new SSTRING("/var/log/ninac/ninac.log")) ; } glocal.ret = 0 ; for (int i = 0 ; i < glocal.logs.size() ; i++){ const char *log = glocal.logs.getitem(i)->get(); int nblines = (log,true); if (nblines < 0){ glocal.ret-- ; } SSTRINGS words; int n = str_splitline(line,' ',words); if (n >= 8){ const char *date = words.getitem(0)->get(); const char *hour = words.getitem(1)->get(); const char *pid = words.getitem(2)->get(); const char *verb = words.getitem(3)->get(); const char *mode = words.getitem(4)->get(); const char *task = words.getitem(5)->get(); const char *cmd = words.getitem(6)->get(); const char *retc = words.getitem(7)->get(); const char *nbout = (n >= 10 ? words.getitem(8)->get() : NULL); const char *nberr = (n >= 10 ? words.getitem(9)->get() : NULL); struct tm t; t.tm_year = atoi(date)-1900; t.tm_mon = atoi(date+5)-1; t.tm_mday = atoi(date+8); t.tm_hour = atoi(hour); t.tm_min = atoi(hour+3); t.tm_sec = atoi(hour+6); #if 0 printf ("%s %s %04d/%02d/%02d %02d:%02d:%02d\n" ,date,hour,t.tm_year+1900,t.tm_mon+1,t.tm_mday ,t.tm_hour,t.tm_min,t.tm_sec); #endif time_t ti = mktime(&t); if (strcmp(verb,"Exec")==0){ if (strcmp(cmd,"Start")==0){ glocal.start[pid] = ti; // On assume que toutes les taches ne sont pas // terminees int duration = (int)(glocal.now - ti); SSTRING tmp; tmp.setfromf (" - - %c %c %02d:%02d:%02d %s" ,'R', (strcmp(mode, "auto") == 0 ? 'A' : 'M') ,duration/3600,(duration%3600)/60 ,duration%60,task); glocal.notend[pid] = tmp.c_str(); }else if (strcmp(cmd,"End")==0 && strncmp(retc,"ret=",4)==0 && strncmp(nbout,"nbout=",6)==0 && strncmp(nberr,"nberr=",6)==0){ SSTRING dur ; time_t start = glocal.start[pid]; if (start > 0){ int duration = (int)(ti - start); dur.setfromf("%02d:%02d:%02d" ,duration/3600,(duration%3600)/60 ,duration%60); } else { dur.setfromf(" -") ; } SSTRING tmp; tmp.setfromf ("%s %s %c %c %s %s" ,date,hour ,get_status(atoi(retc+4),atoi(nbout+6), atoi(nberr+6)) ,(strcmp(mode, "auto") == 0 ? 'A' : 'M') ,dur.c_str(),task); glocal.mp.push_back(tmp.c_str()); glocal.notend.erase(pid); } } } return 0; } if (glocal.ret == -glocal.logs.size()){ // Si aucun fichier ne peut etre ouvert, on quitte tout de suite. return glocal.ret ; } if (! glocal.quiet){ printf ("End Date End Time S M Duration Task\n"); printf ("---------- -------- - - -------- ------------------------\n"); } { // On presente les processus plus recent en premier map rmap; for (map::iterator it=glocal.notend.begin(); it != glocal.notend.end(); it++){ rmap[it->second] = it->first; } for (map::iterator it=rmap.begin(); it != rmap.end(); it++){ pid_t pid = atoi(it->second.c_str()); printf ("%s (pid:%s)\n" ,it->first.c_str() ,kill(pid,0) == -1 ? "???" : it->second.c_str()); } } sort(glocal.mp.rbegin(),glocal.mp.rend()); for (vector::iterator it=glocal.mp.begin(); it != glocal.mp.end(); it++){ printf ("%s\n",it->c_str()); } return glocal.ret; return glocal.ret; }