With the new regulation, the obligation imposed on hosting providers under Law No. 5651 to store site access logs for at least six months is being extended to at least one year, with fines of up to 100,000 TL in case of non-compliance. Unfortunately, archiving access logs is a tedious and costly process. If we take commonly used control panels as an example, cPanel and all other hosting software process access records to generate a series of statistical data in the panel and then delete them to save space. The log archiving process must be done without disrupting control panel functions and without any gaps. In this article, we will share with you the cPanel access log archiving system produced by our R&D team.
One of the best methods for getting cPanel logs in a more organized and desired format is cPanel hooks. These hooks allow us to perform desired operations before, after, or during certain cPanel operations.
Functions related to cPanel logs are grouped under Stats Functions. These functions consist of 4 main functions: RunAll(pre/post) and RunUser(pre/post).
RunUser: The hook you define with RunUser will run for each user with user-level permissions. The hook specified with RunUser is called separately for each user. cPanel sends information about the current process in JSON format via stdin when calling the hook file.
RunAll: A hook can be defined to run after all user stats operations are completed with RunUser. cPanel does not send any data when calling this hook function.
Hooks can be written in many languages, but let’s create a backup hook in Perl based on the examples cPanel provides us.
mkdir /usr/local/cpanel/Netinternet
touch /usr/local/cpanel/Netinternet/NetinternetBackup.pmCreate a simple Perl module with the describe method required by cPanel hooks:
package Netinternet::NetinternetBackup;
use strict;
use warnings;
sub describe {
my $hooks = [
{
‘namespace’ => ‘Stats’,
‘function’ => ‘RunUser’,
‘hook’ => ‘Netinternet::NetinternetBackup::copy_logfiles’,
‘stage’ => ‘pre’,
},
];
return $hooks;
}
sub copy_logfiles {
return 1;
}
1;
The final version of our script:
package Netinternet::NetinternetBackup;
use File::Copy;
use File::Path;
use strict;
use warnings;
use POSIX;
sub describe {
my $hooks = [
{
‘namespace’ => ‘Stats’,
‘function’ => ‘RunUser’,
‘hook’ => ‘Netinternet::NetinternetBackup::copy_logfiles’,
‘stage’ => ‘pre’,
},
];
return $hooks;
}
sub copylogfiles {
my $year = POSIX::strftime(‘%Y’, localtime());
my $mon = POSIX::strftime(‘%m’, localtime());
my $mday = POSIX::strftime(‘%d’, localtime());
my ( $context, $args ) = @;
my $user = $args->{‘user’};
foreach my $log_ref ( @{ $args->{‘logfiledesc’} } ) {
my $access_log = $log_ref->{‘logfile’};
my $backup_location = “/backup/$user/$year/$mon/$mday/” . $log_ref->{‘domain’} . ‘.’ . time();
if ( !-e “/backup/$user/$year/$mon/$mday” ) {
mkpath(“/backup/$user/$year/$mon/$mday/”);
}
print “Backing up ” . $log_ref->{‘domain’} . “ domain to: $backup_location
”;
File::Copy::copy( $access_log, $backup_location ) || print STDERR $! . “
”;
}
return 1;
}
1;
To register the module with cPanel:
/usr/local/cpanel/bin/manage_hooks add module Netinternet::NetinternetBackupAs a result of these operations, while statistical data is being generated for each cPanel user, a copy of the logs will be copied to the specified directory. You can also add a compression function while copying logs, but keep in mind that this may extend the processing time. At Netinternet, we perform compression operations after obtaining the logs in raw form. Since access files are text files, tools like gunzip can compress them by up to 90%.
Leave a Comment
* Your comment will be published after approval.
Comments
4<p>Haftalardır bomboş klasör hook olarak eklendi sorun ne olabilir?</p>
<p>çok teşekkürler,</p><p> my $backup_location = "/usr/local/cpanel/netinternet/yedek/$user/$year/$mon/$mday/" . $log_ref->{'domain'} . '.' . time();</p><p> if ( !-e "/yedek/$user/$year/$mon/$mday" ) {</p><p> mkpath("/usr/local/cpanel/netinternet/yedek/$user/$year/$mon/$mday/", 0777);</p><p>netinternet klasörüne ve yedek klasörüne 0777 yazma izni vererek düzelttim.</p><p>Bu mevzudaki (5651) düşüncem ise:<br>Anladığım kadarıyla Linux sunuculardaki loglama sistemi için tib bir format düşünmemiş. Mevcut log sistemini windows sunuculardaki şekliyle yapmak istesek manuel bir yapı ile düzenleme yapmak gerekiyor. Yanlış anlamadıysam TİB, kullanıcının ip adresini, server'a girdiği çıktığı zamanı ve MAC adresininin loglamasını istiyor. Bunun bu şekilde olması CPanel kullanıcıları için şu an için zor gibi gözüküyor. Birde biz bu logları arzu edilen formata çevirsek bile güvenirliğinin elle değiştirilebilinir olduğu gerçeği unutulmuş gibi:). sonuç olarak bu logların tutulmasına eyvallah ama sistemin bacaklarının linux'u unutması bir format belirlenmemesi ilginç. Logların imzalanması güzel fikir fakat Loglar'ın makine ile eş zamanlı olmadan imzalanması gibi bir durum sözkonusu gliba bu ilginç bir çıkmaz. Allah kolaylık versin :) ilginize teşekkür ederim.</p>
<p>Dediğiniz gibi belirli bir format istenmemiş ki açıkcası belirli bir format istenmesi durumunda ortaya tahmin edilemez birçok problem çıkacaktır.Sonuç olarak herkes apache veya nginx tabanlı bir sistem kullanmamakta.Durum başlı başına sıkıntılı bir süreç ve bu yasa için pek de üzerinde oturulup düşünüldüğünü sanmıyorum.</p><p>Bu konularda söyleycek çok sözüm olsada maalesef burada paylaşmam mümkün değil :)</p><p>Bu arada logların yollarını değiştirin.<br>if ile kontrol edilen ve içerisinde oluşturulan dizinin aynı dizin olmasına dikkat edin.Ben bu örnekte /yedek yani ana dizin içerisindeki yedek klasörünü kullandım ama siz /usr/local/cpanel/ dizini içerisinde klasörleri oluşturmaktasınız.Cpanel dosyaları içerisinde yedekleri tutmayın ve tahmin edilemez yollar kullanmayı deneyin.Örnek olarak bir yapılandırma daha ekliyorum ;</p><p>my $backup_location = "/var/log/yedek/$user/$year/$mon/$mday/" . $log_ref->{'domain'} . '.' . time();</p><p>if ( !-e "/var/log/yedek/$user/$year/$mon/$mday" ) {</p><p> mkpath("/var/log/yedek/$user/$year/$mon/$mday/");</p><p>}</p>
<p>Teşekkürler çok faydalı bir makale olmuş. Logları sıkıştırma işlemini nasıl yaptığınızıda açıklayabilir misiniz ?</p>