felmoltor

OVH Apache Access log periodic download

Mar 5th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.91 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Author: Felipe Molina (@felmoltor)
  4. # Date: 04/03/2015
  5. # Summary:
  6. #   If you manage a hosted webpage in OVH maybe this script is for you.
  7. #   This script automaticaly access to https://logs.ovh.net/ and downloads the access logs of your apache server.
  8. #   You can filter the downloaded logs by start and stop date.
  9. #   Set a cron to download every day or week the OVH logs. Then analyze them in your own box.
  10. #   TODO: Download also the "error" logs and "ftp" logs
  11.  
  12. usr="<YOUR OVH USER>"
  13. pwd="<YOUR OVH PASSWORD>"
  14. domain="<YOUR OVH DOMAIN>"
  15.  
  16. function printUsage() {
  17.     echo "Usage: $0 <from date> [<to date>]"
  18.     echo "* Format of the dates: (month/day/year MM/DD/YYYY)"
  19.     echo "* from date:  Is mandatory "
  20.     echo "* to date:    Is optional. Default value is today $(date +%M/%d/%Y)"
  21. }
  22.  
  23. if [[ "$#" -gt 2 || "$#" -lt 1 ]];then
  24.     printUsage 
  25.     exit 1 
  26. fi
  27.  
  28. fromdate=$1
  29. todate=$2
  30.  
  31. if [[ "$fromdate" != "" ]];then
  32.     if [[ ! $fromdate =~ ^[[:digit:]]{2}/[[:digit:]]{2}/[[:digit:]]{4}$ ]];then
  33.         printUsage
  34.         exit 2
  35.     fi
  36. fi
  37. if [[ "$todate" != "" ]];then
  38.     if [[ ! $todate =~ ^[[:digit:]]{2}/[[:digit:]]{2}/[[:digit:]]{4}$ ]];then
  39.         printUsage
  40.         exit 3
  41.     fi
  42. else
  43.     todate=$(date +%M/%d/%Y)
  44. fi
  45.  
  46. echo "Retrieving log files from $fromdate to $todate"
  47.  
  48. frommonth=$(echo $fromdate | cut -d'/' -f1)
  49. fromyear=$(echo $fromdate | cut -d'/' -f3)
  50. fromday=$(echo $fromdate | cut -d'/' -f2)
  51.  
  52. tomonth=$(echo $todate | cut -d'/' -f1)
  53. toyear=$(echo $todate | cut -d'/' -f3)
  54. today=$(echo $todate | cut -d'/' -f2)
  55.  
  56. fromepoch=$(date -d "$fromdate" +%s)
  57. toepoch=$(date -d "$todate" +%s)
  58.  
  59. if [[ ! -d tmp ]];then
  60.     mkdir tmp
  61. fi
  62. if [[ ! -d lists ]];then
  63.     mkdir lists
  64. fi
  65. if [[ ! -d logs ]];then
  66.     mkdir logs
  67. fi
  68.  
  69. # Get the available logs dates
  70. echo "Obtaining the list of available folders for domain $domain"
  71. wget --quiet --http-user=$usr --http-password=$pwd https://logs.ovh.net/$domain/ -O tmp/$domain.available.logs.html
  72. # get only the name of the foler and filter it by month date
  73. folderlist=$(egrep -o "<a href=\"logs-.*\">" tmp/$domain.available.logs.html | cut -f2 -d'"' | cut -f1 -d'"' | sort -u)
  74. for folder in $(echo $folderlist);do
  75.     fmonth=$(echo $folder | cut -d'-' -f2)
  76.     fyear=$(echo $folder | cut -d'-' -f3)
  77.     download=0
  78.     if [[ "10#$year" -lt "10#$toyear" && "10#$fyear" -gt "10#$fromyear" ]];then
  79.         download=1
  80.     elif [[ "10#$fyear" -eq "10#$toyear" || "10#$fyear" -eq "10#$fromyear" ]];then
  81.         if [[ "10#$fmonth" -le "10#$tomonth" && "10#$fmonth" -ge "10#$frommonth" ]];then
  82.             download=1
  83.         fi
  84.     fi
  85.     if [[ "$download" -eq "1" ]];then
  86.         echo "Will visit folder $folder"
  87.         echo $folder >> "lists/$domain.available.logs.list"
  88.     fi
  89. done
  90. rm "tmp/$domain.available.logs.html"
  91.  
  92. for folder in $(cat lists/$domain.available.logs.list); do
  93.     wget --quiet --http-user=$usr --http-password=$pwd https://logs.ovh.net/$domain/$folder/ -O tmp/$domain.$folder.available.logs.html
  94.     filelist=$(egrep -o "<a href=\".*.log.gz\">" tmp/$domain.$folder.available.logs.html | cut -f2 -d'"' | cut -f1 -d'"' | sort -u)
  95.     echo "======================="
  96.     echo "==== $folder ==="
  97.     echo "======================="
  98.     for file in $(echo $filelist);do
  99.         ffile=$(echo $file | egrep -o "[[:digit:]]{2}-[[:digit:]]{2}-[[:digit:]]{4}" | sed 's/\(.*\)-\(.*\)-\(.*\)/\2\/\1\/\3/g' )
  100.         fyear=$(echo $ffile|cut -d'/' -f3)
  101.         fmonth=$(echo $ffile|cut -d'/' -f1)
  102.         fday=$(echo $ffile|cut -d'/' -f2)
  103.         fepoch=$(date -d "$fmonth/$fday/$fyear" +%s)
  104.         if [[ "10#$fepoch" -ge "10#$fromepoch" && "10#$fepoch" -le "10#$toepoch" ]];then
  105.             echo " * Downloading file $file..."
  106.             wget --quiet --http-user=$usr --http-password=$pwd https://logs.ovh.net/$domain/$folder/$file -O logs/$file
  107.         fi
  108.     done
  109.     rm "tmp/$domain.$folder.available.logs.html"
  110. done
  111.  
  112. rm -rf tmp
  113. rm -rf lists
Add Comment
Please, Sign In to add comment