AU14ED02-Scripting Hacks
AU14ED02-Scripting Hacks
Roland Schock
ARS Computer und Consulting GmbH
Session Code: ED02
2014-09-12 | Platform: DB2 for LUW
#IDUG
2
This session
• Get started with DB2 command line, CLPplus and the system
commands
• Shells, platform differences, shell variables, proper quoting and
calling db2 properly
• Pipes and Redirection
• Exploit GNU text utilities to get things done easily.
• Samples and Goodies
#IDUG
3
• Quote:
"The removal of Control Center
improved our command line skills"
#IDUG
4
Motivation
Motivation (cont.)
• Perils of a consultant
• When you are called to customers to help; you don‘t want to tell them,
we need a particular tools suite that costs $$$ to be able to help
• We have to find ways work with the tools provided by db2
• Perhaps, you can convince the customer to use some free tools,
we can also leave behind without license fee issues
• All roads lead to Rome. ()
• TIMTOWTDI = "There is more than one way to do it"
So our examples might not be perfect… ;-)
• We can review some samples a bit further in the presentation
#IDUG
6
Motivation (cont.)
• Options can be used To exit db2 interactive mode, type QUIT at the command prompt. Outside
interactive mode, all commands must be prefixed with 'db2'.
('-t' for termination To list the current command option settings, type LIST COMMAND OPTIONS.
char ';') For more detailed help, refer to the Online Reference Manual.
db2 =>
#IDUG
9
• Trick: start 'db2 -t' to copy and paste db2 commands interactively
from other scripts
• Hint: Check for other useful parameters via 'db2 list command options'
• Put your favorites in an environment variable:
export DB2OPTIONS='+a -c +ec -o -p'
• Use ! to spawn any shell command from inside CLP
• Within the DB2 shell, you can start logging with
UPDATE COMMAND OPTIONS USING Z ON outfile.txt V ON
#IDUG
13
• Use the return code of db2 for error handling in your scripts
• Code Description
0 DB2 command or SQL statement executed successfully
1 SELECT or FETCH statement returned no rows
2 DB2 command or SQL statement warning
4 DB2 command or SQL statement error
8 Command line processor system error
#IDUG
14
CLPPlus
CLPPlus Start
CLPPlus Commands
CLPPlus Example
#IDUG
18
• db2pd
• Direct access to DB2 system memory (i.e. dirty uncommitted read ;-)
• Low level details, but very helpful for analysis
• Samples:
• db2pd -db SAMPLE -locks // check for locks
• db2pd -help | more
• db2pd -db SAMPLE -tcbstats // lists table control block statistics
• db2pd -edus interval=10 top=5 // top five CPU consumers of last 10 secs
• db2pd -alldbs -hadr | grep -e onnected -e artition -e "andby " -e "rimary "
// multiple dbs
#IDUG
20
• db2diag
• db2diag –H // displays last 30 minutes of the db2diag.log
• db2diag –A // archives the db2diag.log and creates a new one
• db2diag –g db=TEST // shows the entries only for the Database TEST
• db2diag -l severe -H 2d // last 2 days of severe errors
• db2diag -gvi msg:=DB2_MAX_INACT_STMTS | more
• db2diag -gi data:="password validation" -fmt "%{tsmonth}-%{tsday}-
%{tshour}.%{tsmin}.%{tssec}\t%{database}\t%{data}" | sed
"s/DATA.*bytes//;N;s/\n//"
2013-07-22-15.51.33 SAMPLE Password validation for user janedoo failed with rc = -2146500507
2013-07-22-15.51.59 SAMPLE Password validation for user james007 failed with rc = -2146500502
#IDUG
21
• Platform differences
• Windows CMD, PowerShell, (Posix-Shell with R2)
• Linux: bash, csh, sh, pdksh, …
• AIX: ksh, bash, csh, …
• Solaris, HP-UX and other Unix dialects… ;-)
• set DB2INSTANCE=db2
• echo %DB2Instance%
• Variables are not case sensitive
• export DB2INSTANCE=db2
• echo $DB2INSTANCE
• Shell is case sensitive about variable names
• Some characters are interpreted in the shell and may need quotes
• Watch out with *, ?, %, &, |, <, >, \, ", !, ^, =, ~, $, , , {, }, (, ), ', `
• DB2 uses single quotes for Text ' '
• db2 UPDATE employee SET lastname='Smith' where ID='00210'
• Shell uses double quotes " ", but can also use single quotes
• db2 'select count(*) from employee'
• Brackets have different meaning in a Unix shell:
• tar cf - * | (mkdir /tmp/demo ; cd /tmp/demo ; tar xvf - )
#IDUG
28
• Nesting can be tricky! Here we add quotes around our reorg command:
• Windows:
db2 "select 'db2 ""reorg table '||rtrim(tabschema)||'.'||rtrim(tabname)||' "" '
from syscat.tables where stats_time < current timestamp - 5 days"
• Unix:
db2 "select 'db2 " ' " ' "reorg table '||rtrim(tabschema)||'.'||rtrim(tabname)||'
" ' " ' " ' from syscat.tables where stats_time < current timestamp - 5 days"
• Or if you know the ASCII codes… ;-)
db2 "select 'db2 '||chr(34)||'reorg table
'||rtrim(tabschema)||'.'||rtrim(tabname)||chr(34) from syscat.tables where
stats_time < current timestamp - 5 days"
• All of the above commands can be directly used in your shell or in a script
#IDUG
29
• Batch files can get input parameters from the command line
• Windows use them as %1, %2, etc.
• Windows Shell Extensions can use e.g. %~x1 to split filenames in
parameters
• Concatenation of commands
• Sending output from one command to some other tool via pipes
• db2 list applications | more
• Multiple pipes
• All commands in the pipe can run at the same time
• Check for DB2 msgs in loads:
cat LOAD*.msg | grep "^DB2" | sort | uniq –c
• Conditional execution
• db2start && db2 connect to SAMPLE
• Using return codes for error handling
#IDUG
32
Standard streams
• Very useful to count output lines (-l) or maximum line lenght (-L)
number of chars (-c) or number of words (-w)
• Findout how many dbs are running as HADR Primary on this machine
db2pd -hadr -alldbs | grep -e 'rimary ' | wc –l
• Number of connections from certain IP range
db2 list applications | fgrep 172.25. | wc -l
#IDUG
42
• Part 4 – Samples
#IDUG
49
• Samples of all things you can play around and customize to your
needs
• Quick way find out what DDL was executed on your DB
• db2 list history all for db MYPRODDB | grep DDL
• Check if your instance is running
• ps -ef | grep $DB2INSTANCE | grep db2sysc | grep -v grep | wc –l
• db2pd -agents | grep -e Partition -e Unable
• Setup – Part 2:
• Log onto Machine "B"
• Secure copy the id_dsa.pub key across to the local machine
• Now look at the file /~.ssh/id_dsa.pub, if it starts with "ssh-dss", ie
• ssh-dss
AAAAB3NzaC1kc3MAAAC+CLO2M9OfcIjEaFBJ+cNAubJeCw8dtlH
• Then append it to the file ./ssh/authorized_keys2 and ./ssh2/authorized_keys2
• cat id_dsa.pub >> ~/.ssh/authorized_keys2
• cat id_dsa.pub >> ~/.ssh2/authorized_keys2
• change permissions on files to user read/write only
• Test – now you will be able to ssh from Machine A to Machine B
(no password req.)
#IDUG
58
Useful links
Roland Schock
ARS Computer und Consulting GmbH
schock@ars.de
• Appendix
#IDUG
62