Advertisement
Guest User

Nao

a guest
May 4th, 2009
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.69 KB | None | 0 0
  1. #!/bin/sh
  2. # SARG - Daily/Weekly/Monthly Squid usage reports creation tool
  3. # Written by Ugo Viti <ugo.viti@initzero.it>
  4.  
  5. # ----------------------------------------------------------------------------
  6. #
  7. # Copyright (C) 2005 Ugo Viti
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22. #
  23. # ----------------------------------------------------------------------------
  24.  
  25.  
  26. # Thanks for enanchements to:
  27. # - martijn
  28. # - Stas Degteff https://sourceforge.net/users/stas_degteff/
  29.  
  30. VER=20050202
  31.  
  32. ## What is this?
  33. # sarg-reports (this file) is a simple bash script written to automate
  34. # the SARG (a powerful squid log analyzer) reports and log management.
  35. # Sarg it self, provide to end user a generic interface to create
  36. # reports based on squid access log (begin of log to current date).
  37. # sarg-reports (this script) is useful because it allow you to easly
  38. # create and manage Daily, Weekly and Monthly reports.
  39. # Try it, within 5 minutes you will be ready to rule :-)
  40. # using sarg-reports is very easy, read the following 3 steps to know how
  41.  
  42. ## Requirements
  43. # a) An unix system with bash shell (like GNU/Linux, FreeBSD, etc...)
  44. # b) Squid - http://www.squid-cache.org
  45. # c) Sarg - http://web.onda.com.br/orso/sarg.html
  46.  
  47.  
  48. ##
  49. ## Installation guide and configuration parameters
  50. ##
  51.  
  52. # 1) Download Squid and Sarg, Install, Configure and Tune
  53. #    they before continue reading
  54.  
  55. # 2) In root crontab (crontab -e) insert the following lines:
  56. #    (the today report creation time depend mostly of your squid server
  57. #    load average, tune it):
  58. #
  59. #    --- BEGIN ROOT CRONTAB ---
  60. #    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
  61. #    00 08-18/1 * * * sarg-reports today
  62. #    00 00      * * * sarg-reports daily
  63. #    00 01      * * 1 sarg-reports weekly
  64. #    30 02      1 * * sarg-reports monthly
  65. #    --- END ROOT CRONTAB ---
  66. #
  67. #    REMEMBER: if you use logrotate, configure it to rotate the logs within MONTHLY basis,
  68. #              AFTER sarg-reports created the monthly html report.
  69.  
  70. # 3) Customize the following variables:
  71. #    (Please, configure accurately the sarg.conf file before)
  72. #
  73. #        (SARG) The sarg executable location
  74. #      (CONFIG) The sarg main configuration file location
  75. #     (HTMLOUT) Location where will be saved the reports
  76. #   (PAGETITLE) The title of main index page
  77. #     (LOGOIMG) Image logo to view in main index page
  78. #    (LOGOLINK) HTTP web page link of logo
  79. #       (DAILY) Word 'daily' translation, translate it to your language
  80. #      (WEEKLY) Word 'weekly' translation, translate it to your language
  81. #     (MONTHLY) Word 'monthly' translation, translate it to your language
  82. # (EXCLUDELOG1) Exclude text from cron emails
  83. #       +       (normally, sarg, during cron activity, if it don't find any valid records,
  84. # (EXCLUDELOG2) it will output an error message (usually on 'today' reports).
  85. #               I don't want to be warned by email about this, so, i wrote the 'text'
  86. #               that will be never logged.
  87. #               This is useful to receive email of real problems only (enjoy that)
  88.  
  89. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  90. ## Italian Language
  91.        SARG=/usr/bin/sarg
  92.      CONFIG=/usr/local/sarg/sarg.conf
  93.     HTMLOUT=/var/www/html/sarg
  94.   PAGETITLE="Statistic for $(hostname)"
  95.  #   LOGOIMG=http://www.initzero.it/images/initzero-logo.jpg
  96.  #  LOGOLINK=http://www.initzero.it
  97.       DAILY=Daily
  98.      WEEKLY=Weekly
  99.     MONTHLY=Monthly
  100. #EXCLUDELOG1="SARG: Nessun records trovato."
  101. #EXCLUDELOG2="SARG: Fine"
  102. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  103.  
  104.  
  105. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  106. ## Russian Language
  107. #       SARG="/usr/bin/sarg"
  108. #     CONFIG=/etc/sarg/sarg.conf
  109. #    HTMLOUT=/var/www/sarg-reports
  110. #  PAGETITLE="Статистика сервера прокси Squid"   # russian koi8-r
  111. #    LOGOIMG=http://litek.ru/images/logotop.gif
  112. #   LOGOLINK=http://litek.ru
  113. #      DAILY="Ежедневная"     # russian koi8-r
  114. #     WEEKLY="Еженедельная"   # russian koi8-r
  115. #    MONTHLY="Ежемесячная"    # russian koi8-r
  116. #EXCLUDELOG1="SARG: Records in file:"
  117. #EXCLUDELOG2="SARG: Отчет успешно сгенерирован в" # sarg.conf: language Russian_koi8
  118. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  119.  
  120.  
  121. ######################################################################
  122. ## The configuration is end, so don't touch anything bellow
  123.  
  124. # TEMP Files
  125. TMPFILE=/tmp/sarg-reports.$RANDOM
  126.  ERRORS="${TMPFILE}.errors"
  127.  
  128. # Date Calc
  129. MANUALDATE=$2
  130. case "$(uname)" in
  131. "FreeBSD")
  132.          TODAY=$(date +%d/%m/%Y)
  133.      YESTERDAY=$(date -v-1d +%d/%m/%Y)
  134.        WEEKAGO=$(date -v-1w +%d/%m/%Y)
  135.       MONTHAGO=$(date -v-1m +01/%m/%Y)-$(date -v-1m +31/%m/%Y)
  136.    ;;
  137. "OpenBSD")
  138.          TODAY=$(date +%d/%m/%Y)
  139.      YESTERDAY=$(date -r $((`date +%s` - 86400 )) +%d/%m/%Y)
  140.        WEEKAGO=$(date -r $((`date +%s` - 604800)) +%d/%m/%Y)
  141.       MONTHAGO=$(perl -e '@t=localtime(time); $y=$t[4]==0?$t[5]+1899:$t[5]+1900; $m=$t[4]==0?12:$t[4]; print "1/$m/$y-",$m==2?$y%4>0?28:29:$m==4||$m==6||$m==9||$m==11?30:31 ,"/$m/$y\n";')
  142.    ;;
  143. *)
  144.          TODAY=$(date --date "today" +%d/%m/%Y)
  145.      YESTERDAY=$(date --date "1 day ago" +%d/%m/%Y)
  146.        WEEKAGO=$(date --date "1 week ago" +%d/%m/%Y)
  147.       MONTHAGO=$(date --date "1 month ago" +01/%m/%Y)-$(date --date "1 month ago" +31/%m/%Y)
  148.    ;;
  149. esac
  150.  
  151. # Fix for Red Hat 9 systems and coreutils prior to 5.0 version
  152. export LC_ALL=C
  153.  
  154. # Main index.html creation
  155. create_index_html ()
  156. {
  157.   echo -e "\
  158.  <html>\n\
  159.  <head>\n\
  160.  <title>$PAGETITLE</title>\n\
  161.  </head>\n\
  162.  <body>\n\
  163.  <div align=center>\n\
  164.    <a href=$LOGOLINK><img border=0 src=$LOGOIMG></a>\n\
  165.    <table border=0 cellspacing=6 cellpadding=7>\n\
  166.      <tr>\n\
  167.        <th align=center nowrap><b><font face=Arial size=4 color=green>$PAGETITLE</font></b></th>\n\
  168.      </tr>\n\
  169.      <tr>\n\
  170.        <td align=center bgcolor=beige><font face=Arial size=3><a href=$DAILY>$DAILY</a></font></td>\n\
  171.      </tr>\n\
  172.      <tr>\n\
  173.        <td align=center bgcolor=beige><font face=Arial size=3><a href=$WEEKLY>$WEEKLY</a></font></td>\n\
  174.      </tr>\n\
  175.      <tr>\n\
  176.        <td align=center bgcolor=beige><font face=Arial size=3><a href=$MONTHLY>$MONTHLY</a></font></td>\n\
  177.      </tr>\n\
  178.    </table>\n\
  179.  </div>\n\
  180.  </body>\n\
  181.  </html>" > $HTMLOUT/index.html
  182. }
  183.  
  184.  
  185. # Functions
  186. exclude_from_log ()
  187. {
  188.   #cat $ERRORS | grep -v "$EXCLUDELOG1" | grep -v "$EXCLUDELOG2"
  189.   cat $ERRORS
  190.   rm -f $TMPFILE*
  191. }
  192.  
  193. manual ()
  194. {
  195.   DAILYOUT=$HTMLOUT/$DAILY
  196.   mkdir -p $DAILYOUT
  197.   create_index_html
  198.   if [ -z "$MANUALDATE" ]
  199.     then
  200.       echo "No date given, please specify a valid date (DD/MM/YYYY)"
  201.     else
  202.       $SARG -f $CONFIG -d $MANUALDATE -o $DAILYOUT
  203.   fi
  204. }
  205.  
  206. today ()
  207. {
  208.   DAILYOUT=$HTMLOUT/$DAILY
  209.   mkdir -p $DAILYOUT
  210.   create_index_html
  211.   $SARG -f $CONFIG -d $TODAY -o $DAILYOUT >$ERRORS 2>&1
  212.   echo $TODAY
  213.   exclude_from_log
  214. }
  215.  
  216. daily ()
  217. {
  218.   DAILYOUT=$HTMLOUT/$DAILY
  219.   mkdir -p $DAILYOUT
  220.   create_index_html
  221.   $SARG -f $CONFIG -d $YESTERDAY -o $DAILYOUT >$ERRORS 2>&1
  222.   exclude_from_log
  223. }
  224.  
  225. weekly ()
  226. {
  227.   WEEKLYOUT=$HTMLOUT/$WEEKLY
  228.   mkdir -p $WEEKLYOUT
  229.   create_index_html
  230.   $SARG -f $CONFIG -d $WEEKAGO-$YESTERDAY -o $WEEKLYOUT >$ERRORS 2>&1
  231.   exclude_from_log
  232. }
  233.  
  234. monthly ()
  235. {
  236.   MONTHLYOUT=$HTMLOUT/$MONTHLY
  237.   mkdir -p $MONTHLYOUT
  238.   create_index_html
  239.   $SARG -f $CONFIG -d $MONTHAGO -o $MONTHLYOUT >$ERRORS 2>&1
  240.   #logrotate -f /usr/local/etc/logrotate.d/squid.conf
  241.   squid -k rotate
  242.   exclude_from_log
  243. }
  244.  
  245. case $1 in
  246.     manual)
  247.         manual
  248.         ;;
  249.     today)
  250.         today
  251.         ;;
  252.     daily)
  253.         daily
  254.         ;;
  255.     weekly)
  256.         weekly
  257.         ;;
  258.     monthly)
  259.         monthly
  260.         ;;
  261.      *)
  262.         echo "SARG - Daily / Weekly / Monthly - Squid proxy usage reports creation tool"
  263.         echo "Written by Ugo Viti <ugo.viti@initzero.it>"
  264.         echo "Version: $VER"
  265.         echo
  266.         echo "Usage: $0 [OPTIONS]"
  267.         echo
  268.         echo "Allowed options:"
  269.         echo "    manual,  Create Manual report"
  270.         echo "     today,  Create Today report"
  271.         echo "     daily,  Create Daily report"
  272.         echo "    weekly,  Create Weekly report"
  273.         echo "    montly,  Create Monthly report"
  274.         exit 0
  275. esac
  276.  
  277.  
  278. ## HISTORY:
  279. # 20050502 - Stas Degteff added support for non latin Charset and added support for OpenBSD
  280. # 20030826 - FreeBSD support (thanks to martijn to let me coding on your FreeBSD server :-))
  281. # 20030715 - Some cleanups
  282. # 20030623 - Manual report creation
  283. # 20030620 - Main Index creation
  284. # 20030619 - Solved 'sort' bug on Red Hat 9 systems
  285. # 20030618 - First Version
  286.  
  287.  
  288. ## TODO:
  289. # - Smarty weekly recognition...
  290. #   Like "begin of last week to end of last week",
  291. #   doesn't like this script do: "7 days ago to yesterday"
  292. # - Monthly recognition isn't so elegant (is very ugly, i know)
  293. # - Suggestions are welcome :-)
  294. # - If you Rotate the squid logs before sarg-reports will run,
  295. #   it will not create any html reports
  296. #   (TIPS: Rotate the logs after sarg-reports)
  297.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement