Created
December 2, 2014 20:53
-
-
Save zetas/d369c31afe70c8810707 to your computer and use it in GitHub Desktop.
Status Display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once dirname(__FILE__) . '/config.php'; | |
use nzedb\libraries\Cache; | |
use nzedb\libraries\CacheException; | |
use nzedb\db\Settings; | |
/** | |
* Update this directive to reflect your chosen webserver. | |
* Currently we only support nginx or apache out of the box. | |
* If you wish to add support for an additional webserver, | |
* simply modify the AWK script below to fit your access_log | |
* format. | |
*/ | |
$webserver = "nginx"; //Options are nginx or apache. | |
/** | |
* Access Log Location | |
* This is usually /var/log/apache2/access.log or | |
* /var/log/nginx/access.log | |
*/ | |
$access_log = "/var/log/nginx/access.log"; | |
//No Touchy below this line unless you know what you're doing! | |
$pdo = new Settings(); | |
try { | |
$cacheServer = new Cache(); | |
} catch (CacheException $error) { | |
$pdo->log->error('Unable to initialize cache server: ' . $error->getMessage()); | |
exit; | |
} | |
$access_points = array('api', 'getnzb'); | |
$nginx = array('a' => 4, 'b' => 4, 'c' => 5, 'd' => 4, 'e' => 7); | |
$apache = array('a' => 5, 'b' => 5, 'c' => 6, 'd' => 5, 'e' => 8); | |
$vars = ($webserver == "nginx") ? $nginx : $apache; | |
$awk = <<<AWK | |
BEGIN{ | |
#create an array that maps Month Names to Month Numbers | |
m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|") | |
for(o=1;o<=m;o++){ | |
date[d[o]]=sprintf("%%02d",o) | |
} | |
} | |
{ | |
#Set MKTIME to the number of seconds since epoch the log event happened | |
gsub(/\[/,"",$%d); gsub(":","/",$%d); gsub(/\]/,"",$%d) | |
n=split($%d, DATE,"/") | |
day=DATE[1] | |
mth=DATE[2] | |
year=DATE[3] | |
hr=DATE[4] | |
min=DATE[5] | |
sec=DATE[6] | |
MKTIME= mktime(year" "date[mth]" "day" "hr" "min" "sec) | |
# if MKTIME is too old then skip it | |
if ( MKTIME < systime()-3600 ) next | |
#split the request into pieces based on / and ? | |
split($%d, request, /[\/?]/) | |
#count each of the lines based on the second piece of the request | |
counter[request[2]] ++ | |
} | |
END { | |
#loop over all the things we counted and print their counts | |
for ( a in counter ) printf "%%d-%%s|", counter[a], a | |
} | |
AWK; | |
$vnstat_cmd = <<<SH | |
vnstat -tr |grep -v '^$' |grep -v Traffic |grep -v sampled |awk '{print "[" $2 "|" $3 "]"}' |tr -d '\n' | |
SH; | |
$releases_added_query = "select count(*) from releases where adddate >= DATE_SUB(NOW(),INTERVAL 1 HOUR)"; | |
// do work! | |
/** | |
* /usr/bin/awk -f /var/www/nZEDb/scripts/api_calls.awk $LOGFILE |grep -v help |grep api > /tmp/apicalls.log | |
/usr/bin/awk -f /var/www/nZEDb/scripts/api_calls.awk $LOGFILE |grep getnzb > /tmp/grabs.log | |
*/ | |
if (!is_readable('/tmp/logprocess.awk')) { | |
$file = fopen('/tmp/logprocess.awk', 'w'); | |
extract($vars); | |
$awk_contents = sprintf($awk, $a, $b, $c, $d, $e); | |
fwrite($file, $awk_contents); | |
fclose($file); | |
} | |
exec("/usr/bin/awk -f /tmp/logprocess.awk $access_log", $awk_result, $ret_code); | |
if ($ret_code == 0) { | |
$access_list = explode('|',$awk_result); | |
foreach ($access_list as $res) { | |
$res_arr = explode('-', $res); | |
$point = $res_arr[0]; | |
$count = $res_arr[1]; | |
if (in_array($point,$access_points)) { | |
try { | |
$cacheServer->set($point . "_calls", $count, 600); | |
echo "Setting $point calls to $count\n"; | |
} catch (CacheException $error) { | |
$pdo->log->error('Unable to add to cache: ' . $error->getMessage()); | |
exit; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment