CHARON on Windows - Rotating log files cleaning

Version: 8 - Published 

Table of contents

Description

This Powershell script looks for defined virtual machines in the Charon Virtual Machines Manager and for those that have the log rotation feature enabled, it zip files older than a defined number of days and delete others older than a defined number of days (greater than the one for zipping files). In all cases it will keep at least the latest log files (by default the 3 latest log files are kept). It can also manage console log files.

(info) This document relates to script versions 1.11 & 1.12.

Installation

  • Create a folder dedicated to store the scripts, for example: "C:\Charon"
  • Download the Powershell script file from our SFTP server (please ask us connection credentials if you have no access) and move it to this folder.

Restrictions

  • Powershell V4.0 minimum is required for script to work (V2.0 and V3.0 not tested)
  • Powershell V5.1 minimum is required for the archive (zip) functionality. Archiving is skipped otherwise, only deletion will work.
  • Charon-AXP/VAX/PDP version 4.8 minimum
  • Preferably run the script as administrator to avoid permission issues while zipping or deleting files
  • OPA0 log files:
    • They are taken into account only if they are of course also rotating files
    • To use the virtual machine home directory, the OPA0 log file can be specified as: set OPA0 log=".\."
    • Only default log file names will be taken into account: <configuration_name>-OPA0-xxxxxx.log
  • SHUTDOWN log files are taken into account if the Charon Clean Shutdown utility is installed (and their are using the default naming)

  • Retention is based on date found in file names (example: pluto-2019-09-02-11-51-29-000000000.log) and not by last update time


(info) For more, see Powershell version, upgrade, enabling scripts execution, tips and tricks.

Allow execution on the Windows server

To run PowerShell scripts (files that end with .ps1), you must first set the execution policy to Unrestricted (This operation has to be done once).

To do so, open a PowerShell window as an Administrator and use the following command:

PS C:\Charon> Set-ExecutionPolicy Unrestricted

(info) The ExecutionPolicy can also be set to "RemoteSigned". In this case the .ps1 script files will have to be unblocked as described below.

If you are still prompted to allow for execution of the script, please run the following command to unblock the downloaded charon_logarchive.ps1 file:

PS C:\Charon> Unblock-File -path c:\charon\charon_logarchive.ps1

Parameters

  • Retention parameters:
    • -keep    = minimum number of log files to keep in all cases (default = 3, minimum = 1)
      • Overrides the next 2 parameters
      • Keeps the most recent ones
    • -daysarc = number of days to keep the log files before archiving (zipping) them (default = 35 days)
      • The script will look for all log files with .log extension, older than "daysarc" days, and will zip them (if possible)
    • -daysdel = number of days to keep the log and zipped files before deletion (default = 90 days)
      • The script will look for all log files with .log and .zip extension, older than "daysdel" days, and will delete them (if possible). The script will:
        • delete all zipped files older than "daysdel" days in case they have been zipped thanks to the "daysarc" parameter above
        • delete all log files older than "daysdel" days in case they have not been zipped (either too old, unzipped manually or if version of PowerShell is below 5.1)
      • Value must be greater than "daysarc". If this is not the case, the script will:
        • display a warning message,
        • force the value to be "daysarc + 7" and continue
    • -install = installation mode as a scheduled job (see chapter "CHARON on Windows - Rotating log files cleaning#Scheduled task mode" below)
  • Others:
    • -ini <file> = optional configuration file (see below) containing customized values for the retention parameters (keep, daysarc, daysdel). Please note if retention parameters are passed, they will overwrite the file content.
    • -help = display usage information


To display the script's help text, please use either this command:

PS C:\Charon> .\charon_logarchive.ps1 -help

or the Powershell "get-help" command:

PS C:\Charon> get-help c:\charon\charon_logarchive.ps1 -full

Optional configuration file

If you prefer to use a configuration file rather than passing parameters in the command line, a simple file with recommended extension .ini can be created. This file contains values for the 3 retention parameters defined above. Comments are allowed with a leading # character.

Example with file named pluto.ini:

#keep=5

daysarc=90

daysdel=180

Log file

All operations are displayed on the screen and logged in a log file located in the same folder as the Powershell script. The log file is named charon_logarchive.<ddd>.log where ddd corresponds to the first 3 characters of the day of the week (language settings dependent). If the file is older than 1 day, it is reset, allowing a rotation of one week.

Script execution

Interactive mode

  • Open a Powershell window, preferably run "as administrator" to be able to zip/delete log files without permission errors
  • Execute the script with either default retention parameters or define yours.

Example 1 - default parameters:

PS C:\Charon> .\charon_logarchive.ps1

Example 2 - custom parameters:

PS C:\Charon> .\charon_logarchive.ps1 -daysarc 120 -daysdel 365

Example 3 - using configuration file:

PS C:\Charon> .\charon_logarchive.ps1 -ini c:\charon\pluto.ini

Scheduled task mode

The script provides an auto-installer to create a scheduled job that will run each day at 1:00 AM.

Open a PowerShell window as Administrator (mandatory), run the following command and answer the questions:

PS C:\Charon> .\charon_logarchive.ps1 -install

Example:

PS C:\Charon> .\charon_logarchive.ps1 -install

[Info] Data folder found: 'C:\Program Files\CHARON\Toolkit\LogArchiver'
Please specify the following attributes
Number of days to keep the log files before archiving (zipping) them [100] :

Number of days to keep the log and zipped files before deletion [180] :

Minimum number of log files to keep in all cases [5] :

[Info] Scheduled task created in \CHARON folder, name: 'Charon logs archiving'
[Info] Runs everyday at 01:00 AM.

To change the settings after the scheduled job has been created, it is possible to rerun the script with "-install" option, this will update the already existing job.

(info) The installation will create a configuration file in the Charon installation folder under "Toolkit\LogArchiver" (usually "C:\Program Files\CHARON\Toolkit\LogArchiver") and named charon_logarchive.ini.

(lightbulb) The script script creates a log file named charon_logarchive.<ddd>.log" in the installation folder where <ddd> represents the weekday (so a week rotation).



© Stromasys, 1999-2024  - All the information is provided on the best effort basis, and might be changed anytime without notice. Information provided does not mean Stromasys commitment to any features described.