Compare commits

3 Commits
main ... main

Author SHA1 Message Date
ef968f8121 maj 2025-09-16 11:09:45 +02:00
5e73e03486 maj 2025-09-16 11:09:06 +02:00
db41c94d58 maj 2025-09-15 17:44:47 +02:00
7 changed files with 136 additions and 100 deletions

33
TP2.txt Normal file
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

BIN
get_ping_from_temp_log.pdf Normal file

Binary file not shown.

BIN
ping-report/ping-report Executable file

Binary file not shown.

BIN
ping-report/ping-report.db Normal file

Binary file not shown.

View File

@@ -22,107 +22,106 @@
Return value :
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(
const char* line,
regex_t* p_reg,
regmatch_t* pmatch,
size_t nmatch
){
int start, end;
size_t size_ping;
char* ping = NULL;
if(regexec(p_reg, line, nmatch, pmatch, 0) != 0){
return NULL; /* no match */
}
/* Extract ping position from line */
start = (int) pmatch[1].rm_so;
end = (int) pmatch[1].rm_eo;
size_ping = (size_t)(end - start);
/* Allocate ping string */
ping = malloc(sizeof(char) * (size_ping + 2));
if(ping == NULL){
return NULL;
}
/* Copy substring */
(void) strncpy(ping, &line[start], size_ping);
ping[size_ping] = '\n';
ping[size_ping+1] = '\0';
return ping;
}
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;
regmatch_t* pmatch;
regex_t* p_reg = NULL;
regmatch_t* pmatch = NULL;
char* ping = NULL;
int start;
int end;
size_t size_ping;
/* regex struct memory allocation */
p_reg = (regex_t *) malloc(sizeof(*p_reg));
/* regex struct allocation */
p_reg = malloc(sizeof(*p_reg));
if(p_reg == NULL){
return ping; /* NULL */
return NULL;
}
/* Open ping log file */
fd = fopen("/var/log/ping-report/last-ping.log","r");
if(fd == NULL){
free(p_reg);
return ping; /* NULL */
return 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 */
if(regcomp(p_reg, "time=(.*) ms", REG_EXTENDED) != 0){
free(p_reg);
fclose(fd);
return 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 */
fclose(fd);
return NULL;
}
/* Read file */
while(getline(&read_line,&n,fd) != -1){
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;
end = (int) pmatch[1].rm_eo;
size_ping = (size_t) (end - start);
/* ping string memory allocation */
ping = malloc(sizeof(char) * (size_ping+2));
if(ping == NULL){
free(read_line);
read_line = NULL;
n = 0;
break;
}
/* Create ping string */
(void) strncpy(ping, &read_line[start], size_ping);
ping[size_ping]='\n';
ping[size_ping+1]='\0';
/* Free memory */
free(read_line);
read_line = NULL;
n = 0;
break;
}
ping = extract_ping_from_line(read_line, p_reg, pmatch, nmatch);
free(read_line);
read_line = NULL;
n = 0;
if(ping != NULL){
break; /* stop when ping found */
}
}
/* free allocated memory */
/* Cleanup */
regfree(p_reg);
free(p_reg);
free(pmatch);
if(read_line != NULL){
free(read_line);
}
fclose(fd);
(void) fclose(fd);
/* ping may be null, then it must mean that the ping request was lost */
return ping;
return ping; /* May be NULL if not found */
}
/*
-- write_ping_log --
Desc :
@@ -171,15 +170,48 @@ void write_ping_log(char* new_ping){
Return value :
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(){
/* Variables */
FILE* fd;
/* Open log file */
fd = fopen("/var/log/ping-report/all-ping.log","r");
FILE* fd = fopen("/var/log/ping-report/all-ping.log","r");
if(fd != NULL){
/* Stats variables */
double ping = 0.0;
double sum = 0.0;
double max = 0.0;
@@ -191,56 +223,27 @@ void set_stats_ping(){
char* read_line = NULL;
size_t n = 0;
/* Read file */
/* Read file line by line */
while(getline(&read_line,&n,fd) != -1){
/* Check getline error */
if(read_line == NULL){
break;
}
/* 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;
}
}
process_ping_line(read_line, &sum, &max, &min, &nb_high, &nb_loss, &nb_ping);
free(read_line);
read_line = NULL;
n = 0;
}
/* Mean calculation */
mean = sum / (double) nb_ping;
(void) fclose(fd);
if(nb_ping > 0){
mean = sum / (double) nb_ping;
}
fclose(fd);
insert_hourly_report(mean,max,min,nb_high,nb_loss,nb_ping);
if(read_line != NULL){
free(read_line);
}
}else{
perror("stats : ");
}

BIN
set_stats_ping.pdf Normal file

Binary file not shown.