Skip to content

Commit

Permalink
oradb_manage_statspack: Redesign of role
Browse files Browse the repository at this point in the history
  • Loading branch information
Rendanic committed Mar 9, 2023
1 parent 558d2d0 commit b73db5f
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 176 deletions.
1 change: 1 addition & 0 deletions changelogs/fragments/ansible-doctor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ minor_changes:
- "orahost_logrotate: ansible-doctor (oravirt#318)"
- "oradb_manage_tablespace: added missing option mode for normal/sysdba connections (oravirt#318)"
- "oradb_manage_profiles: added missing option mode for normal/sysdba connections (oravirt#318)"
- "oradb_manage_statspack: major code refactoring (oravirt#318)"
34 changes: 27 additions & 7 deletions roles/oradb_manage_statspack/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# oradb_manage_statspack

Manage Statspack in Oracle
Manage Statspack in Oracle.

Not RAC aware at the moment.

## Table of content

Expand All @@ -20,6 +22,10 @@ Manage Statspack in Oracle

### purgedays

Global default variable for Statspack purgetime in days.
Could be defined for each database with `oracle_databases`.
An example is shown there.

#### Default value

```YAML
Expand All @@ -28,6 +34,10 @@ purgedays: 35
### purgeinterval
Global default variable for Statspack purge interval in scheduler job.
Could be defined for each database with `oracle_databases`.
An example is shown there.

#### Default value

```YAML
Expand All @@ -36,6 +46,10 @@ purgeinterval: FREQ=daily;byhour=3;byminute=15;bysecond=0

### snapinterval

Global default variable for Statspack snap interval in scheduler job.
Could be defined for each database with `oracle_databases`.
An example is shown there.

#### Default value

```YAML
Expand All @@ -44,6 +58,10 @@ snapinterval: FREQ=hourly;byminute=0;bysecond=0

### snaplevel

Global default variable for Statspack snaplevel.
Could be defined for each database with `oracle_databases`.
An example is shown there.

#### Default value

```YAML
Expand All @@ -52,15 +70,17 @@ snaplevel: 7

## Discovered Tags

**_spcdb_**
**_spcreate_**
**_spcreate_**\
 Install Statspack in nonCDB, CDB or PDB

**_spdrop_**
**_spdrop_**\
 Remove Statspack from nonCDB, CDB or PDB

**_spjob_**
**_spjob_**\
 Configure Statspack Scheduler Jobs

**_sppdb_**
**_statspack_**\
 Do all Tasks for Statspack


## Dependencies
Expand Down
29 changes: 23 additions & 6 deletions roles/oradb_manage_statspack/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
---
# Global Defaults for Purge and Snapshot-Jobs.
# could be overwritten per dtabase with:
# oracle_databases:
# - oracle_db_name: test
# statspack:
# purgedays: 60
# @var purgedays:description: >
# Global default variable for Statspack purgetime in days.
# Could be defined for each database with `oracle_databases`.
# An example is shown there.
# @end
purgedays: 35

# @var purgeinterval:description: >
# Global default variable for Statspack purge interval in scheduler job.
# Could be defined for each database with `oracle_databases`.
# An example is shown there.
# @end
purgeinterval: "FREQ=daily;byhour=3;byminute=15;bysecond=0"

# @var snapinterval:description: >
# Global default variable for Statspack snap interval in scheduler job.
# Could be defined for each database with `oracle_databases`.
# An example is shown there.
# @end
snapinterval: "FREQ=hourly;byminute=0;bysecond=0"

# @var snaplevel:description: >
# Global default variable for Statspack snaplevel.
# Could be defined for each database with `oracle_databases`.
# An example is shown there.
# @end
snaplevel: 7
108 changes: 96 additions & 12 deletions roles/oradb_manage_statspack/files/spcreate.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
#!/usr/bin/bash
#!/usr/bin/env bash
#
# List of return codes:
# 10 Error during spcreate
# 11 Login with / as sysdba not possible

set -euo pipefail

cd $HOME
function check_sqlplus_exec {
echo "Check sqlplus executable"
test -x "${SQLPLUS}"
}

env | grep ^ORACLE | sort
function execute_spcreate {
echo "Starting sqlplus"

PDB=${pdb_name:-"_"}

${ORACLE_HOME}/bin/sqlplus -S -L /nolog <<EOF
"${SQLPLUS}" -S -L /nolog <<EOF
conn / as sysdba
define perfstat_password=${perfstat_password}
define temporary_tablespace=${temporary_tablespace}
define default_tablespace=${default_tablespace}
define purgedates=${purgedates}
define snaplevel=${snaplevel}
define perfstat_password=${perfstat_password:?}
define temporary_tablespace=${temporary_tablespace:?}
define default_tablespace=${default_tablespace:?}
define purgedates=${purgedates:?}
define snaplevel=${snaplevel:?}
whenever sqlerror exit 1 rollback
whenever oserror exit 99 rollback
begin
if '${PDB}' <> '_' then
Expand All @@ -40,4 +48,80 @@ PROMPT Create custom index for selecting sql_id from STATS\$SQLTEXT
create index perfstat.STATS\$SQLTEXT_UK1 on STATS\$SQLTEXT(sql_id, piece);
exit
EOF
EOF
}

function spcreate {
echo ""
echo "Check for existing PERFSTAT user"
sqlcmd="conn / as sysdba
whenever sqlerror exit 10
begin
if '${PDB}' <> '_' then
execute immediate 'alter session set container = ${PDB}';
end if;
end;
/
declare
v_username varchar2(20);
begin
select username
into v_username
from dba_users
where username = 'PERFSTAT';
end;
/
"

if echo "${sqlcmd}" | "${SQLPLUS}" -S -L > /dev/null 2>&1 ; then
echo "User is existing. Skip installation of Statspack!"
else
execute_spcreate
echo "Installation of Statspack completed."
fi
}

function sqlplus_login {

echo ""
echo "Check for Login into Oracle Instance"

sqlcmd="conn / as sysdba
whenever sqlerror exit 11
begin
if '${PDB}' <> '_' then
execute immediate 'alter session set container = ${PDB}';
end if;
end;
/
"

if ! echo "${sqlcmd}" | "${SQLPLUS}" -S -L /nolog > /dev/null 2>&1 ; then
echo "Connect wint / as sysdba not possible or pluggable database not existing!"
exit 11
fi
}

###########################
###########################
cd "$HOME"

PDB=${pdb_name:-"_"}

echo ""
env | grep ^ORACLE | sort
echo ""
if [[ "${PDB}" == "_" ]] ; then
echo "Target: CDB"
else
echo "Target: PDB: ${PDB}"
fi
echo ""

SQLPLUS="${ORACLE_HOME}/bin/sqlplus"

check_sqlplus_exec
sqlplus_login
spcreate
87 changes: 81 additions & 6 deletions roles/oradb_manage_statspack/files/spdrop.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
#!/usr/bin/bash
#!/usr/bin/env bash
set -euo pipefail

cd $HOME
function sqlplus_login {

env | grep ^ORACLE | sort
echo ""
echo "Check for Login into Oracle Instance"

PDB=${pdb_name:-"_"}
sqlcmd="conn / as sysdba
whenever sqlerror exit 11
begin
if '${PDB}' <> '_' then
execute immediate 'alter session set container = ${PDB}';
end if;
end;
/
"

${ORACLE_HOME}/bin/sqlplus -S -L /nolog <<EOF
if ! echo "${sqlcmd}" | "${SQLPLUS}" -S -L /nolog > /dev/null 2>&1 ; then
echo "Connect with / as sysdba not possible or pluggable database not existing!"
exit 11
fi
}

function spdrop {
echo ""
echo "Check for existing PERFSTAT user"
sqlcmd="conn / as sysdba
whenever sqlerror exit 10
begin
if '${PDB}' <> '_' then
execute immediate 'alter session set container = ${PDB}';
end if;
end;
/
declare
v_username varchar2(20);
begin
select username
into v_username
from dba_users
where username = 'PERFSTAT';
end;
/
"

if ! echo "${sqlcmd}" | "${SQLPLUS}" -S -L > /dev/null 2>&1 ; then
echo "User is not existing. Nothing to do."
exit 0
else

sqlcmd="
conn / as sysdba
whenever oserror exit 1 rollback
Expand All @@ -20,4 +64,35 @@ end;
@?/rdbms/admin/spdrop
exit
EOF
"

if echo "${sqlcmd}" | "${SQLPLUS}" -S -L /nolog ; then
echo "Statspack dropped"
else
echo "Failure during statspack drop."
exit 12
fi

fi

}
###########################
###########################
cd "$HOME"

PDB=${pdb_name:-"_"}

echo ""
env | grep ^ORACLE | sort
echo ""
if [[ "${PDB}" == "_" ]] ; then
echo "Target: CDB"
else
echo "Target: PDB: ${PDB}"
fi
echo ""

SQLPLUS="${ORACLE_HOME}/bin/sqlplus"

sqlplus_login
spdrop
5 changes: 4 additions & 1 deletion roles/oradb_manage_statspack/meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
---
# @meta description: >
# Manage Statspack in Oracle
# Manage Statspack in Oracle.
#
# Not RAC aware at the moment.
# @end
# @meta author: [Thorsten Bruhns]
# #todo improvement: Add support for RAC Setups
galaxy_info:
role_name: oradb_manage_statspack
author: Thorsten Bruhns
Expand Down
Loading

0 comments on commit b73db5f

Please sign in to comment.