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



## hongfxhp (Jan 6, 2022)

Dear Everyone,
Could everyone can help check and edit my script run daily sarg report,

```
#!/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





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

thank you


----------



## SirDice (Jan 6, 2022)

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):

```
-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.
```



hongfxhp said:


> ```
> today = $(date -v -1d "+%d/%m/%Y")
> export LC_ALL = C
> ```


These are syntax errors.


			POSIX Shell Tutorial


----------



## eternal_noob (Jan 6, 2022)

Try `yesterday="$(date -d yesterday '+%d/%m/%Y')"`



SirDice said:


> These are syntax errors.


Good catch. I didn't even notice the spaces around the assignment.


----------



## im (Jan 6, 2022)

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

```
#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

```
#!/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

```
#!/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
```


----------



## hongfxhp (Jan 7, 2022)

I rewrite it to be above is running ok.

When I do with weekly with code below

```
#!/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 .


----------



## im (Jan 7, 2022)

Try to increase the verbose level of bash. 
Just add "-v" to the end of the first line, and run the script again.

```
#!/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


----------



## hongfxhp (Jan 8, 2022)

im said:


> Try to increase the verbose level of bash.
> Just add "-v" to the end of the first line, and run the script again.
> 
> ```
> ...


Thank you so much for your comment
I changed as below

```
#!/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


----------



## AngryChris (Jan 8, 2022)

```
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.


----------



## hongfxhp (Jan 9, 2022)

AngryChris said:


> ```
> 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


----------



## SirDice (Jan 10, 2022)

hongfxhp said:


> ```
> #!/usr/local/bin/sh -v
> ```


This should be `#!/bin/sh -v`


----------



## hongfxhp (Jan 14, 2022)

SirDice said:


> This should be `#!/bin/sh -v`


thank you, but one machine is running, and one is not running daily. I don't know reason why, because 2 pc setup same.


----------

