Wednesday, November 16, 2022

Why and How to check list if file exist or not in Windows?

Why ?

I have a system will generate a list of files in daily bases and I need to make sure that these file got generated by running a CMD script to check if file exist on that folder or not , this purpose can be achieved using different way however my monitoring was only possible to be using CMS, thus, I have wrote the below code to achieve this goal .

How ?

Below is a Windows CMD code that will check 19 files with pattern file{date} and date format is differ in some files, If you run the code against these file you will be reported if it is exist or not and you may custom the code to achieve different goal as well ,:

1- Code below:

@echo off

for /f "tokens=2 delims==" %%G in ('wmic os get localdatetime /value') do set datetime=%%G

 

set year=%datetime:~0,4%

set month=%datetime:~4,2%

set day=%datetime:~6,2%

 

echo checking files for today %year%/%month%/%day%%time%

 

 

::MonthName.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::

setlocal EnableDelayedExpansion

for /F "tokens=1-4 delims=.-/ " %%A in ("%DATE%") do set month=%%B

set monstring=JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC

set /a mm=(100%month%%%100)*3-3

:: Use a call to get %mm% evaluated first.

call set mmm=%%monstring:~%mm%,3%%

::echo Monthname=%mmm%

::MonthName.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::

 

set elem[0]=file1_%year%%month%%day%.txt

set elem[1]=file2_%day%%mmm%%year%

set elem[2]=file3%day%%mmm%%year%

set elem[3]=file4%day%%mmm%%year%

set elem[4]=file5_%day%%mmm%%year%

set elem[5]=file6%year%%month%%day%.txt

set elem[6]=file7%year%%month%%day%.txt

set elem[7]=file8_%year%%month%%day%.txt

set elem[8]=file9_%year%%month%%day%.txt

set elem[9]=file10_%day%%mmm%%year%

set elem[10]=file11%day%%mmm%%year%

set elem[11]=file12%day%%mmm%%year%

set elem[12]=%day%%mmm%%year%

set elem[13]=file13%day%%mmm%%year%.txt

set elem[14]=file14%day%%mmm%%year%.txt

set elem[15]=file15_%year%%month%%day%.txt

set elem[16]=file16_%year%%month%%day%.txt

set elem[17]=file17_%year%%month%%day%.txt

set elem[18]=file18_%day%%mmm%%year%

 

cd C:{path_to_files}\files_generation_checkup\files

::@echo on

for /L %%a in (0,1,18) do (

if exist !elem[%%a]! (call echo FILE %%elem[%%a]%% IS EXISTS) ELSE (call echo FILE %%elem[%%a]%% DOES NOT EXIST)

)

::if not exist !elem[%%a]! (call echo FILE %%elem[%%a]%% DOES NOT EXIST)

::echo !elem[%%a]!

pause

Thursday, November 10, 2022

Why and How to get a list of none Oracle maintained users ?

Why ?

In oracle database there are users created and managed by oracle and there are another users created by administrator, starting from 12c there is a new field in dba_users that categorized these user maintained by oracle or not , below I will show a query for both version.

How ?

Below are select statement that will list the administrator create user and not maintained by oracle users only :

1- Oracle 12c and above you can directory used this query:

select* from dba_users where oracle_maintained = 'N'

ORDER BY 10 ASC NULLS LAST;

 

2- 11g and below you may use the below query where assumption for database create date and below is oracle maintained because these users are created during the installation and rest of users will be administrator created..

SELECT *

FROM dba_users

WHERE TRUNC(created) > (SELECT MIN(TRUNC(created))

FROM dba_users)

ORDER BY 10 ASC NULLS LAST;

Why and How to get last modified file in Windows PowerShell?

Why ?

If you have a man logs file in same folder and it is rotated many times that may lead you have more than 10 files in the same day, you may have a case to check file each minute to ensure for example if any error or for any other reason, it will be a good optimizing in your code to test last file instead of checking all generated file.

How ?

Below is sample script to get the last modified file from wildcard list:

1- using the below PowerShell code, the last output will the last modified file, I am assuming you have a list of file with date/time in same folder:

$log_path = "{path_to_files}"

$log_files =  "{file_name_txt}"+(Get-Date).ToString('yyyyMMdd')+"*.log";

$logfile_name =  Get-ChildItem -Path $log_path -Recurse -Filter $log_files  | sort LastWriteTime |select -last 1  | select-object -expandproperty name;

$logfile_path = $log_path+$logfile_name;

 

print $logfile_path

Tuesday, October 4, 2022

Why and How to create a logrotate in Linux?

Why ?

In Linux one of the great feature that you can manage and organize any log file to be rotated on any criteria , for example if you have web service that will create a file.log and you want to have instead a daily file and nominated with that date , or/and have a specify size and/or number of files ..etc , you may use logrotate feature to achieve this.

How ?

Below are sample script to rotate a catalina.out file which is the main tomcat log file in daily bases and compress it and keep it for one year, the new file will be date only and orginal file will be kept same as it  ,:

1- may use man to check all features of logrotate:

man logrotate

2- create the configuration file under logrotate.d :

vi /etc/logrotate.d/tomcat

/<path_to_logs>/catalina.out

{

su user group

copytruncate

compress

daily

missingok

delaycompress

dateext

maxage 365

}

3- You may force the logrotate to run immediately instead of wait for next day.

logrotate –fv /etc/logrotate.d/tomcat

 

Monday, October 3, 2022

Why and How to add Oracle user to TFA access list?

 Why?

Trace File Analyzer (TFA) is a tools provided by Oracle as a part of diagnostic free tools , now a day it is part of Automatic Health Framework (AHF), in RAC environment grid user will be added by default however if you need to run this to collect any issue in DB home and you are installing this using different user (oracle for DB and grid for Grid ), then you have to add oracle user to the access of TFA or it will failed to run and show you a message about this , below in simple steps I am shown how to add this user .

How?

Below are steps to check and grant Oracle access to TFA:

1- Check the tfactl status using root or grid user, a waring for old TFA you may got if it is old, then upgrade is recommended:

tfactl status

WARNING - TFA Software is older than 180 days. Please consider upgrading TFA to the latest version.

 

.---------------------------------------------------------------------------------------------.

| Host   | Status of TFA | PID  | Port | Version    | Build ID             | Inventory Status |

+--------+---------------+------+------+------------+----------------------+------------------+

| <hostname> | RUNNING       | 5648 | 5000 | 18.1.2.0.0 | 18120020180510233906 | COMPLETE         |

| <hostname2> | RUNNING       | 6230 | 5000 | 18.1.2.0.0 | 18120020180510233906 | COMPLETE         |

'--------+---------------+------+------+------------+----------------------+------------------'

2- Check the path of this tfactl:

[root@<hosname> ~]# which tfactl

/u01/app/12.1.0/grid/bin/tfactl

3- Use help to see all commands for tfactl and check the add user command.

Usage : /u01/app/12.1.0/grid/bin/tfactl <command> [options]

    commands:diagcollect|collection|analyze|ips|run|start|stop|enable|disable|status|print|access|purge|directory|host|receiver|set|toolstatus|uninstall|diagnosetfa|syncnodes|setupmos|upload

For detailed help on each command use:

  /u01/app/12.1.0/grid/bin/tfactl <command> -help

access -help

 

Add or Remove or List TFA Users and Groups

 

Usage : /u01/app/12.1.0/grid/bin/tfactl access <command> [options]

    commands:lsusers|add|remove|block|unblock|enable|disable|reset|removeall

For detailed help on each command use:

  /u01/app/12.1.0/grid/bin/tfactl access <command> -help

 

 

4- List the current user:

                                                                                       tfactl> access lsusers

.---------------------------------.

|       TFA Users in <hosname1>   |

+-----------+-----------+---------+

| User Name | User Type | Status  |

+-----------+-----------+---------+

| grid      | USER      | Allowed |

'-----------+-----------+---------'

 

.---------------------------------.

|       TFA Users in <hostname2>  |

+-----------+-----------+---------+

| User Name | User Type | Status  |

+-----------+-----------+---------+

| grid      | USER      | Allowed |

'-----------+-----------+---------'

 

5- Add oracle user to the list:

 

tfactl> access add -user oracle

Successfully added 'oracle' to TFA Access list.

.---------------------------------.

|       TFA Users in <hostname1>  |

+-----------+-----------+---------+

| User Name | User Type | Status  |

+-----------+-----------+---------+

| grid      | USER      | Allowed |

| oracle    | USER      | Allowed |

'-----------+-----------+---------'

.---------------------------------.

|       TFA Users in <hostname2>  |

+-----------+-----------+---------+

| User Name | User Type | Status  |

+-----------+-----------+---------+

| grid      | USER      | Allowed |

| oracle    | USER      | Allowed |

'-----------+-----------+---------'

 

Why and How to install Grid 19c on RHEL 8?

  Why ? Simply we will be requested to install Oracle Grid RAC DB on Redhat RHEL 8, below is my note for this installation . How ? 1-  OS in...