Skip to content

Commit

Permalink
Create a new function to run when hooked
Browse files Browse the repository at this point in the history
  • Loading branch information
jorianvo committed Jun 12, 2015
1 parent 9b31f08 commit dab89f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 123 deletions.
7 changes: 5 additions & 2 deletions lib/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class OC_esLog_Hooks {
// For now just reading and writing data
// ---------------------

// This function is called every time a file is read
static public function read($path) {
OC_esLog::log($path,'File read');
OC_esLog::sendToStatsd($path,'File read');
}

// This function is called every time a file is written
static public function write($path) {
OC_esLog::log($path,'File write');
OC_esLog::sendToStatsd($path,'File write');
}

}
142 changes: 21 additions & 121 deletions lib/log.php
Original file line number Diff line number Diff line change
@@ -1,133 +1,33 @@
<?php

// Change the patch to your /vendor directory
// See the Elasticsearch PHP API document for more details
// http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/

require_once 'apps/eslog/vendor/autoload.php';

class OC_esLog {

public function __construct(){
public function __construct(){

}

public static function log($path,$path2,$action,$protocol='http'){
if(isset($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_USER']))
$user = $_SERVER['PHP_AUTH_USER'];
else
$user = OCP\User::getUser();

// Replace user with the right value
if ($action == 'User login attempt' || $action == 'User login') {
$user = $path;
$path = '';
}

if ($action == 'File shared' || $action == 'File unshared') {
$vars=$path;
$file=basename($vars['fileTarget']);
$folder=dirname($vars['fileTarget']);
$user=$vars['uidOwner'];
if (!empty($vars['shareWith'])) {
$folder2=$vars['shareWith'];
}
else {
$folder2=\OCP\Util::linkToPublic('files') .'&t=' . $vars['token'];
}
$path=$vars['fileTarget'];
$path2='';
}

$folder = is_array($path)?dirname($path['path']):dirname($path);
$file = is_array($path)?basename($path['path']):basename($path);

if (empty($folder2))
$folder2 = is_array($path2)?dirname($path2['path']):(!empty($path2)?dirname($path2):'');
$file2 = is_array($path2)?basename($path2['path']):(!empty($path2)?basename($path2):'');
$type='unknown';

if(!empty($file2)){
if($protocol=='http'){
$type = \OC\Files\Filesystem::filetype($folder2.'/'.$file2);
}
elseif($protocol=='caldav'){
$type = $_SERVER['CONTENT_TYPE'];
}
elseif($protocol=='carddav'){
$type = $_SERVER['CONTENT_TYPE'];
}
else{
$CONFIG_DATADIRECTORY = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
if(is_dir($CONFIG_DATADIRECTORY.'/'.$user.'/files')){
$type='unknown';
if(is_file($CONFIG_DATADIRECTORY.'/'.$user.'/files'.$folder.$file)) $type='file';
elseif(is_dir($CONFIG_DATADIRECTORY.'/'.$user.'/files'.$folder.$file)) $type='dir';
}
}
if(strpos($type,';')){
$type=substr($type,0,strpos($type,';'));
}
}

self::send2elasticsearch($user, $protocol, $type, $folder, $file,$folder2,$file2, $action);
}

public static function send2elasticsearch($user, $protocol, $type, $folder, $file,$folder2,$file2, $action)
{
$params = array();
$params['hosts'] = array(
OC_Appconfig::getValue('eslog', 'eslog_host', '127.0.0.1:9200')
);
if (OC_Appconfig::getValue('eslog', 'eslog_auth', 'none') != "none") {
$params['connectionParams']['auth'] = array(
OC_Appconfig::getValue('eslog', 'eslog_user', ''),
OC_Appconfig::getValue('eslog', 'eslog_password', ''),
OC_Appconfig::getValue('eslog', 'eslog_auth', 'none')
);
// This function will gather the stats and send them statsd
// The path is the path of the file that is read or written
// currently this is not used. The action is either 'File read'
// or 'File write' and determines what value to send to statsd
public static function sendToStatsd($path,$action){
// Create the udp socket to send the data to statsd
$sender = new SocketSender('localhost', 8126, 'udp');

// Create the client which sends to data to statsd using the sender
$client = new StatsdClient($sender);
$factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData');
$service = new StatsdService($client, $factory);

// Check if file is read or written
if ($action == "File read"){
$service->increment('read');
} elseif ($action == "File write"){
$service->increment('write');
}

$client = new Elasticsearch\Client($params);

// $date = date('Y-m-d H:i:s');
$date = date('c');
$request=$_REQUEST;
if(isset($request['password'])) $request['password']='******';

$server=$_SERVER;
if(isset($server['PHP_AUTH_PW'])) $server['PHP_AUTH_PW']='******';
if(isset($server['HTTP_COOKIE'])) $server['HTTP_COOKIE']='******';
if(isset($server['HTTP_AUTHORIZATION'])) $server['HTTP_AUTHORIZATION']='******';

if (isset($server['REMOTE_ADDR'])) {
$remote_address = $server['REMOTE_ADDR'];
}
if (isset($server['REMOTE_PORT'])) {
$remote_port = $server['REMOTE_PORT'];
}

$vars=serialize(array(
'request'=>$request,
'server'=>$server
));
$params = array();
$params['index'] = OC_Appconfig::getValue('eslog', 'eslog_index', 'owncloud');
$params['type'] = OC_Appconfig::getValue('eslog', 'eslog_type', 'owncloud');
$params['body'] = array(
'@timestamp' => $date,
'user' => $user,
'src_ip' => $remote_address,
'src_port' => $remote_port,
'date' => $date,
'proto' => $protocol,
'content_type' => $type,
'folder' => $folder,
'file' => $file,
'folder2' => $folder2,
'file2' => $file2,
'action' => $action,
'variables' => $vars
);
$ret = $client->index($params);
// Send the data over the socket to statsd
$service->flush();
}
}

0 comments on commit dab89f9

Please sign in to comment.