Auto create daily, weekly, Monthly report in Sarg with bash in freebsd

Dear Everyone,
Could everyone can help check and edit my script run daily sarg report,
Bash:
#!/usr/local/bin/bash
today = $(date -v -1d "+%d/%m/%Y")
export LC_ALL = C
/usr/local/bin/sarg -o /usr/local/www/apache24/data/daily -d $today>/dev/null 2> 1
exit 0

but when I run script by command
Untitled.png


pls help to correct and how about weekly and monthly report.

thank you
 
You don't need to use bash(1) for this. It's such a simple script, it'll work fine with sh(1).

sarg(1):
Code:
  -d date
	   Use date to restrict	the report to some date	range during log file
	   processing. Format for date is dd/mm/yyyy-dd/mm/yyyy	or a single
	   date	dd/mm/yyyy. Date ranges	can also be specified as day-n,
	   week-n, or month-n where n is the number of days, weeks or months
	   to jump backward. Note that there is	no spaces around the hyphen.

Code:
today = $(date -v -1d "+%d/%m/%Y") 
export LC_ALL = C
These are syntax errors.
 
I did something similar over ten years ago.

For monthly report I collected squid's log for the whole month.
I used crontab for squid's log rotate.
Also I generated another reports for few recent days.
My own configs and scripts here. Tune it by yourself if you need it.

Be careful! These scripts will delete data directories like /usr/local/www/data/stat/ /usr/local/www/data/stat-m/

/etc/crontab
Code:
#minute hour    mday    month   wday    who     command
1       0       1       *       *       root    /usr/local/sbin/squid -k rotate
0       5       *       *       *       admin   /usr/local/etc/sarg_stat
0       1       1       *       *       admin   /usr/local/etc/sarg_stat_m

Report for the previous month:
/usr/local/etc/sarg_stat_m
Code:
#!/bin/sh
log0_name='/usr/local/squid/logs/access.log.0'
rm -rf /usr/local/www/data/stat-m/* >/dev/null
/usr/local/bin/sarg -l ${log0_name} -o /usr/local/www/data/stat-m/ -w /tmp >/dev/null

Reports for current month, and recent two days:
/usr/local/etc/sarg_stat
Code:
#!/bin/sh
day1=`/bin/date "+01/%m/%Y"`
yesterday=`/bin/date -v-1d "+%d/%m/%Y"`
beforeyesterday=`/bin/date -v-2d "+%d/%m/%Y"`
today=`/bin/date "+%d/%m/%Y"`
logfile='/usr/local/squid/logs/access.log'

rm -rf /usr/local/www/data/stat/* >/dev/null
/usr/local/bin/sarg -l ${logfile} -o /usr/local/www/data/stat/ -d ${day1}-${yesterday} -w /usr/local/www/data/stat/tmp >/dev/null
##day_before_yesterday
/usr/local/bin/sarg -l ${logfile} -o /usr/local/www/data/stat/ -d ${beforeyesterday}-${beforeyesterday} -w /usr/local/www/data/stat/tmp >/dev/null
##yesterday
/usr/local/bin/sarg -l ${logfile} -o /usr/local/www/data/stat/ -d ${yesterday}-${yesterday} -w /usr/local/www/data/stat/tmp >/dev/null
 
2.png


I rewrite it to be above is running ok.

When I do with weekly with code below
Bash:
#!/usr/local/bin/bash
#Generate Access.log for correct weekly reports
cat /var/log/squid/access.log /var/log/squid/access.log> /var/log/squid/access.log.week
#Get yesterday date
yesterday="$(date -v -1d  '+%d/%m/%Y')"
#Get one week ago date
weekago="$(date -v -7d  '+%d/%m/%Y')"
/usr/local/bin/sarg -l /var/log/squid/access.log.week -o /usr/local/www/apache24/data/weekly -d $weekago-$yesterday>/dev/null 2>&1
/usr/local/sbin/squid -k rotate
exit 0

Error Ambiguous output redirect, I don't know wher is error.

Could everyone can help me .
 
Try to increase the verbose level of bash.
Just add "-v" to the end of the first line, and run the script again.
Code:
#!/usr/local/bin/bash -v

In my opinion. I prefer to use #!/bin/sh for FreeBSD scripting.
Using the bash gives no benefits for simple scripts like yours
 
Try to increase the verbose level of bash.
Just add "-v" to the end of the first line, and run the script again.
Code:
#!/usr/local/bin/bash -v

In my opinion. I prefer to use #!/bin/sh for FreeBSD scripting.
Using the bash gives no benefits for simple scripts like yours
Thank you so much for your comment
I changed as below
Bash:
#!/usr/local/bin/sh -v
#Generate Access.log for correct weekly reports
cat /var/log/squid/access.log /var/log/squid/access.log> /var/log/squid/access.log.week
#Get yesterday date
yesterday="$(date -v -1d  '+%d/%m/%Y')"
#Get one week ago date
weekago="$(date -v -7d  '+%d/%m/%Y')"
/usr/local/bin/sarg -l /var/log/squid/access.log.week -o /usr/local/www/apache24/data/weekly -d $weekago-$yesterday>/dev/null 2>&1
/usr/local/sbin/squid -k rotate
exit 0

It worked. I will create monthly report, if have any problem everyone pls help.
Thank you
 
Bash:
cat /var/log/squid/access.log /var/log/squid/access.log> /var/log/squid/access.log.week

This is simply dumping 2 copies of the same log file (/var/log/squid/access.log) into access.log.week. You're not getting a "weeks worth of logging", you're getting "two copies of the same file, repeated." Are you sure this is what you want to be doing? If this is your goal, no problem, but it doesn't seem to fit with the problem description.
 
Bash:
cat /var/log/squid/access.log /var/log/squid/access.log> /var/log/squid/access.log.week

This is simply dumping 2 copies of the same log file (/var/log/squid/access.log) into access.log.week. You're not getting a "weeks worth of logging", you're getting "two copies of the same file, repeated." Are you sure this is what you want to be doing? If this is your goal, no problem, but it doesn't seem to fit with the problem description.
Thank for your info, but when i check access.log in log of squid after run this then access.log be zero. I don't know but exactly I want get report of day, week and month, if you have any suggess pls help. Thank you
 
Back
Top