From 34ad8e64c3542fd16d89665555f93b7b38b9d486 Mon Sep 17 00:00:00 2001 From: Frederic Marchal Date: Sun, 1 Nov 2015 16:23:30 +0100 Subject: [PATCH] Create a user agent report if the input log provides the information An extended log can contain the user agent identification string. Sarg can now use it to generate the user agent report. The new user_agent type is added to report_type to generate a user agent report. For compatibility with previous versions, that report type is automatically selected if at least one file name is provided with the useragent option. --- getconf.c | 1 + include/conf.h | 1 + log.c | 5 ++++- sarg.conf | 17 +++++++++++------ useragent.c | 24 ++++++++++++++---------- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/getconf.c b/getconf.c index ee8bd0e..8c3060a 100644 --- a/getconf.c +++ b/getconf.c @@ -84,6 +84,7 @@ static struct param_list report_type_values[]= {"auth_failures",REPORT_TYPE_AUTH_FAILURES,0}, {"site_user_time_date",REPORT_TYPE_SITE_USER_TIME_DATE,0}, {"downloads",REPORT_TYPE_DOWNLOADS,0}, + {"user_agent",REPORT_TYPE_USERAGENT,0}, }; static struct param_list data_field_values[]= diff --git a/include/conf.h b/include/conf.h index 875804c..c04196f 100644 --- a/include/conf.h +++ b/include/conf.h @@ -197,6 +197,7 @@ Sarg will complain that there are too many days in the files if this limit is ov #define REPORT_TYPE_DENIED 0x0040UL #define REPORT_TYPE_AUTH_FAILURES 0x0080UL #define REPORT_TYPE_DOWNLOADS 0x0100UL +#define REPORT_TYPE_USERAGENT 0x0200UL #define DATA_FIELD_USER 0x0001UL #define DATA_FIELD_DATE 0x0002UL diff --git a/log.c b/log.c index 9d4f114..d117041 100644 --- a/log.c +++ b/log.c @@ -531,8 +531,11 @@ int main(int argc,char *argv[]) if(ReportType == 0) { ReportType=REPORT_TYPE_TOPUSERS | REPORT_TYPE_TOPSITES | REPORT_TYPE_USERS_SITES | REPORT_TYPE_SITES_USERS | REPORT_TYPE_DATE_TIME | REPORT_TYPE_DENIED | - REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | REPORT_TYPE_DOWNLOADS; + REPORT_TYPE_AUTH_FAILURES | REPORT_TYPE_SITE_USER_TIME_DATE | + REPORT_TYPE_DOWNLOADS | REPORT_TYPE_USERAGENT; } + if (!FileList_IsEmpty(UserAgentLog)) + ReportType|=REPORT_TYPE_USERAGENT; if(access(ExcludeUsers, R_OK) == 0) { getuexclude(ExcludeUsers,debug); diff --git a/sarg.conf b/sarg.conf index acb0734..2c6b697 100644 --- a/sarg.conf +++ b/sarg.conf @@ -225,6 +225,9 @@ # # Wildcards are allowed (see access_log). # +# When this option is used the user_agent report is implicitly +# selected in report_type. +# #useragent_log none # TAG: date_format @@ -362,18 +365,20 @@ # TAG: report_type type # What kind of reports to generate. # topusers - users, sites, times, bytes, connects, links to accessed sites, etc -# topsites - site, connect and bytes report -# sites_users - users and sites report -# users_sites - accessed sites by the user report -# date_time - bytes used per day and hour report -# denied - denied sites with full URL report +# topsites - site, connect and bytes report +# sites_users - users and sites report +# users_sites - accessed sites by the user report +# date_time - bytes used per day and hour report +# denied - denied sites with full URL report # auth_failures - autentication failures report # site_user_time_date - sites, dates, times and bytes report # downloads - downloads per user report +# user_agent - user agent identification strings report (this report is always selected +# if at least one file is provided with useragent option) # # Eg.: report_type topsites denied # -#report_type topusers topsites sites_users users_sites date_time denied auth_failures site_user_time_date downloads +#report_type topusers topsites sites_users users_sites date_time denied auth_failures site_user_time_date downloads user_agent # TAG: usertab filename # You can change the "userid" or the "ip address" to be a real user name on the reports. diff --git a/useragent.c b/useragent.c index 53c5f4d..1193420 100644 --- a/useragent.c +++ b/useragent.c @@ -45,19 +45,21 @@ static struct tm UserAgentEndDate; */ FILE *UserAgent_Open(void) { - FILE *fp_ou; + FILE *fp_ou=NULL; if (UserAgentTempLog[0]) { debuga(__FILE__,__LINE__,_("Useragent log already opened\n")); exit(EXIT_FAILURE); } - snprintf(UserAgentTempLog,sizeof(UserAgentTempLog),"%s/squagent.int_unsort",tmp); - if ((fp_ou=fopen(UserAgentTempLog,"w"))==NULL) { - debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),UserAgentTempLog,strerror(errno)); - exit(EXIT_FAILURE); + if ((ReportType & REPORT_TYPE_USERAGENT)!=0) { + snprintf(UserAgentTempLog,sizeof(UserAgentTempLog),"%s/squagent.int_unsort",tmp); + if ((fp_ou=fopen(UserAgentTempLog,"w"))==NULL) { + debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),UserAgentTempLog,strerror(errno)); + exit(EXIT_FAILURE); + } + memset(&UserAgentStartDate,0,sizeof(UserAgentStartDate)); + memset(&UserAgentEndDate,0,sizeof(UserAgentEndDate)); } - memset(&UserAgentStartDate,0,sizeof(UserAgentStartDate)); - memset(&UserAgentEndDate,0,sizeof(UserAgentEndDate)); return(fp_ou); } @@ -71,8 +73,10 @@ FILE *UserAgent_Open(void) */ void UserAgent_Write(FILE *fp,const char *Ip,const char *User,const char *Agent) { - fprintf(fp,"%s\t%s\t%s\n",Ip,Agent,User); - useragent_count++; + if (fp) { + fprintf(fp,"%s\t%s\t%s\n",Ip,Agent,User); + useragent_count++; + } } /*! @@ -209,7 +213,7 @@ void UserAgent(void) double perc; struct getwordstruct gwarea; - if (!UserAgentTempLog[0]) return; + if (!UserAgentTempLog[0] || useragent_count==0) return; snprintf(tmp2,sizeof(tmp2),"%s/squagent.int_log",tmp); if (debug) { -- 2.39.2