-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ip_nginx collecte les IP de tous les fichier présent sous /var/log/ng…
…inx et en extrait les IP
- Loading branch information
1 parent
978cd90
commit f419369
Showing
2 changed files
with
107 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,131 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <dirent.h> | ||
#include <zlib.h> | ||
|
||
#define MAX_LINE_LENGTH 1000 | ||
#define MAX_IP_ADDRESSES 10000 | ||
#define MAX_IP_COUNT 100000 | ||
|
||
struct IP { | ||
char address[20]; | ||
typedef struct { | ||
char ip[16]; | ||
int count; | ||
} IPs[MAX_IP_ADDRESSES]; | ||
|
||
int num_ips = 0; | ||
} ip_entry; | ||
|
||
void process_line(char* line) { | ||
// Extraire l'adresse IP de la ligne | ||
char* token = strtok(line, " "); | ||
char* ip_address = token; | ||
int compare_count(const void *a, const void *b) { | ||
return ((ip_entry *)b)->count - ((ip_entry *)a)->count; | ||
} | ||
|
||
// Rechercher si l'adresse IP existe déjà dans le tableau | ||
int ip_index = -1; | ||
for (int i = 0; i < num_ips; i++) { | ||
if (strcmp(IPs[i].address, ip_address) == 0) { | ||
ip_index = i; | ||
break; | ||
} | ||
int main(int argc, char *argv[]) { | ||
DIR *dir; | ||
struct dirent *ent; | ||
gzFile gzf; | ||
char buffer[1024]; | ||
ip_entry ip_counts[MAX_IP_COUNT]; | ||
int ip_count = 0; | ||
if (argc < 2) { | ||
printf("Usage: %s directory [--html|--text]\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
// Si l'adresse IP n'existe pas, l'ajouter au tableau | ||
if (ip_index == -1) { | ||
strcpy(IPs[num_ips].address, ip_address); | ||
IPs[num_ips].count = 1; | ||
num_ips++; | ||
} | ||
// Sinon, incrémenter le compteur pour cette adresse IP | ||
else { | ||
IPs[ip_index].count++; | ||
int display_html = 0; | ||
int display_text = 0; | ||
if (argc >= 3) { | ||
if (strcmp(argv[2], "--html") == 0) { | ||
display_html = 1; | ||
} else if (strcmp(argv[2], "--text") == 0) { | ||
display_text = 1; | ||
} else { | ||
printf("Invalid option %s\n", argv[2]); | ||
return 1; | ||
} | ||
} | ||
} | ||
// Out pour tests return 1; | ||
|
||
int cmp(const void* a, const void* b) { | ||
struct IP* ip1 = (struct IP*) a; | ||
struct IP* ip2 = (struct IP*) b; | ||
return ip2->count - ip1->count; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
if (argc != 2) { | ||
printf("Utilisation : %s <fichier de log>\n", argv[0]); | ||
// Vérifier que le répertoire est fourni en argument | ||
if (argc < 2) { | ||
printf("Missing directory argument\n"); | ||
return 1; | ||
} | ||
|
||
// Ouvrir le fichier de log | ||
char* log_file = argv[1]; | ||
FILE* fp = fopen(log_file, "r"); | ||
if (fp == NULL) { | ||
printf("Impossible d'ouvrir le fichier %s\n", log_file); | ||
// Ouvrir le répertoire fourni en argument | ||
dir = opendir(argv[1]); | ||
if (!dir) { | ||
printf("Failed to open directory %s\n", argv[1]); | ||
return 1; | ||
} | ||
|
||
// Traiter chaque ligne du fichier | ||
char line[MAX_LINE_LENGTH]; | ||
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) { | ||
process_line(line); | ||
// Parcourir les fichiers du répertoire | ||
while ((ent = readdir(dir)) != NULL) { | ||
char *filename = ent->d_name; | ||
if (strncmp(filename, "access.log.", 11) == 0 && strstr(filename, ".gz")) { | ||
|
||
|
||
|
||
|
||
|
||
// Ouvrir le fichier gz | ||
char path[256]; | ||
sprintf(path, "%s/%s", argv[1], filename); | ||
gzf = gzopen(path, "r"); | ||
if (!gzf) { | ||
printf("Failed to open file %s\n", path); | ||
continue; | ||
} | ||
|
||
// Lire les lignes du fichier gz | ||
while (gzgets(gzf, buffer, sizeof(buffer))) { | ||
char *ip = strtok(buffer, " "); | ||
if (!ip) { | ||
continue; | ||
} | ||
|
||
// Rechercher l'adresse IP dans le tableau | ||
int i; | ||
for (i = 0; i < ip_count; i++) { | ||
if (strcmp(ip_counts[i].ip, ip) == 0) { | ||
// Incrémenter le compteur pour cette adresse IP | ||
ip_counts[i].count++; | ||
break; | ||
} | ||
} | ||
if (i == ip_count) { | ||
// Ajouter une nouvelle entrée pour cette adresse IP | ||
if (ip_count == MAX_IP_COUNT) { | ||
printf("Maximum IP count reached\n"); | ||
break; | ||
} | ||
strcpy(ip_counts[ip_count].ip, ip); | ||
ip_counts[ip_count].count = 1; | ||
ip_count++; | ||
} | ||
} | ||
|
||
// Fermer le fichier gz | ||
gzclose(gzf); | ||
} | ||
} | ||
|
||
// Trier le tableau d'adresses IP par nombre de connexions décroissant | ||
qsort(IPs, num_ips, sizeof(struct IP), cmp); | ||
// Trier le tableau par nombre d'accès en ordre décroissant | ||
qsort(ip_counts, ip_count, sizeof(ip_entry), compare_count); | ||
|
||
// Générer le tableau HTML | ||
// Afficher les résultats en format HTML | ||
if (display_html) { | ||
printf("<table>\n"); | ||
printf("<tr><th>Adresse IP</th><th>Nombre de connexions</th></tr>\n"); | ||
for (int i = 0; i < num_ips; i++) { | ||
printf("<tr><td>%s</td><td>%d</td></tr>\n", IPs[i].address, IPs[i].count); | ||
} | ||
printf("</table>\n"); | ||
fclose(fp); | ||
return 0; | ||
printf("<tr><th>Adresse IP</th><th>Nombre d'accès</th></tr>\n"); | ||
for (int i = 0; i < ip_count; i++) { | ||
|
||
printf("<tr><td>%s</td><td>%d</td></tr>\n", ip_counts[i].ip, ip_counts[i].count); | ||
} | ||
printf("</table>\n"); | ||
} else if(display_text) { | ||
for (int i = 0; i < ip_count; i++) { | ||
printf("%s: %d\n", ip_counts[i].ip, ip_counts[i].count); | ||
} | ||
} else { | ||
printf("Invalid option, use --html or --text\n"); | ||
} | ||
|
||
|
||
return 0; | ||
} |