Manage Logs Linux Daily Weekly Monthly so Neat on Web Server

Manage Logs Linux Daily Weekly Monthly so Neat on Web Server
Manage Logs Linux Daily Weekly Monthly so Neat on Web Server

Manage Logs Linux Daily Weekly Monthly so Neat on Web Server - I make logs that are stored on the server so that the location is neat. in the future when there is an issue, you can easily retrieve the log.

Managing logs on a Linux web server is a critical aspect of maintaining system health and security. Effective log management ensures that administrators can quickly identify and address issues, track system performance, and analyze trends over time.

In this article, we will explore strategies for organizing and managing logs on a daily, weekly, and monthly basis to keep the server environment neat and optimized for efficient troubleshooting and analysis.

Case

  1. Application = /apps/logs/*.log
  2. Nginx Log =  /var/logs/nginx/*.log
  3. Zend Log = /usr/local/zend/var/log/*.log

Preparation

Backup logrotate. conf

# cp /etc/logrotate.conf /etc/logrotate.conf.ori

Make Directory Location Log

# mkdir /apps/data/KitsakeLog/daily/apps
# mkdir /apps/data/KitsakeLog/daily/nginx
# mkdir /apps/data/KitsakeLog/daily/zend
# mkdir -p /apps/data/KitsakeLog/weekly/
# mkdir -p /apps/data/KitsakeLog/monthly/

Change Permission & Owner

# chmod -R 744 /apps/data/KitsakeLog/
# chown -R kitsake.kitsake /apps/data/KitsakeLog/

Configure

Configure in logrotate.conf

# vi /etc/logrotate.conf
.................
include /etc/logrotate.d/daily
.................

save & exit

Make Directory Configure of Custom Log

# cd /etc/logrotate.d/
# mkdir -p daily

Move Existing Configure

# cd /etc/logrotate.d/
# mv nginx daily/

Bashscript Ownership

# mkdir /etc/logrotate.d/scripts
# vi  /etc/logrotate.d/scripts/apps-ownership.sh
#!/bin/bash
chown kitsake:kitsake /apps/data/KitsakeLog/daily/nginx/*
chmod 644 /apps/data/KitsakeLog/daily/nginx/*

save & exit

# vi /etc/logrotate.d/scripts/zend-ownership.sh
#!/bin/bash
chown kitsake:kitsake /apps/data/KitsakeLog/daily/zend/*
chmod 644 /apps/data/KitsakeLog/daily/zend/*
# chmod 755 /etc/logrotate.d/scripts/apps-ownership.sh
# chmod 755 /etc/logrotate.d/scripts/zend-ownership.sh

Daily Logrotate

# vi /etc/logrotate.d/daily/apps
/apps/logs/*.log {
    olddir /apps/data/KitsakeLog/daily/apps
        daily
        rotate 7
    missingok
    create 640 kitsake kitsake
}

save & exit

# vi /etc/logrotate.d/daily/nginx
/apps/logs/nginx/*.log {
    su root root
    olddir /apps/data/KitsakeLog/daily/nginx
        daily
        rotate 7
    missingok
    create 640 nginx zend
    lastaction
        /etc/logrotate.d/scripts/apps-ownership.sh
    endscript
    postrotate
        if [ -f /var/run/nginx.pid ]; then
            kill -USR1 `cat /var/run/nginx.pid`
        fi
    endscript
}

save & exit

# vi /etc/logrotate.d/daily/zend
/usr/local/zend/var/log/*.log {
    su root root
    olddir /apps/data/KitsakeLog/daily/zend
        daily
        rotate 7
    missingok
    lastaction
        /etc/logrotate.d/scripts/zend-ownership.sh
    endscript
}

save & exit

Weekly Logrotate Save

# mkdir -p /home/scripts
# chown -R kitsake.kitsake /home/scripts
# vi /home/scripts/weekly.sh
#!/bin/bash
tgl=`/bin/date +%Y%m%d`
cd /apps/data/KitsakeLog/daily/
tar -czvf weekly_$tgl.tgz *
mv weekly_$tgl.tgz /apps/data/KitsakeLog/weekly/

find /apps/data/KitsakeLog/daily/apps/ -maxdepth 1 -type f -mtime +7 -exec rm -rf {} \;
find /apps/data/KitsakeLog/daily/nginx/ -maxdepth 1 -type f -mtime +7 -exec rm -rf {} \;
find /apps/data/KitsakeLog/daily/supervisor/ -maxdepth 1 -type f -mtime +7 -exec rm -rf {} \;
find /apps/data/KitsakeLog/daily/zend/ -maxdepth 1 -type f -mtime +7 -exec rm -rf {} \;

save & exit

# chmod 755 /home/scripts/weekly.sh

Monthly Logrotate Save

# vi /home/scripts/monthly.sh
#!/bin/bash
tgl=`/bin/date +%Y%m%d`
cd /apps/data/KitsakeLog/weekly/
tar -czvf monthly_$tgl.tgz *
mv monthly_$tgl.tgz /apps/data/KitsakeLog/monthly/

find /apps/data/KitsakeLog/weekly/ -maxdepth 1 -type d -mtime +4 -exec rm -rf {} \;

save & exit

# chmod 755 /home/scripts/monthly.sh

Make Crontab

# vi /etc/crontab
00 00 */7 * * kitsake /home/scripts/weekly.sh
00 00 */30 * * kitsake /home/scripts/monthly.sh

Manage Logs Linux Daily Weekly Monthly
Manage Logs Linux Daily Weekly Monthly

Closing statement

In conclusion, effective log management is paramount for maintaining the health and security of a Linux web server

By implementing a meticulous approach to daily, weekly, and monthly log reviews, administrators can promptly identify and address potential issues, ensuring optimal performance and safeguarding against security threats. 

The organization and cleanliness of logs contribute significantly to the efficiency of troubleshooting and system analysis. Therefore, adopting a systematic routine for log management not only enhances the server's overall functionality but also streamlines the process of identifying trends and patterns that can be crucial for preemptive maintenance. 

Ultimately, a well-managed log system is instrumental in promoting a robust and resilient web server environment.

Bangkit Ade Saputra
Bangkit Ade Saputra At the end of the day, my job involves people. we're complicated, we're always changing, we have millions of things going on in our lives, and changing jobs is always a big decision.

Post a Comment for "Manage Logs Linux Daily Weekly Monthly so Neat on Web Server"