Написал программку может кому поможет...Да не судите строго...С англиским беда
по этому описание ключей писал на искаверкнутом латинском...
Идея прогрммки в следующем есть лог например сквид, указываем поля которые хотим обработать и получаем конечный результат - >
Работает на мой взглят очень шустро 2 минуты 500mb лога
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include </usr/include/sys/io.h>
#include </usr/include/sys/stat.h>struct real_date
{
char r_mes[10][20];
char r_dey[10][10];
}real_date_info;
char data();
char summa(char *opt_ip, char *opt_name);
char options(char *znach1, char *znach2);
char open_access_file(char *znach1, char *znach2, char *kol_pol, char *vvod_data, char *vvod_ip, char *vvod_bait);
char final_sort_great_file(char *log_date, char *log_ip, char *log_url, char *log_bait);
int main(int argc, char *argv[])
{
data();
if(argv[1]!=0)
{
if(!strcmp(argv[1],"-d"))
{
open_access_file(argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]); //argv[3]-kol-vo strok;argv[4]-date;argv[5]-ip;argv[6]-bait
options(argv[1],argv[2]);
printf("Vse OK\n");
}
if(!strcmp(argv[1],"-m"))
{
open_access_file(argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]);
options(argv[1],argv[2]);
printf("Vse OK\n");
}
if(!strcmp(argv[1],"-a"))
{
printf("Y napisal a kto eche\n");
printf("Mtraf - parser for log SQUID\n");
printf("Send mail: wiza_rd@mail.ru\n");
}
if(!strcmp(argv[1],"--help"))
{
printf("================================================================================================\n");
printf(" -> dey | razdelitel(-p;-t) | kol-vo stolbcov | ( stolbcu ot 0 -> esle data 0 to -1\n");
printf("example -> mtraf -d -p 11 -1 1 2\n");
printf("================================================================================================\n");
printf("-d Enter key to tekchiy day\n");
printf("-m Enter key to month\n");
printf("-p Razdelitel probel\n");
printf("-t Razdelitel tab\n");
printf("-a Kto napisal\n");
printf("Stolbcu ukazuvaqt v sleduqchem porydke ot 0 -> kol-vo stolbcov data,ip,bait pri etom esle data edet 0 um stolbcom to -1\n");
}
}else{
printf("mtraf --help\n");
}
return 0;
}
char open_access_file(char *znach1, char *znach2, char *kol_pol, char *vvod_data, char *vvod_ip, char *vvod_bait)
{
FILE *fp;
char buffer[5000]; //strka iz access.log
char buffer1[5000]; //razbituy na leksemu stroka
char buffer2[50][50]; //stroka & kol-vo simvolov
char datess[50][50];
char new_date[11];
char new_date1[11];
int buflen,x,dates,k,syze_kol,syze_data,syze_ip,syze_bait;
char len;
char *p;
char *p1;
char *g;
char *g1;
char *dte;
time_t now;
dte=(char *)malloc(11);
now = time(NULL);
strncpy(dte,ctime(&now),11);
p1=buffer1;
p=buffer;
buflen=5000;
g=new_date;
g1=new_date1;
if ((fp = fopen("access.log", "r")) == NULL) //otkruvaem config fail
{
printf("Error open file\n");
exit(1);
}
syze_kol=atoi(kol_pol); //poluchaem kol-vo stolbcov
syze_data=atoi(vvod_data); //poluchaem kol-vo stolbcov
syze_ip=atoi(vvod_ip); //poluchaem kol-vo stolbcov
syze_bait=atoi(vvod_bait); //poluchaem kol-vo stolbcov
while(!feof(fp)) //read fail
{
fgets(buffer, buflen, fp); //reaf fail and write "buffer"
len=strlen(buffer); //see size "buffer"
if(!strcmp(znach2,"-p"))
{
p1 = strtok(p," "); //tak kak pbuffer nachinaem razbor strok
}else{
if(!strcmp(znach2,"-t"))
{
p1 = strtok(p,"\t"); //tak kak pbuffer nachinaem razbor strok
}else{
printf("%s\n", "Error key!!!");
exit(0);
}
}
for(x=0;x<=syze_kol;x++) //9 ento kolichestvo stolbcov
{
while (p1 != NULL) //otkruvaem cukl poka p1 ne stanet ravno 0
{
if(!strcmp(znach2,"-p"))
{
p1 = strtok(NULL," ");
}
if(!strcmp(znach2,"-t"))
{
p1 = strtok(NULL,"\t");
}
if(p1 != NULL)
{
strcpy(buffer2[x],p1); //pomechaem leksemu v buffer2
break;
}
}
}
if(!strcmp(znach1,"-d"))
{
if(syze_data == -1)
{
dates=atoi(buffer);
strncpy(new_date,ctime(&dates),11);
if(!strcmp(dte,new_date))
{
final_sort_great_file(new_date,&buffer2[syze_ip-1][0],&buffer2[5][0],&buffer2[syze_bait-1][0]);
}
}else{
dates=atoi(&buffer2[syze_data-1][0]);
strncpy(new_date,ctime(&dates),11);
if(!strcmp(dte,new_date))
{
final_sort_great_file(&buffer2[syze_data-1][0],&buffer2[syze_ip-1][0],&buffer2[5][0],
&buffer2[syze_bait-1][0]);
}
}
}
if(!strcmp(znach1,"-m"))
{
if(syze_data == -1)
{
dates=atoi(buffer);
strncpy(new_date,ctime(&dates),11);
g1 = strtok(g," ");
for(k=0;k<=2;k++)
{
while(g1 != NULL)
{
g1 = strtok(NULL," ");
if(g1 != NULL)
{
strcpy(datess[k],g1);
break;
}
}
}
//printf("%s\n",&datess[0][0]);
if(!strcmp(&datess[0][0],&real_date_info.r_mes[0][0]))
{
final_sort_great_file(&datess[0][0],&buffer2[syze_ip-1][0],&buffer2[5][0],&buffer2[syze_bait-1][0]);
}
}else{
dates=atoi(&buffer2[syze_data-1][0]);
strncpy(new_date,ctime(&dates),11);
g1 = strtok(g," ");
//printf("%s",g1);
for(k=0;k<=2;k++)
{
while(g1 != NULL)
{
g1 = strtok(NULL," ");
if(g1 != NULL)
{
strcpy(datess[k],g1);
// printf("%s",g1);
break;
}
}
}
printf("%s\n",&datess[0][0]);
if(!strcmp(&datess[0][0],&real_date_info.r_mes[0][0]))
{
final_sort_great_file(&datess[0][0],&buffer2[syze_ip-1][0],&buffer2[5][0],&buffer2[syze_bait-1][0]);
}
}
}
}//end while
fclose(fp);
free(dte);//osvobajdaem datu
return;
}
char final_sort_great_file(char *log_date, char *log_ip, char *log_url, char *log_bait)
{
FILE *fpip;
if((fpip=fopen(log_ip,"a+"))==NULL)
{
printf("Error creat ip file\n");
exit(1);
}
fprintf(fpip,"%s %s\n",log_ip,log_bait);
fclose(fpip);
}
//2 Chast programmu
char options(char *znach1, char *znach2)
{
FILE *fpop;
char *opt_ip;
char *opt_name;
if((fpop=fopen("options","r"))==NULL)
{
printf("Error OPTIONS file\n");
exit(1);
}
while(fpop)
{
opt_ip=(char *)malloc(100);
opt_name=(char *)malloc(100);
if(fscanf(fpop,"%s %s\n",opt_ip,opt_name)!=EOF)
{
summa(opt_ip,opt_name);
free(opt_ip);
free(opt_name);
}else{
fclose(fpop);
return;
}
}
}
char summa(char *opt_ip, char *opt_name)
{
FILE *fpip;
FILE *fpit;
char *log_ip;
char *log_bait;
double sum,kon_bait;
sum=0;
if((fpip=fopen(opt_ip,"r"))==NULL)
{
// printf("Error ip file\n");
}
if((fpit=fopen("Itog_day","a+"))==NULL)
{
printf("Error ETOG file\n");
exit(1);
}
while(fpip)
{
log_ip=(char *)malloc(100);
log_bait=(char *)malloc(100);
if(fscanf(fpip,"%s %s",log_ip,log_bait)!=EOF)
{
kon_bait=atof(log_bait);
sum=kon_bait+sum;
free(log_ip);
free(log_bait);
}else{
//printf("%d\n",sum);
fprintf(fpit,"%s %s %f\n",opt_ip,opt_name,sum);
fclose(fpip);
fclose(fpit);
return;
}
}
}
char data()
{
char *dte;
char *dte1;
char dates1[50][50];
int x;
time_t now;
dte=(char *)malloc(11);
dte1=(char *)malloc(11);
now = time(NULL);
strncpy(dte,ctime(&now),11);
dte1 = strtok(dte," ");
for(x=0;x<=2;x++)
{
while(dte1 != NULL)
{
dte1=strtok(NULL," ");
if(dte1 != NULL)
{
strcpy(dates1[x],dte1);
break;
}
}
}
//printf("-%s\n",&dates1[0][0]);
for(x=0;x<=2;x++)
{
strcpy(real_date_info.r_mes[x],&dates1[0][0]);
}
free(dte);
free(dte1);
return;
}
Только не слишком бейте!