REQUEST: RMM log cleanup - would like to use as a scheduled task

Noticing my RMM logs are getting out of hand and it doesn’t look like they are managed by ITSM settings.

Would like a quick little procedure I could setup to run as a scheduled item to clean them up once a day for files older than a variable time (that can be set or disabled).

I’m enjoying my playing around with Python but I think this might be out of my skill set for the moment.

HI @zabolyx

Thank you for contacting us for the script requirement. We have escalated this request to the proper channel and update you once completed.

Hi @zabolyx

Please refer the below-mentioned script for your request,

https://scripts.comodo.com/frontend/web/topic/cleanup-files-from-specific-path-older-than-threshold-day

Let us know your feedback.

Thanks

Works great. I’ve also added into it the other RMM logs path to more cleaning in my environment.

Tried the same script procedure on my PC, however, it did not delete a single file from the rmmlogs folder. Suggestions?

Hi @pledgetech

https://scripts.comodo.com/frontend/web/topic/cleanup-files-from-specific-path-older-than-threshold-day

This script will not delete logs files which are in use by ITSM agent and files older than specified days. Please ensure these conditions and let us know the feedback.

Thanks
Kannan

I also modified the script to hit the files in the RMM logging location not just the ITSM location.

path=r"C:\Program Files (x86)\Comodo\Comodo ITSM\rmmlogs" ## Here mention the path of th file
Threshold_day='1' ## Here mention number of days to delete file more than given days
print "Threshold days given in the script: "+ Threshold_day
print "****************************************"
import os
import time
import ctypes
import datetime
from datetime import datetime

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

with disable_file_system_redirection():
    result=os.listdir(path)
    print '*********Cleanup logs **********'
    for i in result:          
        print "File Name: "+ i
        fpath=os.path.join(path,i);
        str_date= time.ctime(os.stat(fpath).st_ctime)
        d_date = datetime.strptime(str_date , '%a %b %d %H:%M:%S %Y')
        now = datetime.now()
        file2_date=now.strftime("%Y-%m-%d %H:%M:%S")
        d_d2 = datetime.strptime( file2_date,"%Y-%m-%d %H:%M:%S")
        file1_date = d_date.strftime("%Y-%m-%d %H:%M:%S")
        print "File Creation date: "+ file1_date
        print "Current date: "+ file2_date
        file2_date= datetime.strptime(file2_date, "%Y-%m-%d %H:%M:%S").date()
        file1_date= datetime.strptime(file1_date, "%Y-%m-%d %H:%M:%S").date()
        date_format = "%Y-%m-%d %H:%M:%S"
        day1 =  datetime.strptime("%s"%d_date, date_format)
        day2 =  datetime.strptime("%s"%d_d2, date_format)
        diff = day2 - day1
        days = diff.days
        j=(str(days))
        days_to_hours = days * 24
        No_days=(str(days))
        print "Number Of days from creation:"+ No_days
        diff_btw_two_times = (diff.seconds) / 3600
        overall_hours = days_to_hours + diff_btw_two_times
        No_hours=(str(overall_hours));
        if No_days > Threshold_day:
            try:
                    if os.path.isfile(fpath):
                        os.chmod(fpath,0644)
                        os.remove(fpath)
                        print "Deleting file:"+ i
                        print "Deleted Successfully"
            except Exception as error:
                print "The files working in another process:" +  fpath +"
"

path=r"C:\Program Files\COMODO\RMM Agent Service\logs" ## Here mention the path of th file

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

with disable_file_system_redirection():
    result=os.listdir(path)
    print '*********Cleanup logs **********'
    for i in result:          
        print "File Name: "+ i
        fpath=os.path.join(path,i);
        str_date= time.ctime(os.stat(fpath).st_ctime)
        d_date = datetime.strptime(str_date , '%a %b %d %H:%M:%S %Y')
        now = datetime.now()
        file2_date=now.strftime("%Y-%m-%d %H:%M:%S")
        d_d2 = datetime.strptime( file2_date,"%Y-%m-%d %H:%M:%S")
        file1_date = d_date.strftime("%Y-%m-%d %H:%M:%S")
        print "File Creation date: "+ file1_date
        print "Current date: "+ file2_date
        file2_date= datetime.strptime(file2_date, "%Y-%m-%d %H:%M:%S").date()
        file1_date= datetime.strptime(file1_date, "%Y-%m-%d %H:%M:%S").date()
        date_format = "%Y-%m-%d %H:%M:%S"
        day1 =  datetime.strptime("%s"%d_date, date_format)
        day2 =  datetime.strptime("%s"%d_d2, date_format)
        diff = day2 - day1
        days = diff.days
        j=(str(days))
        days_to_hours = days * 24
        No_days=(str(days))
        print "Number Of days from creation:"+ No_days
        diff_btw_two_times = (diff.seconds) / 3600
        overall_hours = days_to_hours + diff_btw_two_times
        No_hours=(str(overall_hours));
        if No_days > Threshold_day:
            try:
                    if os.path.isfile(fpath):
                        os.chmod(fpath,0644)
                        os.remove(fpath)
                        print "Deleting file:"+ i
                        print "Deleted Successfully"
            except Exception as error:
                print "The files working in another process:" +  fpath +"
"

I’ve used this to clean up 50GB of logs on some of my machines. I currently have it set to run daily but will likely move this to a disk space monitor to be triggered.