3 Commits

Author SHA1 Message Date
David AKAGUNDUZ ef968f8121 maj 2025-09-16 11:09:45 +02:00
David AKAGUNDUZ 5e73e03486 maj 2025-09-16 11:09:06 +02:00
David AKAGUNDUZ db41c94d58 maj 2025-09-15 17:44:47 +02:00
6 changed files with 136 additions and 100 deletions
+33
View File
@@ -0,0 +1,33 @@
TP2 David AKAGUNDUZ Bamba TOP
Nous allons analyser lesfichier et trouver leur nombre de compléxité cyclonique :
Fichier daemon.c :
Fonction create_deamon : 4
Fonction ping_request : 2
Fonction send_check : 4
Fonction check_keep_working : 4
Fonction daemon_work : 3
Fichier db-sqlite.c :
Fonction db_connect : 1
Fonction db_disconnect : 1
Fonction insert_hourly_report : 1
Fichier ping-report.c :
Focntion main : 4
Fichier stats.c :
Fonction get_ping_from_temp_log : 15
Fonction write_ping_log : 5
Fonction set_stats_ping : 17
Fichier utils.c :
Fonction write_pid_file : 2
Fonction remove_file : 1
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
+111 -108
View File
@@ -22,107 +22,106 @@
Return value : Return value :
Ping value as a string or NULL if an error occured Ping value as a string or NULL if an error occured
*/ */
/*@null@*/char* get_ping_from_temp_log(){
/* Variables */ static char* extract_ping_from_line(
FILE* fd = NULL; const char* line,
char* read_line = NULL; regex_t* p_reg,
size_t n = 0; regmatch_t* pmatch,
size_t nmatch = 2; size_t nmatch
regex_t *p_reg; ){
regmatch_t* pmatch; int start, end;
char* ping = NULL;
int start;
int end;
size_t size_ping; size_t size_ping;
char* ping = NULL;
/* regex struct memory allocation */ if(regexec(p_reg, line, nmatch, pmatch, 0) != 0){
p_reg = (regex_t *) malloc(sizeof(*p_reg)); return NULL; /* no match */
if(p_reg == NULL){
return ping; /* NULL */
} }
/* Open ping log file */ /* Extract ping position from line */
fd = fopen("/var/log/ping-report/last-ping.log","r");
if(fd == NULL){
free(p_reg);
return ping; /* NULL */
}
/* Construct regex to get ping from log file */
if(regcomp(p_reg,"time=(.*) ms",REG_EXTENDED) != 0){
if(p_reg != NULL){
free(p_reg);
}
(void) fclose(fd);
return ping; /* NULL */
}
/* match info memory allocation */
pmatch = malloc(sizeof(*pmatch) * nmatch);
if(pmatch == NULL){
(void) fclose(fd);
regfree(p_reg);
free(p_reg);
return ping; /* NULL */
}
/* Read file */
while(getline(&read_line,&n,fd) != -1){
if(read_line == NULL){
break;
}
/* Exec regex to find ping */
if(regexec(p_reg,read_line,nmatch,pmatch,0) == 0){
/* Extract ping position from read line */
start = (int) pmatch[1].rm_so; start = (int) pmatch[1].rm_so;
end = (int) pmatch[1].rm_eo; end = (int) pmatch[1].rm_eo;
size_ping = (size_t)(end - start); size_ping = (size_t)(end - start);
/* ping string memory allocation */ /* Allocate ping string */
ping = malloc(sizeof(char) * (size_ping + 2)); ping = malloc(sizeof(char) * (size_ping + 2));
if(ping == NULL){ if(ping == NULL){
free(read_line); return NULL;
read_line = NULL;
n = 0;
break;
} }
/* Create ping string */ /* Copy substring */
(void) strncpy(ping, &read_line[start], size_ping); (void) strncpy(ping, &line[start], size_ping);
ping[size_ping] = '\n'; ping[size_ping] = '\n';
ping[size_ping+1] = '\0'; ping[size_ping+1] = '\0';
/* Free memory */ return ping;
free(read_line); }
read_line = NULL;
n = 0;
char* get_ping_from_temp_log(){
FILE* fd = NULL;
char* read_line = NULL;
size_t n = 0;
size_t nmatch = 2;
regex_t* p_reg = NULL;
regmatch_t* pmatch = NULL;
char* ping = NULL;
/* regex struct allocation */
p_reg = malloc(sizeof(*p_reg));
if(p_reg == NULL){
return NULL;
}
fd = fopen("/var/log/ping-report/last-ping.log","r");
if(fd == NULL){
free(p_reg);
return NULL;
}
if(regcomp(p_reg, "time=(.*) ms", REG_EXTENDED) != 0){
free(p_reg);
fclose(fd);
return NULL;
}
pmatch = malloc(sizeof(*pmatch) * nmatch);
if(pmatch == NULL){
regfree(p_reg);
free(p_reg);
fclose(fd);
return NULL;
}
while(getline(&read_line, &n, fd) != -1){
if(read_line == NULL){
break; break;
} }
ping = extract_ping_from_line(read_line, p_reg, pmatch, nmatch);
free(read_line); free(read_line);
read_line = NULL; read_line = NULL;
n = 0; n = 0;
if(ping != NULL){
break; /* stop when ping found */
}
} }
/* free allocated memory */ /* Cleanup */
regfree(p_reg); regfree(p_reg);
free(p_reg); free(p_reg);
free(pmatch); free(pmatch);
if(read_line != NULL){ if(read_line != NULL){
free(read_line); free(read_line);
} }
fclose(fd);
(void) fclose(fd); return ping; /* May be NULL if not found */
/* ping may be null, then it must mean that the ping request was lost */
return ping;
} }
/* /*
-- write_ping_log -- -- write_ping_log --
Desc : Desc :
@@ -171,15 +170,48 @@ void write_ping_log(char* new_ping){
Return value : Return value :
None None
*/ */
static void process_ping_line(
const char* line,
double* sum,
double* max,
double* min,
int* nb_high,
int* nb_loss,
int* nb_ping
){
double ping;
if(strcmp(line,"LOSS") == 0){
(*nb_loss)++;
return;
}
/* Evaluate the ping as a double */
ping = strtod(line,NULL);
if(ping < 0.1){
return; /* Ignore null ping */
}
(*nb_ping)++;
if(ping > *max){
*max = ping;
}
if(ping < *min){
*min = ping;
}
if(ping > 100.0){
(*nb_high)++;
}
*sum += ping;
}
void set_stats_ping(){ void set_stats_ping(){
/* Variables */ FILE* fd = fopen("/var/log/ping-report/all-ping.log","r");
FILE* fd;
/* Open log file */
fd = fopen("/var/log/ping-report/all-ping.log","r");
if(fd != NULL){ if(fd != NULL){
/* Stats variables */
double ping = 0.0; double ping = 0.0;
double sum = 0.0; double sum = 0.0;
double max = 0.0; double max = 0.0;
@@ -191,56 +223,27 @@ void set_stats_ping(){
char* read_line = NULL; char* read_line = NULL;
size_t n = 0; size_t n = 0;
/* Read file */ /* Read file line by line */
while(getline(&read_line,&n,fd) != -1){ while(getline(&read_line,&n,fd) != -1){
/* Check getline error */
if(read_line == NULL){ if(read_line == NULL){
break; break;
} }
process_ping_line(read_line, &sum, &max, &min, &nb_high, &nb_loss, &nb_ping);
/* Check if the ping is flagged as LOSS */
if(strcmp(read_line,"LOSS") == 0){
nb_loss++;
}else{
/* Evaluate the ping as a double */
ping = strtod(read_line,NULL);
/* Test null ping */
if(ping < 0.1){
/* Ignore null ping */
}else{
/* Number of ping readed (for mean calculation) */
nb_ping++;
/* Max ping */
if(ping > max){
max = ping;
}
/* Min ping */
if(ping < min){
min = ping;
}
/* Number of ping above 100 ms */
if(ping > 100.0){
nb_high++;
}
/* Sum (for mean calculation) */
sum += ping;
}
}
free(read_line); free(read_line);
read_line = NULL;
n = 0; n = 0;
} }
/* Mean calculation */ if(nb_ping > 0){
mean = sum / (double) nb_ping; mean = sum / (double) nb_ping;
(void) fclose(fd); }
fclose(fd);
insert_hourly_report(mean,max,min,nb_high,nb_loss,nb_ping); insert_hourly_report(mean,max,min,nb_high,nb_loss,nb_ping);
if(read_line != NULL){ if(read_line != NULL){
free(read_line); free(read_line);
} }
}else{ }else{
perror("stats : "); perror("stats : ");
} }
Binary file not shown.