Actualiser ping-report/src/stats.c
This commit is contained in:
parent
301e4d33c2
commit
bded7b49eb
@ -23,103 +23,69 @@
|
|||||||
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() {
|
/*@null@*/char* get_ping_from_temp_log() {
|
||||||
|
|
||||||
/* Variables */
|
|
||||||
FILE* fd = NULL;
|
FILE* fd = NULL;
|
||||||
char* read_line = NULL;
|
char* read_line = NULL;
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
size_t nmatch = 2;
|
size_t nmatch = 2;
|
||||||
regex_t *p_reg;
|
regex_t* p_reg = malloc(sizeof(*p_reg));
|
||||||
regmatch_t* pmatch;
|
regmatch_t* pmatch = NULL;
|
||||||
char* ping = NULL;
|
char* ping = NULL;
|
||||||
int start;
|
|
||||||
int end;
|
|
||||||
size_t size_ping;
|
|
||||||
|
|
||||||
/* regex struct memory allocation */
|
if (!p_reg) return NULL;
|
||||||
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");
|
fd = fopen("/var/log/ping-report/last-ping.log", "r");
|
||||||
if(fd == NULL){
|
if (!fd) {
|
||||||
free(p_reg);
|
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 (regcomp(p_reg, "time=(.*) ms", REG_EXTENDED) != 0) {
|
||||||
if(p_reg != NULL){
|
cleanup(fd, p_reg, NULL, NULL);
|
||||||
free(p_reg);
|
return NULL;
|
||||||
}
|
|
||||||
(void) fclose(fd);
|
|
||||||
return ping; /* NULL */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* match info memory allocation */
|
|
||||||
pmatch = malloc(sizeof(*pmatch) * nmatch);
|
pmatch = malloc(sizeof(*pmatch) * nmatch);
|
||||||
if(pmatch == NULL){
|
if (!pmatch) {
|
||||||
(void) fclose(fd);
|
cleanup(fd, p_reg, NULL, NULL);
|
||||||
regfree(p_reg);
|
return NULL;
|
||||||
free(p_reg);
|
|
||||||
return ping; /* NULL */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read file */
|
|
||||||
while (getline(&read_line, &n, fd) != -1) {
|
while (getline(&read_line, &n, fd) != -1) {
|
||||||
|
if (regexec(p_reg, read_line, nmatch, pmatch, 0) == 0) {
|
||||||
if(read_line == NULL){
|
ping = extract_ping(read_line, pmatch);
|
||||||
|
free(read_line);
|
||||||
break;
|
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);
|
free(read_line);
|
||||||
read_line = NULL;
|
read_line = NULL;
|
||||||
n = 0;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create ping string */
|
cleanup(fd, p_reg, pmatch, read_line);
|
||||||
(void) strncpy(ping, &read_line[start], size_ping);
|
return ping;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup(FILE* fd, regex_t* p_reg, regmatch_t* pmatch, char* read_line) {
|
||||||
|
if (fd) fclose(fd);
|
||||||
|
if (p_reg) {
|
||||||
|
regfree(p_reg);
|
||||||
|
free(p_reg);
|
||||||
|
}
|
||||||
|
if (pmatch) free(pmatch);
|
||||||
|
if (read_line) free(read_line);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* extract_ping(char* read_line, regmatch_t* pmatch) {
|
||||||
|
int start = (int)pmatch[1].rm_so;
|
||||||
|
int end = (int)pmatch[1].rm_eo;
|
||||||
|
size_t size_ping = (size_t)(end - start);
|
||||||
|
char* ping = malloc(sizeof(char) * (size_ping + 2));
|
||||||
|
|
||||||
|
if (ping) {
|
||||||
|
strncpy(ping, &read_line[start], size_ping);
|
||||||
ping[size_ping] = '\n';
|
ping[size_ping] = '\n';
|
||||||
ping[size_ping + 1] = '\0';
|
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;
|
return ping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user