From f419369e1cd4bc1af3a8d72b4625d214424cbc5b Mon Sep 17 00:00:00 2001 From: Claude Stabile Date: Thu, 26 Jan 2023 19:20:06 +0100 Subject: [PATCH] =?UTF-8?q?ip=5Fnginx=20collecte=20les=20IP=20de=20tous=20?= =?UTF-8?q?les=20fichier=20pr=C3=A9sent=20sous=20/var/log/nginx=20et=20en?= =?UTF-8?q?=20extrait=20les=20IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ip_nginx | Bin 17224 -> 17304 bytes ip_nginx.c | 162 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 107 insertions(+), 55 deletions(-) diff --git a/ip_nginx b/ip_nginx index f2f27440669460f9c992905a84da6dd51175af49..da882a97caf408cf25d39d7b0d6e1ecb3f9e2408 100755 GIT binary patch literal 17304 zcmeHPeQ;dWb-ycFwlMN)O>B_)upa~!8_-&EWLE~;?8=h-6l74q1~SBXUhh6hTclmF z`_{-F0&-=!*%6x<+L`c2G9{B~;xx=i}ac?tS;&``)`e-PzMsTgOL2jd5}v~C=XH4VlSnUSWEwk=ItT7&6i zaFdi%y{HTtQ@iflOWiU>m>d4a*i`bnn@1#LSf0A5U9mYR~SSgUN)sCvsrW6o%11luQ_DE0VGd!&WV+ z*nha+w9=ygaC9)4HbIlaW`YWdQXyMJ+Db)-4~di+iN)e6kseOP6ILI=iRjR<7|y`h zpmL&*8PS2qjJ`;Gkfc`fF(UXU$RpW4o=&DLC`U&kMqfM;8H^t`rGNM_3@TF9cPJ81 zh+RG1?K_Q4!B!`?DY)4V2wfvp@;{>0uDp%&U()tL+6PM4E~QoR_zJq}^2igGxjv3N zL6!J936Xf-=J`^5%*I1%US@nsby{u6mNU-$#d~a=u3?@d2$^=_bnS7Pap9<-lV)AG zdz{R>aGEbT?N|6R06qpm2VA&&z8QAmJol2>hzpnZQRQpYg}dkH&%1Cco6~U@&M_j{ z^DbQ7ilxa5E_^vV0X^Zu>s_q#55L00*Le6VO~wA-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RESSES 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 \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("\n"); - printf("\n"); -for (int i = 0; i < num_ips; i++) { -printf("\n", IPs[i].address, IPs[i].count); -} -printf("
Adresse IPNombre de connexions
%s%d
\n"); -fclose(fp); -return 0; + printf("Adresse IPNombre d'accès\n"); + for (int i = 0; i < ip_count; i++) { + + printf("%s%d\n", ip_counts[i].ip, ip_counts[i].count); + } + printf("\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; }