Skrypt sh (bash) teoretycznie zliczający transfer z plików logów apache (edukacyjny)

Z Podręcznik Administratora by OPZ SGU
Przejdź do nawigacji Przejdź do wyszukiwania

Skrypt teoretycznie zlicza trasnfer na podstawie plików logów apache.

#!/bin/bash

if [ $# -eq 0 ]
then
echo '-------------------------------------'
echo 'Pormoc skryptu zliczajacego transfer.'
echo '-------------------------------------'
echo 'Uruchom program podając jako pierwszy argument nazwe pliku.log.'
echo 'Przyklad:
licznik.sh xxx.pl-combined.log'
echo '-------------------------------------'
exit 0;
fi


if ! [ -f $1 ]
then
echo "Plik $1 nie istnieje"
exit 0;
fi


cat "$1" | while read linia; do 




echo $linia > temp.tmp
cat temp.tmp | cut -f 10 -d " " > temp2.tmp




if ! [ -f 'temp_data_poczatkowa.tmp' ]
then
cat temp.tmp | cut -f 4 -d " " > temp_d.tmp
cat temp_d.tmp | cut -c 2-21 > temp_data_poczatkowa.tmp
fi


rozmiar=`cat temp2.tmp`

if [ $rozmiar != '-' ]
then
liczba_bajtow=$(( $liczba_bajtow + $rozmiar ));
fi

#suma
echo $liczba_bajtow > rozmiar.tmp
#suma

#data koncowa
cat temp.tmp | cut -f 4 -d " " > temp_d.tmp
cat temp_d.tmp | cut -c 2-21 > temp_data_koncowa.tmp
#data koncowa

done



#podsumowanie
wych=`cat rozmiar.tmp`
data_koncowa=`cat temp_data_koncowa.tmp`;
data_poczatkowa=`cat temp_data_poczatkowa.tmp`;

echo "Transfer wychodzący: $wych bajtów";
echo "Zakres";
echo "Od: $data_poczatkowa";
echo "Do: $data_koncowa";
#podsumowanie

#czyszczenie
rm temp.tmp
rm temp2.tmp
rm rozmiar.tmp
rm temp_d.tmp
rm temp_data_koncowa.tmp
rm temp_data_poczatkowa.tmp
#czyszczenie

PRZYKŁADOWY FORMAT PLIK LOGÓW

207.46.204.236 - - [14/Nov/2010:05:13:01 +0100] "GET /robots.txt HTTP/1.1" 404 1174 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
207.46.204.236 - - [14/Nov/2010:05:33:13 +0100] "GET /index.php?strona=kontakt&language=en HTTP/1.1" 200 1545 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
65.36.241.79 - - [14/Nov/2010:06:33:22 +0100] "HEAD / HTTP/1.1" 200 - "-" "InternetSeer.com"
212.77.100.47 - - [14/Nov/2010:11:34:55 +0100] "GET /robots.txt HTTP/1.1" 404 595 "-" "NetSprint -- 3.0"
212.77.100.47 - - [14/Nov/2010:11:35:02 +0100] "GET / HTTP/1.1" 200 1649 "-" "NetSprint -- 3.0"
212.77.100.47 - - [14/Nov/2010:11:35:09 +0100] "GET /index.php?language=pl&strona=przestarzala HTTP/1.1" 200 1214 "-" "NetSprint -- 3.0"
212.77.100.47 - - [14/Nov/2010:11:35:15 +0100] "GET /index.php?language=en&strona=przestarzala HTTP/1.1" 200 1182 "-" "NetSprint -- 3.0"
212.77.100.47 - - [14/Nov/2010:11:35:22 +0100] "GET /index.php?language=en&strona=przestarzala HTTP/1.1" 200 1182 "-" "NetSprint -- 3.0"
212.77.100.47 - - [14/Nov/2010:11:35:29 +0100] "GET /index.php?strona=kontakt&language=en HTTP/1.1" 200 1546 "-" "NetSprint -- 3.0"
212.77.100.47 - - [14/Nov/2010:11:35:35 +0100] "GET /index.php?strona=oserwisie&language=pl HTTP/1.1" 200 2567 "-" "NetSprint -- 3.0"
65.36.241.79 - - [14/Nov/2010:17:26:23 +0100] "HEAD / HTTP/1.1" 200 - "-" "InternetSeer.com"
65.36.241.79 - - [15/Nov/2010:01:04:07 +0100] "HEAD / HTTP/1.1" 200 - "-" "InternetSeer.com"
157.55.16.229 - - [15/Nov/2010:03:31:22 +0100] "GET /robots.txt HTTP/1.1" 404 1174 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
157.55.16.229 - - [15/Nov/2010:03:33:11 +0100] "GET /index.php?language=en&strona=oserwisie HTTP/1.1" 200 2339 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
66.249.66.146 - - [15/Nov/2010:05:01:51 +0100] "GET /robots.txt HTTP/1.1" 404 595 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
66.249.66.146 - - [15/Nov/2010:05:01:51 +0100] "GET / HTTP/1.1" 200 1653 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
207.46.204.178 - - [15/Nov/2010:05:13:12 +0100] "GET /robots.txt HTTP/1.1" 404 1174 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
207.46.204.178 - - [15/Nov/2010:05:18:10 +0100] "GET / HTTP/1.1" 200 1653 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
84.2.7.139 - - [15/Nov/2010:12:20:48 +0100] "GET /robots.txt HTTP/1.0" 404 1174 "-" "Mozilla/5.0 (compatible; MJ12bot/v1.3.3; http://www.majestic12.co.uk/bot.php?+)"
84.2.7.139 - - [15/Nov/2010:12:20:53 +0100] "GET /index.php?language=en&strona=przestarzala HTTP/1.1" 200 2963 "-" "Mozilla/5.0 (compatible; MJ12bot/v1.3.3; http://www.majestic12.co.uk/bot.php?+)"
84.2.7.139 - - [15/Nov/2010:12:20:54 +0100] "GET /index.php?language=pl&strona=przestarzala HTTP/1.1" 200 1214 "-" "Mozilla/5.0 (compatible; MJ12bot/v1.3.3; http://www.majestic12.co.uk/bot.php?+)"
66.249.66.146 - - [15/Nov/2010:13:22:00 +0100] "GET /index.php?language=pl&strona=oserwisie HTTP/1.1" 200 2567 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
65.36.241.79 - - [15/Nov/2010:13:33:36 +0100] "HEAD / HTTP/1.1" 200 - "-" "InternetSeer.com"
207.46.195.242 - - [15/Nov/2010:14:44:39 +0100] "GET /robots.txt HTTP/1.1" 404 1174 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
207.46.195.242 - - [15/Nov/2010:14:45:41 +0100] "GET /index.php?language=en&strona=zmieniarka HTTP/1.1" 200 3402 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
65.36.241.79 - - [15/Nov/2010:18:46:22 +0100] "HEAD / HTTP/1.1" 200 - "-" "InternetSeer.com"
95.108.248.30 - - [15/Nov/2010:23:15:25 +0100] "GET /robots.txt HTTP/1.1" 404 1174 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"
95.108.248.30 - - [16/Nov/2010:04:29:39 +0100] "GET / HTTP/1.1" 200 1650 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"
65.36.241.79 - - [16/Nov/2010:06:57:53 +0100] "HEAD / HTTP/1.1" 200 - "-" "InternetSeer.com"
95.108.248.30 - - [16/Nov/2010:10:45:57 +0100] "GET /index.php?language=pl HTTP/1.1" 200 1652 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"
65.52.108.12 - - [16/Nov/2010:13:06:16 +0100] "GET /robots.txt HTTP/1.1" 404 1174 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
65.52.108.12 - - [16/Nov/2010:13:08:03 +0100] "GET /index.php?language=en&strona=kontakt HTTP/1.1" 200 1546 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
95.108.248.30 - - [16/Nov/2010:14:11:45 +0100] "GET /index.php?language=pl&strona= HTTP/1.1" 200 1652 "-" "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"
65.36.241.79 - - [16/Nov/2010:17:17:29 +0100] "HEAD / HTTP/1.1" 200 - "-" "InternetSeer.com"
66.249.66.146 - - [16/Nov/2010:22:12:54 +0100] "GET /robots.txt HTTP/1.1" 404 595 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
66.249.66.146 - - [16/Nov/2010:22:12:54 +0100] "GET / HTTP/1.1" 200 1652 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"