Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9741364afd | |||
| 1935af453c | |||
| 31eb3f1cfb |
+332
@@ -0,0 +1,332 @@
|
|||||||
|
# Damien Riera - Tom Momméja - Gaston Chenet
|
||||||
|
|
||||||
|
## daemon.c
|
||||||
|
|
||||||
|
### create_daemon()
|
||||||
|
|
||||||
|
cette fonction a une complexité de 4
|
||||||
|
|
||||||
|
### ping_request()
|
||||||
|
|
||||||
|
Cette fonction a une complexité de 2
|
||||||
|
|
||||||
|
### send_check()
|
||||||
|
|
||||||
|
Cette fonction a une complexité de 3
|
||||||
|
|
||||||
|
### check_keep_working()
|
||||||
|
|
||||||
|
Cette fonction a une complexité de 4
|
||||||
|
|
||||||
|
### daemon_work()
|
||||||
|
|
||||||
|
Cette fonction a une complexité de 3
|
||||||
|
|
||||||
|
## db-sqlite.c
|
||||||
|
|
||||||
|
### db_connect() - db_disconnect() - insert_hourly_report()
|
||||||
|
|
||||||
|
Toutes ces fonctions ont une complexité de 1
|
||||||
|
|
||||||
|
## ping-report.c
|
||||||
|
|
||||||
|
### main()
|
||||||
|
|
||||||
|
Cette fonction a une complexité de 4
|
||||||
|
|
||||||
|
## stats.c
|
||||||
|
|
||||||
|
### get_ping_from_temp_log()
|
||||||
|
|
||||||
|
Cette fonction a une complexité aux alentours de 15
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### write_ping_log()
|
||||||
|
|
||||||
|
Cette fonction a une complexité de 4
|
||||||
|
|
||||||
|
### set_stats_ping()
|
||||||
|
|
||||||
|
Cette fonction a une complexité de 9
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## utils.c
|
||||||
|
|
||||||
|
### write_pid_file()
|
||||||
|
|
||||||
|
Cette fonction a une commplexité de 2
|
||||||
|
|
||||||
|
### remove_file()
|
||||||
|
|
||||||
|
Cette fonction a une complexité de 1
|
||||||
|
|
||||||
|
# Réduire la complexité de set_stats_ping()
|
||||||
|
|
||||||
|
```c
|
||||||
|
/* 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 */
|
||||||
|
}
|
||||||
|
/* ... */
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
à:
|
||||||
|
|
||||||
|
```c
|
||||||
|
/* Evaluate the ping as a double */
|
||||||
|
ping = strtod(read_line,NULL);
|
||||||
|
/* Check if the ping is flagged as LOSS */
|
||||||
|
if(strcmp(read_line,"LOSS") == 0 && ping < 0.1){
|
||||||
|
nb_loss++;
|
||||||
|
}
|
||||||
|
/* ... */
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
# Réduire la complexité de get_ping_from_temp_log()
|
||||||
|
|
||||||
|
```c
|
||||||
|
/*
|
||||||
|
-- get_ping_from_temp_log --
|
||||||
|
Desc :
|
||||||
|
Function which get the ping from a temp log containing the last ping did by the program
|
||||||
|
In-param :
|
||||||
|
None
|
||||||
|
Out-param :
|
||||||
|
None
|
||||||
|
Return value :
|
||||||
|
Ping value as a string or NULL if an error occured
|
||||||
|
*/
|
||||||
|
/*@null@*/char* get_ping_from_temp_log(){
|
||||||
|
|
||||||
|
/* Variables */
|
||||||
|
FILE* fd = NULL;
|
||||||
|
char* read_line = NULL;
|
||||||
|
size_t n = 0;
|
||||||
|
size_t nmatch = 2;
|
||||||
|
regex_t *p_reg;
|
||||||
|
regmatch_t* pmatch;
|
||||||
|
char* ping = NULL;
|
||||||
|
int start;
|
||||||
|
int end;
|
||||||
|
size_t size_ping;
|
||||||
|
|
||||||
|
/* regex struct memory allocation */
|
||||||
|
p_reg = (regex_t *) malloc(sizeof(*p_reg));
|
||||||
|
if(p_reg == NULL){
|
||||||
|
return ping; /* NULL */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open ping log file */
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(read_line);
|
||||||
|
read_line = NULL;
|
||||||
|
n = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* free allocated memory */
|
||||||
|
regfree(p_reg);
|
||||||
|
free(p_reg);
|
||||||
|
free(pmatch);
|
||||||
|
if(read_line != NULL){
|
||||||
|
free(read_line);
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) fclose(fd);
|
||||||
|
|
||||||
|
/* ping may be null, then it must mean that the ping request was lost */
|
||||||
|
return ping;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
à:
|
||||||
|
|
||||||
|
```c
|
||||||
|
char* prepare(regex_t *p_reg, FILE* fd, regmatch_t* pmatch, char* ping, size_t nmatch){
|
||||||
|
/* regex struct memory allocation */
|
||||||
|
p_reg = (regex_t *) malloc(sizeof(*p_reg));
|
||||||
|
if(p_reg == NULL){
|
||||||
|
return ping; /* NULL */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open ping log file */
|
||||||
|
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 */
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL; /* success */
|
||||||
|
}
|
||||||
|
|
||||||
|
int loop_line(char* read_line)
|
||||||
|
{
|
||||||
|
int start;
|
||||||
|
int end;
|
||||||
|
size_t size_ping;
|
||||||
|
|
||||||
|
if(read_line == NULL){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
return 0; /* success */
|
||||||
|
}
|
||||||
|
|
||||||
|
free(read_line);
|
||||||
|
read_line = NULL;
|
||||||
|
n = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* get_ping_from_temp_log(){
|
||||||
|
/* Variables */
|
||||||
|
FILE* fd = NULL;
|
||||||
|
char* read_line = NULL;
|
||||||
|
size_t n = 0;
|
||||||
|
size_t nmatch = 2;
|
||||||
|
regex_t *p_reg;
|
||||||
|
regmatch_t* pmatch;
|
||||||
|
char* ping = NULL;
|
||||||
|
|
||||||
|
if(prepare(p_reg, fd, pmatch, ping, nmatch) != NULL){
|
||||||
|
return ping; /* NULL */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read file */
|
||||||
|
while(getline(&read_line,&n,fd) != -1){
|
||||||
|
if (loop_line(read_line) != 1){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* free allocated memory */
|
||||||
|
regfree(p_reg);
|
||||||
|
free(p_reg);
|
||||||
|
free(pmatch);
|
||||||
|
if(read_line != NULL){
|
||||||
|
free(read_line);
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) fclose(fd);
|
||||||
|
|
||||||
|
/* ping may be null, then it must mean that the ping request was lost */
|
||||||
|
return ping;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
On répartie la fonction en 3 fonctions pour réduire la complexité.
|
||||||
|
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
+74
-76
@@ -11,31 +11,7 @@
|
|||||||
#include "../include/stats.h"
|
#include "../include/stats.h"
|
||||||
#include "../include/db-sqlite.h"
|
#include "../include/db-sqlite.h"
|
||||||
|
|
||||||
/*
|
char* prepare(regex_t *p_reg, FILE* fd, regmatch_t* pmatch, char* ping, size_t nmatch){
|
||||||
-- get_ping_from_temp_log --
|
|
||||||
Desc :
|
|
||||||
Function which get the ping from a temp log containing the last ping did by the program
|
|
||||||
In-param :
|
|
||||||
None
|
|
||||||
Out-param :
|
|
||||||
None
|
|
||||||
Return value :
|
|
||||||
Ping value as a string or NULL if an error occured
|
|
||||||
*/
|
|
||||||
/*@null@*/char* get_ping_from_temp_log(){
|
|
||||||
|
|
||||||
/* Variables */
|
|
||||||
FILE* fd = NULL;
|
|
||||||
char* read_line = NULL;
|
|
||||||
size_t n = 0;
|
|
||||||
size_t nmatch = 2;
|
|
||||||
regex_t *p_reg;
|
|
||||||
regmatch_t* pmatch;
|
|
||||||
char* ping = NULL;
|
|
||||||
int start;
|
|
||||||
int end;
|
|
||||||
size_t size_ping;
|
|
||||||
|
|
||||||
/* regex struct memory allocation */
|
/* regex struct memory allocation */
|
||||||
p_reg = (regex_t *) malloc(sizeof(*p_reg));
|
p_reg = (regex_t *) malloc(sizeof(*p_reg));
|
||||||
if(p_reg == NULL){
|
if(p_reg == NULL){
|
||||||
@@ -67,46 +43,74 @@
|
|||||||
return ping; /* NULL */
|
return ping; /* NULL */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read file */
|
return NULL; /* success */
|
||||||
while(getline(&read_line,&n,fd) != -1){
|
}
|
||||||
|
|
||||||
if(read_line == NULL){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Exec regex to find ping */
|
int loop_line(char* read_line)
|
||||||
|
{
|
||||||
|
int start;
|
||||||
|
int end;
|
||||||
|
size_t size_ping;
|
||||||
|
|
||||||
if(regexec(p_reg,read_line,nmatch,pmatch,0) == 0){
|
if(read_line == NULL){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Extract ping position from read line */
|
/* Exec regex to find ping */
|
||||||
start = (int) pmatch[1].rm_so;
|
|
||||||
end = (int) pmatch[1].rm_eo;
|
|
||||||
size_ping = (size_t) (end - start);
|
|
||||||
|
|
||||||
/* ping string memory allocation */
|
if(regexec(p_reg,read_line,nmatch,pmatch,0) == 0){
|
||||||
ping = malloc(sizeof(char) * (size_ping+2));
|
|
||||||
if(ping == NULL){
|
|
||||||
free(read_line);
|
|
||||||
read_line = NULL;
|
|
||||||
n = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create ping string */
|
/* Extract ping position from read line */
|
||||||
(void) strncpy(ping, &read_line[start], size_ping);
|
start = (int) pmatch[1].rm_so;
|
||||||
ping[size_ping]='\n';
|
end = (int) pmatch[1].rm_eo;
|
||||||
ping[size_ping+1]='\0';
|
size_ping = (size_t) (end - start);
|
||||||
|
|
||||||
/* Free memory */
|
/* ping string memory allocation */
|
||||||
|
ping = malloc(sizeof(char) * (size_ping+2));
|
||||||
|
if(ping == NULL){
|
||||||
free(read_line);
|
free(read_line);
|
||||||
read_line = NULL;
|
read_line = NULL;
|
||||||
n = 0;
|
n = 0;
|
||||||
break;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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);
|
free(read_line);
|
||||||
read_line = NULL;
|
read_line = NULL;
|
||||||
n = 0;
|
n = 0;
|
||||||
|
return 0; /* success */
|
||||||
|
}
|
||||||
|
|
||||||
|
free(read_line);
|
||||||
|
read_line = NULL;
|
||||||
|
n = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* get_ping_from_temp_log(){
|
||||||
|
/* Variables */
|
||||||
|
FILE* fd = NULL;
|
||||||
|
char* read_line = NULL;
|
||||||
|
size_t n = 0;
|
||||||
|
size_t nmatch = 2;
|
||||||
|
regex_t *p_reg;
|
||||||
|
regmatch_t* pmatch;
|
||||||
|
char* ping = NULL;
|
||||||
|
|
||||||
|
if(prepare(p_reg, fd, pmatch, ping, nmatch) != NULL){
|
||||||
|
return ping; /* NULL */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read file */
|
||||||
|
while(getline(&read_line,&n,fd) != -1){
|
||||||
|
if (loop_line(read_line) != 1){
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free allocated memory */
|
/* free allocated memory */
|
||||||
@@ -198,35 +202,29 @@ void set_stats_ping(){
|
|||||||
if(read_line == NULL){
|
if(read_line == NULL){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
/* Evaluate the ping as a double */
|
||||||
|
ping = strtod(read_line,NULL);
|
||||||
/* Check if the ping is flagged as LOSS */
|
/* Check if the ping is flagged as LOSS */
|
||||||
if(strcmp(read_line,"LOSS") == 0){
|
if(strcmp(read_line,"LOSS") == 0 && ping < 0.1){
|
||||||
nb_loss++;
|
nb_loss++;
|
||||||
}else{
|
}else{
|
||||||
/* Evaluate the ping as a double */
|
/* Number of ping readed (for mean calculation) */
|
||||||
ping = strtod(read_line,NULL);
|
nb_ping++;
|
||||||
/* Test null ping */
|
/* Max ping */
|
||||||
if(ping < 0.1){
|
if(ping > max){
|
||||||
/* Ignore null ping */
|
max = 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;
|
|
||||||
}
|
}
|
||||||
}
|
/* 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);
|
||||||
n = 0;
|
n = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user