Merge lp:~percona-toolkit-dev/percona-toolkit/fix-sleep-bug-979092 into lp:percona-toolkit/2.0
- fix-sleep-bug-979092
- Merge into 2.0
Proposed by
Daniel Nichter
Status: | Merged |
---|---|
Merged at revision: | 241 |
Proposed branch: | lp:~percona-toolkit-dev/percona-toolkit/fix-sleep-bug-979092 |
Merge into: | lp:percona-toolkit/2.0 |
Diff against target: |
66121 lines (+52092/-8394) (has conflicts) 341 files modified
Changelog (+21/-0) MANIFEST (+2/-0) Makefile.PL (+1/-1) bin/pt-align (+1/-1) bin/pt-archiver (+1/-1) bin/pt-config-diff (+1/-1) bin/pt-deadlock-logger (+1/-1) bin/pt-diskstats (+1/-1) bin/pt-duplicate-key-checker (+171/-92) bin/pt-fifo-split (+1/-1) bin/pt-find (+1/-1) bin/pt-fingerprint (+2143/-0) bin/pt-fk-error-logger (+1/-1) bin/pt-heartbeat (+1/-1) bin/pt-index-usage (+170/-92) bin/pt-ioprofile (+1/-1) bin/pt-kill (+1/-1) bin/pt-log-player (+1/-1) bin/pt-mext (+1/-1) bin/pt-mysql-summary (+2079/-679) bin/pt-online-schema-change (+4617/-1243) bin/pt-pmp (+1/-1) bin/pt-query-advisor (+248/-372) bin/pt-query-digest (+9/-9) bin/pt-show-grants (+1/-1) bin/pt-sift (+1/-1) bin/pt-slave-delay (+1/-1) bin/pt-slave-find (+1/-1) bin/pt-slave-restart (+1/-1) bin/pt-stalk (+1/-1) bin/pt-summary (+2019/-836) bin/pt-table-checksum (+191/-81) bin/pt-table-sync (+554/-405) bin/pt-table-usage (+7223/-0) bin/pt-tcp-model (+1/-1) bin/pt-trend (+1/-1) bin/pt-upgrade (+1/-1) bin/pt-variable-advisor (+1/-1) bin/pt-visual-explain (+1/-1) config/deb/changelog (+23/-0) config/sphinx-build/conf.py (+2/-2) docs/percona-toolkit.pod (+12/-4) docs/release_notes.rst (+48/-0) lib/CleanupTask.pm (+7/-2) lib/Cxn.pm (+1/-1) lib/MySQLStatusWaiter.pm (+31/-1) lib/NibbleIterator.pm (+166/-28) lib/OSCCaptureSync.pm (+0/-142) lib/OobNibbleIterator.pm (+1/-1) lib/QueryAdvisorRules.pm (+2/-2) lib/QueryRewriter.pm (+24/-4) lib/SQLParser.pm (+103/-8) lib/Schema.pm (+5/-5) lib/SchemaIterator.pm (+26/-44) lib/SlowLogWriter.pm (+2/-2) lib/TableUsage.pm (+1060/-0) lib/bash/alt_cmds.sh (+25/-1) lib/bash/collect_mysql_info.sh (+263/-0) lib/bash/collect_system_info.sh (+574/-0) lib/bash/log_warn_die.sh (+6/-0) lib/bash/report_formatting.sh (+117/-0) lib/bash/report_mysql_info.sh (+1391/-0) lib/bash/report_system_info.sh (+1050/-0) lib/bash/summary_common.sh (+155/-0) t/lib/CleanupTask.t (+18/-1) t/lib/MySQLStatusWaiter.t (+41/-0) t/lib/NibbleIterator.t (+44/-3) t/lib/OSCCaptureSync.t (+0/-131) t/lib/OobNibbleIterator.t (+8/-3) t/lib/QueryRewriter.t (+59/-1) t/lib/SQLParser.t (+1/-1) t/lib/SchemaIterator.t (+9/-50) t/lib/SlowLogParser.t (+63/-63) t/lib/TableUsage.t (+816/-0) t/lib/bash/collect_mysql_info.sh (+197/-0) t/lib/bash/collect_system_info.sh (+307/-0) t/lib/bash/report_formatting.sh (+109/-0) t/lib/bash/report_mysql_info.sh (+734/-0) t/lib/bash/report_system_info.sh (+1568/-0) t/lib/bash/summary_common.sh (+79/-0) t/lib/samples/SchemaIterator/all-dbs-tbls-5.0.txt (+421/-0) t/lib/samples/SchemaIterator/all-dbs-tbls.txt (+486/-0) t/lib/samples/SchemaIterator/resume-from-ignored-sakila-payment-5.0.txt (+52/-0) t/lib/samples/SchemaIterator/resume-from-ignored-sakila-payment.txt (+49/-0) t/lib/samples/SchemaIterator/resume-from-sakila-payment-5.0.txt (+70/-0) t/lib/samples/SchemaIterator/resume-from-sakila-payment.txt (+66/-0) t/lib/samples/slowlogs/slow002.txt (+16/-16) t/lib/samples/slowlogs/slow003.txt (+2/-2) t/lib/samples/slowlogs/slow005.txt (+2/-2) t/lib/samples/slowlogs/slow006.txt (+12/-12) t/lib/samples/slowlogs/slow007.txt (+2/-2) t/lib/samples/slowlogs/slow022.txt (+12/-12) t/lib/samples/slowlogs/slow030.txt (+899/-899) t/lib/samples/slowlogs/slow032-rewritten.txt (+2/-2) t/lib/samples/slowlogs/slow032.txt (+2/-2) t/lib/samples/slowlogs/slow034.txt (+18/-18) t/lib/samples/slowlogs/slow035.txt (+2/-2) t/pt-deadlock-logger/basics.t (+1/-1) t/pt-fingerprint/basics.t (+101/-0) t/pt-fingerprint/samples/query001 (+2/-0) t/pt-fingerprint/samples/query001.fingerprint (+1/-0) t/pt-fingerprint/samples/query002 (+2/-0) t/pt-fingerprint/samples/query002.fingerprint (+1/-0) t/pt-log-player/samples/log001.txt (+16/-16) t/pt-mysql-summary/find_my_cnf_file.sh (+0/-20) t/pt-mysql-summary/format_binlog_filters.sh (+0/-12) t/pt-mysql-summary/format_innodb_status.sh (+0/-147) t/pt-mysql-summary/format_overall_db_stats.sh (+0/-61) t/pt-mysql-summary/format_status_variables.sh (+0/-97) t/pt-mysql-summary/fuzz.sh (+0/-7) t/pt-mysql-summary/get_mysql_info.sh (+0/-26) t/pt-mysql-summary/parse_mysqld_instances.sh (+0/-47) t/pt-mysql-summary/pretty_print_cnf_file.sh (+0/-40) t/pt-mysql-summary/pt-mysql-summary.t (+54/-2) t/pt-mysql-summary/samples/expected_output_temp002.txt (+276/-0) t/pt-mysql-summary/samples/expected_output_temp003.txt (+219/-0) t/pt-mysql-summary/samples/expected_output_temp004.txt (+218/-0) t/pt-mysql-summary/samples/expected_output_temp005.txt (+291/-0) t/pt-mysql-summary/samples/expected_result_report_summary.txt (+257/-0) t/pt-mysql-summary/samples/mysql-variables-with-semisync.txt (+326/-0) t/pt-mysql-summary/samples/temp001/mysql-status (+304/-0) t/pt-mysql-summary/samples/temp001/mysql-variables (+356/-0) t/pt-mysql-summary/samples/temp002/innodb-status (+118/-0) t/pt-mysql-summary/samples/temp002/mysql-config-file (+26/-0) t/pt-mysql-summary/samples/temp002/mysql-databases (+6/-0) t/pt-mysql-summary/samples/temp002/mysql-plugins (+35/-0) t/pt-mysql-summary/samples/temp002/mysql-processlist (+12/-0) t/pt-mysql-summary/samples/temp002/mysql-status (+370/-0) t/pt-mysql-summary/samples/temp002/mysql-status-defer (+370/-0) t/pt-mysql-summary/samples/temp002/mysql-users (+1/-0) t/pt-mysql-summary/samples/temp002/mysql-variables (+372/-0) t/pt-mysql-summary/samples/temp002/mysqld-instances (+4/-0) t/pt-mysql-summary/samples/temp002/mysqldump (+396/-0) t/pt-mysql-summary/samples/temp003/innodb-status (+77/-0) t/pt-mysql-summary/samples/temp003/mysql-config-file (+26/-0) t/pt-mysql-summary/samples/temp003/mysql-databases (+2/-0) t/pt-mysql-summary/samples/temp003/mysql-master-logs (+1/-0) t/pt-mysql-summary/samples/temp003/mysql-master-status (+1/-0) t/pt-mysql-summary/samples/temp003/mysql-plugins (+10/-0) t/pt-mysql-summary/samples/temp003/mysql-processlist (+9/-0) t/pt-mysql-summary/samples/temp003/mysql-status (+291/-0) t/pt-mysql-summary/samples/temp003/mysql-status-defer (+291/-0) t/pt-mysql-summary/samples/temp003/mysql-users (+1/-0) t/pt-mysql-summary/samples/temp003/mysql-variables (+285/-0) t/pt-mysql-summary/samples/temp003/mysqld-instances (+2/-0) t/pt-mysql-summary/samples/temp004/innodb-status (+77/-0) t/pt-mysql-summary/samples/temp004/mysql-config-file (+26/-0) t/pt-mysql-summary/samples/temp004/mysql-databases (+3/-0) t/pt-mysql-summary/samples/temp004/mysql-master-logs (+2/-0) t/pt-mysql-summary/samples/temp004/mysql-master-status (+1/-0) t/pt-mysql-summary/samples/temp004/mysql-plugins (+10/-0) t/pt-mysql-summary/samples/temp004/mysql-processlist (+9/-0) t/pt-mysql-summary/samples/temp004/mysql-status (+291/-0) t/pt-mysql-summary/samples/temp004/mysql-status-defer (+291/-0) t/pt-mysql-summary/samples/temp004/mysql-users (+1/-0) t/pt-mysql-summary/samples/temp004/mysql-variables (+285/-0) t/pt-mysql-summary/samples/temp004/mysqld-instances (+2/-0) t/pt-mysql-summary/samples/temp005/innodb-status (+108/-0) t/pt-mysql-summary/samples/temp005/mysql-config-file (+26/-0) t/pt-mysql-summary/samples/temp005/mysql-databases (+3/-0) t/pt-mysql-summary/samples/temp005/mysql-master-logs (+1/-0) t/pt-mysql-summary/samples/temp005/mysql-master-status (+1/-0) t/pt-mysql-summary/samples/temp005/mysql-plugins (+28/-0) t/pt-mysql-summary/samples/temp005/mysql-processlist (+18/-0) t/pt-mysql-summary/samples/temp005/mysql-status (+304/-0) t/pt-mysql-summary/samples/temp005/mysql-status-defer (+304/-0) t/pt-mysql-summary/samples/temp005/mysql-users (+1/-0) t/pt-mysql-summary/samples/temp005/mysql-variables (+363/-0) t/pt-mysql-summary/samples/temp005/mysqld-executables (+1/-0) t/pt-mysql-summary/samples/temp005/mysqld-instances (+4/-0) t/pt-mysql-summary/samples/temp005/mysqldump (+1084/-0) t/pt-mysql-summary/samples/tempdir/innodb-status (+77/-0) t/pt-mysql-summary/samples/tempdir/mysql-config-file (+26/-0) t/pt-mysql-summary/samples/tempdir/mysql-databases (+3/-0) t/pt-mysql-summary/samples/tempdir/mysql-master-logs (+3/-0) t/pt-mysql-summary/samples/tempdir/mysql-master-status (+1/-0) t/pt-mysql-summary/samples/tempdir/mysql-plugins (+10/-0) t/pt-mysql-summary/samples/tempdir/mysql-processlist (+9/-0) t/pt-mysql-summary/samples/tempdir/mysql-status (+291/-0) t/pt-mysql-summary/samples/tempdir/mysql-status-defer (+291/-0) t/pt-mysql-summary/samples/tempdir/mysql-users (+5/-0) t/pt-mysql-summary/samples/tempdir/mysql-variables (+283/-0) t/pt-mysql-summary/samples/tempdir/mysqld-instances (+4/-0) t/pt-mysql-summary/samples/tempdir/mysqldump (+328/-0) t/pt-mysql-summary/samples/tempdir/tempfile (+130/-0) t/pt-mysql-summary/summarize_binlogs.sh (+0/-13) t/pt-mysql-summary/summarize_processlist.sh (+0/-64) t/pt-online-schema-change/alter_active_table.t (+56/-46) t/pt-online-schema-change/basics.t (+541/-218) t/pt-online-schema-change/check_tables.t (+0/-126) t/pt-online-schema-change/option_sanity.t (+12/-12) t/pt-online-schema-change/samples/basic_no_fks.data (+500/-500) t/pt-online-schema-change/samples/basic_no_fks.sql (+30/-0) t/pt-online-schema-change/samples/basic_with_fks.sql (+56/-0) t/pt-online-schema-change/samples/fk_tables_schema.sql (+0/-31) t/pt-online-schema-change/samples/query_table.pl (+8/-5) t/pt-online-schema-change/samples/small_table.sql (+0/-27) t/pt-online-schema-change/sanity_checks.t (+113/-0) t/pt-query-digest/read_timeout.t (+1/-1) t/pt-query-digest/review.t (+4/-4) t/pt-query-digest/samples/save-results/slow002-limit-3.txt (+4/-4) t/pt-query-digest/samples/save-results/slow002.txt (+2/-2) t/pt-query-digest/samples/save-results/slow006.txt (+1/-1) t/pt-query-digest/samples/slow-issue-611.txt (+2/-2) t/pt-query-digest/samples/slow002-orderbynonexistent.txt (+6/-6) t/pt-query-digest/samples/slow002_iters_2.txt (+1/-1) t/pt-query-digest/samples/slow002_orderbyreport.txt (+2/-2) t/pt-query-digest/samples/slow002_report.txt (+6/-6) t/pt-query-digest/samples/slow002_report_filtered.txt (+1/-1) t/pt-query-digest/samples/slow006-first2.txt (+8/-8) t/pt-query-digest/samples/slow006-order-by-re.txt (+2/-2) t/pt-query-digest/samples/slow006_AR_1.txt (+2/-2) t/pt-query-digest/samples/slow006_AR_2.txt (+1/-1) t/pt-query-digest/samples/slow006_AR_4.txt (+2/-2) t/pt-query-digest/samples/slow006_AR_5.txt (+1/-1) t/pt-query-digest/samples/slow006_report.txt (+2/-2) t/pt-query-digest/samples/slow034-inheritance.txt (+18/-18) t/pt-query-digest/samples/slow034-no-ts-inheritance.txt (+18/-18) t/pt-query-digest/samples/slow034-order-by-Locktime-sum-with-Locktime-distro.txt (+8/-8) t/pt-query-digest/samples/slow034-order-by-Locktime-sum.txt (+8/-8) t/pt-query-digest/samples/slow035.txt (+1/-1) t/pt-stalk/pt-stalk.t (+1/-1) t/pt-summary/format_vmstat.sh (+0/-37) t/pt-summary/parse_arcconf.sh (+0/-176) t/pt-summary/parse_dmidecode_mem_devices.sh (+0/-104) t/pt-summary/parse_ethernet_controller_lspci.sh (+0/-11) t/pt-summary/parse_fdisk.sh (+0/-16) t/pt-summary/parse_filesystems.sh (+0/-52) t/pt-summary/parse_free_minus_b.sh (+0/-67) t/pt-summary/parse_fusionmpt_lsiutil.sh (+0/-50) t/pt-summary/parse_hpacucli.sh (+0/-26) t/pt-summary/parse_ip_s_link.sh (+0/-28) t/pt-summary/parse_lsi_megaraid.sh (+0/-696) t/pt-summary/parse_netstat.sh (+0/-49) t/pt-summary/parse_proc_cpuinfo.sh (+0/-74) t/pt-summary/parse_raid_controller_dmesg.sh (+0/-32) t/pt-summary/parse_raid_controller_lspci.sh (+0/-39) t/pt-summary/parse_virtualization_dmesg.sh (+0/-10) t/pt-summary/pt-summary.t (+12/-2) t/pt-summary/samples/BSD/freebsd_001/mounted_fs (+7/-0) t/pt-summary/samples/BSD/freebsd_001/notable_procs (+2/-0) t/pt-summary/samples/BSD/freebsd_001/processes (+10/-0) t/pt-summary/samples/BSD/freebsd_001/summary (+10/-0) t/pt-summary/samples/BSD/freebsd_001/sysctl (+1481/-0) t/pt-summary/samples/BSD/freebsd_001/uptime (+1/-0) t/pt-summary/samples/BSD/freebsd_001/vmstat (+7/-0) t/pt-summary/samples/BSD/netbsd_001/mounted_fs (+5/-0) t/pt-summary/samples/BSD/netbsd_001/notable_procs (+2/-0) t/pt-summary/samples/BSD/netbsd_001/proc_cpuinfo_copy (+14/-0) t/pt-summary/samples/BSD/netbsd_001/processes (+10/-0) t/pt-summary/samples/BSD/netbsd_001/summary (+10/-0) t/pt-summary/samples/BSD/netbsd_001/swapctl (+1/-0) t/pt-summary/samples/BSD/netbsd_001/sysctl (+511/-0) t/pt-summary/samples/BSD/netbsd_001/uptime (+1/-0) t/pt-summary/samples/BSD/netbsd_001/vmstat (+7/-0) t/pt-summary/samples/BSD/openbsd_001/mounted_fs (+4/-0) t/pt-summary/samples/BSD/openbsd_001/notable_procs (+2/-0) t/pt-summary/samples/BSD/openbsd_001/processes (+10/-0) t/pt-summary/samples/BSD/openbsd_001/summary (+10/-0) t/pt-summary/samples/BSD/openbsd_001/swapctl (+1/-0) t/pt-summary/samples/BSD/openbsd_001/sysctl (+423/-0) t/pt-summary/samples/BSD/openbsd_001/uptime (+1/-0) t/pt-summary/samples/BSD/openbsd_001/vmstat (+7/-0) t/pt-summary/samples/Linux/001/dmesg_file (+786/-0) t/pt-summary/samples/Linux/001/dmidecode (+412/-0) t/pt-summary/samples/Linux/001/ip (+24/-0) t/pt-summary/samples/Linux/001/lspci_file (+17/-0) t/pt-summary/samples/Linux/001/lvs (+1/-0) t/pt-summary/samples/Linux/001/memory (+50/-0) t/pt-summary/samples/Linux/001/mounted_fs (+12/-0) t/pt-summary/samples/Linux/001/netstat (+6/-0) t/pt-summary/samples/Linux/001/notable_procs (+5/-0) t/pt-summary/samples/Linux/001/partitioning (+30/-0) t/pt-summary/samples/Linux/001/proc_cpuinfo_copy (+58/-0) t/pt-summary/samples/Linux/001/proc_cpuinfo_copy.unq (+1/-0) t/pt-summary/samples/Linux/001/processes (+10/-0) t/pt-summary/samples/Linux/001/summary (+23/-0) t/pt-summary/samples/Linux/001/sysctl (+905/-0) t/pt-summary/samples/Linux/001/uptime (+1/-0) t/pt-summary/samples/Linux/001/vmstat (+7/-0) t/pt-summary/samples/Linux/002/dmesg_file (+283/-0) t/pt-summary/samples/Linux/002/memory (+34/-0) t/pt-summary/samples/Linux/002/mounted_fs (+3/-0) t/pt-summary/samples/Linux/002/netstat (+6/-0) t/pt-summary/samples/Linux/002/notable_procs (+2/-0) t/pt-summary/samples/Linux/002/partitioning (+9/-0) t/pt-summary/samples/Linux/002/proc_cpuinfo_copy (+19/-0) t/pt-summary/samples/Linux/002/processes (+10/-0) t/pt-summary/samples/Linux/002/summary (+19/-0) t/pt-summary/samples/Linux/002/uptime (+1/-0) t/pt-summary/samples/Linux/002/vmstat (+7/-0) t/pt-summary/samples/Linux/003/dmesg_file (+283/-0) t/pt-summary/samples/Linux/003/memory (+34/-0) t/pt-summary/samples/Linux/003/mounted_fs (+3/-0) t/pt-summary/samples/Linux/003/netstat (+6/-0) t/pt-summary/samples/Linux/003/notable_procs (+2/-0) t/pt-summary/samples/Linux/003/partitioning (+1/-0) t/pt-summary/samples/Linux/003/proc_cpuinfo_copy (+19/-0) t/pt-summary/samples/Linux/003/processes (+10/-0) t/pt-summary/samples/Linux/003/summary (+19/-0) t/pt-summary/samples/Linux/003/uptime (+1/-0) t/pt-summary/samples/Linux/003/vmstat (+7/-0) t/pt-summary/samples/Linux/output_002.txt (+82/-0) t/pt-summary/samples/Linux/output_003.txt (+79/-0) t/pt-summary/samples/MegaCli64_AdpAllInfo_aALL001.txt (+227/-0) t/pt-summary/samples/MegaCli64_LdPdInfo_aALL_886223 (+214/-0) t/pt-summary/samples/arcconf-001.txt (+133/-0) t/pt-summary/samples/arcconf-003_900285.txt (+228/-0) t/pt-summary/samples/arcconf-004_917781.txt (+162/-0) t/pt-summary/samples/dmesg-005.txt (+787/-0) t/pt-summary/samples/dmesg-007.txt (+136/-0) t/pt-summary/samples/hpaculi-001.txt (+11/-0) t/pt-summary/samples/hpaculi-002.txt (+354/-0) t/pt-summary/samples/hpaculi-003.txt (+11/-0) t/pt-summary/samples/ip-s-link-003.txt (+24/-0) t/pt-summary/samples/lspci-005.txt (+38/-0) t/pt-summary/samples/netstat-002.txt (+1328/-0) t/pt-summary/samples/proc_cpuinfo001.txt (+57/-0) t/pt-summary/samples/proc_cpuinfo001.txt.unq (+1/-0) t/pt-summary/samples/proc_cpuinfo002.txt (+57/-0) t/pt-summary/samples/proc_cpuinfo002.txt.unq (+1/-0) t/pt-table-sync/issue_408.t (+1/-1) t/pt-table-usage/basics.t (+138/-0) t/pt-table-usage/create_table_definitions.t (+41/-0) t/pt-table-usage/explain_extended.t (+79/-0) t/pt-table-usage/samples/ee.out (+6/-0) t/pt-table-usage/samples/ee.sql (+26/-0) t/pt-table-usage/samples/in/slow001.txt (+24/-0) t/pt-table-usage/samples/in/slow002.txt (+20/-0) t/pt-table-usage/samples/in/slow003.txt (+3/-0) t/pt-table-usage/samples/out/create-table-defs-001.txt (+4/-0) t/pt-table-usage/samples/out/create001.txt (+5/-0) t/pt-table-usage/samples/out/drop-table-if-exists.txt (+3/-0) t/pt-table-usage/samples/out/query001.txt (+6/-0) t/pt-table-usage/samples/out/query002.txt (+5/-0) t/pt-table-usage/samples/out/slow001.txt (+31/-0) t/pt-table-usage/samples/out/slow002.txt (+40/-0) t/pt-table-usage/samples/out/slow003-001.txt (+6/-0) t/pt-table-usage/samples/out/slow003-002.txt (+8/-0) t/pt-table-usage/samples/out/slow003-003.txt (+6/-0) util/test-bash-functions (+48/-0) Text conflict in bin/pt-online-schema-change Text conflict in bin/pt-table-checksum Text conflict in lib/MySQLStatusWaiter.pm Text conflict in lib/NibbleIterator.pm Text conflict in t/lib/MySQLStatusWaiter.t Text conflict in t/lib/NibbleIterator.t |
To merge this branch: | bzr merge lp:~percona-toolkit-dev/percona-toolkit/fix-sleep-bug-979092 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Daniel Nichter | Approve | ||
Review via email: mp+108057@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
Daniel Nichter (daniel-nichter) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'Changelog' | |||
2 | --- Changelog 2012-03-07 23:41:54 +0000 | |||
3 | +++ Changelog 2012-05-30 21:00:30 +0000 | |||
4 | @@ -1,5 +1,26 @@ | |||
5 | 1 | Changelog for Percona Toolkit | 1 | Changelog for Percona Toolkit |
6 | 2 | 2 | ||
7 | 3 | v2.1.1 released 2012-04-03 | ||
8 | 4 | |||
9 | 5 | * Completely redesigned pt-online-schema-change | ||
10 | 6 | * Completely redesigned pt-mysql-summary | ||
11 | 7 | * Completely redesigned pt-summary | ||
12 | 8 | * Added new tool: pt-table-usage | ||
13 | 9 | * Added new tool: pt-fingerprint | ||
14 | 10 | * Fixed bug 955860: pt-stalk doesn't run vmstat, iostat, and mpstat for --run-time | ||
15 | 11 | * Fixed bug 960513: SHOW TABLE STATUS is used needlessly | ||
16 | 12 | * Fixed bug 969726: pt-online-schema-change loses foreign keys | ||
17 | 13 | * Fixed bug 846028: pt-online-schema-change does not show progress until completed | ||
18 | 14 | * Fixed bug 898695: pt-online-schema-change add useless ORDER BY | ||
19 | 15 | * Fixed bug 952727: pt-diskstats shows incorrect wr_mb_s | ||
20 | 16 | * Fixed bug 963225: pt-query-digest fails to set history columns for disk tmp tables and disk filesort | ||
21 | 17 | * Fixed bug 967451: Char chunking doesn't quote column name | ||
22 | 18 | * Fixed bug 972399: pt-table-checksum docs are not rendered right | ||
23 | 19 | * Fixed bug 896553: Various documentation spelling fixes | ||
24 | 20 | * Fixed bug 949154: pt-variable-advisor advice for relay-log-space-limit | ||
25 | 21 | * Fixed bug 953461: pt-upgrade manual broken 'output' section | ||
26 | 22 | * Fixed bug 949653: pt-table-checksum docs don't mention risks posed by inconsistent schemas | ||
27 | 23 | |||
28 | 3 | v2.0.4 released 2012-03-07 | 24 | v2.0.4 released 2012-03-07 |
29 | 4 | 25 | ||
30 | 5 | * Added --filter to pt-kill to allow arbitrary --group-by | 26 | * Added --filter to pt-kill to allow arbitrary --group-by |
31 | 6 | 27 | ||
32 | === modified file 'MANIFEST' | |||
33 | --- MANIFEST 2012-02-03 23:25:29 +0000 | |||
34 | +++ MANIFEST 2012-05-30 21:00:30 +0000 | |||
35 | @@ -12,6 +12,7 @@ | |||
36 | 12 | bin/pt-duplicate-key-checker | 12 | bin/pt-duplicate-key-checker |
37 | 13 | bin/pt-fifo-split | 13 | bin/pt-fifo-split |
38 | 14 | bin/pt-find | 14 | bin/pt-find |
39 | 15 | bin/pt-fingerprint | ||
40 | 15 | bin/pt-fk-error-logger | 16 | bin/pt-fk-error-logger |
41 | 16 | bin/pt-heartbeat | 17 | bin/pt-heartbeat |
42 | 17 | bin/pt-index-usage | 18 | bin/pt-index-usage |
43 | @@ -33,6 +34,7 @@ | |||
44 | 33 | bin/pt-summary | 34 | bin/pt-summary |
45 | 34 | bin/pt-table-checksum | 35 | bin/pt-table-checksum |
46 | 35 | bin/pt-table-sync | 36 | bin/pt-table-sync |
47 | 37 | bin/pt-table-usage | ||
48 | 36 | bin/pt-tcp-model | 38 | bin/pt-tcp-model |
49 | 37 | bin/pt-trend | 39 | bin/pt-trend |
50 | 38 | bin/pt-upgrade | 40 | bin/pt-upgrade |
51 | 39 | 41 | ||
52 | === modified file 'Makefile.PL' | |||
53 | --- Makefile.PL 2012-03-07 23:41:54 +0000 | |||
54 | +++ Makefile.PL 2012-05-30 21:00:30 +0000 | |||
55 | @@ -2,7 +2,7 @@ | |||
56 | 2 | 2 | ||
57 | 3 | WriteMakefile( | 3 | WriteMakefile( |
58 | 4 | NAME => 'percona-toolkit', | 4 | NAME => 'percona-toolkit', |
60 | 5 | VERSION => '2.0.4', | 5 | VERSION => '2.1.1', |
61 | 6 | EXE_FILES => [ <bin/*> ], | 6 | EXE_FILES => [ <bin/*> ], |
62 | 7 | MAN1PODS => { | 7 | MAN1PODS => { |
63 | 8 | 'docs/percona-toolkit.pod' => 'blib/man1/percona-toolkit.1', | 8 | 'docs/percona-toolkit.pod' => 'blib/man1/percona-toolkit.1', |
64 | 9 | 9 | ||
65 | === modified file 'bin/pt-align' | |||
66 | --- bin/pt-align 2012-03-07 23:41:54 +0000 | |||
67 | +++ bin/pt-align 2012-05-30 21:00:30 +0000 | |||
68 | @@ -218,6 +218,6 @@ | |||
69 | 218 | 218 | ||
70 | 219 | =head1 VERSION | 219 | =head1 VERSION |
71 | 220 | 220 | ||
73 | 221 | pt-align 2.0.4 | 221 | pt-align 2.1.1 |
74 | 222 | 222 | ||
75 | 223 | =cut | 223 | =cut |
76 | 224 | 224 | ||
77 | === modified file 'bin/pt-archiver' | |||
78 | --- bin/pt-archiver 2012-05-30 20:59:47 +0000 | |||
79 | +++ bin/pt-archiver 2012-05-30 21:00:30 +0000 | |||
80 | @@ -5731,6 +5731,6 @@ | |||
81 | 5731 | 5731 | ||
82 | 5732 | =head1 VERSION | 5732 | =head1 VERSION |
83 | 5733 | 5733 | ||
85 | 5734 | pt-archiver 2.0.4 | 5734 | pt-archiver 2.1.1 |
86 | 5735 | 5735 | ||
87 | 5736 | =cut | 5736 | =cut |
88 | 5737 | 5737 | ||
89 | === modified file 'bin/pt-config-diff' | |||
90 | --- bin/pt-config-diff 2012-05-24 17:52:01 +0000 | |||
91 | +++ bin/pt-config-diff 2012-05-30 21:00:30 +0000 | |||
92 | @@ -3408,6 +3408,6 @@ | |||
93 | 3408 | 3408 | ||
94 | 3409 | =head1 VERSION | 3409 | =head1 VERSION |
95 | 3410 | 3410 | ||
97 | 3411 | pt-config-diff 2.0.4 | 3411 | pt-config-diff 2.1.1 |
98 | 3412 | 3412 | ||
99 | 3413 | =cut | 3413 | =cut |
100 | 3414 | 3414 | ||
101 | === modified file 'bin/pt-deadlock-logger' | |||
102 | --- bin/pt-deadlock-logger 2012-05-30 17:54:33 +0000 | |||
103 | +++ bin/pt-deadlock-logger 2012-05-30 21:00:30 +0000 | |||
104 | @@ -2748,6 +2748,6 @@ | |||
105 | 2748 | 2748 | ||
106 | 2749 | =head1 VERSION | 2749 | =head1 VERSION |
107 | 2750 | 2750 | ||
109 | 2751 | pt-deadlock-logger 2.0.4 | 2751 | pt-deadlock-logger 2.1.1 |
110 | 2752 | 2752 | ||
111 | 2753 | =cut | 2753 | =cut |
112 | 2754 | 2754 | ||
113 | === modified file 'bin/pt-diskstats' | |||
114 | --- bin/pt-diskstats 2012-05-11 14:28:14 +0000 | |||
115 | +++ bin/pt-diskstats 2012-05-30 21:00:30 +0000 | |||
116 | @@ -4106,6 +4106,6 @@ | |||
117 | 4106 | 4106 | ||
118 | 4107 | =head1 VERSION | 4107 | =head1 VERSION |
119 | 4108 | 4108 | ||
121 | 4109 | pt-diskstats 2.0.4 | 4109 | pt-diskstats 2.1.1 |
122 | 4110 | 4110 | ||
123 | 4111 | =cut | 4111 | =cut |
124 | 4112 | 4112 | ||
125 | === modified file 'bin/pt-duplicate-key-checker' | |||
126 | --- bin/pt-duplicate-key-checker 2012-05-24 17:52:01 +0000 | |||
127 | +++ bin/pt-duplicate-key-checker 2012-05-30 21:00:30 +0000 | |||
128 | @@ -199,19 +199,58 @@ | |||
129 | 199 | return bless $self, $class; | 199 | return bless $self, $class; |
130 | 200 | } | 200 | } |
131 | 201 | 201 | ||
132 | 202 | sub get_create_table { | ||
133 | 203 | my ( $self, $dbh, $db, $tbl ) = @_; | ||
134 | 204 | die "I need a dbh parameter" unless $dbh; | ||
135 | 205 | die "I need a db parameter" unless $db; | ||
136 | 206 | die "I need a tbl parameter" unless $tbl; | ||
137 | 207 | my $q = $self->{Quoter}; | ||
138 | 208 | |||
139 | 209 | my $new_sql_mode | ||
140 | 210 | = '/*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, ' | ||
141 | 211 | . q{@@SQL_MODE := REPLACE(REPLACE(@@SQL_MODE, 'ANSI_QUOTES', ''), ',,', ','), } | ||
142 | 212 | . '@OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, ' | ||
143 | 213 | . '@@SQL_QUOTE_SHOW_CREATE := 1 */'; | ||
144 | 214 | |||
145 | 215 | my $old_sql_mode = '/*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, ' | ||
146 | 216 | . '@@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */'; | ||
147 | 217 | |||
148 | 218 | PTDEBUG && _d($new_sql_mode); | ||
149 | 219 | eval { $dbh->do($new_sql_mode); }; | ||
150 | 220 | PTDEBUG && $EVAL_ERROR && _d($EVAL_ERROR); | ||
151 | 221 | |||
152 | 222 | my $use_sql = 'USE ' . $q->quote($db); | ||
153 | 223 | PTDEBUG && _d($dbh, $use_sql); | ||
154 | 224 | $dbh->do($use_sql); | ||
155 | 225 | |||
156 | 226 | my $show_sql = "SHOW CREATE TABLE " . $q->quote($db, $tbl); | ||
157 | 227 | PTDEBUG && _d($show_sql); | ||
158 | 228 | my $href; | ||
159 | 229 | eval { $href = $dbh->selectrow_hashref($show_sql); }; | ||
160 | 230 | if ( $EVAL_ERROR ) { | ||
161 | 231 | PTDEBUG && _d($EVAL_ERROR); | ||
162 | 232 | |||
163 | 233 | PTDEBUG && _d($old_sql_mode); | ||
164 | 234 | $dbh->do($old_sql_mode); | ||
165 | 235 | |||
166 | 236 | return; | ||
167 | 237 | } | ||
168 | 238 | |||
169 | 239 | PTDEBUG && _d($old_sql_mode); | ||
170 | 240 | $dbh->do($old_sql_mode); | ||
171 | 241 | |||
172 | 242 | my ($key) = grep { m/create (?:table|view)/i } keys %$href; | ||
173 | 243 | if ( !$key ) { | ||
174 | 244 | die "Error: no 'Create Table' or 'Create View' in result set from " | ||
175 | 245 | . "$show_sql: " . Dumper($href); | ||
176 | 246 | } | ||
177 | 247 | |||
178 | 248 | return $href->{$key}; | ||
179 | 249 | } | ||
180 | 250 | |||
181 | 202 | sub parse { | 251 | sub parse { |
182 | 203 | my ( $self, $ddl, $opts ) = @_; | 252 | my ( $self, $ddl, $opts ) = @_; |
183 | 204 | return unless $ddl; | 253 | return unless $ddl; |
184 | 205 | if ( ref $ddl eq 'ARRAY' ) { | ||
185 | 206 | if ( lc $ddl->[0] eq 'table' ) { | ||
186 | 207 | $ddl = $ddl->[1]; | ||
187 | 208 | } | ||
188 | 209 | else { | ||
189 | 210 | return { | ||
190 | 211 | engine => 'VIEW', | ||
191 | 212 | }; | ||
192 | 213 | } | ||
193 | 214 | } | ||
194 | 215 | 254 | ||
195 | 216 | if ( $ddl !~ m/CREATE (?:TEMPORARY )?TABLE `/ ) { | 255 | if ( $ddl !~ m/CREATE (?:TEMPORARY )?TABLE `/ ) { |
196 | 217 | die "Cannot parse table definition; is ANSI quoting " | 256 | die "Cannot parse table definition; is ANSI quoting " |
197 | @@ -518,41 +557,31 @@ | |||
198 | 518 | return $ddl; | 557 | return $ddl; |
199 | 519 | } | 558 | } |
200 | 520 | 559 | ||
236 | 521 | sub remove_secondary_indexes { | 560 | sub get_table_status { |
237 | 522 | my ( $self, $ddl ) = @_; | 561 | my ( $self, $dbh, $db, $like ) = @_; |
238 | 523 | my $sec_indexes_ddl; | 562 | my $q = $self->{Quoter}; |
239 | 524 | my $tbl_struct = $self->parse($ddl); | 563 | my $sql = "SHOW TABLE STATUS FROM " . $q->quote($db); |
240 | 525 | 564 | my @params; | |
241 | 526 | if ( ($tbl_struct->{engine} || '') =~ m/InnoDB/i ) { | 565 | if ( $like ) { |
242 | 527 | my $clustered_key = $tbl_struct->{clustered_key}; | 566 | $sql .= ' LIKE ?'; |
243 | 528 | $clustered_key ||= ''; | 567 | push @params, $like; |
244 | 529 | 568 | } | |
245 | 530 | my @sec_indexes = map { | 569 | PTDEBUG && _d($sql, @params); |
246 | 531 | my $key_def = $_->{ddl}; | 570 | my $sth = $dbh->prepare($sql); |
247 | 532 | $key_def =~ s/([\(\)])/\\$1/g; | 571 | eval { $sth->execute(@params); }; |
248 | 533 | $ddl =~ s/\s+$key_def//i; | 572 | if ($EVAL_ERROR) { |
249 | 534 | 573 | PTDEBUG && _d($EVAL_ERROR); | |
250 | 535 | my $key_ddl = "ADD $_->{ddl}"; | 574 | return; |
251 | 536 | $key_ddl .= ',' unless $key_ddl =~ m/,$/; | 575 | } |
252 | 537 | $key_ddl; | 576 | my @tables = @{$sth->fetchall_arrayref({})}; |
253 | 538 | } | 577 | @tables = map { |
254 | 539 | grep { $_->{name} ne $clustered_key } | 578 | my %tbl; # Make a copy with lowercased keys |
255 | 540 | values %{$tbl_struct->{keys}}; | 579 | @tbl{ map { lc $_ } keys %$_ } = values %$_; |
256 | 541 | PTDEBUG && _d('Secondary indexes:', Dumper(\@sec_indexes)); | 580 | $tbl{engine} ||= $tbl{type} || $tbl{comment}; |
257 | 542 | 581 | delete $tbl{type}; | |
258 | 543 | if ( @sec_indexes ) { | 582 | \%tbl; |
259 | 544 | $sec_indexes_ddl = join(' ', @sec_indexes); | 583 | } @tables; |
260 | 545 | $sec_indexes_ddl =~ s/,$//; | 584 | return @tables; |
226 | 546 | } | ||
227 | 547 | |||
228 | 548 | $ddl =~ s/,(\n\) )/$1/s; | ||
229 | 549 | } | ||
230 | 550 | else { | ||
231 | 551 | PTDEBUG && _d('Not removing secondary indexes from', | ||
232 | 552 | $tbl_struct->{engine}, 'table'); | ||
233 | 553 | } | ||
234 | 554 | |||
235 | 555 | return $ddl, $sec_indexes_ddl, $tbl_struct; | ||
261 | 556 | } | 585 | } |
262 | 557 | 586 | ||
263 | 558 | sub _d { | 587 | sub _d { |
264 | @@ -3200,7 +3229,7 @@ | |||
265 | 3200 | 3229 | ||
266 | 3201 | sub new { | 3230 | sub new { |
267 | 3202 | my ( $class, %args ) = @_; | 3231 | my ( $class, %args ) = @_; |
269 | 3203 | my @required_args = qw(OptionParser Quoter); | 3232 | my @required_args = qw(OptionParser TableParser Quoter); |
270 | 3204 | foreach my $arg ( @required_args ) { | 3233 | foreach my $arg ( @required_args ) { |
271 | 3205 | die "I need a $arg argument" unless $args{$arg}; | 3234 | die "I need a $arg argument" unless $args{$arg}; |
272 | 3206 | } | 3235 | } |
273 | @@ -3209,8 +3238,19 @@ | |||
274 | 3209 | die "I need either a dbh or file_itr argument" | 3238 | die "I need either a dbh or file_itr argument" |
275 | 3210 | if (!$dbh && !$file_itr) || ($dbh && $file_itr); | 3239 | if (!$dbh && !$file_itr) || ($dbh && $file_itr); |
276 | 3211 | 3240 | ||
277 | 3241 | my %resume; | ||
278 | 3242 | if ( my $table = $args{resume} ) { | ||
279 | 3243 | PTDEBUG && _d('Will resume from or after', $table); | ||
280 | 3244 | my ($db, $tbl) = $args{Quoter}->split_unquote($table); | ||
281 | 3245 | die "Resume table must be database-qualified: $table" | ||
282 | 3246 | unless $db && $tbl; | ||
283 | 3247 | $resume{db} = $db; | ||
284 | 3248 | $resume{tbl} = $tbl; | ||
285 | 3249 | } | ||
286 | 3250 | |||
287 | 3212 | my $self = { | 3251 | my $self = { |
288 | 3213 | %args, | 3252 | %args, |
289 | 3253 | resume => \%resume, | ||
290 | 3214 | filters => _make_filters(%args), | 3254 | filters => _make_filters(%args), |
291 | 3215 | }; | 3255 | }; |
292 | 3216 | 3256 | ||
293 | @@ -3271,9 +3311,19 @@ | |||
294 | 3271 | return \%filters; | 3311 | return \%filters; |
295 | 3272 | } | 3312 | } |
296 | 3273 | 3313 | ||
298 | 3274 | sub next_schema_object { | 3314 | sub next { |
299 | 3275 | my ( $self ) = @_; | 3315 | my ( $self ) = @_; |
300 | 3276 | 3316 | ||
301 | 3317 | if ( !$self->{initialized} ) { | ||
302 | 3318 | $self->{initialized} = 1; | ||
303 | 3319 | if ( $self->{resume}->{tbl} | ||
304 | 3320 | && !$self->table_is_allowed(@{$self->{resume}}{qw(db tbl)}) ) { | ||
305 | 3321 | PTDEBUG && _d('Will resume after', | ||
306 | 3322 | join('.', @{$self->{resume}}{qw(db tbl)})); | ||
307 | 3323 | $self->{resume}->{after} = 1; | ||
308 | 3324 | } | ||
309 | 3325 | } | ||
310 | 3326 | |||
311 | 3277 | my $schema_obj; | 3327 | my $schema_obj; |
312 | 3278 | if ( $self->{file_itr} ) { | 3328 | if ( $self->{file_itr} ) { |
313 | 3279 | $schema_obj= $self->_iterate_files(); | 3329 | $schema_obj= $self->_iterate_files(); |
314 | @@ -3283,19 +3333,13 @@ | |||
315 | 3283 | } | 3333 | } |
316 | 3284 | 3334 | ||
317 | 3285 | if ( $schema_obj ) { | 3335 | if ( $schema_obj ) { |
318 | 3286 | if ( $schema_obj->{ddl} && $self->{TableParser} ) { | ||
319 | 3287 | $schema_obj->{tbl_struct} | ||
320 | 3288 | = $self->{TableParser}->parse($schema_obj->{ddl}); | ||
321 | 3289 | } | ||
322 | 3290 | |||
323 | 3291 | delete $schema_obj->{ddl} unless $self->{keep_ddl}; | ||
324 | 3292 | |||
325 | 3293 | if ( my $schema = $self->{Schema} ) { | 3336 | if ( my $schema = $self->{Schema} ) { |
326 | 3294 | $schema->add_schema_object($schema_obj); | 3337 | $schema->add_schema_object($schema_obj); |
327 | 3295 | } | 3338 | } |
328 | 3339 | PTDEBUG && _d('Next schema object:', | ||
329 | 3340 | $schema_obj->{db}, $schema_obj->{tbl}); | ||
330 | 3296 | } | 3341 | } |
331 | 3297 | 3342 | ||
332 | 3298 | PTDEBUG && _d('Next schema object:', $schema_obj->{db}, $schema_obj->{tbl}); | ||
333 | 3299 | return $schema_obj; | 3343 | return $schema_obj; |
334 | 3300 | } | 3344 | } |
335 | 3301 | 3345 | ||
336 | @@ -3321,7 +3365,8 @@ | |||
337 | 3321 | my $db = $1; # XXX | 3365 | my $db = $1; # XXX |
338 | 3322 | $db =~ s/^`//; # strip leading ` | 3366 | $db =~ s/^`//; # strip leading ` |
339 | 3323 | $db =~ s/`$//; # and trailing ` | 3367 | $db =~ s/`$//; # and trailing ` |
341 | 3324 | if ( $self->database_is_allowed($db) ) { | 3368 | if ( $self->database_is_allowed($db) |
342 | 3369 | && $self->_resume_from_database($db) ) { | ||
343 | 3325 | $self->{db} = $db; | 3370 | $self->{db} = $db; |
344 | 3326 | } | 3371 | } |
345 | 3327 | } | 3372 | } |
346 | @@ -3334,21 +3379,22 @@ | |||
347 | 3334 | my ($tbl) = $chunk =~ m/$tbl_name/; | 3379 | my ($tbl) = $chunk =~ m/$tbl_name/; |
348 | 3335 | $tbl =~ s/^\s*`//; | 3380 | $tbl =~ s/^\s*`//; |
349 | 3336 | $tbl =~ s/`\s*$//; | 3381 | $tbl =~ s/`\s*$//; |
351 | 3337 | if ( $self->table_is_allowed($self->{db}, $tbl) ) { | 3382 | if ( $self->_resume_from_table($tbl) |
352 | 3383 | && $self->table_is_allowed($self->{db}, $tbl) ) { | ||
353 | 3338 | my ($ddl) = $chunk =~ m/^(?:$open_comment)?(CREATE TABLE.+?;)$/ms; | 3384 | my ($ddl) = $chunk =~ m/^(?:$open_comment)?(CREATE TABLE.+?;)$/ms; |
354 | 3339 | if ( !$ddl ) { | 3385 | if ( !$ddl ) { |
355 | 3340 | warn "Failed to parse CREATE TABLE from\n" . $chunk; | 3386 | warn "Failed to parse CREATE TABLE from\n" . $chunk; |
356 | 3341 | next CHUNK; | 3387 | next CHUNK; |
357 | 3342 | } | 3388 | } |
358 | 3343 | $ddl =~ s/ \*\/;\Z/;/; # remove end of version comment | 3389 | $ddl =~ s/ \*\/;\Z/;/; # remove end of version comment |
363 | 3344 | 3390 | my $tbl_struct = $self->{TableParser}->parse($ddl); | |
364 | 3345 | my ($engine) = $ddl =~ m/\).*?(?:ENGINE|TYPE)=(\w+)/; | 3391 | if ( $self->engine_is_allowed($tbl_struct->{engine}) ) { |
361 | 3346 | |||
362 | 3347 | if ( !$engine || $self->engine_is_allowed($engine) ) { | ||
365 | 3348 | return { | 3392 | return { |
369 | 3349 | db => $self->{db}, | 3393 | db => $self->{db}, |
370 | 3350 | tbl => $tbl, | 3394 | tbl => $tbl, |
371 | 3351 | ddl => $ddl, | 3395 | name => $self->{Quoter}->quote($self->{db}, $tbl), |
372 | 3396 | ddl => $ddl, | ||
373 | 3397 | tbl_struct => $tbl_struct, | ||
374 | 3352 | }; | 3398 | }; |
375 | 3353 | } | 3399 | } |
376 | 3354 | } | 3400 | } |
377 | @@ -3365,6 +3411,7 @@ | |||
378 | 3365 | sub _iterate_dbh { | 3411 | sub _iterate_dbh { |
379 | 3366 | my ( $self ) = @_; | 3412 | my ( $self ) = @_; |
380 | 3367 | my $q = $self->{Quoter}; | 3413 | my $q = $self->{Quoter}; |
381 | 3414 | my $tp = $self->{TableParser}; | ||
382 | 3368 | my $dbh = $self->{dbh}; | 3415 | my $dbh = $self->{dbh}; |
383 | 3369 | PTDEBUG && _d('Getting next schema object from dbh', $dbh); | 3416 | PTDEBUG && _d('Getting next schema object from dbh', $dbh); |
384 | 3370 | 3417 | ||
385 | @@ -3378,7 +3425,9 @@ | |||
386 | 3378 | } | 3425 | } |
387 | 3379 | 3426 | ||
388 | 3380 | if ( !$self->{db} ) { | 3427 | if ( !$self->{db} ) { |
390 | 3381 | $self->{db} = shift @{$self->{dbs}}; | 3428 | do { |
391 | 3429 | $self->{db} = shift @{$self->{dbs}}; | ||
392 | 3430 | } until $self->_resume_from_database($self->{db}); | ||
393 | 3382 | PTDEBUG && _d('Next database:', $self->{db}); | 3431 | PTDEBUG && _d('Next database:', $self->{db}); |
394 | 3383 | return unless $self->{db}; | 3432 | return unless $self->{db}; |
395 | 3384 | } | 3433 | } |
396 | @@ -3391,8 +3440,9 @@ | |||
397 | 3391 | } | 3440 | } |
398 | 3392 | grep { | 3441 | grep { |
399 | 3393 | my ($tbl, $type) = @$_; | 3442 | my ($tbl, $type) = @$_; |
402 | 3394 | $self->table_is_allowed($self->{db}, $tbl) | 3443 | (!$type || ($type ne 'VIEW')) |
403 | 3395 | && (!$type || ($type ne 'VIEW')); | 3444 | && $self->_resume_from_table($tbl) |
404 | 3445 | && $self->table_is_allowed($self->{db}, $tbl); | ||
405 | 3396 | } | 3446 | } |
406 | 3397 | @{$dbh->selectall_arrayref($sql)}; | 3447 | @{$dbh->selectall_arrayref($sql)}; |
407 | 3398 | PTDEBUG && _d('Found', scalar @tbls, 'tables in database', $self->{db}); | 3448 | PTDEBUG && _d('Found', scalar @tbls, 'tables in database', $self->{db}); |
408 | @@ -3400,27 +3450,15 @@ | |||
409 | 3400 | } | 3450 | } |
410 | 3401 | 3451 | ||
411 | 3402 | while ( my $tbl = shift @{$self->{tbls}} ) { | 3452 | while ( my $tbl = shift @{$self->{tbls}} ) { |
429 | 3403 | my $engine; | 3453 | my $ddl = $tp->get_create_table($dbh, $self->{db}, $tbl); |
430 | 3404 | if ( $self->{filters}->{'engines'} | 3454 | my $tbl_struct = $tp->parse($ddl); |
431 | 3405 | || $self->{filters}->{'ignore-engines'} ) { | 3455 | if ( $self->engine_is_allowed($tbl_struct->{engine}) ) { |
415 | 3406 | my $sql = "SHOW TABLE STATUS FROM " . $q->quote($self->{db}) | ||
416 | 3407 | . " LIKE \'$tbl\'"; | ||
417 | 3408 | PTDEBUG && _d($sql); | ||
418 | 3409 | $engine = $dbh->selectrow_hashref($sql)->{engine}; | ||
419 | 3410 | PTDEBUG && _d($tbl, 'uses', $engine, 'engine'); | ||
420 | 3411 | } | ||
421 | 3412 | |||
422 | 3413 | |||
423 | 3414 | if ( !$engine || $self->engine_is_allowed($engine) ) { | ||
424 | 3415 | my $ddl; | ||
425 | 3416 | if ( my $du = $self->{MySQLDump} ) { | ||
426 | 3417 | $ddl = $du->get_create_table($dbh, $q, $self->{db}, $tbl)->[1]; | ||
427 | 3418 | } | ||
428 | 3419 | |||
432 | 3420 | return { | 3456 | return { |
436 | 3421 | db => $self->{db}, | 3457 | db => $self->{db}, |
437 | 3422 | tbl => $tbl, | 3458 | tbl => $tbl, |
438 | 3423 | ddl => $ddl, | 3459 | name => $q->quote($self->{db}, $tbl), |
439 | 3460 | ddl => $ddl, | ||
440 | 3461 | tbl_struct => $tbl_struct, | ||
441 | 3424 | }; | 3462 | }; |
442 | 3425 | } | 3463 | } |
443 | 3426 | } | 3464 | } |
444 | @@ -3481,6 +3519,10 @@ | |||
445 | 3481 | 3519 | ||
446 | 3482 | my $filter = $self->{filters}; | 3520 | my $filter = $self->{filters}; |
447 | 3483 | 3521 | ||
448 | 3522 | if ( $db eq 'mysql' && ($tbl eq 'general_log' || $tbl eq 'slow_log') ) { | ||
449 | 3523 | return 0; | ||
450 | 3524 | } | ||
451 | 3525 | |||
452 | 3484 | if ( $filter->{'ignore-tables'}->{$tbl} | 3526 | if ( $filter->{'ignore-tables'}->{$tbl} |
453 | 3485 | && ($filter->{'ignore-tables'}->{$tbl} eq '*' | 3527 | && ($filter->{'ignore-tables'}->{$tbl} eq '*' |
454 | 3486 | || $filter->{'ignore-tables'}->{$tbl} eq $db) ) { | 3528 | || $filter->{'ignore-tables'}->{$tbl} eq $db) ) { |
455 | @@ -3520,7 +3562,11 @@ | |||
456 | 3520 | 3562 | ||
457 | 3521 | sub engine_is_allowed { | 3563 | sub engine_is_allowed { |
458 | 3522 | my ( $self, $engine ) = @_; | 3564 | my ( $self, $engine ) = @_; |
460 | 3523 | die "I need an engine argument" unless $engine; | 3565 | |
461 | 3566 | if ( !$engine ) { | ||
462 | 3567 | PTDEBUG && _d('No engine specified; allowing the table'); | ||
463 | 3568 | return 1; | ||
464 | 3569 | } | ||
465 | 3524 | 3570 | ||
466 | 3525 | $engine = lc $engine; | 3571 | $engine = lc $engine; |
467 | 3526 | 3572 | ||
468 | @@ -3540,6 +3586,40 @@ | |||
469 | 3540 | return 1; | 3586 | return 1; |
470 | 3541 | } | 3587 | } |
471 | 3542 | 3588 | ||
472 | 3589 | sub _resume_from_database { | ||
473 | 3590 | my ($self, $db) = @_; | ||
474 | 3591 | |||
475 | 3592 | return 1 unless $self->{resume}->{db}; | ||
476 | 3593 | |||
477 | 3594 | if ( $db eq $self->{resume}->{db} ) { | ||
478 | 3595 | PTDEBUG && _d('At resume db', $db); | ||
479 | 3596 | delete $self->{resume}->{db}; | ||
480 | 3597 | return 1; | ||
481 | 3598 | } | ||
482 | 3599 | |||
483 | 3600 | return 0; | ||
484 | 3601 | } | ||
485 | 3602 | |||
486 | 3603 | sub _resume_from_table { | ||
487 | 3604 | my ($self, $tbl) = @_; | ||
488 | 3605 | |||
489 | 3606 | return 1 unless $self->{resume}->{tbl}; | ||
490 | 3607 | |||
491 | 3608 | if ( $tbl eq $self->{resume}->{tbl} ) { | ||
492 | 3609 | if ( !$self->{resume}->{after} ) { | ||
493 | 3610 | PTDEBUG && _d('Resuming from table', $tbl); | ||
494 | 3611 | delete $self->{resume}->{tbl}; | ||
495 | 3612 | return 1; | ||
496 | 3613 | } | ||
497 | 3614 | else { | ||
498 | 3615 | PTDEBUG && _d('Resuming after table', $tbl); | ||
499 | 3616 | delete $self->{resume}->{tbl}; | ||
500 | 3617 | } | ||
501 | 3618 | } | ||
502 | 3619 | |||
503 | 3620 | return 0; | ||
504 | 3621 | } | ||
505 | 3622 | |||
506 | 3543 | sub _d { | 3623 | sub _d { |
507 | 3544 | my ($package, undef, $line) = caller 0; | 3624 | my ($package, undef, $line) = caller 0; |
508 | 3545 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | 3625 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } |
509 | @@ -3649,11 +3729,10 @@ | |||
510 | 3649 | MySQLDump => $du, | 3729 | MySQLDump => $du, |
511 | 3650 | TableParser => $tp, | 3730 | TableParser => $tp, |
512 | 3651 | Schema => $schema, | 3731 | Schema => $schema, |
513 | 3652 | keep_ddl => 1, | ||
514 | 3653 | ); | 3732 | ); |
515 | 3654 | TABLE: | 3733 | TABLE: |
518 | 3655 | while ( my $tbl = $schema_itr->next_schema_object() ) { | 3734 | while ( my $tbl = $schema_itr->next() ) { |
519 | 3656 | $tbl->{engine} = $tp->get_engine($tbl->{ddl}); | 3735 | $tbl->{engine} = $tbl->{tbl_struct}->{engine}; |
520 | 3657 | 3736 | ||
521 | 3658 | my ($keys, $clustered_key, $fks); | 3737 | my ($keys, $clustered_key, $fks); |
522 | 3659 | if ( $get_keys ) { | 3738 | if ( $get_keys ) { |
523 | @@ -4279,6 +4358,6 @@ | |||
524 | 4279 | 4358 | ||
525 | 4280 | =head1 VERSION | 4359 | =head1 VERSION |
526 | 4281 | 4360 | ||
528 | 4282 | pt-duplicate-key-checker 2.0.4 | 4361 | pt-duplicate-key-checker 2.1.1 |
529 | 4283 | 4362 | ||
530 | 4284 | =cut | 4363 | =cut |
531 | 4285 | 4364 | ||
532 | === modified file 'bin/pt-fifo-split' | |||
533 | --- bin/pt-fifo-split 2012-03-07 23:41:54 +0000 | |||
534 | +++ bin/pt-fifo-split 2012-05-30 21:00:30 +0000 | |||
535 | @@ -1547,6 +1547,6 @@ | |||
536 | 1547 | 1547 | ||
537 | 1548 | =head1 VERSION | 1548 | =head1 VERSION |
538 | 1549 | 1549 | ||
540 | 1550 | pt-fifo-split 2.0.4 | 1550 | pt-fifo-split 2.1.1 |
541 | 1551 | 1551 | ||
542 | 1552 | =cut | 1552 | =cut |
543 | 1553 | 1553 | ||
544 | === modified file 'bin/pt-find' | |||
545 | --- bin/pt-find 2012-05-24 17:52:01 +0000 | |||
546 | +++ bin/pt-find 2012-05-30 21:00:30 +0000 | |||
547 | @@ -3827,6 +3827,6 @@ | |||
548 | 3827 | 3827 | ||
549 | 3828 | =head1 VERSION | 3828 | =head1 VERSION |
550 | 3829 | 3829 | ||
552 | 3830 | pt-find 2.0.4 | 3830 | pt-find 2.1.1 |
553 | 3831 | 3831 | ||
554 | 3832 | =cut | 3832 | =cut |
555 | 3833 | 3833 | ||
556 | === added file 'bin/pt-fingerprint' | |||
557 | --- bin/pt-fingerprint 1970-01-01 00:00:00 +0000 | |||
558 | +++ bin/pt-fingerprint 2012-05-30 21:00:30 +0000 | |||
559 | @@ -0,0 +1,2143 @@ | |||
560 | 1 | #!/usr/bin/env perl | ||
561 | 2 | |||
562 | 3 | # This program is part of Percona Toolkit: http://www.percona.com/software/ | ||
563 | 4 | # See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal | ||
564 | 5 | # notices and disclaimers. | ||
565 | 6 | |||
566 | 7 | use strict; | ||
567 | 8 | use warnings FATAL => 'all'; | ||
568 | 9 | use constant MKDEBUG => $ENV{MKDEBUG} || 0; | ||
569 | 10 | |||
570 | 11 | # ########################################################################### | ||
571 | 12 | # OptionParser package | ||
572 | 13 | # This package is a copy without comments from the original. The original | ||
573 | 14 | # with comments and its test file can be found in the Bazaar repository at, | ||
574 | 15 | # lib/OptionParser.pm | ||
575 | 16 | # t/lib/OptionParser.t | ||
576 | 17 | # See https://launchpad.net/percona-toolkit for more information. | ||
577 | 18 | # ########################################################################### | ||
578 | 19 | { | ||
579 | 20 | package OptionParser; | ||
580 | 21 | |||
581 | 22 | use strict; | ||
582 | 23 | use warnings FATAL => 'all'; | ||
583 | 24 | use English qw(-no_match_vars); | ||
584 | 25 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
585 | 26 | |||
586 | 27 | use List::Util qw(max); | ||
587 | 28 | use Getopt::Long; | ||
588 | 29 | |||
589 | 30 | my $POD_link_re = '[LC]<"?([^">]+)"?>'; | ||
590 | 31 | |||
591 | 32 | sub new { | ||
592 | 33 | my ( $class, %args ) = @_; | ||
593 | 34 | my @required_args = qw(); | ||
594 | 35 | foreach my $arg ( @required_args ) { | ||
595 | 36 | die "I need a $arg argument" unless $args{$arg}; | ||
596 | 37 | } | ||
597 | 38 | |||
598 | 39 | my ($program_name) = $PROGRAM_NAME =~ m/([.A-Za-z-]+)$/; | ||
599 | 40 | $program_name ||= $PROGRAM_NAME; | ||
600 | 41 | my $home = $ENV{HOME} || $ENV{HOMEPATH} || $ENV{USERPROFILE} || '.'; | ||
601 | 42 | |||
602 | 43 | my %attributes = ( | ||
603 | 44 | 'type' => 1, | ||
604 | 45 | 'short form' => 1, | ||
605 | 46 | 'group' => 1, | ||
606 | 47 | 'default' => 1, | ||
607 | 48 | 'cumulative' => 1, | ||
608 | 49 | 'negatable' => 1, | ||
609 | 50 | ); | ||
610 | 51 | |||
611 | 52 | my $self = { | ||
612 | 53 | head1 => 'OPTIONS', # These args are used internally | ||
613 | 54 | skip_rules => 0, # to instantiate another Option- | ||
614 | 55 | item => '--(.*)', # Parser obj that parses the | ||
615 | 56 | attributes => \%attributes, # DSN OPTIONS section. Tools | ||
616 | 57 | parse_attributes => \&_parse_attribs, # don't tinker with these args. | ||
617 | 58 | |||
618 | 59 | %args, | ||
619 | 60 | |||
620 | 61 | strict => 1, # disabled by a special rule | ||
621 | 62 | program_name => $program_name, | ||
622 | 63 | opts => {}, | ||
623 | 64 | got_opts => 0, | ||
624 | 65 | short_opts => {}, | ||
625 | 66 | defaults => {}, | ||
626 | 67 | groups => {}, | ||
627 | 68 | allowed_groups => {}, | ||
628 | 69 | errors => [], | ||
629 | 70 | rules => [], # desc of rules for --help | ||
630 | 71 | mutex => [], # rule: opts are mutually exclusive | ||
631 | 72 | atleast1 => [], # rule: at least one opt is required | ||
632 | 73 | disables => {}, # rule: opt disables other opts | ||
633 | 74 | defaults_to => {}, # rule: opt defaults to value of other opt | ||
634 | 75 | DSNParser => undef, | ||
635 | 76 | default_files => [ | ||
636 | 77 | "/etc/percona-toolkit/percona-toolkit.conf", | ||
637 | 78 | "/etc/percona-toolkit/$program_name.conf", | ||
638 | 79 | "$home/.percona-toolkit.conf", | ||
639 | 80 | "$home/.$program_name.conf", | ||
640 | 81 | ], | ||
641 | 82 | types => { | ||
642 | 83 | string => 's', # standard Getopt type | ||
643 | 84 | int => 'i', # standard Getopt type | ||
644 | 85 | float => 'f', # standard Getopt type | ||
645 | 86 | Hash => 'H', # hash, formed from a comma-separated list | ||
646 | 87 | hash => 'h', # hash as above, but only if a value is given | ||
647 | 88 | Array => 'A', # array, similar to Hash | ||
648 | 89 | array => 'a', # array, similar to hash | ||
649 | 90 | DSN => 'd', # DSN | ||
650 | 91 | size => 'z', # size with kMG suffix (powers of 2^10) | ||
651 | 92 | time => 'm', # time, with an optional suffix of s/h/m/d | ||
652 | 93 | }, | ||
653 | 94 | }; | ||
654 | 95 | |||
655 | 96 | return bless $self, $class; | ||
656 | 97 | } | ||
657 | 98 | |||
658 | 99 | sub get_specs { | ||
659 | 100 | my ( $self, $file ) = @_; | ||
660 | 101 | $file ||= $self->{file} || __FILE__; | ||
661 | 102 | my @specs = $self->_pod_to_specs($file); | ||
662 | 103 | $self->_parse_specs(@specs); | ||
663 | 104 | |||
664 | 105 | open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; | ||
665 | 106 | my $contents = do { local $/ = undef; <$fh> }; | ||
666 | 107 | close $fh; | ||
667 | 108 | if ( $contents =~ m/^=head1 DSN OPTIONS/m ) { | ||
668 | 109 | PTDEBUG && _d('Parsing DSN OPTIONS'); | ||
669 | 110 | my $dsn_attribs = { | ||
670 | 111 | dsn => 1, | ||
671 | 112 | copy => 1, | ||
672 | 113 | }; | ||
673 | 114 | my $parse_dsn_attribs = sub { | ||
674 | 115 | my ( $self, $option, $attribs ) = @_; | ||
675 | 116 | map { | ||
676 | 117 | my $val = $attribs->{$_}; | ||
677 | 118 | if ( $val ) { | ||
678 | 119 | $val = $val eq 'yes' ? 1 | ||
679 | 120 | : $val eq 'no' ? 0 | ||
680 | 121 | : $val; | ||
681 | 122 | $attribs->{$_} = $val; | ||
682 | 123 | } | ||
683 | 124 | } keys %$attribs; | ||
684 | 125 | return { | ||
685 | 126 | key => $option, | ||
686 | 127 | %$attribs, | ||
687 | 128 | }; | ||
688 | 129 | }; | ||
689 | 130 | my $dsn_o = new OptionParser( | ||
690 | 131 | description => 'DSN OPTIONS', | ||
691 | 132 | head1 => 'DSN OPTIONS', | ||
692 | 133 | dsn => 0, # XXX don't infinitely recurse! | ||
693 | 134 | item => '\* (.)', # key opts are a single character | ||
694 | 135 | skip_rules => 1, # no rules before opts | ||
695 | 136 | attributes => $dsn_attribs, | ||
696 | 137 | parse_attributes => $parse_dsn_attribs, | ||
697 | 138 | ); | ||
698 | 139 | my @dsn_opts = map { | ||
699 | 140 | my $opts = { | ||
700 | 141 | key => $_->{spec}->{key}, | ||
701 | 142 | dsn => $_->{spec}->{dsn}, | ||
702 | 143 | copy => $_->{spec}->{copy}, | ||
703 | 144 | desc => $_->{desc}, | ||
704 | 145 | }; | ||
705 | 146 | $opts; | ||
706 | 147 | } $dsn_o->_pod_to_specs($file); | ||
707 | 148 | $self->{DSNParser} = DSNParser->new(opts => \@dsn_opts); | ||
708 | 149 | } | ||
709 | 150 | |||
710 | 151 | if ( $contents =~ m/^=head1 VERSION\n\n^(.+)$/m ) { | ||
711 | 152 | $self->{version} = $1; | ||
712 | 153 | PTDEBUG && _d($self->{version}); | ||
713 | 154 | } | ||
714 | 155 | |||
715 | 156 | return; | ||
716 | 157 | } | ||
717 | 158 | |||
718 | 159 | sub DSNParser { | ||
719 | 160 | my ( $self ) = @_; | ||
720 | 161 | return $self->{DSNParser}; | ||
721 | 162 | }; | ||
722 | 163 | |||
723 | 164 | sub get_defaults_files { | ||
724 | 165 | my ( $self ) = @_; | ||
725 | 166 | return @{$self->{default_files}}; | ||
726 | 167 | } | ||
727 | 168 | |||
728 | 169 | sub _pod_to_specs { | ||
729 | 170 | my ( $self, $file ) = @_; | ||
730 | 171 | $file ||= $self->{file} || __FILE__; | ||
731 | 172 | open my $fh, '<', $file or die "Cannot open $file: $OS_ERROR"; | ||
732 | 173 | |||
733 | 174 | my @specs = (); | ||
734 | 175 | my @rules = (); | ||
735 | 176 | my $para; | ||
736 | 177 | |||
737 | 178 | local $INPUT_RECORD_SEPARATOR = ''; | ||
738 | 179 | while ( $para = <$fh> ) { | ||
739 | 180 | next unless $para =~ m/^=head1 $self->{head1}/; | ||
740 | 181 | last; | ||
741 | 182 | } | ||
742 | 183 | |||
743 | 184 | while ( $para = <$fh> ) { | ||
744 | 185 | last if $para =~ m/^=over/; | ||
745 | 186 | next if $self->{skip_rules}; | ||
746 | 187 | chomp $para; | ||
747 | 188 | $para =~ s/\s+/ /g; | ||
748 | 189 | $para =~ s/$POD_link_re/$1/go; | ||
749 | 190 | PTDEBUG && _d('Option rule:', $para); | ||
750 | 191 | push @rules, $para; | ||
751 | 192 | } | ||
752 | 193 | |||
753 | 194 | die "POD has no $self->{head1} section" unless $para; | ||
754 | 195 | |||
755 | 196 | do { | ||
756 | 197 | if ( my ($option) = $para =~ m/^=item $self->{item}/ ) { | ||
757 | 198 | chomp $para; | ||
758 | 199 | PTDEBUG && _d($para); | ||
759 | 200 | my %attribs; | ||
760 | 201 | |||
761 | 202 | $para = <$fh>; # read next paragraph, possibly attributes | ||
762 | 203 | |||
763 | 204 | if ( $para =~ m/: / ) { # attributes | ||
764 | 205 | $para =~ s/\s+\Z//g; | ||
765 | 206 | %attribs = map { | ||
766 | 207 | my ( $attrib, $val) = split(/: /, $_); | ||
767 | 208 | die "Unrecognized attribute for --$option: $attrib" | ||
768 | 209 | unless $self->{attributes}->{$attrib}; | ||
769 | 210 | ($attrib, $val); | ||
770 | 211 | } split(/; /, $para); | ||
771 | 212 | if ( $attribs{'short form'} ) { | ||
772 | 213 | $attribs{'short form'} =~ s/-//; | ||
773 | 214 | } | ||
774 | 215 | $para = <$fh>; # read next paragraph, probably short help desc | ||
775 | 216 | } | ||
776 | 217 | else { | ||
777 | 218 | PTDEBUG && _d('Option has no attributes'); | ||
778 | 219 | } | ||
779 | 220 | |||
780 | 221 | $para =~ s/\s+\Z//g; | ||
781 | 222 | $para =~ s/\s+/ /g; | ||
782 | 223 | $para =~ s/$POD_link_re/$1/go; | ||
783 | 224 | |||
784 | 225 | $para =~ s/\.(?:\n.*| [A-Z].*|\Z)//s; | ||
785 | 226 | PTDEBUG && _d('Short help:', $para); | ||
786 | 227 | |||
787 | 228 | die "No description after option spec $option" if $para =~ m/^=item/; | ||
788 | 229 | |||
789 | 230 | if ( my ($base_option) = $option =~ m/^\[no\](.*)/ ) { | ||
790 | 231 | $option = $base_option; | ||
791 | 232 | $attribs{'negatable'} = 1; | ||
792 | 233 | } | ||
793 | 234 | |||
794 | 235 | push @specs, { | ||
795 | 236 | spec => $self->{parse_attributes}->($self, $option, \%attribs), | ||
796 | 237 | desc => $para | ||
797 | 238 | . (defined $attribs{default} ? " (default $attribs{default})" : ''), | ||
798 | 239 | group => ($attribs{'group'} ? $attribs{'group'} : 'default'), | ||
799 | 240 | }; | ||
800 | 241 | } | ||
801 | 242 | while ( $para = <$fh> ) { | ||
802 | 243 | last unless $para; | ||
803 | 244 | if ( $para =~ m/^=head1/ ) { | ||
804 | 245 | $para = undef; # Can't 'last' out of a do {} block. | ||
805 | 246 | last; | ||
806 | 247 | } | ||
807 | 248 | last if $para =~ m/^=item /; | ||
808 | 249 | } | ||
809 | 250 | } while ( $para ); | ||
810 | 251 | |||
811 | 252 | die "No valid specs in $self->{head1}" unless @specs; | ||
812 | 253 | |||
813 | 254 | close $fh; | ||
814 | 255 | return @specs, @rules; | ||
815 | 256 | } | ||
816 | 257 | |||
817 | 258 | sub _parse_specs { | ||
818 | 259 | my ( $self, @specs ) = @_; | ||
819 | 260 | my %disables; # special rule that requires deferred checking | ||
820 | 261 | |||
821 | 262 | foreach my $opt ( @specs ) { | ||
822 | 263 | if ( ref $opt ) { # It's an option spec, not a rule. | ||
823 | 264 | PTDEBUG && _d('Parsing opt spec:', | ||
824 | 265 | map { ($_, '=>', $opt->{$_}) } keys %$opt); | ||
825 | 266 | |||
826 | 267 | my ( $long, $short ) = $opt->{spec} =~ m/^([\w-]+)(?:\|([^!+=]*))?/; | ||
827 | 268 | if ( !$long ) { | ||
828 | 269 | die "Cannot parse long option from spec $opt->{spec}"; | ||
829 | 270 | } | ||
830 | 271 | $opt->{long} = $long; | ||
831 | 272 | |||
832 | 273 | die "Duplicate long option --$long" if exists $self->{opts}->{$long}; | ||
833 | 274 | $self->{opts}->{$long} = $opt; | ||
834 | 275 | |||
835 | 276 | if ( length $long == 1 ) { | ||
836 | 277 | PTDEBUG && _d('Long opt', $long, 'looks like short opt'); | ||
837 | 278 | $self->{short_opts}->{$long} = $long; | ||
838 | 279 | } | ||
839 | 280 | |||
840 | 281 | if ( $short ) { | ||
841 | 282 | die "Duplicate short option -$short" | ||
842 | 283 | if exists $self->{short_opts}->{$short}; | ||
843 | 284 | $self->{short_opts}->{$short} = $long; | ||
844 | 285 | $opt->{short} = $short; | ||
845 | 286 | } | ||
846 | 287 | else { | ||
847 | 288 | $opt->{short} = undef; | ||
848 | 289 | } | ||
849 | 290 | |||
850 | 291 | $opt->{is_negatable} = $opt->{spec} =~ m/!/ ? 1 : 0; | ||
851 | 292 | $opt->{is_cumulative} = $opt->{spec} =~ m/\+/ ? 1 : 0; | ||
852 | 293 | $opt->{is_required} = $opt->{desc} =~ m/required/ ? 1 : 0; | ||
853 | 294 | |||
854 | 295 | $opt->{group} ||= 'default'; | ||
855 | 296 | $self->{groups}->{ $opt->{group} }->{$long} = 1; | ||
856 | 297 | |||
857 | 298 | $opt->{value} = undef; | ||
858 | 299 | $opt->{got} = 0; | ||
859 | 300 | |||
860 | 301 | my ( $type ) = $opt->{spec} =~ m/=(.)/; | ||
861 | 302 | $opt->{type} = $type; | ||
862 | 303 | PTDEBUG && _d($long, 'type:', $type); | ||
863 | 304 | |||
864 | 305 | |||
865 | 306 | $opt->{spec} =~ s/=./=s/ if ( $type && $type =~ m/[HhAadzm]/ ); | ||
866 | 307 | |||
867 | 308 | if ( (my ($def) = $opt->{desc} =~ m/default\b(?: ([^)]+))?/) ) { | ||
868 | 309 | $self->{defaults}->{$long} = defined $def ? $def : 1; | ||
869 | 310 | PTDEBUG && _d($long, 'default:', $def); | ||
870 | 311 | } | ||
871 | 312 | |||
872 | 313 | if ( $long eq 'config' ) { | ||
873 | 314 | $self->{defaults}->{$long} = join(',', $self->get_defaults_files()); | ||
874 | 315 | } | ||
875 | 316 | |||
876 | 317 | if ( (my ($dis) = $opt->{desc} =~ m/(disables .*)/) ) { | ||
877 | 318 | $disables{$long} = $dis; | ||
878 | 319 | PTDEBUG && _d('Deferring check of disables rule for', $opt, $dis); | ||
879 | 320 | } | ||
880 | 321 | |||
881 | 322 | $self->{opts}->{$long} = $opt; | ||
882 | 323 | } | ||
883 | 324 | else { # It's an option rule, not a spec. | ||
884 | 325 | PTDEBUG && _d('Parsing rule:', $opt); | ||
885 | 326 | push @{$self->{rules}}, $opt; | ||
886 | 327 | my @participants = $self->_get_participants($opt); | ||
887 | 328 | my $rule_ok = 0; | ||
888 | 329 | |||
889 | 330 | if ( $opt =~ m/mutually exclusive|one and only one/ ) { | ||
890 | 331 | $rule_ok = 1; | ||
891 | 332 | push @{$self->{mutex}}, \@participants; | ||
892 | 333 | PTDEBUG && _d(@participants, 'are mutually exclusive'); | ||
893 | 334 | } | ||
894 | 335 | if ( $opt =~ m/at least one|one and only one/ ) { | ||
895 | 336 | $rule_ok = 1; | ||
896 | 337 | push @{$self->{atleast1}}, \@participants; | ||
897 | 338 | PTDEBUG && _d(@participants, 'require at least one'); | ||
898 | 339 | } | ||
899 | 340 | if ( $opt =~ m/default to/ ) { | ||
900 | 341 | $rule_ok = 1; | ||
901 | 342 | $self->{defaults_to}->{$participants[0]} = $participants[1]; | ||
902 | 343 | PTDEBUG && _d($participants[0], 'defaults to', $participants[1]); | ||
903 | 344 | } | ||
904 | 345 | if ( $opt =~ m/restricted to option groups/ ) { | ||
905 | 346 | $rule_ok = 1; | ||
906 | 347 | my ($groups) = $opt =~ m/groups ([\w\s\,]+)/; | ||
907 | 348 | my @groups = split(',', $groups); | ||
908 | 349 | %{$self->{allowed_groups}->{$participants[0]}} = map { | ||
909 | 350 | s/\s+//; | ||
910 | 351 | $_ => 1; | ||
911 | 352 | } @groups; | ||
912 | 353 | } | ||
913 | 354 | if( $opt =~ m/accepts additional command-line arguments/ ) { | ||
914 | 355 | $rule_ok = 1; | ||
915 | 356 | $self->{strict} = 0; | ||
916 | 357 | PTDEBUG && _d("Strict mode disabled by rule"); | ||
917 | 358 | } | ||
918 | 359 | |||
919 | 360 | die "Unrecognized option rule: $opt" unless $rule_ok; | ||
920 | 361 | } | ||
921 | 362 | } | ||
922 | 363 | |||
923 | 364 | foreach my $long ( keys %disables ) { | ||
924 | 365 | my @participants = $self->_get_participants($disables{$long}); | ||
925 | 366 | $self->{disables}->{$long} = \@participants; | ||
926 | 367 | PTDEBUG && _d('Option', $long, 'disables', @participants); | ||
927 | 368 | } | ||
928 | 369 | |||
929 | 370 | return; | ||
930 | 371 | } | ||
931 | 372 | |||
932 | 373 | sub _get_participants { | ||
933 | 374 | my ( $self, $str ) = @_; | ||
934 | 375 | my @participants; | ||
935 | 376 | foreach my $long ( $str =~ m/--(?:\[no\])?([\w-]+)/g ) { | ||
936 | 377 | die "Option --$long does not exist while processing rule $str" | ||
937 | 378 | unless exists $self->{opts}->{$long}; | ||
938 | 379 | push @participants, $long; | ||
939 | 380 | } | ||
940 | 381 | PTDEBUG && _d('Participants for', $str, ':', @participants); | ||
941 | 382 | return @participants; | ||
942 | 383 | } | ||
943 | 384 | |||
944 | 385 | sub opts { | ||
945 | 386 | my ( $self ) = @_; | ||
946 | 387 | my %opts = %{$self->{opts}}; | ||
947 | 388 | return %opts; | ||
948 | 389 | } | ||
949 | 390 | |||
950 | 391 | sub short_opts { | ||
951 | 392 | my ( $self ) = @_; | ||
952 | 393 | my %short_opts = %{$self->{short_opts}}; | ||
953 | 394 | return %short_opts; | ||
954 | 395 | } | ||
955 | 396 | |||
956 | 397 | sub set_defaults { | ||
957 | 398 | my ( $self, %defaults ) = @_; | ||
958 | 399 | $self->{defaults} = {}; | ||
959 | 400 | foreach my $long ( keys %defaults ) { | ||
960 | 401 | die "Cannot set default for nonexistent option $long" | ||
961 | 402 | unless exists $self->{opts}->{$long}; | ||
962 | 403 | $self->{defaults}->{$long} = $defaults{$long}; | ||
963 | 404 | PTDEBUG && _d('Default val for', $long, ':', $defaults{$long}); | ||
964 | 405 | } | ||
965 | 406 | return; | ||
966 | 407 | } | ||
967 | 408 | |||
968 | 409 | sub get_defaults { | ||
969 | 410 | my ( $self ) = @_; | ||
970 | 411 | return $self->{defaults}; | ||
971 | 412 | } | ||
972 | 413 | |||
973 | 414 | sub get_groups { | ||
974 | 415 | my ( $self ) = @_; | ||
975 | 416 | return $self->{groups}; | ||
976 | 417 | } | ||
977 | 418 | |||
978 | 419 | sub _set_option { | ||
979 | 420 | my ( $self, $opt, $val ) = @_; | ||
980 | 421 | my $long = exists $self->{opts}->{$opt} ? $opt | ||
981 | 422 | : exists $self->{short_opts}->{$opt} ? $self->{short_opts}->{$opt} | ||
982 | 423 | : die "Getopt::Long gave a nonexistent option: $opt"; | ||
983 | 424 | |||
984 | 425 | $opt = $self->{opts}->{$long}; | ||
985 | 426 | if ( $opt->{is_cumulative} ) { | ||
986 | 427 | $opt->{value}++; | ||
987 | 428 | } | ||
988 | 429 | else { | ||
989 | 430 | $opt->{value} = $val; | ||
990 | 431 | } | ||
991 | 432 | $opt->{got} = 1; | ||
992 | 433 | PTDEBUG && _d('Got option', $long, '=', $val); | ||
993 | 434 | } | ||
994 | 435 | |||
995 | 436 | sub get_opts { | ||
996 | 437 | my ( $self ) = @_; | ||
997 | 438 | |||
998 | 439 | foreach my $long ( keys %{$self->{opts}} ) { | ||
999 | 440 | $self->{opts}->{$long}->{got} = 0; | ||
1000 | 441 | $self->{opts}->{$long}->{value} | ||
1001 | 442 | = exists $self->{defaults}->{$long} ? $self->{defaults}->{$long} | ||
1002 | 443 | : $self->{opts}->{$long}->{is_cumulative} ? 0 | ||
1003 | 444 | : undef; | ||
1004 | 445 | } | ||
1005 | 446 | $self->{got_opts} = 0; | ||
1006 | 447 | |||
1007 | 448 | $self->{errors} = []; | ||
1008 | 449 | |||
1009 | 450 | if ( @ARGV && $ARGV[0] eq "--config" ) { | ||
1010 | 451 | shift @ARGV; | ||
1011 | 452 | $self->_set_option('config', shift @ARGV); | ||
1012 | 453 | } | ||
1013 | 454 | if ( $self->has('config') ) { | ||
1014 | 455 | my @extra_args; | ||
1015 | 456 | foreach my $filename ( split(',', $self->get('config')) ) { | ||
1016 | 457 | eval { | ||
1017 | 458 | push @extra_args, $self->_read_config_file($filename); | ||
1018 | 459 | }; | ||
1019 | 460 | if ( $EVAL_ERROR ) { | ||
1020 | 461 | if ( $self->got('config') ) { | ||
1021 | 462 | die $EVAL_ERROR; | ||
1022 | 463 | } | ||
1023 | 464 | elsif ( PTDEBUG ) { | ||
1024 | 465 | _d($EVAL_ERROR); | ||
1025 | 466 | } | ||
1026 | 467 | } | ||
1027 | 468 | } | ||
1028 | 469 | unshift @ARGV, @extra_args; | ||
1029 | 470 | } | ||
1030 | 471 | |||
1031 | 472 | Getopt::Long::Configure('no_ignore_case', 'bundling'); | ||
1032 | 473 | GetOptions( | ||
1033 | 474 | map { $_->{spec} => sub { $self->_set_option(@_); } } | ||
1034 | 475 | grep { $_->{long} ne 'config' } # --config is handled specially above. | ||
1035 | 476 | values %{$self->{opts}} | ||
1036 | 477 | ) or $self->save_error('Error parsing options'); | ||
1037 | 478 | |||
1038 | 479 | if ( exists $self->{opts}->{version} && $self->{opts}->{version}->{got} ) { | ||
1039 | 480 | if ( $self->{version} ) { | ||
1040 | 481 | print $self->{version}, "\n"; | ||
1041 | 482 | } | ||
1042 | 483 | else { | ||
1043 | 484 | print "Error parsing version. See the VERSION section of the tool's documentation.\n"; | ||
1044 | 485 | } | ||
1045 | 486 | exit 0; | ||
1046 | 487 | } | ||
1047 | 488 | |||
1048 | 489 | if ( @ARGV && $self->{strict} ) { | ||
1049 | 490 | $self->save_error("Unrecognized command-line options @ARGV"); | ||
1050 | 491 | } | ||
1051 | 492 | |||
1052 | 493 | foreach my $mutex ( @{$self->{mutex}} ) { | ||
1053 | 494 | my @set = grep { $self->{opts}->{$_}->{got} } @$mutex; | ||
1054 | 495 | if ( @set > 1 ) { | ||
1055 | 496 | my $err = join(', ', map { "--$self->{opts}->{$_}->{long}" } | ||
1056 | 497 | @{$mutex}[ 0 .. scalar(@$mutex) - 2] ) | ||
1057 | 498 | . ' and --'.$self->{opts}->{$mutex->[-1]}->{long} | ||
1058 | 499 | . ' are mutually exclusive.'; | ||
1059 | 500 | $self->save_error($err); | ||
1060 | 501 | } | ||
1061 | 502 | } | ||
1062 | 503 | |||
1063 | 504 | foreach my $required ( @{$self->{atleast1}} ) { | ||
1064 | 505 | my @set = grep { $self->{opts}->{$_}->{got} } @$required; | ||
1065 | 506 | if ( @set == 0 ) { | ||
1066 | 507 | my $err = join(', ', map { "--$self->{opts}->{$_}->{long}" } | ||
1067 | 508 | @{$required}[ 0 .. scalar(@$required) - 2] ) | ||
1068 | 509 | .' or --'.$self->{opts}->{$required->[-1]}->{long}; | ||
1069 | 510 | $self->save_error("Specify at least one of $err"); | ||
1070 | 511 | } | ||
1071 | 512 | } | ||
1072 | 513 | |||
1073 | 514 | $self->_check_opts( keys %{$self->{opts}} ); | ||
1074 | 515 | $self->{got_opts} = 1; | ||
1075 | 516 | return; | ||
1076 | 517 | } | ||
1077 | 518 | |||
1078 | 519 | sub _check_opts { | ||
1079 | 520 | my ( $self, @long ) = @_; | ||
1080 | 521 | my $long_last = scalar @long; | ||
1081 | 522 | while ( @long ) { | ||
1082 | 523 | foreach my $i ( 0..$#long ) { | ||
1083 | 524 | my $long = $long[$i]; | ||
1084 | 525 | next unless $long; | ||
1085 | 526 | my $opt = $self->{opts}->{$long}; | ||
1086 | 527 | if ( $opt->{got} ) { | ||
1087 | 528 | if ( exists $self->{disables}->{$long} ) { | ||
1088 | 529 | my @disable_opts = @{$self->{disables}->{$long}}; | ||
1089 | 530 | map { $self->{opts}->{$_}->{value} = undef; } @disable_opts; | ||
1090 | 531 | PTDEBUG && _d('Unset options', @disable_opts, | ||
1091 | 532 | 'because', $long,'disables them'); | ||
1092 | 533 | } | ||
1093 | 534 | |||
1094 | 535 | if ( exists $self->{allowed_groups}->{$long} ) { | ||
1095 | 536 | |||
1096 | 537 | my @restricted_groups = grep { | ||
1097 | 538 | !exists $self->{allowed_groups}->{$long}->{$_} | ||
1098 | 539 | } keys %{$self->{groups}}; | ||
1099 | 540 | |||
1100 | 541 | my @restricted_opts; | ||
1101 | 542 | foreach my $restricted_group ( @restricted_groups ) { | ||
1102 | 543 | RESTRICTED_OPT: | ||
1103 | 544 | foreach my $restricted_opt ( | ||
1104 | 545 | keys %{$self->{groups}->{$restricted_group}} ) | ||
1105 | 546 | { | ||
1106 | 547 | next RESTRICTED_OPT if $restricted_opt eq $long; | ||
1107 | 548 | push @restricted_opts, $restricted_opt | ||
1108 | 549 | if $self->{opts}->{$restricted_opt}->{got}; | ||
1109 | 550 | } | ||
1110 | 551 | } | ||
1111 | 552 | |||
1112 | 553 | if ( @restricted_opts ) { | ||
1113 | 554 | my $err; | ||
1114 | 555 | if ( @restricted_opts == 1 ) { | ||
1115 | 556 | $err = "--$restricted_opts[0]"; | ||
1116 | 557 | } | ||
1117 | 558 | else { | ||
1118 | 559 | $err = join(', ', | ||
1119 | 560 | map { "--$self->{opts}->{$_}->{long}" } | ||
1120 | 561 | grep { $_ } | ||
1121 | 562 | @restricted_opts[0..scalar(@restricted_opts) - 2] | ||
1122 | 563 | ) | ||
1123 | 564 | . ' or --'.$self->{opts}->{$restricted_opts[-1]}->{long}; | ||
1124 | 565 | } | ||
1125 | 566 | $self->save_error("--$long is not allowed with $err"); | ||
1126 | 567 | } | ||
1127 | 568 | } | ||
1128 | 569 | |||
1129 | 570 | } | ||
1130 | 571 | elsif ( $opt->{is_required} ) { | ||
1131 | 572 | $self->save_error("Required option --$long must be specified"); | ||
1132 | 573 | } | ||
1133 | 574 | |||
1134 | 575 | $self->_validate_type($opt); | ||
1135 | 576 | if ( $opt->{parsed} ) { | ||
1136 | 577 | delete $long[$i]; | ||
1137 | 578 | } | ||
1138 | 579 | else { | ||
1139 | 580 | PTDEBUG && _d('Temporarily failed to parse', $long); | ||
1140 | 581 | } | ||
1141 | 582 | } | ||
1142 | 583 | |||
1143 | 584 | die "Failed to parse options, possibly due to circular dependencies" | ||
1144 | 585 | if @long == $long_last; | ||
1145 | 586 | $long_last = @long; | ||
1146 | 587 | } | ||
1147 | 588 | |||
1148 | 589 | return; | ||
1149 | 590 | } | ||
1150 | 591 | |||
1151 | 592 | sub _validate_type { | ||
1152 | 593 | my ( $self, $opt ) = @_; | ||
1153 | 594 | return unless $opt; | ||
1154 | 595 | |||
1155 | 596 | if ( !$opt->{type} ) { | ||
1156 | 597 | $opt->{parsed} = 1; | ||
1157 | 598 | return; | ||
1158 | 599 | } | ||
1159 | 600 | |||
1160 | 601 | my $val = $opt->{value}; | ||
1161 | 602 | |||
1162 | 603 | if ( $val && $opt->{type} eq 'm' ) { # type time | ||
1163 | 604 | PTDEBUG && _d('Parsing option', $opt->{long}, 'as a time value'); | ||
1164 | 605 | my ( $prefix, $num, $suffix ) = $val =~ m/([+-]?)(\d+)([a-z])?$/; | ||
1165 | 606 | if ( !$suffix ) { | ||
1166 | 607 | my ( $s ) = $opt->{desc} =~ m/\(suffix (.)\)/; | ||
1167 | 608 | $suffix = $s || 's'; | ||
1168 | 609 | PTDEBUG && _d('No suffix given; using', $suffix, 'for', | ||
1169 | 610 | $opt->{long}, '(value:', $val, ')'); | ||
1170 | 611 | } | ||
1171 | 612 | if ( $suffix =~ m/[smhd]/ ) { | ||
1172 | 613 | $val = $suffix eq 's' ? $num # Seconds | ||
1173 | 614 | : $suffix eq 'm' ? $num * 60 # Minutes | ||
1174 | 615 | : $suffix eq 'h' ? $num * 3600 # Hours | ||
1175 | 616 | : $num * 86400; # Days | ||
1176 | 617 | $opt->{value} = ($prefix || '') . $val; | ||
1177 | 618 | PTDEBUG && _d('Setting option', $opt->{long}, 'to', $val); | ||
1178 | 619 | } | ||
1179 | 620 | else { | ||
1180 | 621 | $self->save_error("Invalid time suffix for --$opt->{long}"); | ||
1181 | 622 | } | ||
1182 | 623 | } | ||
1183 | 624 | elsif ( $val && $opt->{type} eq 'd' ) { # type DSN | ||
1184 | 625 | PTDEBUG && _d('Parsing option', $opt->{long}, 'as a DSN'); | ||
1185 | 626 | my $prev = {}; | ||
1186 | 627 | my $from_key = $self->{defaults_to}->{ $opt->{long} }; | ||
1187 | 628 | if ( $from_key ) { | ||
1188 | 629 | PTDEBUG && _d($opt->{long}, 'DSN copies from', $from_key, 'DSN'); | ||
1189 | 630 | if ( $self->{opts}->{$from_key}->{parsed} ) { | ||
1190 | 631 | $prev = $self->{opts}->{$from_key}->{value}; | ||
1191 | 632 | } | ||
1192 | 633 | else { | ||
1193 | 634 | PTDEBUG && _d('Cannot parse', $opt->{long}, 'until', | ||
1194 | 635 | $from_key, 'parsed'); | ||
1195 | 636 | return; | ||
1196 | 637 | } | ||
1197 | 638 | } | ||
1198 | 639 | my $defaults = $self->{DSNParser}->parse_options($self); | ||
1199 | 640 | $opt->{value} = $self->{DSNParser}->parse($val, $prev, $defaults); | ||
1200 | 641 | } | ||
1201 | 642 | elsif ( $val && $opt->{type} eq 'z' ) { # type size | ||
1202 | 643 | PTDEBUG && _d('Parsing option', $opt->{long}, 'as a size value'); | ||
1203 | 644 | $self->_parse_size($opt, $val); | ||
1204 | 645 | } | ||
1205 | 646 | elsif ( $opt->{type} eq 'H' || (defined $val && $opt->{type} eq 'h') ) { | ||
1206 | 647 | $opt->{value} = { map { $_ => 1 } split(/(?<!\\),\s*/, ($val || '')) }; | ||
1207 | 648 | } | ||
1208 | 649 | elsif ( $opt->{type} eq 'A' || (defined $val && $opt->{type} eq 'a') ) { | ||
1209 | 650 | $opt->{value} = [ split(/(?<!\\),\s*/, ($val || '')) ]; | ||
1210 | 651 | } | ||
1211 | 652 | else { | ||
1212 | 653 | PTDEBUG && _d('Nothing to validate for option', | ||
1213 | 654 | $opt->{long}, 'type', $opt->{type}, 'value', $val); | ||
1214 | 655 | } | ||
1215 | 656 | |||
1216 | 657 | $opt->{parsed} = 1; | ||
1217 | 658 | return; | ||
1218 | 659 | } | ||
1219 | 660 | |||
1220 | 661 | sub get { | ||
1221 | 662 | my ( $self, $opt ) = @_; | ||
1222 | 663 | my $long = (length $opt == 1 ? $self->{short_opts}->{$opt} : $opt); | ||
1223 | 664 | die "Option $opt does not exist" | ||
1224 | 665 | unless $long && exists $self->{opts}->{$long}; | ||
1225 | 666 | return $self->{opts}->{$long}->{value}; | ||
1226 | 667 | } | ||
1227 | 668 | |||
1228 | 669 | sub got { | ||
1229 | 670 | my ( $self, $opt ) = @_; | ||
1230 | 671 | my $long = (length $opt == 1 ? $self->{short_opts}->{$opt} : $opt); | ||
1231 | 672 | die "Option $opt does not exist" | ||
1232 | 673 | unless $long && exists $self->{opts}->{$long}; | ||
1233 | 674 | return $self->{opts}->{$long}->{got}; | ||
1234 | 675 | } | ||
1235 | 676 | |||
1236 | 677 | sub has { | ||
1237 | 678 | my ( $self, $opt ) = @_; | ||
1238 | 679 | my $long = (length $opt == 1 ? $self->{short_opts}->{$opt} : $opt); | ||
1239 | 680 | return defined $long ? exists $self->{opts}->{$long} : 0; | ||
1240 | 681 | } | ||
1241 | 682 | |||
1242 | 683 | sub set { | ||
1243 | 684 | my ( $self, $opt, $val ) = @_; | ||
1244 | 685 | my $long = (length $opt == 1 ? $self->{short_opts}->{$opt} : $opt); | ||
1245 | 686 | die "Option $opt does not exist" | ||
1246 | 687 | unless $long && exists $self->{opts}->{$long}; | ||
1247 | 688 | $self->{opts}->{$long}->{value} = $val; | ||
1248 | 689 | return; | ||
1249 | 690 | } | ||
1250 | 691 | |||
1251 | 692 | sub save_error { | ||
1252 | 693 | my ( $self, $error ) = @_; | ||
1253 | 694 | push @{$self->{errors}}, $error; | ||
1254 | 695 | return; | ||
1255 | 696 | } | ||
1256 | 697 | |||
1257 | 698 | sub errors { | ||
1258 | 699 | my ( $self ) = @_; | ||
1259 | 700 | return $self->{errors}; | ||
1260 | 701 | } | ||
1261 | 702 | |||
1262 | 703 | sub usage { | ||
1263 | 704 | my ( $self ) = @_; | ||
1264 | 705 | warn "No usage string is set" unless $self->{usage}; # XXX | ||
1265 | 706 | return "Usage: " . ($self->{usage} || '') . "\n"; | ||
1266 | 707 | } | ||
1267 | 708 | |||
1268 | 709 | sub descr { | ||
1269 | 710 | my ( $self ) = @_; | ||
1270 | 711 | warn "No description string is set" unless $self->{description}; # XXX | ||
1271 | 712 | my $descr = ($self->{description} || $self->{program_name} || '') | ||
1272 | 713 | . " For more details, please use the --help option, " | ||
1273 | 714 | . "or try 'perldoc $PROGRAM_NAME' " | ||
1274 | 715 | . "for complete documentation."; | ||
1275 | 716 | $descr = join("\n", $descr =~ m/(.{0,80})(?:\s+|$)/g) | ||
1276 | 717 | unless $ENV{DONT_BREAK_LINES}; | ||
1277 | 718 | $descr =~ s/ +$//mg; | ||
1278 | 719 | return $descr; | ||
1279 | 720 | } | ||
1280 | 721 | |||
1281 | 722 | sub usage_or_errors { | ||
1282 | 723 | my ( $self, $file, $return ) = @_; | ||
1283 | 724 | $file ||= $self->{file} || __FILE__; | ||
1284 | 725 | |||
1285 | 726 | if ( !$self->{description} || !$self->{usage} ) { | ||
1286 | 727 | PTDEBUG && _d("Getting description and usage from SYNOPSIS in", $file); | ||
1287 | 728 | my %synop = $self->_parse_synopsis($file); | ||
1288 | 729 | $self->{description} ||= $synop{description}; | ||
1289 | 730 | $self->{usage} ||= $synop{usage}; | ||
1290 | 731 | PTDEBUG && _d("Description:", $self->{description}, | ||
1291 | 732 | "\nUsage:", $self->{usage}); | ||
1292 | 733 | } | ||
1293 | 734 | |||
1294 | 735 | if ( $self->{opts}->{help}->{got} ) { | ||
1295 | 736 | print $self->print_usage() or die "Cannot print usage: $OS_ERROR"; | ||
1296 | 737 | exit 0 unless $return; | ||
1297 | 738 | } | ||
1298 | 739 | elsif ( scalar @{$self->{errors}} ) { | ||
1299 | 740 | print $self->print_errors() or die "Cannot print errors: $OS_ERROR"; | ||
1300 | 741 | exit 0 unless $return; | ||
1301 | 742 | } | ||
1302 | 743 | |||
1303 | 744 | return; | ||
1304 | 745 | } | ||
1305 | 746 | |||
1306 | 747 | sub print_errors { | ||
1307 | 748 | my ( $self ) = @_; | ||
1308 | 749 | my $usage = $self->usage() . "\n"; | ||
1309 | 750 | if ( (my @errors = @{$self->{errors}}) ) { | ||
1310 | 751 | $usage .= join("\n * ", 'Errors in command-line arguments:', @errors) | ||
1311 | 752 | . "\n"; | ||
1312 | 753 | } | ||
1313 | 754 | return $usage . "\n" . $self->descr(); | ||
1314 | 755 | } | ||
1315 | 756 | |||
1316 | 757 | sub print_usage { | ||
1317 | 758 | my ( $self ) = @_; | ||
1318 | 759 | die "Run get_opts() before print_usage()" unless $self->{got_opts}; | ||
1319 | 760 | my @opts = values %{$self->{opts}}; | ||
1320 | 761 | |||
1321 | 762 | my $maxl = max( | ||
1322 | 763 | map { | ||
1323 | 764 | length($_->{long}) # option long name | ||
1324 | 765 | + ($_->{is_negatable} ? 4 : 0) # "[no]" if opt is negatable | ||
1325 | 766 | + ($_->{type} ? 2 : 0) # "=x" where x is the opt type | ||
1326 | 767 | } | ||
1327 | 768 | @opts); | ||
1328 | 769 | |||
1329 | 770 | my $maxs = max(0, | ||
1330 | 771 | map { | ||
1331 | 772 | length($_) | ||
1332 | 773 | + ($self->{opts}->{$_}->{is_negatable} ? 4 : 0) | ||
1333 | 774 | + ($self->{opts}->{$_}->{type} ? 2 : 0) | ||
1334 | 775 | } | ||
1335 | 776 | values %{$self->{short_opts}}); | ||
1336 | 777 | |||
1337 | 778 | my $lcol = max($maxl, ($maxs + 3)); | ||
1338 | 779 | my $rcol = 80 - $lcol - 6; | ||
1339 | 780 | my $rpad = ' ' x ( 80 - $rcol ); | ||
1340 | 781 | |||
1341 | 782 | $maxs = max($lcol - 3, $maxs); | ||
1342 | 783 | |||
1343 | 784 | my $usage = $self->descr() . "\n" . $self->usage(); | ||
1344 | 785 | |||
1345 | 786 | my @groups = reverse sort grep { $_ ne 'default'; } keys %{$self->{groups}}; | ||
1346 | 787 | push @groups, 'default'; | ||
1347 | 788 | |||
1348 | 789 | foreach my $group ( reverse @groups ) { | ||
1349 | 790 | $usage .= "\n".($group eq 'default' ? 'Options' : $group).":\n\n"; | ||
1350 | 791 | foreach my $opt ( | ||
1351 | 792 | sort { $a->{long} cmp $b->{long} } | ||
1352 | 793 | grep { $_->{group} eq $group } | ||
1353 | 794 | @opts ) | ||
1354 | 795 | { | ||
1355 | 796 | my $long = $opt->{is_negatable} ? "[no]$opt->{long}" : $opt->{long}; | ||
1356 | 797 | my $short = $opt->{short}; | ||
1357 | 798 | my $desc = $opt->{desc}; | ||
1358 | 799 | |||
1359 | 800 | $long .= $opt->{type} ? "=$opt->{type}" : ""; | ||
1360 | 801 | |||
1361 | 802 | if ( $opt->{type} && $opt->{type} eq 'm' ) { | ||
1362 | 803 | my ($s) = $desc =~ m/\(suffix (.)\)/; | ||
1363 | 804 | $s ||= 's'; | ||
1364 | 805 | $desc =~ s/\s+\(suffix .\)//; | ||
1365 | 806 | $desc .= ". Optional suffix s=seconds, m=minutes, h=hours, " | ||
1366 | 807 | . "d=days; if no suffix, $s is used."; | ||
1367 | 808 | } | ||
1368 | 809 | $desc = join("\n$rpad", grep { $_ } $desc =~ m/(.{0,$rcol})(?:\s+|$)/g); | ||
1369 | 810 | $desc =~ s/ +$//mg; | ||
1370 | 811 | if ( $short ) { | ||
1371 | 812 | $usage .= sprintf(" --%-${maxs}s -%s %s\n", $long, $short, $desc); | ||
1372 | 813 | } | ||
1373 | 814 | else { | ||
1374 | 815 | $usage .= sprintf(" --%-${lcol}s %s\n", $long, $desc); | ||
1375 | 816 | } | ||
1376 | 817 | } | ||
1377 | 818 | } | ||
1378 | 819 | |||
1379 | 820 | $usage .= "\nOption types: s=string, i=integer, f=float, h/H/a/A=comma-separated list, d=DSN, z=size, m=time\n"; | ||
1380 | 821 | |||
1381 | 822 | if ( (my @rules = @{$self->{rules}}) ) { | ||
1382 | 823 | $usage .= "\nRules:\n\n"; | ||
1383 | 824 | $usage .= join("\n", map { " $_" } @rules) . "\n"; | ||
1384 | 825 | } | ||
1385 | 826 | if ( $self->{DSNParser} ) { | ||
1386 | 827 | $usage .= "\n" . $self->{DSNParser}->usage(); | ||
1387 | 828 | } | ||
1388 | 829 | $usage .= "\nOptions and values after processing arguments:\n\n"; | ||
1389 | 830 | foreach my $opt ( sort { $a->{long} cmp $b->{long} } @opts ) { | ||
1390 | 831 | my $val = $opt->{value}; | ||
1391 | 832 | my $type = $opt->{type} || ''; | ||
1392 | 833 | my $bool = $opt->{spec} =~ m/^[\w-]+(?:\|[\w-])?!?$/; | ||
1393 | 834 | $val = $bool ? ( $val ? 'TRUE' : 'FALSE' ) | ||
1394 | 835 | : !defined $val ? '(No value)' | ||
1395 | 836 | : $type eq 'd' ? $self->{DSNParser}->as_string($val) | ||
1396 | 837 | : $type =~ m/H|h/ ? join(',', sort keys %$val) | ||
1397 | 838 | : $type =~ m/A|a/ ? join(',', @$val) | ||
1398 | 839 | : $val; | ||
1399 | 840 | $usage .= sprintf(" --%-${lcol}s %s\n", $opt->{long}, $val); | ||
1400 | 841 | } | ||
1401 | 842 | return $usage; | ||
1402 | 843 | } | ||
1403 | 844 | |||
1404 | 845 | sub prompt_noecho { | ||
1405 | 846 | shift @_ if ref $_[0] eq __PACKAGE__; | ||
1406 | 847 | my ( $prompt ) = @_; | ||
1407 | 848 | local $OUTPUT_AUTOFLUSH = 1; | ||
1408 | 849 | print $prompt | ||
1409 | 850 | or die "Cannot print: $OS_ERROR"; | ||
1410 | 851 | my $response; | ||
1411 | 852 | eval { | ||
1412 | 853 | require Term::ReadKey; | ||
1413 | 854 | Term::ReadKey::ReadMode('noecho'); | ||
1414 | 855 | chomp($response = <STDIN>); | ||
1415 | 856 | Term::ReadKey::ReadMode('normal'); | ||
1416 | 857 | print "\n" | ||
1417 | 858 | or die "Cannot print: $OS_ERROR"; | ||
1418 | 859 | }; | ||
1419 | 860 | if ( $EVAL_ERROR ) { | ||
1420 | 861 | die "Cannot read response; is Term::ReadKey installed? $EVAL_ERROR"; | ||
1421 | 862 | } | ||
1422 | 863 | return $response; | ||
1423 | 864 | } | ||
1424 | 865 | |||
1425 | 866 | sub _read_config_file { | ||
1426 | 867 | my ( $self, $filename ) = @_; | ||
1427 | 868 | open my $fh, "<", $filename or die "Cannot open $filename: $OS_ERROR\n"; | ||
1428 | 869 | my @args; | ||
1429 | 870 | my $prefix = '--'; | ||
1430 | 871 | my $parse = 1; | ||
1431 | 872 | |||
1432 | 873 | LINE: | ||
1433 | 874 | while ( my $line = <$fh> ) { | ||
1434 | 875 | chomp $line; | ||
1435 | 876 | next LINE if $line =~ m/^\s*(?:\#|\;|$)/; | ||
1436 | 877 | $line =~ s/\s+#.*$//g; | ||
1437 | 878 | $line =~ s/^\s+|\s+$//g; | ||
1438 | 879 | if ( $line eq '--' ) { | ||
1439 | 880 | $prefix = ''; | ||
1440 | 881 | $parse = 0; | ||
1441 | 882 | next LINE; | ||
1442 | 883 | } | ||
1443 | 884 | if ( $parse | ||
1444 | 885 | && (my($opt, $arg) = $line =~ m/^\s*([^=\s]+?)(?:\s*=\s*(.*?)\s*)?$/) | ||
1445 | 886 | ) { | ||
1446 | 887 | push @args, grep { defined $_ } ("$prefix$opt", $arg); | ||
1447 | 888 | } | ||
1448 | 889 | elsif ( $line =~ m/./ ) { | ||
1449 | 890 | push @args, $line; | ||
1450 | 891 | } | ||
1451 | 892 | else { | ||
1452 | 893 | die "Syntax error in file $filename at line $INPUT_LINE_NUMBER"; | ||
1453 | 894 | } | ||
1454 | 895 | } | ||
1455 | 896 | close $fh; | ||
1456 | 897 | return @args; | ||
1457 | 898 | } | ||
1458 | 899 | |||
1459 | 900 | sub read_para_after { | ||
1460 | 901 | my ( $self, $file, $regex ) = @_; | ||
1461 | 902 | open my $fh, "<", $file or die "Can't open $file: $OS_ERROR"; | ||
1462 | 903 | local $INPUT_RECORD_SEPARATOR = ''; | ||
1463 | 904 | my $para; | ||
1464 | 905 | while ( $para = <$fh> ) { | ||
1465 | 906 | next unless $para =~ m/^=pod$/m; | ||
1466 | 907 | last; | ||
1467 | 908 | } | ||
1468 | 909 | while ( $para = <$fh> ) { | ||
1469 | 910 | next unless $para =~ m/$regex/; | ||
1470 | 911 | last; | ||
1471 | 912 | } | ||
1472 | 913 | $para = <$fh>; | ||
1473 | 914 | chomp($para); | ||
1474 | 915 | close $fh or die "Can't close $file: $OS_ERROR"; | ||
1475 | 916 | return $para; | ||
1476 | 917 | } | ||
1477 | 918 | |||
1478 | 919 | sub clone { | ||
1479 | 920 | my ( $self ) = @_; | ||
1480 | 921 | |||
1481 | 922 | my %clone = map { | ||
1482 | 923 | my $hashref = $self->{$_}; | ||
1483 | 924 | my $val_copy = {}; | ||
1484 | 925 | foreach my $key ( keys %$hashref ) { | ||
1485 | 926 | my $ref = ref $hashref->{$key}; | ||
1486 | 927 | $val_copy->{$key} = !$ref ? $hashref->{$key} | ||
1487 | 928 | : $ref eq 'HASH' ? { %{$hashref->{$key}} } | ||
1488 | 929 | : $ref eq 'ARRAY' ? [ @{$hashref->{$key}} ] | ||
1489 | 930 | : $hashref->{$key}; | ||
1490 | 931 | } | ||
1491 | 932 | $_ => $val_copy; | ||
1492 | 933 | } qw(opts short_opts defaults); | ||
1493 | 934 | |||
1494 | 935 | foreach my $scalar ( qw(got_opts) ) { | ||
1495 | 936 | $clone{$scalar} = $self->{$scalar}; | ||
1496 | 937 | } | ||
1497 | 938 | |||
1498 | 939 | return bless \%clone; | ||
1499 | 940 | } | ||
1500 | 941 | |||
1501 | 942 | sub _parse_size { | ||
1502 | 943 | my ( $self, $opt, $val ) = @_; | ||
1503 | 944 | |||
1504 | 945 | if ( lc($val || '') eq 'null' ) { | ||
1505 | 946 | PTDEBUG && _d('NULL size for', $opt->{long}); | ||
1506 | 947 | $opt->{value} = 'null'; | ||
1507 | 948 | return; | ||
1508 | 949 | } | ||
1509 | 950 | |||
1510 | 951 | my %factor_for = (k => 1_024, M => 1_048_576, G => 1_073_741_824); | ||
1511 | 952 | my ($pre, $num, $factor) = $val =~ m/^([+-])?(\d+)([kMG])?$/; | ||
1512 | 953 | if ( defined $num ) { | ||
1513 | 954 | if ( $factor ) { | ||
1514 | 955 | $num *= $factor_for{$factor}; | ||
1515 | 956 | PTDEBUG && _d('Setting option', $opt->{y}, | ||
1516 | 957 | 'to num', $num, '* factor', $factor); | ||
1517 | 958 | } | ||
1518 | 959 | $opt->{value} = ($pre || '') . $num; | ||
1519 | 960 | } | ||
1520 | 961 | else { | ||
1521 | 962 | $self->save_error("Invalid size for --$opt->{long}: $val"); | ||
1522 | 963 | } | ||
1523 | 964 | return; | ||
1524 | 965 | } | ||
1525 | 966 | |||
1526 | 967 | sub _parse_attribs { | ||
1527 | 968 | my ( $self, $option, $attribs ) = @_; | ||
1528 | 969 | my $types = $self->{types}; | ||
1529 | 970 | return $option | ||
1530 | 971 | . ($attribs->{'short form'} ? '|' . $attribs->{'short form'} : '' ) | ||
1531 | 972 | . ($attribs->{'negatable'} ? '!' : '' ) | ||
1532 | 973 | . ($attribs->{'cumulative'} ? '+' : '' ) | ||
1533 | 974 | . ($attribs->{'type'} ? '=' . $types->{$attribs->{type}} : '' ); | ||
1534 | 975 | } | ||
1535 | 976 | |||
1536 | 977 | sub _parse_synopsis { | ||
1537 | 978 | my ( $self, $file ) = @_; | ||
1538 | 979 | $file ||= $self->{file} || __FILE__; | ||
1539 | 980 | PTDEBUG && _d("Parsing SYNOPSIS in", $file); | ||
1540 | 981 | |||
1541 | 982 | local $INPUT_RECORD_SEPARATOR = ''; # read paragraphs | ||
1542 | 983 | open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; | ||
1543 | 984 | my $para; | ||
1544 | 985 | 1 while defined($para = <$fh>) && $para !~ m/^=head1 SYNOPSIS/; | ||
1545 | 986 | die "$file does not contain a SYNOPSIS section" unless $para; | ||
1546 | 987 | my @synop; | ||
1547 | 988 | for ( 1..2 ) { # 1 for the usage, 2 for the description | ||
1548 | 989 | my $para = <$fh>; | ||
1549 | 990 | push @synop, $para; | ||
1550 | 991 | } | ||
1551 | 992 | close $fh; | ||
1552 | 993 | PTDEBUG && _d("Raw SYNOPSIS text:", @synop); | ||
1553 | 994 | my ($usage, $desc) = @synop; | ||
1554 | 995 | die "The SYNOPSIS section in $file is not formatted properly" | ||
1555 | 996 | unless $usage && $desc; | ||
1556 | 997 | |||
1557 | 998 | $usage =~ s/^\s*Usage:\s+(.+)/$1/; | ||
1558 | 999 | chomp $usage; | ||
1559 | 1000 | |||
1560 | 1001 | $desc =~ s/\n/ /g; | ||
1561 | 1002 | $desc =~ s/\s{2,}/ /g; | ||
1562 | 1003 | $desc =~ s/\. ([A-Z][a-z])/. $1/g; | ||
1563 | 1004 | $desc =~ s/\s+$//; | ||
1564 | 1005 | |||
1565 | 1006 | return ( | ||
1566 | 1007 | description => $desc, | ||
1567 | 1008 | usage => $usage, | ||
1568 | 1009 | ); | ||
1569 | 1010 | }; | ||
1570 | 1011 | |||
1571 | 1012 | sub _d { | ||
1572 | 1013 | my ($package, undef, $line) = caller 0; | ||
1573 | 1014 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
1574 | 1015 | map { defined $_ ? $_ : 'undef' } | ||
1575 | 1016 | @_; | ||
1576 | 1017 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
1577 | 1018 | } | ||
1578 | 1019 | |||
1579 | 1020 | if ( PTDEBUG ) { | ||
1580 | 1021 | print '# ', $^X, ' ', $], "\n"; | ||
1581 | 1022 | if ( my $uname = `uname -a` ) { | ||
1582 | 1023 | $uname =~ s/\s+/ /g; | ||
1583 | 1024 | print "# $uname\n"; | ||
1584 | 1025 | } | ||
1585 | 1026 | print '# Arguments: ', | ||
1586 | 1027 | join(' ', map { my $a = "_[$_]_"; $a =~ s/\n/\n# /g; $a; } @ARGV), "\n"; | ||
1587 | 1028 | } | ||
1588 | 1029 | |||
1589 | 1030 | 1; | ||
1590 | 1031 | } | ||
1591 | 1032 | # ########################################################################### | ||
1592 | 1033 | # End OptionParser package | ||
1593 | 1034 | # ########################################################################### | ||
1594 | 1035 | |||
1595 | 1036 | # ########################################################################### | ||
1596 | 1037 | # QueryParser package | ||
1597 | 1038 | # This package is a copy without comments from the original. The original | ||
1598 | 1039 | # with comments and its test file can be found in the Bazaar repository at, | ||
1599 | 1040 | # lib/QueryParser.pm | ||
1600 | 1041 | # t/lib/QueryParser.t | ||
1601 | 1042 | # See https://launchpad.net/percona-toolkit for more information. | ||
1602 | 1043 | # ########################################################################### | ||
1603 | 1044 | { | ||
1604 | 1045 | package QueryParser; | ||
1605 | 1046 | |||
1606 | 1047 | use strict; | ||
1607 | 1048 | use warnings FATAL => 'all'; | ||
1608 | 1049 | use English qw(-no_match_vars); | ||
1609 | 1050 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
1610 | 1051 | |||
1611 | 1052 | our $tbl_ident = qr/(?:`[^`]+`|\w+)(?:\.(?:`[^`]+`|\w+))?/; | ||
1612 | 1053 | our $tbl_regex = qr{ | ||
1613 | 1054 | \b(?:FROM|JOIN|(?<!KEY\s)UPDATE|INTO) # Words that precede table names | ||
1614 | 1055 | \b\s* | ||
1615 | 1056 | \(? # Optional paren around tables | ||
1616 | 1057 | ($tbl_ident | ||
1617 | 1058 | (?: (?:\s+ (?:AS\s+)? \w+)?, \s*$tbl_ident )* | ||
1618 | 1059 | ) | ||
1619 | 1060 | }xio; | ||
1620 | 1061 | our $has_derived = qr{ | ||
1621 | 1062 | \b(?:FROM|JOIN|,) | ||
1622 | 1063 | \s*\(\s*SELECT | ||
1623 | 1064 | }xi; | ||
1624 | 1065 | |||
1625 | 1066 | our $data_def_stmts = qr/(?:CREATE|ALTER|TRUNCATE|DROP|RENAME)/i; | ||
1626 | 1067 | |||
1627 | 1068 | our $data_manip_stmts = qr/(?:INSERT|UPDATE|DELETE|REPLACE)/i; | ||
1628 | 1069 | |||
1629 | 1070 | sub new { | ||
1630 | 1071 | my ( $class ) = @_; | ||
1631 | 1072 | bless {}, $class; | ||
1632 | 1073 | } | ||
1633 | 1074 | |||
1634 | 1075 | sub get_tables { | ||
1635 | 1076 | my ( $self, $query ) = @_; | ||
1636 | 1077 | return unless $query; | ||
1637 | 1078 | PTDEBUG && _d('Getting tables for', $query); | ||
1638 | 1079 | |||
1639 | 1080 | my ( $ddl_stmt ) = $query =~ m/^\s*($data_def_stmts)\b/i; | ||
1640 | 1081 | if ( $ddl_stmt ) { | ||
1641 | 1082 | PTDEBUG && _d('Special table type:', $ddl_stmt); | ||
1642 | 1083 | $query =~ s/IF\s+(?:NOT\s+)?EXISTS//i; | ||
1643 | 1084 | if ( $query =~ m/$ddl_stmt DATABASE\b/i ) { | ||
1644 | 1085 | PTDEBUG && _d('Query alters a database, not a table'); | ||
1645 | 1086 | return (); | ||
1646 | 1087 | } | ||
1647 | 1088 | if ( $ddl_stmt =~ m/CREATE/i && $query =~ m/$ddl_stmt\b.+?\bSELECT\b/i ) { | ||
1648 | 1089 | my ($select) = $query =~ m/\b(SELECT\b.+)/is; | ||
1649 | 1090 | PTDEBUG && _d('CREATE TABLE ... SELECT:', $select); | ||
1650 | 1091 | return $self->get_tables($select); | ||
1651 | 1092 | } | ||
1652 | 1093 | my ($tbl) = $query =~ m/TABLE\s+($tbl_ident)(\s+.*)?/i; | ||
1653 | 1094 | PTDEBUG && _d('Matches table:', $tbl); | ||
1654 | 1095 | return ($tbl); | ||
1655 | 1096 | } | ||
1656 | 1097 | |||
1657 | 1098 | $query =~ s/ (?:LOW_PRIORITY|IGNORE|STRAIGHT_JOIN)//ig; | ||
1658 | 1099 | |||
1659 | 1100 | if ( $query =~ /^\s*LOCK TABLES/i ) { | ||
1660 | 1101 | PTDEBUG && _d('Special table type: LOCK TABLES'); | ||
1661 | 1102 | $query =~ s/^(\s*LOCK TABLES\s+)//; | ||
1662 | 1103 | $query =~ s/\s+(?:READ|WRITE|LOCAL)+\s*//g; | ||
1663 | 1104 | PTDEBUG && _d('Locked tables:', $query); | ||
1664 | 1105 | $query = "FROM $query"; | ||
1665 | 1106 | } | ||
1666 | 1107 | |||
1667 | 1108 | $query =~ s/\\["']//g; # quoted strings | ||
1668 | 1109 | $query =~ s/".*?"/?/sg; # quoted strings | ||
1669 | 1110 | $query =~ s/'.*?'/?/sg; # quoted strings | ||
1670 | 1111 | |||
1671 | 1112 | my @tables; | ||
1672 | 1113 | foreach my $tbls ( $query =~ m/$tbl_regex/gio ) { | ||
1673 | 1114 | PTDEBUG && _d('Match tables:', $tbls); | ||
1674 | 1115 | |||
1675 | 1116 | next if $tbls =~ m/\ASELECT\b/i; | ||
1676 | 1117 | |||
1677 | 1118 | foreach my $tbl ( split(',', $tbls) ) { | ||
1678 | 1119 | $tbl =~ s/\s*($tbl_ident)(\s+.*)?/$1/gio; | ||
1679 | 1120 | |||
1680 | 1121 | if ( $tbl !~ m/[a-zA-Z]/ ) { | ||
1681 | 1122 | PTDEBUG && _d('Skipping suspicious table name:', $tbl); | ||
1682 | 1123 | next; | ||
1683 | 1124 | } | ||
1684 | 1125 | |||
1685 | 1126 | push @tables, $tbl; | ||
1686 | 1127 | } | ||
1687 | 1128 | } | ||
1688 | 1129 | return @tables; | ||
1689 | 1130 | } | ||
1690 | 1131 | |||
1691 | 1132 | sub has_derived_table { | ||
1692 | 1133 | my ( $self, $query ) = @_; | ||
1693 | 1134 | my $match = $query =~ m/$has_derived/; | ||
1694 | 1135 | PTDEBUG && _d($query, 'has ' . ($match ? 'a' : 'no') . ' derived table'); | ||
1695 | 1136 | return $match; | ||
1696 | 1137 | } | ||
1697 | 1138 | |||
1698 | 1139 | sub get_aliases { | ||
1699 | 1140 | my ( $self, $query, $list ) = @_; | ||
1700 | 1141 | |||
1701 | 1142 | my $result = { | ||
1702 | 1143 | DATABASE => {}, | ||
1703 | 1144 | TABLE => {}, | ||
1704 | 1145 | }; | ||
1705 | 1146 | return $result unless $query; | ||
1706 | 1147 | |||
1707 | 1148 | $query =~ s/ (?:LOW_PRIORITY|IGNORE|STRAIGHT_JOIN)//ig; | ||
1708 | 1149 | |||
1709 | 1150 | $query =~ s/ (?:INNER|OUTER|CROSS|LEFT|RIGHT|NATURAL)//ig; | ||
1710 | 1151 | |||
1711 | 1152 | my @tbl_refs; | ||
1712 | 1153 | my ($tbl_refs, $from) = $query =~ m{ | ||
1713 | 1154 | ( | ||
1714 | 1155 | (FROM|INTO|UPDATE)\b\s* # Keyword before table refs | ||
1715 | 1156 | .+? # Table refs | ||
1716 | 1157 | ) | ||
1717 | 1158 | (?:\s+|\z) # If the query does not end with the table | ||
1718 | 1159 | (?:WHERE|ORDER|LIMIT|HAVING|SET|VALUES|\z) # Keyword after table refs | ||
1719 | 1160 | }ix; | ||
1720 | 1161 | |||
1721 | 1162 | if ( $tbl_refs ) { | ||
1722 | 1163 | |||
1723 | 1164 | if ( $query =~ m/^(?:INSERT|REPLACE)/i ) { | ||
1724 | 1165 | $tbl_refs =~ s/\([^\)]+\)\s*//; | ||
1725 | 1166 | } | ||
1726 | 1167 | |||
1727 | 1168 | PTDEBUG && _d('tbl refs:', $tbl_refs); | ||
1728 | 1169 | |||
1729 | 1170 | my $before_tbl = qr/(?:,|JOIN|\s|$from)+/i; | ||
1730 | 1171 | |||
1731 | 1172 | my $after_tbl = qr/(?:,|JOIN|ON|USING|\z)/i; | ||
1732 | 1173 | |||
1733 | 1174 | $tbl_refs =~ s/ = /=/g; | ||
1734 | 1175 | |||
1735 | 1176 | while ( | ||
1736 | 1177 | $tbl_refs =~ m{ | ||
1737 | 1178 | $before_tbl\b\s* | ||
1738 | 1179 | ( ($tbl_ident) (?:\s+ (?:AS\s+)? (\w+))? ) | ||
1739 | 1180 | \s*$after_tbl | ||
1740 | 1181 | }xgio ) | ||
1741 | 1182 | { | ||
1742 | 1183 | my ( $tbl_ref, $db_tbl, $alias ) = ($1, $2, $3); | ||
1743 | 1184 | PTDEBUG && _d('Match table:', $tbl_ref); | ||
1744 | 1185 | push @tbl_refs, $tbl_ref; | ||
1745 | 1186 | $alias = $self->trim_identifier($alias); | ||
1746 | 1187 | |||
1747 | 1188 | if ( $tbl_ref =~ m/^AS\s+\w+/i ) { | ||
1748 | 1189 | PTDEBUG && _d('Subquery', $tbl_ref); | ||
1749 | 1190 | $result->{TABLE}->{$alias} = undef; | ||
1750 | 1191 | next; | ||
1751 | 1192 | } | ||
1752 | 1193 | |||
1753 | 1194 | my ( $db, $tbl ) = $db_tbl =~ m/^(?:(.*?)\.)?(.*)/; | ||
1754 | 1195 | $db = $self->trim_identifier($db); | ||
1755 | 1196 | $tbl = $self->trim_identifier($tbl); | ||
1756 | 1197 | $result->{TABLE}->{$alias || $tbl} = $tbl; | ||
1757 | 1198 | $result->{DATABASE}->{$tbl} = $db if $db; | ||
1758 | 1199 | } | ||
1759 | 1200 | } | ||
1760 | 1201 | else { | ||
1761 | 1202 | PTDEBUG && _d("No tables ref in", $query); | ||
1762 | 1203 | } | ||
1763 | 1204 | |||
1764 | 1205 | if ( $list ) { | ||
1765 | 1206 | return \@tbl_refs; | ||
1766 | 1207 | } | ||
1767 | 1208 | else { | ||
1768 | 1209 | return $result; | ||
1769 | 1210 | } | ||
1770 | 1211 | } | ||
1771 | 1212 | |||
1772 | 1213 | sub split { | ||
1773 | 1214 | my ( $self, $query ) = @_; | ||
1774 | 1215 | return unless $query; | ||
1775 | 1216 | $query = $self->clean_query($query); | ||
1776 | 1217 | PTDEBUG && _d('Splitting', $query); | ||
1777 | 1218 | |||
1778 | 1219 | my $verbs = qr{SELECT|INSERT|UPDATE|DELETE|REPLACE|UNION|CREATE}i; | ||
1779 | 1220 | |||
1780 | 1221 | my @split_statements = grep { $_ } split(m/\b($verbs\b(?!(?:\s*\()))/io, $query); | ||
1781 | 1222 | |||
1782 | 1223 | my @statements; | ||
1783 | 1224 | if ( @split_statements == 1 ) { | ||
1784 | 1225 | push @statements, $query; | ||
1785 | 1226 | } | ||
1786 | 1227 | else { | ||
1787 | 1228 | for ( my $i = 0; $i <= $#split_statements; $i += 2 ) { | ||
1788 | 1229 | push @statements, $split_statements[$i].$split_statements[$i+1]; | ||
1789 | 1230 | |||
1790 | 1231 | if ( $statements[-2] && $statements[-2] =~ m/on duplicate key\s+$/i ) { | ||
1791 | 1232 | $statements[-2] .= pop @statements; | ||
1792 | 1233 | } | ||
1793 | 1234 | } | ||
1794 | 1235 | } | ||
1795 | 1236 | |||
1796 | 1237 | PTDEBUG && _d('statements:', map { $_ ? "<$_>" : 'none' } @statements); | ||
1797 | 1238 | return @statements; | ||
1798 | 1239 | } | ||
1799 | 1240 | |||
1800 | 1241 | sub clean_query { | ||
1801 | 1242 | my ( $self, $query ) = @_; | ||
1802 | 1243 | return unless $query; | ||
1803 | 1244 | $query =~ s!/\*.*?\*/! !g; # Remove /* comment blocks */ | ||
1804 | 1245 | $query =~ s/^\s+//; # Remove leading spaces | ||
1805 | 1246 | $query =~ s/\s+$//; # Remove trailing spaces | ||
1806 | 1247 | $query =~ s/\s{2,}/ /g; # Remove extra spaces | ||
1807 | 1248 | return $query; | ||
1808 | 1249 | } | ||
1809 | 1250 | |||
1810 | 1251 | sub split_subquery { | ||
1811 | 1252 | my ( $self, $query ) = @_; | ||
1812 | 1253 | return unless $query; | ||
1813 | 1254 | $query = $self->clean_query($query); | ||
1814 | 1255 | $query =~ s/;$//; | ||
1815 | 1256 | |||
1816 | 1257 | my @subqueries; | ||
1817 | 1258 | my $sqno = 0; # subquery number | ||
1818 | 1259 | my $pos = 0; | ||
1819 | 1260 | while ( $query =~ m/(\S+)(?:\s+|\Z)/g ) { | ||
1820 | 1261 | $pos = pos($query); | ||
1821 | 1262 | my $word = $1; | ||
1822 | 1263 | PTDEBUG && _d($word, $sqno); | ||
1823 | 1264 | if ( $word =~ m/^\(?SELECT\b/i ) { | ||
1824 | 1265 | my $start_pos = $pos - length($word) - 1; | ||
1825 | 1266 | if ( $start_pos ) { | ||
1826 | 1267 | $sqno++; | ||
1827 | 1268 | PTDEBUG && _d('Subquery', $sqno, 'starts at', $start_pos); | ||
1828 | 1269 | $subqueries[$sqno] = { | ||
1829 | 1270 | start_pos => $start_pos, | ||
1830 | 1271 | end_pos => 0, | ||
1831 | 1272 | len => 0, | ||
1832 | 1273 | words => [$word], | ||
1833 | 1274 | lp => 1, # left parentheses | ||
1834 | 1275 | rp => 0, # right parentheses | ||
1835 | 1276 | done => 0, | ||
1836 | 1277 | }; | ||
1837 | 1278 | } | ||
1838 | 1279 | else { | ||
1839 | 1280 | PTDEBUG && _d('Main SELECT at pos 0'); | ||
1840 | 1281 | } | ||
1841 | 1282 | } | ||
1842 | 1283 | else { | ||
1843 | 1284 | next unless $sqno; # next unless we're in a subquery | ||
1844 | 1285 | PTDEBUG && _d('In subquery', $sqno); | ||
1845 | 1286 | my $sq = $subqueries[$sqno]; | ||
1846 | 1287 | if ( $sq->{done} ) { | ||
1847 | 1288 | PTDEBUG && _d('This subquery is done; SQL is for', | ||
1848 | 1289 | ($sqno - 1 ? "subquery $sqno" : "the main SELECT")); | ||
1849 | 1290 | next; | ||
1850 | 1291 | } | ||
1851 | 1292 | push @{$sq->{words}}, $word; | ||
1852 | 1293 | my $lp = ($word =~ tr/\(//) || 0; | ||
1853 | 1294 | my $rp = ($word =~ tr/\)//) || 0; | ||
1854 | 1295 | PTDEBUG && _d('parentheses left', $lp, 'right', $rp); | ||
1855 | 1296 | if ( ($sq->{lp} + $lp) - ($sq->{rp} + $rp) == 0 ) { | ||
1856 | 1297 | my $end_pos = $pos - 1; | ||
1857 | 1298 | PTDEBUG && _d('Subquery', $sqno, 'ends at', $end_pos); | ||
1858 | 1299 | $sq->{end_pos} = $end_pos; | ||
1859 | 1300 | $sq->{len} = $end_pos - $sq->{start_pos}; | ||
1860 | 1301 | } | ||
1861 | 1302 | } | ||
1862 | 1303 | } | ||
1863 | 1304 | |||
1864 | 1305 | for my $i ( 1..$#subqueries ) { | ||
1865 | 1306 | my $sq = $subqueries[$i]; | ||
1866 | 1307 | next unless $sq; | ||
1867 | 1308 | $sq->{sql} = join(' ', @{$sq->{words}}); | ||
1868 | 1309 | substr $query, | ||
1869 | 1310 | $sq->{start_pos} + 1, # +1 for ( | ||
1870 | 1311 | $sq->{len} - 1, # -1 for ) | ||
1871 | 1312 | "__subquery_$i"; | ||
1872 | 1313 | } | ||
1873 | 1314 | |||
1874 | 1315 | return $query, map { $_->{sql} } grep { defined $_ } @subqueries; | ||
1875 | 1316 | } | ||
1876 | 1317 | |||
1877 | 1318 | sub query_type { | ||
1878 | 1319 | my ( $self, $query, $qr ) = @_; | ||
1879 | 1320 | my ($type, undef) = $qr->distill_verbs($query); | ||
1880 | 1321 | my $rw; | ||
1881 | 1322 | if ( $type =~ m/^SELECT\b/ ) { | ||
1882 | 1323 | $rw = 'read'; | ||
1883 | 1324 | } | ||
1884 | 1325 | elsif ( $type =~ m/^$data_manip_stmts\b/ | ||
1885 | 1326 | || $type =~ m/^$data_def_stmts\b/ ) { | ||
1886 | 1327 | $rw = 'write' | ||
1887 | 1328 | } | ||
1888 | 1329 | |||
1889 | 1330 | return { | ||
1890 | 1331 | type => $type, | ||
1891 | 1332 | rw => $rw, | ||
1892 | 1333 | } | ||
1893 | 1334 | } | ||
1894 | 1335 | |||
1895 | 1336 | sub get_columns { | ||
1896 | 1337 | my ( $self, $query ) = @_; | ||
1897 | 1338 | my $cols = []; | ||
1898 | 1339 | return $cols unless $query; | ||
1899 | 1340 | my $cols_def; | ||
1900 | 1341 | |||
1901 | 1342 | if ( $query =~ m/^SELECT/i ) { | ||
1902 | 1343 | $query =~ s/ | ||
1903 | 1344 | ^SELECT\s+ | ||
1904 | 1345 | (?:ALL | ||
1905 | 1346 | |DISTINCT | ||
1906 | 1347 | |DISTINCTROW | ||
1907 | 1348 | |HIGH_PRIORITY | ||
1908 | 1349 | |STRAIGHT_JOIN | ||
1909 | 1350 | |SQL_SMALL_RESULT | ||
1910 | 1351 | |SQL_BIG_RESULT | ||
1911 | 1352 | |SQL_BUFFER_RESULT | ||
1912 | 1353 | |SQL_CACHE | ||
1913 | 1354 | |SQL_NO_CACHE | ||
1914 | 1355 | |SQL_CALC_FOUND_ROWS | ||
1915 | 1356 | )\s+ | ||
1916 | 1357 | /SELECT /xgi; | ||
1917 | 1358 | ($cols_def) = $query =~ m/^SELECT\s+(.+?)\s+FROM/i; | ||
1918 | 1359 | } | ||
1919 | 1360 | elsif ( $query =~ m/^(?:INSERT|REPLACE)/i ) { | ||
1920 | 1361 | ($cols_def) = $query =~ m/\(([^\)]+)\)\s*VALUE/i; | ||
1921 | 1362 | } | ||
1922 | 1363 | |||
1923 | 1364 | PTDEBUG && _d('Columns:', $cols_def); | ||
1924 | 1365 | if ( $cols_def ) { | ||
1925 | 1366 | @$cols = split(',', $cols_def); | ||
1926 | 1367 | map { | ||
1927 | 1368 | my $col = $_; | ||
1928 | 1369 | $col = s/^\s+//g; | ||
1929 | 1370 | $col = s/\s+$//g; | ||
1930 | 1371 | $col; | ||
1931 | 1372 | } @$cols; | ||
1932 | 1373 | } | ||
1933 | 1374 | |||
1934 | 1375 | return $cols; | ||
1935 | 1376 | } | ||
1936 | 1377 | |||
1937 | 1378 | sub parse { | ||
1938 | 1379 | my ( $self, $query ) = @_; | ||
1939 | 1380 | return unless $query; | ||
1940 | 1381 | my $parsed = {}; | ||
1941 | 1382 | |||
1942 | 1383 | $query =~ s/\n/ /g; | ||
1943 | 1384 | $query = $self->clean_query($query); | ||
1944 | 1385 | |||
1945 | 1386 | $parsed->{query} = $query, | ||
1946 | 1387 | $parsed->{tables} = $self->get_aliases($query, 1); | ||
1947 | 1388 | $parsed->{columns} = $self->get_columns($query); | ||
1948 | 1389 | |||
1949 | 1390 | my ($type) = $query =~ m/^(\w+)/; | ||
1950 | 1391 | $parsed->{type} = lc $type; | ||
1951 | 1392 | |||
1952 | 1393 | |||
1953 | 1394 | $parsed->{sub_queries} = []; | ||
1954 | 1395 | |||
1955 | 1396 | return $parsed; | ||
1956 | 1397 | } | ||
1957 | 1398 | |||
1958 | 1399 | sub extract_tables { | ||
1959 | 1400 | my ( $self, %args ) = @_; | ||
1960 | 1401 | my $query = $args{query}; | ||
1961 | 1402 | my $default_db = $args{default_db}; | ||
1962 | 1403 | my $q = $self->{Quoter} || $args{Quoter}; | ||
1963 | 1404 | return unless $query; | ||
1964 | 1405 | PTDEBUG && _d('Extracting tables'); | ||
1965 | 1406 | my @tables; | ||
1966 | 1407 | my %seen; | ||
1967 | 1408 | foreach my $db_tbl ( $self->get_tables($query) ) { | ||
1968 | 1409 | next unless $db_tbl; | ||
1969 | 1410 | next if $seen{$db_tbl}++; # Unique-ify for issue 337. | ||
1970 | 1411 | my ( $db, $tbl ) = $q->split_unquote($db_tbl); | ||
1971 | 1412 | push @tables, [ $db || $default_db, $tbl ]; | ||
1972 | 1413 | } | ||
1973 | 1414 | return @tables; | ||
1974 | 1415 | } | ||
1975 | 1416 | |||
1976 | 1417 | sub trim_identifier { | ||
1977 | 1418 | my ($self, $str) = @_; | ||
1978 | 1419 | return unless defined $str; | ||
1979 | 1420 | $str =~ s/`//g; | ||
1980 | 1421 | $str =~ s/^\s+//; | ||
1981 | 1422 | $str =~ s/\s+$//; | ||
1982 | 1423 | return $str; | ||
1983 | 1424 | } | ||
1984 | 1425 | |||
1985 | 1426 | sub _d { | ||
1986 | 1427 | my ($package, undef, $line) = caller 0; | ||
1987 | 1428 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
1988 | 1429 | map { defined $_ ? $_ : 'undef' } | ||
1989 | 1430 | @_; | ||
1990 | 1431 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
1991 | 1432 | } | ||
1992 | 1433 | |||
1993 | 1434 | 1; | ||
1994 | 1435 | } | ||
1995 | 1436 | # ########################################################################### | ||
1996 | 1437 | # End QueryParser package | ||
1997 | 1438 | # ########################################################################### | ||
1998 | 1439 | |||
1999 | 1440 | # ########################################################################### | ||
2000 | 1441 | # QueryRewriter package | ||
2001 | 1442 | # This package is a copy without comments from the original. The original | ||
2002 | 1443 | # with comments and its test file can be found in the Bazaar repository at, | ||
2003 | 1444 | # lib/QueryRewriter.pm | ||
2004 | 1445 | # t/lib/QueryRewriter.t | ||
2005 | 1446 | # See https://launchpad.net/percona-toolkit for more information. | ||
2006 | 1447 | # ########################################################################### | ||
2007 | 1448 | { | ||
2008 | 1449 | package QueryRewriter; | ||
2009 | 1450 | |||
2010 | 1451 | use strict; | ||
2011 | 1452 | use warnings FATAL => 'all'; | ||
2012 | 1453 | use English qw(-no_match_vars); | ||
2013 | 1454 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
2014 | 1455 | |||
2015 | 1456 | our $verbs = qr{^SHOW|^FLUSH|^COMMIT|^ROLLBACK|^BEGIN|SELECT|INSERT | ||
2016 | 1457 | |UPDATE|DELETE|REPLACE|^SET|UNION|^START|^LOCK}xi; | ||
2017 | 1458 | my $quote_re = qr/"(?:(?!(?<!\\)").)*"|'(?:(?!(?<!\\)').)*'/; # Costly! | ||
2018 | 1459 | my $bal; | ||
2019 | 1460 | $bal = qr/ | ||
2020 | 1461 | \( | ||
2021 | 1462 | (?: | ||
2022 | 1463 | (?> [^()]+ ) # Non-parens without backtracking | ||
2023 | 1464 | | | ||
2024 | 1465 | (??{ $bal }) # Group with matching parens | ||
2025 | 1466 | )* | ||
2026 | 1467 | \) | ||
2027 | 1468 | /x; | ||
2028 | 1469 | |||
2029 | 1470 | my $olc_re = qr/(?:--|#)[^'"\r\n]*(?=[\r\n]|\Z)/; # One-line comments | ||
2030 | 1471 | my $mlc_re = qr#/\*[^!].*?\*/#sm; # But not /*!version */ | ||
2031 | 1472 | my $vlc_re = qr#/\*.*?[0-9+].*?\*/#sm; # For SHOW + /*!version */ | ||
2032 | 1473 | my $vlc_rf = qr#^(SHOW).*?/\*![0-9+].*?\*/#sm; # Variation for SHOW | ||
2033 | 1474 | |||
2034 | 1475 | |||
2035 | 1476 | sub new { | ||
2036 | 1477 | my ( $class, %args ) = @_; | ||
2037 | 1478 | my $self = { %args }; | ||
2038 | 1479 | return bless $self, $class; | ||
2039 | 1480 | } | ||
2040 | 1481 | |||
2041 | 1482 | sub strip_comments { | ||
2042 | 1483 | my ( $self, $query ) = @_; | ||
2043 | 1484 | return unless $query; | ||
2044 | 1485 | $query =~ s/$olc_re//go; | ||
2045 | 1486 | $query =~ s/$mlc_re//go; | ||
2046 | 1487 | if ( $query =~ m/$vlc_rf/i ) { # contains show + version | ||
2047 | 1488 | $query =~ s/$vlc_re//go; | ||
2048 | 1489 | } | ||
2049 | 1490 | return $query; | ||
2050 | 1491 | } | ||
2051 | 1492 | |||
2052 | 1493 | sub shorten { | ||
2053 | 1494 | my ( $self, $query, $length ) = @_; | ||
2054 | 1495 | $query =~ s{ | ||
2055 | 1496 | \A( | ||
2056 | 1497 | (?:INSERT|REPLACE) | ||
2057 | 1498 | (?:\s+LOW_PRIORITY|DELAYED|HIGH_PRIORITY|IGNORE)? | ||
2058 | 1499 | (?:\s\w+)*\s+\S+\s+VALUES\s*\(.*?\) | ||
2059 | 1500 | ) | ||
2060 | 1501 | \s*,\s*\(.*?(ON\s+DUPLICATE|\Z)} | ||
2061 | 1502 | {$1 /*... omitted ...*/$2}xsi; | ||
2062 | 1503 | |||
2063 | 1504 | return $query unless $query =~ m/IN\s*\(\s*(?!select)/i; | ||
2064 | 1505 | |||
2065 | 1506 | my $last_length = 0; | ||
2066 | 1507 | my $query_length = length($query); | ||
2067 | 1508 | while ( | ||
2068 | 1509 | $length > 0 | ||
2069 | 1510 | && $query_length > $length | ||
2070 | 1511 | && $query_length < ( $last_length || $query_length + 1 ) | ||
2071 | 1512 | ) { | ||
2072 | 1513 | $last_length = $query_length; | ||
2073 | 1514 | $query =~ s{ | ||
2074 | 1515 | (\bIN\s*\() # The opening of an IN list | ||
2075 | 1516 | ([^\)]+) # Contents of the list, assuming no item contains paren | ||
2076 | 1517 | (?=\)) # Close of the list | ||
2077 | 1518 | } | ||
2078 | 1519 | { | ||
2079 | 1520 | $1 . __shorten($2) | ||
2080 | 1521 | }gexsi; | ||
2081 | 1522 | } | ||
2082 | 1523 | |||
2083 | 1524 | return $query; | ||
2084 | 1525 | } | ||
2085 | 1526 | |||
2086 | 1527 | sub __shorten { | ||
2087 | 1528 | my ( $snippet ) = @_; | ||
2088 | 1529 | my @vals = split(/,/, $snippet); | ||
2089 | 1530 | return $snippet unless @vals > 20; | ||
2090 | 1531 | my @keep = splice(@vals, 0, 20); # Remove and save the first 20 items | ||
2091 | 1532 | return | ||
2092 | 1533 | join(',', @keep) | ||
2093 | 1534 | . "/*... omitted " | ||
2094 | 1535 | . scalar(@vals) | ||
2095 | 1536 | . " items ...*/"; | ||
2096 | 1537 | } | ||
2097 | 1538 | |||
2098 | 1539 | sub fingerprint { | ||
2099 | 1540 | my ( $self, $query ) = @_; | ||
2100 | 1541 | |||
2101 | 1542 | $query =~ m#\ASELECT /\*!40001 SQL_NO_CACHE \*/ \* FROM `# # mysqldump query | ||
2102 | 1543 | && return 'mysqldump'; | ||
2103 | 1544 | $query =~ m#/\*\w+\.\w+:[0-9]/[0-9]\*/# # pt-table-checksum, etc query | ||
2104 | 1545 | && return 'percona-toolkit'; | ||
2105 | 1546 | $query =~ m/\Aadministrator command: / | ||
2106 | 1547 | && return $query; | ||
2107 | 1548 | $query =~ m/\A\s*(call\s+\S+)\(/i | ||
2108 | 1549 | && return lc($1); # Warning! $1 used, be careful. | ||
2109 | 1550 | if ( my ($beginning) = $query =~ m/\A((?:INSERT|REPLACE)(?: IGNORE)?\s+INTO.+?VALUES\s*\(.*?\))\s*,\s*\(/is ) { | ||
2110 | 1551 | $query = $beginning; # Shorten multi-value INSERT statements ASAP | ||
2111 | 1552 | } | ||
2112 | 1553 | |||
2113 | 1554 | $query =~ s/$olc_re//go; | ||
2114 | 1555 | $query =~ s/$mlc_re//go; | ||
2115 | 1556 | $query =~ s/\Ause \S+\Z/use ?/i # Abstract the DB in USE | ||
2116 | 1557 | && return $query; | ||
2117 | 1558 | |||
2118 | 1559 | $query =~ s/\\["']//g; # quoted strings | ||
2119 | 1560 | $query =~ s/".*?"/?/sg; # quoted strings | ||
2120 | 1561 | $query =~ s/'.*?'/?/sg; # quoted strings | ||
2121 | 1562 | |||
2122 | 1563 | if ( $self->{match_md5_checksums} ) { | ||
2123 | 1564 | $query =~ s/([._-])[a-f0-9]{32}/$1?/g; | ||
2124 | 1565 | } | ||
2125 | 1566 | |||
2126 | 1567 | if ( !$self->{match_embedded_numbers} ) { | ||
2127 | 1568 | $query =~ s/[0-9+-][0-9a-f.xb+-]*/?/g; | ||
2128 | 1569 | } | ||
2129 | 1570 | else { | ||
2130 | 1571 | $query =~ s/\b[0-9+-][0-9a-f.xb+-]*/?/g; | ||
2131 | 1572 | } | ||
2132 | 1573 | |||
2133 | 1574 | if ( $self->{match_md5_checksums} ) { | ||
2134 | 1575 | $query =~ s/[xb+-]\?/?/g; | ||
2135 | 1576 | } | ||
2136 | 1577 | else { | ||
2137 | 1578 | $query =~ s/[xb.+-]\?/?/g; | ||
2138 | 1579 | } | ||
2139 | 1580 | |||
2140 | 1581 | $query =~ s/\A\s+//; # Chop off leading whitespace | ||
2141 | 1582 | chomp $query; # Kill trailing whitespace | ||
2142 | 1583 | $query =~ tr[ \n\t\r\f][ ]s; # Collapse whitespace | ||
2143 | 1584 | $query = lc $query; | ||
2144 | 1585 | $query =~ s/\bnull\b/?/g; # Get rid of NULLs | ||
2145 | 1586 | $query =~ s{ # Collapse IN and VALUES lists | ||
2146 | 1587 | \b(in|values?)(?:[\s,]*\([\s?,]*\))+ | ||
2147 | 1588 | } | ||
2148 | 1589 | {$1(?+)}gx; | ||
2149 | 1590 | $query =~ s{ # Collapse UNION | ||
2150 | 1591 | \b(select\s.*?)(?:(\sunion(?:\sall)?)\s\1)+ | ||
2151 | 1592 | } | ||
2152 | 1593 | {$1 /*repeat$2*/}xg; | ||
2153 | 1594 | $query =~ s/\blimit \?(?:, ?\?| offset \?)?/limit ?/; # LIMIT | ||
2154 | 1595 | |||
2155 | 1596 | if ( $query =~ m/\bORDER BY /gi ) { # Find, anchor on ORDER BY clause | ||
2156 | 1597 | 1 while $query =~ s/\G(.+?)\s+ASC/$1/gi && pos $query; | ||
2157 | 1598 | } | ||
2158 | 1599 | |||
2159 | 1600 | return $query; | ||
2160 | 1601 | } | ||
2161 | 1602 | |||
2162 | 1603 | sub distill_verbs { | ||
2163 | 1604 | my ( $self, $query ) = @_; | ||
2164 | 1605 | |||
2165 | 1606 | $query =~ m/\A\s*call\s+(\S+)\(/i && return "CALL $1"; | ||
2166 | 1607 | $query =~ m/\A\s*use\s+/ && return "USE"; | ||
2167 | 1608 | $query =~ m/\A\s*UNLOCK TABLES/i && return "UNLOCK"; | ||
2168 | 1609 | $query =~ m/\A\s*xa\s+(\S+)/i && return "XA_$1"; | ||
2169 | 1610 | |||
2170 | 1611 | if ( $query =~ m/\Aadministrator command:/ ) { | ||
2171 | 1612 | $query =~ s/administrator command:/ADMIN/; | ||
2172 | 1613 | $query = uc $query; | ||
2173 | 1614 | return $query; | ||
2174 | 1615 | } | ||
2175 | 1616 | |||
2176 | 1617 | $query = $self->strip_comments($query); | ||
2177 | 1618 | |||
2178 | 1619 | if ( $query =~ m/\A\s*SHOW\s+/i ) { | ||
2179 | 1620 | PTDEBUG && _d($query); | ||
2180 | 1621 | |||
2181 | 1622 | $query = uc $query; | ||
2182 | 1623 | $query =~ s/\s+(?:GLOBAL|SESSION|FULL|STORAGE|ENGINE)\b/ /g; | ||
2183 | 1624 | $query =~ s/\s+COUNT[^)]+\)//g; | ||
2184 | 1625 | |||
2185 | 1626 | $query =~ s/\s+(?:FOR|FROM|LIKE|WHERE|LIMIT|IN)\b.+//ms; | ||
2186 | 1627 | |||
2187 | 1628 | $query =~ s/\A(SHOW(?:\s+\S+){1,2}).*\Z/$1/s; | ||
2188 | 1629 | $query =~ s/\s+/ /g; | ||
2189 | 1630 | PTDEBUG && _d($query); | ||
2190 | 1631 | return $query; | ||
2191 | 1632 | } | ||
2192 | 1633 | |||
2193 | 1634 | eval $QueryParser::data_def_stmts; | ||
2194 | 1635 | eval $QueryParser::tbl_ident; | ||
2195 | 1636 | my ( $dds ) = $query =~ /^\s*($QueryParser::data_def_stmts)\b/i; | ||
2196 | 1637 | if ( $dds) { | ||
2197 | 1638 | my ( $obj ) = $query =~ m/$dds.+(DATABASE|TABLE)\b/i; | ||
2198 | 1639 | $obj = uc $obj if $obj; | ||
2199 | 1640 | PTDEBUG && _d('Data def statment:', $dds, 'obj:', $obj); | ||
2200 | 1641 | my ($db_or_tbl) | ||
2201 | 1642 | = $query =~ m/(?:TABLE|DATABASE)\s+($QueryParser::tbl_ident)(\s+.*)?/i; | ||
2202 | 1643 | PTDEBUG && _d('Matches db or table:', $db_or_tbl); | ||
2203 | 1644 | return uc($dds . ($obj ? " $obj" : '')), $db_or_tbl; | ||
2204 | 1645 | } | ||
2205 | 1646 | |||
2206 | 1647 | my @verbs = $query =~ m/\b($verbs)\b/gio; | ||
2207 | 1648 | @verbs = do { | ||
2208 | 1649 | my $last = ''; | ||
2209 | 1650 | grep { my $pass = $_ ne $last; $last = $_; $pass } map { uc } @verbs; | ||
2210 | 1651 | }; | ||
2211 | 1652 | |||
2212 | 1653 | if ( ($verbs[0] || '') eq 'SELECT' && @verbs > 1 ) { | ||
2213 | 1654 | PTDEBUG && _d("False-positive verbs after SELECT:", @verbs[1..$#verbs]); | ||
2214 | 1655 | my $union = grep { $_ eq 'UNION' } @verbs; | ||
2215 | 1656 | @verbs = $union ? qw(SELECT UNION) : qw(SELECT); | ||
2216 | 1657 | } | ||
2217 | 1658 | |||
2218 | 1659 | my $verb_str = join(q{ }, @verbs); | ||
2219 | 1660 | return $verb_str; | ||
2220 | 1661 | } | ||
2221 | 1662 | |||
2222 | 1663 | sub __distill_tables { | ||
2223 | 1664 | my ( $self, $query, $table, %args ) = @_; | ||
2224 | 1665 | my $qp = $args{QueryParser} || $self->{QueryParser}; | ||
2225 | 1666 | die "I need a QueryParser argument" unless $qp; | ||
2226 | 1667 | |||
2227 | 1668 | my @tables = map { | ||
2228 | 1669 | $_ =~ s/`//g; | ||
2229 | 1670 | $_ =~ s/(_?)[0-9]+/$1?/g; | ||
2230 | 1671 | $_; | ||
2231 | 1672 | } grep { defined $_ } $qp->get_tables($query); | ||
2232 | 1673 | |||
2233 | 1674 | push @tables, $table if $table; | ||
2234 | 1675 | |||
2235 | 1676 | @tables = do { | ||
2236 | 1677 | my $last = ''; | ||
2237 | 1678 | grep { my $pass = $_ ne $last; $last = $_; $pass } @tables; | ||
2238 | 1679 | }; | ||
2239 | 1680 | |||
2240 | 1681 | return @tables; | ||
2241 | 1682 | } | ||
2242 | 1683 | |||
2243 | 1684 | sub distill { | ||
2244 | 1685 | my ( $self, $query, %args ) = @_; | ||
2245 | 1686 | |||
2246 | 1687 | if ( $args{generic} ) { | ||
2247 | 1688 | my ($cmd, $arg) = $query =~ m/^(\S+)\s+(\S+)/; | ||
2248 | 1689 | return '' unless $cmd; | ||
2249 | 1690 | $query = (uc $cmd) . ($arg ? " $arg" : ''); | ||
2250 | 1691 | } | ||
2251 | 1692 | else { | ||
2252 | 1693 | my ($verbs, $table) = $self->distill_verbs($query, %args); | ||
2253 | 1694 | |||
2254 | 1695 | if ( $verbs && $verbs =~ m/^SHOW/ ) { | ||
2255 | 1696 | my %alias_for = qw( | ||
2256 | 1697 | SCHEMA DATABASE | ||
2257 | 1698 | KEYS INDEX | ||
2258 | 1699 | INDEXES INDEX | ||
2259 | 1700 | ); | ||
2260 | 1701 | map { $verbs =~ s/$_/$alias_for{$_}/ } keys %alias_for; | ||
2261 | 1702 | $query = $verbs; | ||
2262 | 1703 | } | ||
2263 | 1704 | else { | ||
2264 | 1705 | my @tables = $self->__distill_tables($query, $table, %args); | ||
2265 | 1706 | $query = join(q{ }, $verbs, @tables); | ||
2266 | 1707 | } | ||
2267 | 1708 | } | ||
2268 | 1709 | |||
2269 | 1710 | if ( $args{trf} ) { | ||
2270 | 1711 | $query = $args{trf}->($query, %args); | ||
2271 | 1712 | } | ||
2272 | 1713 | |||
2273 | 1714 | return $query; | ||
2274 | 1715 | } | ||
2275 | 1716 | |||
2276 | 1717 | sub convert_to_select { | ||
2277 | 1718 | my ( $self, $query ) = @_; | ||
2278 | 1719 | return unless $query; | ||
2279 | 1720 | |||
2280 | 1721 | return if $query =~ m/=\s*\(\s*SELECT /i; | ||
2281 | 1722 | |||
2282 | 1723 | $query =~ s{ | ||
2283 | 1724 | \A.*? | ||
2284 | 1725 | update(?:\s+(?:low_priority|ignore))?\s+(.*?) | ||
2285 | 1726 | \s+set\b(.*?) | ||
2286 | 1727 | (?:\s*where\b(.*?))? | ||
2287 | 1728 | (limit\s*[0-9]+(?:\s*,\s*[0-9]+)?)? | ||
2288 | 1729 | \Z | ||
2289 | 1730 | } | ||
2290 | 1731 | {__update_to_select($1, $2, $3, $4)}exsi | ||
2291 | 1732 | || $query =~ s{ | ||
2292 | 1733 | \A.*? | ||
2293 | 1734 | (?:insert(?:\s+ignore)?|replace)\s+ | ||
2294 | 1735 | .*?\binto\b(.*?)\(([^\)]+)\)\s* | ||
2295 | 1736 | values?\s*(\(.*?\))\s* | ||
2296 | 1737 | (?:\blimit\b|on\s+duplicate\s+key.*)?\s* | ||
2297 | 1738 | \Z | ||
2298 | 1739 | } | ||
2299 | 1740 | {__insert_to_select($1, $2, $3)}exsi | ||
2300 | 1741 | || $query =~ s{ | ||
2301 | 1742 | \A.*? | ||
2302 | 1743 | (?:insert(?:\s+ignore)?|replace)\s+ | ||
2303 | 1744 | (?:.*?\binto)\b(.*?)\s* | ||
2304 | 1745 | set\s+(.*?)\s* | ||
2305 | 1746 | (?:\blimit\b|on\s+duplicate\s+key.*)?\s* | ||
2306 | 1747 | \Z | ||
2307 | 1748 | } | ||
2308 | 1749 | {__insert_to_select_with_set($1, $2)}exsi | ||
2309 | 1750 | || $query =~ s{ | ||
2310 | 1751 | \A.*? | ||
2311 | 1752 | delete\s+(.*?) | ||
2312 | 1753 | \bfrom\b(.*) | ||
2313 | 1754 | \Z | ||
2314 | 1755 | } | ||
2315 | 1756 | {__delete_to_select($1, $2)}exsi; | ||
2316 | 1757 | $query =~ s/\s*on\s+duplicate\s+key\s+update.*\Z//si; | ||
2317 | 1758 | $query =~ s/\A.*?(?=\bSELECT\s*\b)//ism; | ||
2318 | 1759 | return $query; | ||
2319 | 1760 | } | ||
2320 | 1761 | |||
2321 | 1762 | sub convert_select_list { | ||
2322 | 1763 | my ( $self, $query ) = @_; | ||
2323 | 1764 | $query =~ s{ | ||
2324 | 1765 | \A\s*select(.*?)\bfrom\b | ||
2325 | 1766 | } | ||
2326 | 1767 | {$1 =~ m/\*/ ? "select 1 from" : "select isnull(coalesce($1)) from"}exi; | ||
2327 | 1768 | return $query; | ||
2328 | 1769 | } | ||
2329 | 1770 | |||
2330 | 1771 | sub __delete_to_select { | ||
2331 | 1772 | my ( $delete, $join ) = @_; | ||
2332 | 1773 | if ( $join =~ m/\bjoin\b/ ) { | ||
2333 | 1774 | return "select 1 from $join"; | ||
2334 | 1775 | } | ||
2335 | 1776 | return "select * from $join"; | ||
2336 | 1777 | } | ||
2337 | 1778 | |||
2338 | 1779 | sub __insert_to_select { | ||
2339 | 1780 | my ( $tbl, $cols, $vals ) = @_; | ||
2340 | 1781 | PTDEBUG && _d('Args:', @_); | ||
2341 | 1782 | my @cols = split(/,/, $cols); | ||
2342 | 1783 | PTDEBUG && _d('Cols:', @cols); | ||
2343 | 1784 | $vals =~ s/^\(|\)$//g; # Strip leading/trailing parens | ||
2344 | 1785 | my @vals = $vals =~ m/($quote_re|[^,]*${bal}[^,]*|[^,]+)/g; | ||
2345 | 1786 | PTDEBUG && _d('Vals:', @vals); | ||
2346 | 1787 | if ( @cols == @vals ) { | ||
2347 | 1788 | return "select * from $tbl where " | ||
2348 | 1789 | . join(' and ', map { "$cols[$_]=$vals[$_]" } (0..$#cols)); | ||
2349 | 1790 | } | ||
2350 | 1791 | else { | ||
2351 | 1792 | return "select * from $tbl limit 1"; | ||
2352 | 1793 | } | ||
2353 | 1794 | } | ||
2354 | 1795 | |||
2355 | 1796 | sub __insert_to_select_with_set { | ||
2356 | 1797 | my ( $from, $set ) = @_; | ||
2357 | 1798 | $set =~ s/,/ and /g; | ||
2358 | 1799 | return "select * from $from where $set "; | ||
2359 | 1800 | } | ||
2360 | 1801 | |||
2361 | 1802 | sub __update_to_select { | ||
2362 | 1803 | my ( $from, $set, $where, $limit ) = @_; | ||
2363 | 1804 | return "select $set from $from " | ||
2364 | 1805 | . ( $where ? "where $where" : '' ) | ||
2365 | 1806 | . ( $limit ? " $limit " : '' ); | ||
2366 | 1807 | } | ||
2367 | 1808 | |||
2368 | 1809 | sub wrap_in_derived { | ||
2369 | 1810 | my ( $self, $query ) = @_; | ||
2370 | 1811 | return unless $query; | ||
2371 | 1812 | return $query =~ m/\A\s*select/i | ||
2372 | 1813 | ? "select 1 from ($query) as x limit 1" | ||
2373 | 1814 | : $query; | ||
2374 | 1815 | } | ||
2375 | 1816 | |||
2376 | 1817 | sub _d { | ||
2377 | 1818 | my ($package, undef, $line) = caller 0; | ||
2378 | 1819 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
2379 | 1820 | map { defined $_ ? $_ : 'undef' } | ||
2380 | 1821 | @_; | ||
2381 | 1822 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
2382 | 1823 | } | ||
2383 | 1824 | |||
2384 | 1825 | 1; | ||
2385 | 1826 | } | ||
2386 | 1827 | # ########################################################################### | ||
2387 | 1828 | # End QueryRewriter package | ||
2388 | 1829 | # ########################################################################### | ||
2389 | 1830 | |||
2390 | 1831 | # ########################################################################### | ||
2391 | 1832 | # This is a combination of modules and programs in one -- a runnable module. | ||
2392 | 1833 | # http://www.perl.com/pub/a/2006/07/13/lightning-articles.html?page=last | ||
2393 | 1834 | # Or, look it up in the Camel book on pages 642 and 643 in the 3rd edition. | ||
2394 | 1835 | # | ||
2395 | 1836 | # Check at the end of this package for the call to main() which actually runs | ||
2396 | 1837 | # the program. | ||
2397 | 1838 | # ########################################################################### | ||
2398 | 1839 | package pt_fingerprint; | ||
2399 | 1840 | |||
2400 | 1841 | use English qw(-no_match_vars); | ||
2401 | 1842 | use Data::Dumper; | ||
2402 | 1843 | $Data::Dumper::Indent = 1; | ||
2403 | 1844 | $OUTPUT_AUTOFLUSH = 1; | ||
2404 | 1845 | |||
2405 | 1846 | use constant MKDEBUG => $ENV{MKDEBUG} || 0; | ||
2406 | 1847 | |||
2407 | 1848 | sub main { | ||
2408 | 1849 | @ARGV = @_; # set global ARGV for this package | ||
2409 | 1850 | |||
2410 | 1851 | # ########################################################################## | ||
2411 | 1852 | # Get configuration information. | ||
2412 | 1853 | # ########################################################################## | ||
2413 | 1854 | my $o = new OptionParser(); | ||
2414 | 1855 | $o->get_specs(); | ||
2415 | 1856 | $o->get_opts(); | ||
2416 | 1857 | $o->usage_or_errors(); | ||
2417 | 1858 | |||
2418 | 1859 | my $qp = new QueryParser(); | ||
2419 | 1860 | my $qr = new QueryRewriter( | ||
2420 | 1861 | QueryParser => $qp, | ||
2421 | 1862 | match_md5_checksums => $o->get('match-md5-checksums'), | ||
2422 | 1863 | match_embedded_numbers => $o->get('match-embedded-numbers'), | ||
2423 | 1864 | ); | ||
2424 | 1865 | |||
2425 | 1866 | if ( $o->got('query') ) { | ||
2426 | 1867 | print $qr->fingerprint($o->get('query')), "\n"; | ||
2427 | 1868 | } | ||
2428 | 1869 | else { | ||
2429 | 1870 | local $INPUT_RECORD_SEPARATOR = ";\n"; | ||
2430 | 1871 | while ( <> ) { | ||
2431 | 1872 | my $query = $_; | ||
2432 | 1873 | chomp $query; | ||
2433 | 1874 | $query =~ s/^#.+$//mg; | ||
2434 | 1875 | $query =~ s/^\s+//; | ||
2435 | 1876 | next unless $query =~ m/^\w/; | ||
2436 | 1877 | print $qr->fingerprint($query), "\n"; | ||
2437 | 1878 | } | ||
2438 | 1879 | } | ||
2439 | 1880 | } | ||
2440 | 1881 | |||
2441 | 1882 | # ############################################################################ | ||
2442 | 1883 | # Run the program. | ||
2443 | 1884 | # ############################################################################ | ||
2444 | 1885 | if ( !caller ) { exit main(@ARGV); } | ||
2445 | 1886 | |||
2446 | 1887 | 1; # Because this is a module as well as a script. | ||
2447 | 1888 | |||
2448 | 1889 | # ############################################################################# | ||
2449 | 1890 | # Documentation. | ||
2450 | 1891 | # ############################################################################# | ||
2451 | 1892 | |||
2452 | 1893 | =pod | ||
2453 | 1894 | |||
2454 | 1895 | =head1 NAME | ||
2455 | 1896 | |||
2456 | 1897 | pt-fingerprint - Convert queries into fingerprints. | ||
2457 | 1898 | |||
2458 | 1899 | =head1 SYNOPSIS | ||
2459 | 1900 | |||
2460 | 1901 | Usage: pt-fingerprint [OPTIONS] [FILES] | ||
2461 | 1902 | |||
2462 | 1903 | pt-fingerprint converts queries into fingerprints. With the --query | ||
2463 | 1904 | option, converts the option's value into a fingerprint. With no options, treats | ||
2464 | 1905 | command-line arguments as FILEs and reads and converts semicolon-separated | ||
2465 | 1906 | queries from the FILEs. When FILE is -, it read standard input. | ||
2466 | 1907 | |||
2467 | 1908 | Convert a single query: | ||
2468 | 1909 | |||
2469 | 1910 | pt-fingerprint --query "select a, b, c from users where id = 500" | ||
2470 | 1911 | |||
2471 | 1912 | Convert a file full of queries: | ||
2472 | 1913 | |||
2473 | 1914 | pt-fingerprint /path/to/file.txt | ||
2474 | 1915 | |||
2475 | 1916 | =head1 RISKS | ||
2476 | 1917 | |||
2477 | 1918 | The following section is included to inform users about the potential risks, | ||
2478 | 1919 | whether known or unknown, of using this tool. The two main categories of risks | ||
2479 | 1920 | are those created by the nature of the tool (e.g. read-only tools vs. read-write | ||
2480 | 1921 | tools) and those created by bugs. | ||
2481 | 1922 | |||
2482 | 1923 | The pt-fingerprint tool simply reads data and transforms it, so risks are | ||
2483 | 1924 | minimal. | ||
2484 | 1925 | |||
2485 | 1926 | See also L<"BUGS"> for more information on filing bugs and getting help. | ||
2486 | 1927 | |||
2487 | 1928 | =head1 DESCRIPTION | ||
2488 | 1929 | |||
2489 | 1930 | A query fingerprint is the abstracted form of a query, which makes it possible | ||
2490 | 1931 | to group similar queries together. Abstracting a query removes literal values, | ||
2491 | 1932 | normalizes whitespace, and so on. For example, consider these two queries: | ||
2492 | 1933 | |||
2493 | 1934 | SELECT name, password FROM user WHERE id='12823'; | ||
2494 | 1935 | select name, password from user | ||
2495 | 1936 | where id=5; | ||
2496 | 1937 | |||
2497 | 1938 | Both of those queries will fingerprint to | ||
2498 | 1939 | |||
2499 | 1940 | select name, password from user where id=? | ||
2500 | 1941 | |||
2501 | 1942 | Once the query's fingerprint is known, we can then talk about a query as though | ||
2502 | 1943 | it represents all similar queries. | ||
2503 | 1944 | |||
2504 | 1945 | Query fingerprinting accommodates a great many special cases, which have proven | ||
2505 | 1946 | necessary in the real world. For example, an IN list with 5 literals is really | ||
2506 | 1947 | equivalent to one with 4 literals, so lists of literals are collapsed to a | ||
2507 | 1948 | single one. If you want to understand more about how and why all of these cases | ||
2508 | 1949 | are handled, please review the test cases in the Subversion repository. If you | ||
2509 | 1950 | find something that is not fingerprinted properly, please submit a bug report | ||
2510 | 1951 | with a reproducible test case. Here is a list of transformations during | ||
2511 | 1952 | fingerprinting, which might not be exhaustive: | ||
2512 | 1953 | |||
2513 | 1954 | =over | ||
2514 | 1955 | |||
2515 | 1956 | =item * | ||
2516 | 1957 | |||
2517 | 1958 | Group all SELECT queries from mysqldump together, even if they are against | ||
2518 | 1959 | different tables. Ditto for all of pt-table-checksum's checksum queries. | ||
2519 | 1960 | |||
2520 | 1961 | =item * | ||
2521 | 1962 | |||
2522 | 1963 | Shorten multi-value INSERT statements to a single VALUES() list. | ||
2523 | 1964 | |||
2524 | 1965 | =item * | ||
2525 | 1966 | |||
2526 | 1967 | Strip comments. | ||
2527 | 1968 | |||
2528 | 1969 | =item * | ||
2529 | 1970 | |||
2530 | 1971 | Abstract the databases in USE statements, so all USE statements are grouped | ||
2531 | 1972 | together. | ||
2532 | 1973 | |||
2533 | 1974 | =item * | ||
2534 | 1975 | |||
2535 | 1976 | Replace all literals, such as quoted strings. For efficiency, the code that | ||
2536 | 1977 | replaces literal numbers is somewhat non-selective, and might replace some | ||
2537 | 1978 | things as numbers when they really are not. Hexadecimal literals are also | ||
2538 | 1979 | replaced. NULL is treated as a literal. Numbers embedded in identifiers are | ||
2539 | 1980 | also replaced, so tables named similarly will be fingerprinted to the same | ||
2540 | 1981 | values (e.g. users_2009 and users_2010 will fingerprint identically). | ||
2541 | 1982 | |||
2542 | 1983 | =item * | ||
2543 | 1984 | |||
2544 | 1985 | Collapse all whitespace into a single space. | ||
2545 | 1986 | |||
2546 | 1987 | =item * | ||
2547 | 1988 | |||
2548 | 1989 | Lowercase the entire query. | ||
2549 | 1990 | |||
2550 | 1991 | =item * | ||
2551 | 1992 | |||
2552 | 1993 | Replace all literals inside of IN() and VALUES() lists with a single | ||
2553 | 1994 | placeholder, regardless of cardinality. | ||
2554 | 1995 | |||
2555 | 1996 | =item * | ||
2556 | 1997 | |||
2557 | 1998 | Collapse multiple identical UNION queries into a single one. | ||
2558 | 1999 | |||
2559 | 2000 | =back | ||
2560 | 2001 | |||
2561 | 2002 | =head1 OPTIONS | ||
2562 | 2003 | |||
2563 | 2004 | This tool accepts additional command-line arguments. Refer to the | ||
2564 | 2005 | L<"SYNOPSIS"> and usage information for details. | ||
2565 | 2006 | |||
2566 | 2007 | =over | ||
2567 | 2008 | |||
2568 | 2009 | =item --config | ||
2569 | 2010 | |||
2570 | 2011 | type: Array | ||
2571 | 2012 | |||
2572 | 2013 | Read this comma-separated list of config files; if specified, this must be the | ||
2573 | 2014 | first option on the command line. | ||
2574 | 2015 | |||
2575 | 2016 | =item --help | ||
2576 | 2017 | |||
2577 | 2018 | Show help and exit. | ||
2578 | 2019 | |||
2579 | 2020 | =item --match-embedded-numbers | ||
2580 | 2021 | |||
2581 | 2022 | Match numbers embedded in words and replace as single values. This option | ||
2582 | 2023 | causes the tool to be more careful about matching numbers so that words | ||
2583 | 2024 | with numbers, like C<catch22> are matched and replaced as a single C<?> | ||
2584 | 2025 | placeholder. Otherwise the default number matching pattern will replace | ||
2585 | 2026 | C<catch22> as C<catch?>. | ||
2586 | 2027 | |||
2587 | 2028 | This is helpful if database or table names contain numbers. | ||
2588 | 2029 | |||
2589 | 2030 | =item --match-md5-checksums | ||
2590 | 2031 | |||
2591 | 2032 | Match MD5 checksums and replace as single values. This option causes | ||
2592 | 2033 | the tool to be more careful about matching numbers so that MD5 checksums | ||
2593 | 2034 | like C<fbc5e685a5d3d45aa1d0347fdb7c4d35> are matched and replaced as a | ||
2594 | 2035 | single C<?> placeholder. Otherwise, the default number matching pattern will | ||
2595 | 2036 | replace C<fbc5e685a5d3d45aa1d0347fdb7c4d35> as C<fbc?>. | ||
2596 | 2037 | |||
2597 | 2038 | =item --query | ||
2598 | 2039 | |||
2599 | 2040 | type: string | ||
2600 | 2041 | |||
2601 | 2042 | The query to convert into a fingerprint. | ||
2602 | 2043 | |||
2603 | 2044 | =item --version | ||
2604 | 2045 | |||
2605 | 2046 | Show version and exit. | ||
2606 | 2047 | |||
2607 | 2048 | =back | ||
2608 | 2049 | |||
2609 | 2050 | =head1 ENVIRONMENT | ||
2610 | 2051 | |||
2611 | 2052 | The environment variable C<PTDEBUG> enables verbose debugging output to STDERR. | ||
2612 | 2053 | To enable debugging and capture all output to a file, run the tool like: | ||
2613 | 2054 | |||
2614 | 2055 | PTDEBUG=1 pt-fingerprint ... > FILE 2>&1 | ||
2615 | 2056 | |||
2616 | 2057 | Be careful: debugging output is voluminous and can generate several megabytes | ||
2617 | 2058 | of output. | ||
2618 | 2059 | |||
2619 | 2060 | =head1 SYSTEM REQUIREMENTS | ||
2620 | 2061 | |||
2621 | 2062 | You need Perl, DBI, DBD::mysql, and some core packages that ought to be | ||
2622 | 2063 | installed in any reasonably new version of Perl. | ||
2623 | 2064 | |||
2624 | 2065 | =head1 BUGS | ||
2625 | 2066 | |||
2626 | 2067 | For a list of known bugs, see L<http://www.percona.com/bugs/pt-fingerprint>. | ||
2627 | 2068 | |||
2628 | 2069 | Please report bugs at L<https://bugs.launchpad.net/percona-toolkit>. | ||
2629 | 2070 | Include the following information in your bug report: | ||
2630 | 2071 | |||
2631 | 2072 | =over | ||
2632 | 2073 | |||
2633 | 2074 | =item * Complete command-line used to run the tool | ||
2634 | 2075 | |||
2635 | 2076 | =item * Tool L<"--version"> | ||
2636 | 2077 | |||
2637 | 2078 | =item * MySQL version of all servers involved | ||
2638 | 2079 | |||
2639 | 2080 | =item * Output from the tool including STDERR | ||
2640 | 2081 | |||
2641 | 2082 | =item * Input files (log/dump/config files, etc.) | ||
2642 | 2083 | |||
2643 | 2084 | =back | ||
2644 | 2085 | |||
2645 | 2086 | If possible, include debugging output by running the tool with C<PTDEBUG>; | ||
2646 | 2087 | see L<"ENVIRONMENT">. | ||
2647 | 2088 | |||
2648 | 2089 | =head1 DOWNLOADING | ||
2649 | 2090 | |||
2650 | 2091 | Visit L<http://www.percona.com/software/percona-toolkit/> to download the | ||
2651 | 2092 | latest release of Percona Toolkit. Or, get the latest release from the | ||
2652 | 2093 | command line: | ||
2653 | 2094 | |||
2654 | 2095 | wget percona.com/get/percona-toolkit.tar.gz | ||
2655 | 2096 | |||
2656 | 2097 | wget percona.com/get/percona-toolkit.rpm | ||
2657 | 2098 | |||
2658 | 2099 | wget percona.com/get/percona-toolkit.deb | ||
2659 | 2100 | |||
2660 | 2101 | You can also get individual tools from the latest release: | ||
2661 | 2102 | |||
2662 | 2103 | wget percona.com/get/TOOL | ||
2663 | 2104 | |||
2664 | 2105 | Replace C<TOOL> with the name of any tool. | ||
2665 | 2106 | |||
2666 | 2107 | =head1 AUTHORS | ||
2667 | 2108 | |||
2668 | 2109 | Baron Schwartz and Daniel Nichter | ||
2669 | 2110 | |||
2670 | 2111 | =head1 ABOUT PERCONA TOOLKIT | ||
2671 | 2112 | |||
2672 | 2113 | This tool is part of Percona Toolkit, a collection of advanced command-line | ||
2673 | 2114 | tools developed by Percona for MySQL support and consulting. Percona Toolkit | ||
2674 | 2115 | was forked from two projects in June, 2011: Maatkit and Aspersa. Those | ||
2675 | 2116 | projects were created by Baron Schwartz and developed primarily by him and | ||
2676 | 2117 | Daniel Nichter, both of whom are employed by Percona. Visit | ||
2677 | 2118 | L<http://www.percona.com/software/> for more software developed by Percona. | ||
2678 | 2119 | |||
2679 | 2120 | =head1 COPYRIGHT, LICENSE, AND WARRANTY | ||
2680 | 2121 | |||
2681 | 2122 | This program is copyright 2011-2012 Percona Inc. | ||
2682 | 2123 | Feedback and improvements are welcome. | ||
2683 | 2124 | |||
2684 | 2125 | THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
2685 | 2126 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
2686 | 2127 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
2687 | 2128 | |||
2688 | 2129 | This program is free software; you can redistribute it and/or modify it under | ||
2689 | 2130 | the terms of the GNU General Public License as published by the Free Software | ||
2690 | 2131 | Foundation, version 2; OR the Perl Artistic License. On UNIX and similar | ||
2691 | 2132 | systems, you can issue `man perlgpl' or `man perlartistic' to read these | ||
2692 | 2133 | licenses. | ||
2693 | 2134 | |||
2694 | 2135 | You should have received a copy of the GNU General Public License along with | ||
2695 | 2136 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | ||
2696 | 2137 | Place, Suite 330, Boston, MA 02111-1307 USA. | ||
2697 | 2138 | |||
2698 | 2139 | =head1 VERSION | ||
2699 | 2140 | |||
2700 | 2141 | pt-fingerprint 2.1.1 | ||
2701 | 2142 | |||
2702 | 2143 | =cut | ||
2703 | 0 | 2144 | ||
2704 | === modified file 'bin/pt-fk-error-logger' | |||
2705 | --- bin/pt-fk-error-logger 2012-05-24 17:52:01 +0000 | |||
2706 | +++ bin/pt-fk-error-logger 2012-05-30 21:00:30 +0000 | |||
2707 | @@ -2465,6 +2465,6 @@ | |||
2708 | 2465 | 2465 | ||
2709 | 2466 | =head1 VERSION | 2466 | =head1 VERSION |
2710 | 2467 | 2467 | ||
2712 | 2468 | pt-fk-error-logger 2.0.4 | 2468 | pt-fk-error-logger 2.1.1 |
2713 | 2469 | 2469 | ||
2714 | 2470 | =cut | 2470 | =cut |
2715 | 2471 | 2471 | ||
2716 | === modified file 'bin/pt-heartbeat' | |||
2717 | --- bin/pt-heartbeat 2012-05-24 17:52:01 +0000 | |||
2718 | +++ bin/pt-heartbeat 2012-05-30 21:00:30 +0000 | |||
2719 | @@ -4400,6 +4400,6 @@ | |||
2720 | 4400 | 4400 | ||
2721 | 4401 | =head1 VERSION | 4401 | =head1 VERSION |
2722 | 4402 | 4402 | ||
2724 | 4403 | pt-heartbeat 2.0.4 | 4403 | pt-heartbeat 2.1.1 |
2725 | 4404 | 4404 | ||
2726 | 4405 | =cut | 4405 | =cut |
2727 | 4406 | 4406 | ||
2728 | === modified file 'bin/pt-index-usage' | |||
2729 | --- bin/pt-index-usage 2012-05-24 17:52:01 +0000 | |||
2730 | +++ bin/pt-index-usage 2012-05-30 21:00:30 +0000 | |||
2731 | @@ -2674,19 +2674,58 @@ | |||
2732 | 2674 | return bless $self, $class; | 2674 | return bless $self, $class; |
2733 | 2675 | } | 2675 | } |
2734 | 2676 | 2676 | ||
2735 | 2677 | sub get_create_table { | ||
2736 | 2678 | my ( $self, $dbh, $db, $tbl ) = @_; | ||
2737 | 2679 | die "I need a dbh parameter" unless $dbh; | ||
2738 | 2680 | die "I need a db parameter" unless $db; | ||
2739 | 2681 | die "I need a tbl parameter" unless $tbl; | ||
2740 | 2682 | my $q = $self->{Quoter}; | ||
2741 | 2683 | |||
2742 | 2684 | my $new_sql_mode | ||
2743 | 2685 | = '/*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, ' | ||
2744 | 2686 | . q{@@SQL_MODE := REPLACE(REPLACE(@@SQL_MODE, 'ANSI_QUOTES', ''), ',,', ','), } | ||
2745 | 2687 | . '@OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, ' | ||
2746 | 2688 | . '@@SQL_QUOTE_SHOW_CREATE := 1 */'; | ||
2747 | 2689 | |||
2748 | 2690 | my $old_sql_mode = '/*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, ' | ||
2749 | 2691 | . '@@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */'; | ||
2750 | 2692 | |||
2751 | 2693 | PTDEBUG && _d($new_sql_mode); | ||
2752 | 2694 | eval { $dbh->do($new_sql_mode); }; | ||
2753 | 2695 | PTDEBUG && $EVAL_ERROR && _d($EVAL_ERROR); | ||
2754 | 2696 | |||
2755 | 2697 | my $use_sql = 'USE ' . $q->quote($db); | ||
2756 | 2698 | PTDEBUG && _d($dbh, $use_sql); | ||
2757 | 2699 | $dbh->do($use_sql); | ||
2758 | 2700 | |||
2759 | 2701 | my $show_sql = "SHOW CREATE TABLE " . $q->quote($db, $tbl); | ||
2760 | 2702 | PTDEBUG && _d($show_sql); | ||
2761 | 2703 | my $href; | ||
2762 | 2704 | eval { $href = $dbh->selectrow_hashref($show_sql); }; | ||
2763 | 2705 | if ( $EVAL_ERROR ) { | ||
2764 | 2706 | PTDEBUG && _d($EVAL_ERROR); | ||
2765 | 2707 | |||
2766 | 2708 | PTDEBUG && _d($old_sql_mode); | ||
2767 | 2709 | $dbh->do($old_sql_mode); | ||
2768 | 2710 | |||
2769 | 2711 | return; | ||
2770 | 2712 | } | ||
2771 | 2713 | |||
2772 | 2714 | PTDEBUG && _d($old_sql_mode); | ||
2773 | 2715 | $dbh->do($old_sql_mode); | ||
2774 | 2716 | |||
2775 | 2717 | my ($key) = grep { m/create (?:table|view)/i } keys %$href; | ||
2776 | 2718 | if ( !$key ) { | ||
2777 | 2719 | die "Error: no 'Create Table' or 'Create View' in result set from " | ||
2778 | 2720 | . "$show_sql: " . Dumper($href); | ||
2779 | 2721 | } | ||
2780 | 2722 | |||
2781 | 2723 | return $href->{$key}; | ||
2782 | 2724 | } | ||
2783 | 2725 | |||
2784 | 2677 | sub parse { | 2726 | sub parse { |
2785 | 2678 | my ( $self, $ddl, $opts ) = @_; | 2727 | my ( $self, $ddl, $opts ) = @_; |
2786 | 2679 | return unless $ddl; | 2728 | return unless $ddl; |
2787 | 2680 | if ( ref $ddl eq 'ARRAY' ) { | ||
2788 | 2681 | if ( lc $ddl->[0] eq 'table' ) { | ||
2789 | 2682 | $ddl = $ddl->[1]; | ||
2790 | 2683 | } | ||
2791 | 2684 | else { | ||
2792 | 2685 | return { | ||
2793 | 2686 | engine => 'VIEW', | ||
2794 | 2687 | }; | ||
2795 | 2688 | } | ||
2796 | 2689 | } | ||
2797 | 2690 | 2729 | ||
2798 | 2691 | if ( $ddl !~ m/CREATE (?:TEMPORARY )?TABLE `/ ) { | 2730 | if ( $ddl !~ m/CREATE (?:TEMPORARY )?TABLE `/ ) { |
2799 | 2692 | die "Cannot parse table definition; is ANSI quoting " | 2731 | die "Cannot parse table definition; is ANSI quoting " |
2800 | @@ -2993,41 +3032,31 @@ | |||
2801 | 2993 | return $ddl; | 3032 | return $ddl; |
2802 | 2994 | } | 3033 | } |
2803 | 2995 | 3034 | ||
2839 | 2996 | sub remove_secondary_indexes { | 3035 | sub get_table_status { |
2840 | 2997 | my ( $self, $ddl ) = @_; | 3036 | my ( $self, $dbh, $db, $like ) = @_; |
2841 | 2998 | my $sec_indexes_ddl; | 3037 | my $q = $self->{Quoter}; |
2842 | 2999 | my $tbl_struct = $self->parse($ddl); | 3038 | my $sql = "SHOW TABLE STATUS FROM " . $q->quote($db); |
2843 | 3000 | 3039 | my @params; | |
2844 | 3001 | if ( ($tbl_struct->{engine} || '') =~ m/InnoDB/i ) { | 3040 | if ( $like ) { |
2845 | 3002 | my $clustered_key = $tbl_struct->{clustered_key}; | 3041 | $sql .= ' LIKE ?'; |
2846 | 3003 | $clustered_key ||= ''; | 3042 | push @params, $like; |
2847 | 3004 | 3043 | } | |
2848 | 3005 | my @sec_indexes = map { | 3044 | PTDEBUG && _d($sql, @params); |
2849 | 3006 | my $key_def = $_->{ddl}; | 3045 | my $sth = $dbh->prepare($sql); |
2850 | 3007 | $key_def =~ s/([\(\)])/\\$1/g; | 3046 | eval { $sth->execute(@params); }; |
2851 | 3008 | $ddl =~ s/\s+$key_def//i; | 3047 | if ($EVAL_ERROR) { |
2852 | 3009 | 3048 | PTDEBUG && _d($EVAL_ERROR); | |
2853 | 3010 | my $key_ddl = "ADD $_->{ddl}"; | 3049 | return; |
2854 | 3011 | $key_ddl .= ',' unless $key_ddl =~ m/,$/; | 3050 | } |
2855 | 3012 | $key_ddl; | 3051 | my @tables = @{$sth->fetchall_arrayref({})}; |
2856 | 3013 | } | 3052 | @tables = map { |
2857 | 3014 | grep { $_->{name} ne $clustered_key } | 3053 | my %tbl; # Make a copy with lowercased keys |
2858 | 3015 | values %{$tbl_struct->{keys}}; | 3054 | @tbl{ map { lc $_ } keys %$_ } = values %$_; |
2859 | 3016 | PTDEBUG && _d('Secondary indexes:', Dumper(\@sec_indexes)); | 3055 | $tbl{engine} ||= $tbl{type} || $tbl{comment}; |
2860 | 3017 | 3056 | delete $tbl{type}; | |
2861 | 3018 | if ( @sec_indexes ) { | 3057 | \%tbl; |
2862 | 3019 | $sec_indexes_ddl = join(' ', @sec_indexes); | 3058 | } @tables; |
2863 | 3020 | $sec_indexes_ddl =~ s/,$//; | 3059 | return @tables; |
2829 | 3021 | } | ||
2830 | 3022 | |||
2831 | 3023 | $ddl =~ s/,(\n\) )/$1/s; | ||
2832 | 3024 | } | ||
2833 | 3025 | else { | ||
2834 | 3026 | PTDEBUG && _d('Not removing secondary indexes from', | ||
2835 | 3027 | $tbl_struct->{engine}, 'table'); | ||
2836 | 3028 | } | ||
2837 | 3029 | |||
2838 | 3030 | return $ddl, $sec_indexes_ddl, $tbl_struct; | ||
2864 | 3031 | } | 3060 | } |
2865 | 3032 | 3061 | ||
2866 | 3033 | sub _d { | 3062 | sub _d { |
2867 | @@ -3917,7 +3946,7 @@ | |||
2868 | 3917 | 3946 | ||
2869 | 3918 | sub new { | 3947 | sub new { |
2870 | 3919 | my ( $class, %args ) = @_; | 3948 | my ( $class, %args ) = @_; |
2872 | 3920 | my @required_args = qw(OptionParser Quoter); | 3949 | my @required_args = qw(OptionParser TableParser Quoter); |
2873 | 3921 | foreach my $arg ( @required_args ) { | 3950 | foreach my $arg ( @required_args ) { |
2874 | 3922 | die "I need a $arg argument" unless $args{$arg}; | 3951 | die "I need a $arg argument" unless $args{$arg}; |
2875 | 3923 | } | 3952 | } |
2876 | @@ -3926,8 +3955,19 @@ | |||
2877 | 3926 | die "I need either a dbh or file_itr argument" | 3955 | die "I need either a dbh or file_itr argument" |
2878 | 3927 | if (!$dbh && !$file_itr) || ($dbh && $file_itr); | 3956 | if (!$dbh && !$file_itr) || ($dbh && $file_itr); |
2879 | 3928 | 3957 | ||
2880 | 3958 | my %resume; | ||
2881 | 3959 | if ( my $table = $args{resume} ) { | ||
2882 | 3960 | PTDEBUG && _d('Will resume from or after', $table); | ||
2883 | 3961 | my ($db, $tbl) = $args{Quoter}->split_unquote($table); | ||
2884 | 3962 | die "Resume table must be database-qualified: $table" | ||
2885 | 3963 | unless $db && $tbl; | ||
2886 | 3964 | $resume{db} = $db; | ||
2887 | 3965 | $resume{tbl} = $tbl; | ||
2888 | 3966 | } | ||
2889 | 3967 | |||
2890 | 3929 | my $self = { | 3968 | my $self = { |
2891 | 3930 | %args, | 3969 | %args, |
2892 | 3970 | resume => \%resume, | ||
2893 | 3931 | filters => _make_filters(%args), | 3971 | filters => _make_filters(%args), |
2894 | 3932 | }; | 3972 | }; |
2895 | 3933 | 3973 | ||
2896 | @@ -3988,9 +4028,19 @@ | |||
2897 | 3988 | return \%filters; | 4028 | return \%filters; |
2898 | 3989 | } | 4029 | } |
2899 | 3990 | 4030 | ||
2901 | 3991 | sub next_schema_object { | 4031 | sub next { |
2902 | 3992 | my ( $self ) = @_; | 4032 | my ( $self ) = @_; |
2903 | 3993 | 4033 | ||
2904 | 4034 | if ( !$self->{initialized} ) { | ||
2905 | 4035 | $self->{initialized} = 1; | ||
2906 | 4036 | if ( $self->{resume}->{tbl} | ||
2907 | 4037 | && !$self->table_is_allowed(@{$self->{resume}}{qw(db tbl)}) ) { | ||
2908 | 4038 | PTDEBUG && _d('Will resume after', | ||
2909 | 4039 | join('.', @{$self->{resume}}{qw(db tbl)})); | ||
2910 | 4040 | $self->{resume}->{after} = 1; | ||
2911 | 4041 | } | ||
2912 | 4042 | } | ||
2913 | 4043 | |||
2914 | 3994 | my $schema_obj; | 4044 | my $schema_obj; |
2915 | 3995 | if ( $self->{file_itr} ) { | 4045 | if ( $self->{file_itr} ) { |
2916 | 3996 | $schema_obj= $self->_iterate_files(); | 4046 | $schema_obj= $self->_iterate_files(); |
2917 | @@ -4000,19 +4050,13 @@ | |||
2918 | 4000 | } | 4050 | } |
2919 | 4001 | 4051 | ||
2920 | 4002 | if ( $schema_obj ) { | 4052 | if ( $schema_obj ) { |
2921 | 4003 | if ( $schema_obj->{ddl} && $self->{TableParser} ) { | ||
2922 | 4004 | $schema_obj->{tbl_struct} | ||
2923 | 4005 | = $self->{TableParser}->parse($schema_obj->{ddl}); | ||
2924 | 4006 | } | ||
2925 | 4007 | |||
2926 | 4008 | delete $schema_obj->{ddl} unless $self->{keep_ddl}; | ||
2927 | 4009 | |||
2928 | 4010 | if ( my $schema = $self->{Schema} ) { | 4053 | if ( my $schema = $self->{Schema} ) { |
2929 | 4011 | $schema->add_schema_object($schema_obj); | 4054 | $schema->add_schema_object($schema_obj); |
2930 | 4012 | } | 4055 | } |
2931 | 4056 | PTDEBUG && _d('Next schema object:', | ||
2932 | 4057 | $schema_obj->{db}, $schema_obj->{tbl}); | ||
2933 | 4013 | } | 4058 | } |
2934 | 4014 | 4059 | ||
2935 | 4015 | PTDEBUG && _d('Next schema object:', $schema_obj->{db}, $schema_obj->{tbl}); | ||
2936 | 4016 | return $schema_obj; | 4060 | return $schema_obj; |
2937 | 4017 | } | 4061 | } |
2938 | 4018 | 4062 | ||
2939 | @@ -4038,7 +4082,8 @@ | |||
2940 | 4038 | my $db = $1; # XXX | 4082 | my $db = $1; # XXX |
2941 | 4039 | $db =~ s/^`//; # strip leading ` | 4083 | $db =~ s/^`//; # strip leading ` |
2942 | 4040 | $db =~ s/`$//; # and trailing ` | 4084 | $db =~ s/`$//; # and trailing ` |
2944 | 4041 | if ( $self->database_is_allowed($db) ) { | 4085 | if ( $self->database_is_allowed($db) |
2945 | 4086 | && $self->_resume_from_database($db) ) { | ||
2946 | 4042 | $self->{db} = $db; | 4087 | $self->{db} = $db; |
2947 | 4043 | } | 4088 | } |
2948 | 4044 | } | 4089 | } |
2949 | @@ -4051,21 +4096,22 @@ | |||
2950 | 4051 | my ($tbl) = $chunk =~ m/$tbl_name/; | 4096 | my ($tbl) = $chunk =~ m/$tbl_name/; |
2951 | 4052 | $tbl =~ s/^\s*`//; | 4097 | $tbl =~ s/^\s*`//; |
2952 | 4053 | $tbl =~ s/`\s*$//; | 4098 | $tbl =~ s/`\s*$//; |
2954 | 4054 | if ( $self->table_is_allowed($self->{db}, $tbl) ) { | 4099 | if ( $self->_resume_from_table($tbl) |
2955 | 4100 | && $self->table_is_allowed($self->{db}, $tbl) ) { | ||
2956 | 4055 | my ($ddl) = $chunk =~ m/^(?:$open_comment)?(CREATE TABLE.+?;)$/ms; | 4101 | my ($ddl) = $chunk =~ m/^(?:$open_comment)?(CREATE TABLE.+?;)$/ms; |
2957 | 4056 | if ( !$ddl ) { | 4102 | if ( !$ddl ) { |
2958 | 4057 | warn "Failed to parse CREATE TABLE from\n" . $chunk; | 4103 | warn "Failed to parse CREATE TABLE from\n" . $chunk; |
2959 | 4058 | next CHUNK; | 4104 | next CHUNK; |
2960 | 4059 | } | 4105 | } |
2961 | 4060 | $ddl =~ s/ \*\/;\Z/;/; # remove end of version comment | 4106 | $ddl =~ s/ \*\/;\Z/;/; # remove end of version comment |
2966 | 4061 | 4107 | my $tbl_struct = $self->{TableParser}->parse($ddl); | |
2967 | 4062 | my ($engine) = $ddl =~ m/\).*?(?:ENGINE|TYPE)=(\w+)/; | 4108 | if ( $self->engine_is_allowed($tbl_struct->{engine}) ) { |
2964 | 4063 | |||
2965 | 4064 | if ( !$engine || $self->engine_is_allowed($engine) ) { | ||
2968 | 4065 | return { | 4109 | return { |
2972 | 4066 | db => $self->{db}, | 4110 | db => $self->{db}, |
2973 | 4067 | tbl => $tbl, | 4111 | tbl => $tbl, |
2974 | 4068 | ddl => $ddl, | 4112 | name => $self->{Quoter}->quote($self->{db}, $tbl), |
2975 | 4113 | ddl => $ddl, | ||
2976 | 4114 | tbl_struct => $tbl_struct, | ||
2977 | 4069 | }; | 4115 | }; |
2978 | 4070 | } | 4116 | } |
2979 | 4071 | } | 4117 | } |
2980 | @@ -4082,6 +4128,7 @@ | |||
2981 | 4082 | sub _iterate_dbh { | 4128 | sub _iterate_dbh { |
2982 | 4083 | my ( $self ) = @_; | 4129 | my ( $self ) = @_; |
2983 | 4084 | my $q = $self->{Quoter}; | 4130 | my $q = $self->{Quoter}; |
2984 | 4131 | my $tp = $self->{TableParser}; | ||
2985 | 4085 | my $dbh = $self->{dbh}; | 4132 | my $dbh = $self->{dbh}; |
2986 | 4086 | PTDEBUG && _d('Getting next schema object from dbh', $dbh); | 4133 | PTDEBUG && _d('Getting next schema object from dbh', $dbh); |
2987 | 4087 | 4134 | ||
2988 | @@ -4095,7 +4142,9 @@ | |||
2989 | 4095 | } | 4142 | } |
2990 | 4096 | 4143 | ||
2991 | 4097 | if ( !$self->{db} ) { | 4144 | if ( !$self->{db} ) { |
2993 | 4098 | $self->{db} = shift @{$self->{dbs}}; | 4145 | do { |
2994 | 4146 | $self->{db} = shift @{$self->{dbs}}; | ||
2995 | 4147 | } until $self->_resume_from_database($self->{db}); | ||
2996 | 4099 | PTDEBUG && _d('Next database:', $self->{db}); | 4148 | PTDEBUG && _d('Next database:', $self->{db}); |
2997 | 4100 | return unless $self->{db}; | 4149 | return unless $self->{db}; |
2998 | 4101 | } | 4150 | } |
2999 | @@ -4108,8 +4157,9 @@ | |||
3000 | 4108 | } | 4157 | } |
3001 | 4109 | grep { | 4158 | grep { |
3002 | 4110 | my ($tbl, $type) = @$_; | 4159 | my ($tbl, $type) = @$_; |
3005 | 4111 | $self->table_is_allowed($self->{db}, $tbl) | 4160 | (!$type || ($type ne 'VIEW')) |
3006 | 4112 | && (!$type || ($type ne 'VIEW')); | 4161 | && $self->_resume_from_table($tbl) |
3007 | 4162 | && $self->table_is_allowed($self->{db}, $tbl); | ||
3008 | 4113 | } | 4163 | } |
3009 | 4114 | @{$dbh->selectall_arrayref($sql)}; | 4164 | @{$dbh->selectall_arrayref($sql)}; |
3010 | 4115 | PTDEBUG && _d('Found', scalar @tbls, 'tables in database', $self->{db}); | 4165 | PTDEBUG && _d('Found', scalar @tbls, 'tables in database', $self->{db}); |
3011 | @@ -4117,27 +4167,15 @@ | |||
3012 | 4117 | } | 4167 | } |
3013 | 4118 | 4168 | ||
3014 | 4119 | while ( my $tbl = shift @{$self->{tbls}} ) { | 4169 | while ( my $tbl = shift @{$self->{tbls}} ) { |
3032 | 4120 | my $engine; | 4170 | my $ddl = $tp->get_create_table($dbh, $self->{db}, $tbl); |
3033 | 4121 | if ( $self->{filters}->{'engines'} | 4171 | my $tbl_struct = $tp->parse($ddl); |
3034 | 4122 | || $self->{filters}->{'ignore-engines'} ) { | 4172 | if ( $self->engine_is_allowed($tbl_struct->{engine}) ) { |
3018 | 4123 | my $sql = "SHOW TABLE STATUS FROM " . $q->quote($self->{db}) | ||
3019 | 4124 | . " LIKE \'$tbl\'"; | ||
3020 | 4125 | PTDEBUG && _d($sql); | ||
3021 | 4126 | $engine = $dbh->selectrow_hashref($sql)->{engine}; | ||
3022 | 4127 | PTDEBUG && _d($tbl, 'uses', $engine, 'engine'); | ||
3023 | 4128 | } | ||
3024 | 4129 | |||
3025 | 4130 | |||
3026 | 4131 | if ( !$engine || $self->engine_is_allowed($engine) ) { | ||
3027 | 4132 | my $ddl; | ||
3028 | 4133 | if ( my $du = $self->{MySQLDump} ) { | ||
3029 | 4134 | $ddl = $du->get_create_table($dbh, $q, $self->{db}, $tbl)->[1]; | ||
3030 | 4135 | } | ||
3031 | 4136 | |||
3035 | 4137 | return { | 4173 | return { |
3039 | 4138 | db => $self->{db}, | 4174 | db => $self->{db}, |
3040 | 4139 | tbl => $tbl, | 4175 | tbl => $tbl, |
3041 | 4140 | ddl => $ddl, | 4176 | name => $q->quote($self->{db}, $tbl), |
3042 | 4177 | ddl => $ddl, | ||
3043 | 4178 | tbl_struct => $tbl_struct, | ||
3044 | 4141 | }; | 4179 | }; |
3045 | 4142 | } | 4180 | } |
3046 | 4143 | } | 4181 | } |
3047 | @@ -4198,6 +4236,10 @@ | |||
3048 | 4198 | 4236 | ||
3049 | 4199 | my $filter = $self->{filters}; | 4237 | my $filter = $self->{filters}; |
3050 | 4200 | 4238 | ||
3051 | 4239 | if ( $db eq 'mysql' && ($tbl eq 'general_log' || $tbl eq 'slow_log') ) { | ||
3052 | 4240 | return 0; | ||
3053 | 4241 | } | ||
3054 | 4242 | |||
3055 | 4201 | if ( $filter->{'ignore-tables'}->{$tbl} | 4243 | if ( $filter->{'ignore-tables'}->{$tbl} |
3056 | 4202 | && ($filter->{'ignore-tables'}->{$tbl} eq '*' | 4244 | && ($filter->{'ignore-tables'}->{$tbl} eq '*' |
3057 | 4203 | || $filter->{'ignore-tables'}->{$tbl} eq $db) ) { | 4245 | || $filter->{'ignore-tables'}->{$tbl} eq $db) ) { |
3058 | @@ -4237,7 +4279,11 @@ | |||
3059 | 4237 | 4279 | ||
3060 | 4238 | sub engine_is_allowed { | 4280 | sub engine_is_allowed { |
3061 | 4239 | my ( $self, $engine ) = @_; | 4281 | my ( $self, $engine ) = @_; |
3063 | 4240 | die "I need an engine argument" unless $engine; | 4282 | |
3064 | 4283 | if ( !$engine ) { | ||
3065 | 4284 | PTDEBUG && _d('No engine specified; allowing the table'); | ||
3066 | 4285 | return 1; | ||
3067 | 4286 | } | ||
3068 | 4241 | 4287 | ||
3069 | 4242 | $engine = lc $engine; | 4288 | $engine = lc $engine; |
3070 | 4243 | 4289 | ||
3071 | @@ -4257,6 +4303,40 @@ | |||
3072 | 4257 | return 1; | 4303 | return 1; |
3073 | 4258 | } | 4304 | } |
3074 | 4259 | 4305 | ||
3075 | 4306 | sub _resume_from_database { | ||
3076 | 4307 | my ($self, $db) = @_; | ||
3077 | 4308 | |||
3078 | 4309 | return 1 unless $self->{resume}->{db}; | ||
3079 | 4310 | |||
3080 | 4311 | if ( $db eq $self->{resume}->{db} ) { | ||
3081 | 4312 | PTDEBUG && _d('At resume db', $db); | ||
3082 | 4313 | delete $self->{resume}->{db}; | ||
3083 | 4314 | return 1; | ||
3084 | 4315 | } | ||
3085 | 4316 | |||
3086 | 4317 | return 0; | ||
3087 | 4318 | } | ||
3088 | 4319 | |||
3089 | 4320 | sub _resume_from_table { | ||
3090 | 4321 | my ($self, $tbl) = @_; | ||
3091 | 4322 | |||
3092 | 4323 | return 1 unless $self->{resume}->{tbl}; | ||
3093 | 4324 | |||
3094 | 4325 | if ( $tbl eq $self->{resume}->{tbl} ) { | ||
3095 | 4326 | if ( !$self->{resume}->{after} ) { | ||
3096 | 4327 | PTDEBUG && _d('Resuming from table', $tbl); | ||
3097 | 4328 | delete $self->{resume}->{tbl}; | ||
3098 | 4329 | return 1; | ||
3099 | 4330 | } | ||
3100 | 4331 | else { | ||
3101 | 4332 | PTDEBUG && _d('Resuming after table', $tbl); | ||
3102 | 4333 | delete $self->{resume}->{tbl}; | ||
3103 | 4334 | } | ||
3104 | 4335 | } | ||
3105 | 4336 | |||
3106 | 4337 | return 0; | ||
3107 | 4338 | } | ||
3108 | 4339 | |||
3109 | 4260 | sub _d { | 4340 | sub _d { |
3110 | 4261 | my ($package, undef, $line) = caller 0; | 4341 | my ($package, undef, $line) = caller 0; |
3111 | 4262 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | 4342 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } |
3112 | @@ -5149,13 +5229,11 @@ | |||
3113 | 5149 | dbh => $dbh, | 5229 | dbh => $dbh, |
3114 | 5150 | OptionParser => $o, | 5230 | OptionParser => $o, |
3115 | 5151 | Quoter => $q, | 5231 | Quoter => $q, |
3116 | 5152 | MySQLDump => $du, | ||
3117 | 5153 | TableParser => $tp, | 5232 | TableParser => $tp, |
3118 | 5154 | Schema => $schema, | 5233 | Schema => $schema, |
3119 | 5155 | keep_ddl => 1, | ||
3120 | 5156 | ); | 5234 | ); |
3121 | 5157 | TALBE: | 5235 | TALBE: |
3123 | 5158 | while ( my $tbl = $schema_itr->next_schema_object() ) { | 5236 | while ( my $tbl = $schema_itr->next() ) { |
3124 | 5159 | eval { | 5237 | eval { |
3125 | 5160 | my ($indexes) = $tp->get_keys($tbl->{ddl}, {version => $version}); | 5238 | my ($indexes) = $tp->get_keys($tbl->{ddl}, {version => $version}); |
3126 | 5161 | $iu->add_indexes(%$tbl, indexes=>$indexes); | 5239 | $iu->add_indexes(%$tbl, indexes=>$indexes); |
3127 | @@ -6191,6 +6269,6 @@ | |||
3128 | 6191 | 6269 | ||
3129 | 6192 | =head1 VERSION | 6270 | =head1 VERSION |
3130 | 6193 | 6271 | ||
3132 | 6194 | pt-index-usage 2.0.4 | 6272 | pt-index-usage 2.1.1 |
3133 | 6195 | 6273 | ||
3134 | 6196 | =cut | 6274 | =cut |
3135 | 6197 | 6275 | ||
3136 | === modified file 'bin/pt-ioprofile' | |||
3137 | --- bin/pt-ioprofile 2012-05-16 17:31:10 +0000 | |||
3138 | +++ bin/pt-ioprofile 2012-05-30 21:00:30 +0000 | |||
3139 | @@ -1037,7 +1037,7 @@ | |||
3140 | 1037 | 1037 | ||
3141 | 1038 | =head1 VERSION | 1038 | =head1 VERSION |
3142 | 1039 | 1039 | ||
3144 | 1040 | pt-ioprofile 2.0.4 | 1040 | pt-ioprofile 2.1.1 |
3145 | 1041 | 1041 | ||
3146 | 1042 | =cut | 1042 | =cut |
3147 | 1043 | 1043 | ||
3148 | 1044 | 1044 | ||
3149 | === modified file 'bin/pt-kill' | |||
3150 | --- bin/pt-kill 2012-05-24 17:52:01 +0000 | |||
3151 | +++ bin/pt-kill 2012-05-30 21:00:30 +0000 | |||
3152 | @@ -4860,6 +4860,6 @@ | |||
3153 | 4860 | 4860 | ||
3154 | 4861 | =head1 VERSION | 4861 | =head1 VERSION |
3155 | 4862 | 4862 | ||
3157 | 4863 | pt-kill 2.0.4 | 4863 | pt-kill 2.1.1 |
3158 | 4864 | 4864 | ||
3159 | 4865 | =cut | 4865 | =cut |
3160 | 4866 | 4866 | ||
3161 | === modified file 'bin/pt-log-player' | |||
3162 | --- bin/pt-log-player 2012-05-24 17:52:01 +0000 | |||
3163 | +++ bin/pt-log-player 2012-05-30 21:00:30 +0000 | |||
3164 | @@ -3609,6 +3609,6 @@ | |||
3165 | 3609 | 3609 | ||
3166 | 3610 | =head1 VERSION | 3610 | =head1 VERSION |
3167 | 3611 | 3611 | ||
3169 | 3612 | pt-log-player 2.0.4 | 3612 | pt-log-player 2.1.1 |
3170 | 3613 | 3613 | ||
3171 | 3614 | =cut | 3614 | =cut |
3172 | 3615 | 3615 | ||
3173 | === modified file 'bin/pt-mext' | |||
3174 | --- bin/pt-mext 2012-05-16 17:31:10 +0000 | |||
3175 | +++ bin/pt-mext 2012-05-30 21:00:30 +0000 | |||
3176 | @@ -279,7 +279,7 @@ | |||
3177 | 279 | 279 | ||
3178 | 280 | =head1 VERSION | 280 | =head1 VERSION |
3179 | 281 | 281 | ||
3181 | 282 | pt-mext 2.0.4 | 282 | pt-mext 2.1.1 |
3182 | 283 | 283 | ||
3183 | 284 | =cut | 284 | =cut |
3184 | 285 | 285 | ||
3185 | 286 | 286 | ||
3186 | === modified file 'bin/pt-mysql-summary' | |||
3187 | --- bin/pt-mysql-summary 2012-05-16 17:31:10 +0000 | |||
3188 | +++ bin/pt-mysql-summary 2012-05-30 21:00:30 +0000 | |||
3189 | @@ -4,14 +4,421 @@ | |||
3190 | 4 | # See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal | 4 | # See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal |
3191 | 5 | # notices and disclaimers. | 5 | # notices and disclaimers. |
3192 | 6 | 6 | ||
3193 | 7 | set -u | ||
3194 | 8 | |||
3195 | 9 | # ########################################################################### | ||
3196 | 10 | # log_warn_die package | ||
3197 | 11 | # This package is a copy without comments from the original. The original | ||
3198 | 12 | # with comments and its test file can be found in the Bazaar repository at, | ||
3199 | 13 | # lib/bash/log_warn_die.sh | ||
3200 | 14 | # t/lib/bash/log_warn_die.sh | ||
3201 | 15 | # See https://launchpad.net/percona-toolkit for more information. | ||
3202 | 16 | # ########################################################################### | ||
3203 | 17 | |||
3204 | 18 | |||
3205 | 19 | set -u | ||
3206 | 20 | |||
3207 | 21 | PTFUNCNAME="" | ||
3208 | 22 | PTDEBUG="${PTDEBUG:-""}" | ||
3209 | 23 | EXIT_STATUS=0 | ||
3210 | 24 | |||
3211 | 25 | log() { | ||
3212 | 26 | TS=$(date +%F-%T | tr :- _); | ||
3213 | 27 | echo "$TS $*" | ||
3214 | 28 | } | ||
3215 | 29 | |||
3216 | 30 | warn() { | ||
3217 | 31 | log "$*" >&2 | ||
3218 | 32 | EXIT_STATUS=1 | ||
3219 | 33 | } | ||
3220 | 34 | |||
3221 | 35 | die() { | ||
3222 | 36 | warn "$*" | ||
3223 | 37 | exit 1 | ||
3224 | 38 | } | ||
3225 | 39 | |||
3226 | 40 | _d () { | ||
3227 | 41 | [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(log "$*")" >&2 | ||
3228 | 42 | } | ||
3229 | 43 | |||
3230 | 44 | # ########################################################################### | ||
3231 | 45 | # End log_warn_die package | ||
3232 | 46 | # ########################################################################### | ||
3233 | 47 | |||
3234 | 48 | # ########################################################################### | ||
3235 | 49 | # parse_options package | ||
3236 | 50 | # This package is a copy without comments from the original. The original | ||
3237 | 51 | # with comments and its test file can be found in the Bazaar repository at, | ||
3238 | 52 | # lib/bash/parse_options.sh | ||
3239 | 53 | # t/lib/bash/parse_options.sh | ||
3240 | 54 | # See https://launchpad.net/percona-toolkit for more information. | ||
3241 | 55 | # ########################################################################### | ||
3242 | 56 | |||
3243 | 57 | |||
3244 | 58 | |||
3245 | 59 | |||
3246 | 60 | |||
3247 | 61 | set -u | ||
3248 | 62 | |||
3249 | 63 | ARGV="" # Non-option args (probably input files) | ||
3250 | 64 | EXT_ARGV="" # Everything after -- (args for an external command) | ||
3251 | 65 | HAVE_EXT_ARGV="" # Got --, everything else is put into EXT_ARGV | ||
3252 | 66 | OPT_ERRS=0 # How many command line option errors | ||
3253 | 67 | OPT_VERSION="" # If --version was specified | ||
3254 | 68 | OPT_HELP="" # If --help was specified | ||
3255 | 69 | PO_DIR="" # Directory with program option spec files | ||
3256 | 70 | |||
3257 | 7 | usage() { | 71 | usage() { |
3265 | 8 | if [ "${OPT_ERR}" ]; then | 72 | local file="$1" |
3266 | 9 | echo "${OPT_ERR}" >&2 | 73 | |
3267 | 10 | fi | 74 | local usage=$(grep '^Usage: ' "$file") |
3268 | 11 | echo "Usage: pt-mysql-summary [MYSQL-OPTIONS]" >&2 | 75 | echo $usage |
3269 | 12 | echo "For more information, 'man pt-mysql-summary' or 'perldoc $0'" >&2 | 76 | echo |
3270 | 13 | exit 1 | 77 | echo "For more information, 'man $TOOL' or 'perldoc $file'." |
3271 | 14 | } | 78 | } |
3272 | 79 | |||
3273 | 80 | usage_or_errors() { | ||
3274 | 81 | local file="$1" | ||
3275 | 82 | |||
3276 | 83 | if [ "$OPT_VERSION" ]; then | ||
3277 | 84 | local version=$(grep '^pt-[^ ]\+ [0-9]' "$file") | ||
3278 | 85 | echo "$version" | ||
3279 | 86 | return 1 | ||
3280 | 87 | fi | ||
3281 | 88 | |||
3282 | 89 | if [ "$OPT_HELP" ]; then | ||
3283 | 90 | usage "$file" | ||
3284 | 91 | echo | ||
3285 | 92 | echo "Command line options:" | ||
3286 | 93 | echo | ||
3287 | 94 | perl -e ' | ||
3288 | 95 | use strict; | ||
3289 | 96 | use warnings FATAL => qw(all); | ||
3290 | 97 | my $lcol = 20; # Allow this much space for option names. | ||
3291 | 98 | my $rcol = 80 - $lcol; # The terminal is assumed to be 80 chars wide. | ||
3292 | 99 | my $name; | ||
3293 | 100 | while ( <> ) { | ||
3294 | 101 | my $line = $_; | ||
3295 | 102 | chomp $line; | ||
3296 | 103 | if ( $line =~ s/^long:/ --/ ) { | ||
3297 | 104 | $name = $line; | ||
3298 | 105 | } | ||
3299 | 106 | elsif ( $line =~ s/^desc:// ) { | ||
3300 | 107 | $line =~ s/ +$//mg; | ||
3301 | 108 | my @lines = grep { $_ } | ||
3302 | 109 | $line =~ m/(.{0,$rcol})(?:\s+|\Z)/g; | ||
3303 | 110 | if ( length($name) >= $lcol ) { | ||
3304 | 111 | print $name, "\n", (q{ } x $lcol); | ||
3305 | 112 | } | ||
3306 | 113 | else { | ||
3307 | 114 | printf "%-${lcol}s", $name; | ||
3308 | 115 | } | ||
3309 | 116 | print join("\n" . (q{ } x $lcol), @lines); | ||
3310 | 117 | print "\n"; | ||
3311 | 118 | } | ||
3312 | 119 | } | ||
3313 | 120 | ' "$PO_DIR"/* | ||
3314 | 121 | echo | ||
3315 | 122 | echo "Options and values after processing arguments:" | ||
3316 | 123 | echo | ||
3317 | 124 | for opt in $(ls "$PO_DIR"); do | ||
3318 | 125 | local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)" | ||
3319 | 126 | local varvalue="${!varname}" | ||
3320 | 127 | printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}" | ||
3321 | 128 | echo | ||
3322 | 129 | done | ||
3323 | 130 | return 1 | ||
3324 | 131 | fi | ||
3325 | 132 | |||
3326 | 133 | if [ $OPT_ERRS -gt 0 ]; then | ||
3327 | 134 | echo | ||
3328 | 135 | usage "$file" | ||
3329 | 136 | return 1 | ||
3330 | 137 | fi | ||
3331 | 138 | |||
3332 | 139 | return 0 | ||
3333 | 140 | } | ||
3334 | 141 | |||
3335 | 142 | option_error() { | ||
3336 | 143 | local err="$1" | ||
3337 | 144 | OPT_ERRS=$(($OPT_ERRS + 1)) | ||
3338 | 145 | echo "$err" >&2 | ||
3339 | 146 | } | ||
3340 | 147 | |||
3341 | 148 | parse_options() { | ||
3342 | 149 | local file="$1" | ||
3343 | 150 | shift | ||
3344 | 151 | |||
3345 | 152 | ARGV="" | ||
3346 | 153 | EXT_ARGV="" | ||
3347 | 154 | HAVE_EXT_ARGV="" | ||
3348 | 155 | OPT_ERRS=0 | ||
3349 | 156 | OPT_VERSION="" | ||
3350 | 157 | OPT_HELP="" | ||
3351 | 158 | PO_DIR="$TMPDIR/po" | ||
3352 | 159 | |||
3353 | 160 | if [ ! -d "$PO_DIR" ]; then | ||
3354 | 161 | mkdir "$PO_DIR" | ||
3355 | 162 | if [ $? -ne 0 ]; then | ||
3356 | 163 | echo "Cannot mkdir $PO_DIR" >&2 | ||
3357 | 164 | exit 1 | ||
3358 | 165 | fi | ||
3359 | 166 | fi | ||
3360 | 167 | |||
3361 | 168 | rm -rf "$PO_DIR"/* | ||
3362 | 169 | if [ $? -ne 0 ]; then | ||
3363 | 170 | echo "Cannot rm -rf $PO_DIR/*" >&2 | ||
3364 | 171 | exit 1 | ||
3365 | 172 | fi | ||
3366 | 173 | |||
3367 | 174 | _parse_pod "$file" # Parse POD into program option (po) spec files | ||
3368 | 175 | _eval_po # Eval po into existence with default values | ||
3369 | 176 | |||
3370 | 177 | if [ $# -ge 2 ] && [ "$1" = "--config" ]; then | ||
3371 | 178 | shift # --config | ||
3372 | 179 | local user_config_files="$1" | ||
3373 | 180 | shift # that ^ | ||
3374 | 181 | local IFS="," | ||
3375 | 182 | for user_config_file in $user_config_files; do | ||
3376 | 183 | _parse_config_files "$user_config_file" | ||
3377 | 184 | done | ||
3378 | 185 | else | ||
3379 | 186 | _parse_config_files "/etc/percona-toolkit/percona-toolkit.conf" "/etc/percona-toolkit/$TOOL.conf" "$HOME/.percona-toolkit.conf" "$HOME/.$TOOL.conf" | ||
3380 | 187 | fi | ||
3381 | 188 | |||
3382 | 189 | _parse_command_line "$@" | ||
3383 | 190 | } | ||
3384 | 191 | |||
3385 | 192 | _parse_pod() { | ||
3386 | 193 | local file="$1" | ||
3387 | 194 | |||
3388 | 195 | cat "$file" | PO_DIR="$PO_DIR" perl -ne ' | ||
3389 | 196 | BEGIN { $/ = ""; } | ||
3390 | 197 | next unless $_ =~ m/^=head1 OPTIONS/; | ||
3391 | 198 | while ( defined(my $para = <>) ) { | ||
3392 | 199 | last if $para =~ m/^=head1/; | ||
3393 | 200 | chomp; | ||
3394 | 201 | if ( $para =~ m/^=item --(\S+)/ ) { | ||
3395 | 202 | my $opt = $1; | ||
3396 | 203 | my $file = "$ENV{PO_DIR}/$opt"; | ||
3397 | 204 | open my $opt_fh, ">", $file or die "Cannot open $file: $!"; | ||
3398 | 205 | print $opt_fh "long:$opt\n"; | ||
3399 | 206 | $para = <>; | ||
3400 | 207 | chomp; | ||
3401 | 208 | if ( $para =~ m/^[a-z ]+:/ ) { | ||
3402 | 209 | map { | ||
3403 | 210 | chomp; | ||
3404 | 211 | my ($attrib, $val) = split(/: /, $_); | ||
3405 | 212 | print $opt_fh "$attrib:$val\n"; | ||
3406 | 213 | } split(/; /, $para); | ||
3407 | 214 | $para = <>; | ||
3408 | 215 | chomp; | ||
3409 | 216 | } | ||
3410 | 217 | my ($desc) = $para =~ m/^([^?.]+)/; | ||
3411 | 218 | print $opt_fh "desc:$desc.\n"; | ||
3412 | 219 | close $opt_fh; | ||
3413 | 220 | } | ||
3414 | 221 | } | ||
3415 | 222 | last; | ||
3416 | 223 | ' | ||
3417 | 224 | } | ||
3418 | 225 | |||
3419 | 226 | _eval_po() { | ||
3420 | 227 | local IFS=":" | ||
3421 | 228 | for opt_spec in "$PO_DIR"/*; do | ||
3422 | 229 | local opt="" | ||
3423 | 230 | local default_val="" | ||
3424 | 231 | local neg=0 | ||
3425 | 232 | local size=0 | ||
3426 | 233 | while read key val; do | ||
3427 | 234 | case "$key" in | ||
3428 | 235 | long) | ||
3429 | 236 | opt=$(echo $val | sed 's/-/_/g' | tr [:lower:] [:upper:]) | ||
3430 | 237 | ;; | ||
3431 | 238 | default) | ||
3432 | 239 | default_val="$val" | ||
3433 | 240 | ;; | ||
3434 | 241 | "short form") | ||
3435 | 242 | ;; | ||
3436 | 243 | type) | ||
3437 | 244 | [ "$val" = "size" ] && size=1 | ||
3438 | 245 | ;; | ||
3439 | 246 | desc) | ||
3440 | 247 | ;; | ||
3441 | 248 | negatable) | ||
3442 | 249 | if [ "$val" = "yes" ]; then | ||
3443 | 250 | neg=1 | ||
3444 | 251 | fi | ||
3445 | 252 | ;; | ||
3446 | 253 | *) | ||
3447 | 254 | echo "Invalid attribute in $opt_spec: $line" >&2 | ||
3448 | 255 | exit 1 | ||
3449 | 256 | esac | ||
3450 | 257 | done < "$opt_spec" | ||
3451 | 258 | |||
3452 | 259 | if [ -z "$opt" ]; then | ||
3453 | 260 | echo "No long attribute in option spec $opt_spec" >&2 | ||
3454 | 261 | exit 1 | ||
3455 | 262 | fi | ||
3456 | 263 | |||
3457 | 264 | if [ $neg -eq 1 ]; then | ||
3458 | 265 | if [ -z "$default_val" ] || [ "$default_val" != "yes" ]; then | ||
3459 | 266 | echo "Option $opt_spec is negatable but not default: yes" >&2 | ||
3460 | 267 | exit 1 | ||
3461 | 268 | fi | ||
3462 | 269 | fi | ||
3463 | 270 | |||
3464 | 271 | if [ $size -eq 1 -a -n "$default_val" ]; then | ||
3465 | 272 | default_val=$(size_to_bytes $default_val) | ||
3466 | 273 | fi | ||
3467 | 274 | |||
3468 | 275 | eval "OPT_${opt}"="$default_val" | ||
3469 | 276 | done | ||
3470 | 277 | } | ||
3471 | 278 | |||
3472 | 279 | _parse_config_files() { | ||
3473 | 280 | |||
3474 | 281 | for config_file in "$@"; do | ||
3475 | 282 | test -f "$config_file" || continue | ||
3476 | 283 | |||
3477 | 284 | while read config_opt; do | ||
3478 | 285 | |||
3479 | 286 | echo "$config_opt" | grep '^[ ]*[^#]' >/dev/null 2>&1 || continue | ||
3480 | 287 | |||
3481 | 288 | config_opt="$(echo "$config_opt" | sed -e 's/^ *//g' -e 's/ *$//g' -e 's/[ ]*=[ ]*/=/' -e 's/[ ]*#.*$//')" | ||
3482 | 289 | |||
3483 | 290 | [ "$config_opt" = "" ] && continue | ||
3484 | 291 | |||
3485 | 292 | if ! [ "$HAVE_EXT_ARGV" ]; then | ||
3486 | 293 | config_opt="--$config_opt" | ||
3487 | 294 | fi | ||
3488 | 295 | |||
3489 | 296 | _parse_command_line "$config_opt" | ||
3490 | 297 | |||
3491 | 298 | done < "$config_file" | ||
3492 | 299 | |||
3493 | 300 | HAVE_EXT_ARGV="" # reset for each file | ||
3494 | 301 | |||
3495 | 302 | done | ||
3496 | 303 | } | ||
3497 | 304 | |||
3498 | 305 | _parse_command_line() { | ||
3499 | 306 | local opt="" | ||
3500 | 307 | local val="" | ||
3501 | 308 | local next_opt_is_val="" | ||
3502 | 309 | local opt_is_ok="" | ||
3503 | 310 | local opt_is_negated="" | ||
3504 | 311 | local real_opt="" | ||
3505 | 312 | local required_arg="" | ||
3506 | 313 | local spec="" | ||
3507 | 314 | |||
3508 | 315 | for opt in "$@"; do | ||
3509 | 316 | if [ "$opt" = "--" -o "$opt" = "----" ]; then | ||
3510 | 317 | HAVE_EXT_ARGV=1 | ||
3511 | 318 | continue | ||
3512 | 319 | fi | ||
3513 | 320 | if [ "$HAVE_EXT_ARGV" ]; then | ||
3514 | 321 | if [ "$EXT_ARGV" ]; then | ||
3515 | 322 | EXT_ARGV="$EXT_ARGV $opt" | ||
3516 | 323 | else | ||
3517 | 324 | EXT_ARGV="$opt" | ||
3518 | 325 | fi | ||
3519 | 326 | continue | ||
3520 | 327 | fi | ||
3521 | 328 | |||
3522 | 329 | if [ "$next_opt_is_val" ]; then | ||
3523 | 330 | next_opt_is_val="" | ||
3524 | 331 | if [ $# -eq 0 ] || [ $(expr "$opt" : "-") -eq 1 ]; then | ||
3525 | 332 | option_error "$real_opt requires a $required_arg argument" | ||
3526 | 333 | continue | ||
3527 | 334 | fi | ||
3528 | 335 | val="$opt" | ||
3529 | 336 | opt_is_ok=1 | ||
3530 | 337 | else | ||
3531 | 338 | if [ $(expr "$opt" : "-") -eq 0 ]; then | ||
3532 | 339 | if [ -z "$ARGV" ]; then | ||
3533 | 340 | ARGV="$opt" | ||
3534 | 341 | else | ||
3535 | 342 | ARGV="$ARGV $opt" | ||
3536 | 343 | fi | ||
3537 | 344 | continue | ||
3538 | 345 | fi | ||
3539 | 346 | |||
3540 | 347 | real_opt="$opt" | ||
3541 | 348 | |||
3542 | 349 | if $(echo $opt | grep '^--no-' >/dev/null); then | ||
3543 | 350 | opt_is_negated=1 | ||
3544 | 351 | opt=$(echo $opt | sed 's/^--no-//') | ||
3545 | 352 | else | ||
3546 | 353 | opt_is_negated="" | ||
3547 | 354 | opt=$(echo $opt | sed 's/^-*//') | ||
3548 | 355 | fi | ||
3549 | 356 | |||
3550 | 357 | if $(echo $opt | grep '^[a-z-][a-z-]*=' >/dev/null 2>&1); then | ||
3551 | 358 | val="$(echo $opt | awk -F= '{print $2}')" | ||
3552 | 359 | opt="$(echo $opt | awk -F= '{print $1}')" | ||
3553 | 360 | fi | ||
3554 | 361 | |||
3555 | 362 | if [ -f "$TMPDIR/po/$opt" ]; then | ||
3556 | 363 | spec="$TMPDIR/po/$opt" | ||
3557 | 364 | else | ||
3558 | 365 | spec=$(grep "^short form:-$opt\$" "$TMPDIR"/po/* | cut -d ':' -f 1) | ||
3559 | 366 | if [ -z "$spec" ]; then | ||
3560 | 367 | option_error "Unknown option: $real_opt" | ||
3561 | 368 | continue | ||
3562 | 369 | fi | ||
3563 | 370 | fi | ||
3564 | 371 | |||
3565 | 372 | required_arg=$(cat "$spec" | awk -F: '/^type:/{print $2}') | ||
3566 | 373 | if [ "$required_arg" ]; then | ||
3567 | 374 | if [ "$val" ]; then | ||
3568 | 375 | opt_is_ok=1 | ||
3569 | 376 | else | ||
3570 | 377 | next_opt_is_val=1 | ||
3571 | 378 | fi | ||
3572 | 379 | else | ||
3573 | 380 | if [ "$val" ]; then | ||
3574 | 381 | option_error "Option $real_opt does not take a value" | ||
3575 | 382 | continue | ||
3576 | 383 | fi | ||
3577 | 384 | if [ "$opt_is_negated" ]; then | ||
3578 | 385 | val="" | ||
3579 | 386 | else | ||
3580 | 387 | val="yes" | ||
3581 | 388 | fi | ||
3582 | 389 | opt_is_ok=1 | ||
3583 | 390 | fi | ||
3584 | 391 | fi | ||
3585 | 392 | |||
3586 | 393 | if [ "$opt_is_ok" ]; then | ||
3587 | 394 | opt=$(cat "$spec" | grep '^long:' | cut -d':' -f2 | sed 's/-/_/g' | tr [:lower:] [:upper:]) | ||
3588 | 395 | |||
3589 | 396 | if grep "^type:size" "$spec" >/dev/null; then | ||
3590 | 397 | val=$(size_to_bytes $val) | ||
3591 | 398 | fi | ||
3592 | 399 | |||
3593 | 400 | eval "OPT_$opt"="'$val'" | ||
3594 | 401 | |||
3595 | 402 | opt="" | ||
3596 | 403 | val="" | ||
3597 | 404 | next_opt_is_val="" | ||
3598 | 405 | opt_is_ok="" | ||
3599 | 406 | opt_is_negated="" | ||
3600 | 407 | real_opt="" | ||
3601 | 408 | required_arg="" | ||
3602 | 409 | spec="" | ||
3603 | 410 | fi | ||
3604 | 411 | done | ||
3605 | 412 | } | ||
3606 | 413 | |||
3607 | 414 | size_to_bytes() { | ||
3608 | 415 | local size="$1" | ||
3609 | 416 | echo $size | perl -ne '%f=(B=>1, K=>1_024, M=>1_048_576, G=>1_073_741_824, T=>1_099_511_627_776); m/^(\d+)([kMGT])?/i; print $1 * $f{uc($2 || "B")};' | ||
3610 | 417 | } | ||
3611 | 418 | |||
3612 | 419 | # ########################################################################### | ||
3613 | 420 | # End parse_options package | ||
3614 | 421 | # ########################################################################### | ||
3615 | 15 | 422 | ||
3616 | 16 | # ########################################################################### | 423 | # ########################################################################### |
3617 | 17 | # tmpdir package | 424 | # tmpdir package |
3618 | @@ -22,6 +429,9 @@ | |||
3619 | 22 | # See https://launchpad.net/percona-toolkit for more information. | 429 | # See https://launchpad.net/percona-toolkit for more information. |
3620 | 23 | # ########################################################################### | 430 | # ########################################################################### |
3621 | 24 | 431 | ||
3622 | 432 | |||
3623 | 433 | set -u | ||
3624 | 434 | |||
3625 | 25 | TMPDIR="" | 435 | TMPDIR="" |
3626 | 26 | 436 | ||
3627 | 27 | mk_tmpdir() { | 437 | mk_tmpdir() { |
3628 | @@ -51,27 +461,68 @@ | |||
3629 | 51 | # End tmpdir package | 461 | # End tmpdir package |
3630 | 52 | # ########################################################################### | 462 | # ########################################################################### |
3631 | 53 | 463 | ||
3653 | 54 | # ######################################################################## | 464 | # ########################################################################### |
3654 | 55 | # Some global setup is necessary for cross-platform compatibility, even | 465 | # alt_cmds package |
3655 | 56 | # when sourcing this script for testing purposes. | 466 | # This package is a copy without comments from the original. The original |
3656 | 57 | # ######################################################################## | 467 | # with comments and its test file can be found in the Bazaar repository at, |
3657 | 58 | AP_AWK="$(which awk)" | 468 | # lib/bash/alt_cmds.sh |
3658 | 59 | which gawk >/dev/null 2>&1 && AP_AWK="$(which gawk)" | 469 | # t/lib/bash/alt_cmds.sh |
3659 | 60 | AP_SED="$(which sed)" | 470 | # See https://launchpad.net/percona-toolkit for more information. |
3660 | 61 | which gsed >/dev/null 2>&1 && AP_SED="$(which gsed)" | 471 | # ########################################################################### |
3661 | 62 | AP_GREP="$(which grep)" | 472 | |
3662 | 63 | which ggrep >/dev/null 2>&1 && AP_GREP="$(which ggrep)" | 473 | |
3663 | 64 | 474 | set -u | |
3664 | 65 | # ######################################################################## | 475 | |
3665 | 66 | # Globals, helper functions | 476 | _seq() { |
3666 | 67 | # ######################################################################## | 477 | local i="$1" |
3667 | 68 | 478 | awk "BEGIN { for(i=1; i<=$i; i++) print i; }" | |
3668 | 69 | # The awk code for fuzzy rounding. (It's used in a few places, so makes sense | 479 | } |
3669 | 70 | # not to duplicate). It fuzzy-rounds the variable named fuzzy_var. It goes in | 480 | |
3670 | 71 | # steps of 5, 10, 25, then repeats by a factor of 10 larger (50, 100, 250), and | 481 | _pidof() { |
3671 | 72 | # so on, until it finds a number that's large enough. The pattern is slightly | 482 | local cmd="$1" |
3672 | 73 | # broken between the initial 1 and 50, because rounding to the nearest 2.5 | 483 | if ! pidof "$cmd" 2>/dev/null; then |
3673 | 74 | # doesn't seem right to me. | 484 | ps -eo pid,ucomm | awk -v comm="$cmd" '$2 == comm { print $1 }' |
3674 | 485 | fi | ||
3675 | 486 | } | ||
3676 | 487 | |||
3677 | 488 | _lsof() { | ||
3678 | 489 | local pid="$1" | ||
3679 | 490 | if ! lsof -p $pid 2>/dev/null; then | ||
3680 | 491 | /bin/ls -l /proc/$pid/fd 2>/dev/null | ||
3681 | 492 | fi | ||
3682 | 493 | } | ||
3683 | 494 | |||
3684 | 495 | |||
3685 | 496 | |||
3686 | 497 | _which() { | ||
3687 | 498 | if [ -x /usr/bin/which ]; then | ||
3688 | 499 | /usr/bin/which "$1" 2>/dev/null | awk '{print $1}' | ||
3689 | 500 | elif which which 1>/dev/null 2>&1; then | ||
3690 | 501 | which "$1" 2>/dev/null | awk '{print $1}' | ||
3691 | 502 | else | ||
3692 | 503 | echo "$1" | ||
3693 | 504 | fi | ||
3694 | 505 | } | ||
3695 | 506 | |||
3696 | 507 | # ########################################################################### | ||
3697 | 508 | # End alt_cmds package | ||
3698 | 509 | # ########################################################################### | ||
3699 | 510 | |||
3700 | 511 | # ########################################################################### | ||
3701 | 512 | # report_formatting package | ||
3702 | 513 | # This package is a copy without comments from the original. The original | ||
3703 | 514 | # with comments and its test file can be found in the Bazaar repository at, | ||
3704 | 515 | # lib/bash/report_formatting.sh | ||
3705 | 516 | # t/lib/bash/report_formatting.sh | ||
3706 | 517 | # See https://launchpad.net/percona-toolkit for more information. | ||
3707 | 518 | # ########################################################################### | ||
3708 | 519 | |||
3709 | 520 | |||
3710 | 521 | set -u | ||
3711 | 522 | |||
3712 | 523 | POSIXLY_CORRECT=1 | ||
3713 | 524 | export POSIXLY_CORRECT | ||
3714 | 525 | |||
3715 | 75 | fuzzy_formula=' | 526 | fuzzy_formula=' |
3716 | 76 | rounded = 0; | 527 | rounded = 0; |
3717 | 77 | if (fuzzy_var <= 10 ) { | 528 | if (fuzzy_var <= 10 ) { |
3718 | @@ -94,108 +545,461 @@ | |||
3719 | 94 | factor = factor * 10; | 545 | factor = factor * 10; |
3720 | 95 | }' | 546 | }' |
3721 | 96 | 547 | ||
3749 | 97 | # The temp files are for storing working results so we don't call commands many | 548 | fuzz () { |
3750 | 98 | # times (gives inconsistent results, maybe adds load on things I don't want to | 549 | awk -v fuzzy_var="$1" "BEGIN { ${fuzzy_formula} print fuzzy_var;}" |
3751 | 99 | # such as RAID controllers). They must not exist -- if they did, someone would | 550 | } |
3752 | 100 | # symlink them to /etc/passwd and then run this program as root. Call this | 551 | |
3753 | 101 | # function with "rm" or "touch" as an argument. | 552 | fuzzy_pct () { |
3754 | 102 | temp_files() { | 553 | local pct="$(awk -v one="$1" -v two="$2" 'BEGIN{ if (two > 0) { printf "%d", one/two*100; } else {print 0} }')"; |
3755 | 103 | for file in $TMPDIR/percona-toolkit{,-mysql-variables,-mysql-status,-innodb-status} \ | 554 | echo "$(fuzz "${pct}")%" |
3756 | 104 | $TMPDIR/percona-toolkit{2,-mysql-databases,-mysql-processlist,-noncounters} \ | 555 | } |
3757 | 105 | $TMPDIR/percona-toolkit-mysql{dump,-slave}; | 556 | |
3731 | 106 | do | ||
3732 | 107 | case "$1" in | ||
3733 | 108 | touch) | ||
3734 | 109 | if ! touch "${file}"; then | ||
3735 | 110 | echo "I can't make my temp file ${file}"; | ||
3736 | 111 | exit 1; | ||
3737 | 112 | fi | ||
3738 | 113 | ;; | ||
3739 | 114 | rm) | ||
3740 | 115 | rm -f "${file}" | ||
3741 | 116 | ;; | ||
3742 | 117 | esac | ||
3743 | 118 | done | ||
3744 | 119 | } | ||
3745 | 120 | |||
3746 | 121 | # Print a space-padded string into $line. Then translate spaces to hashes, and | ||
3747 | 122 | # underscores to spaces. End result is a line of hashes with words at the | ||
3748 | 123 | # start. | ||
3758 | 124 | section () { | 557 | section () { |
3770 | 125 | line="$(printf '#_%-60s' "$1_")" | 558 | local str="$1" |
3771 | 126 | line="${line// /#}" | 559 | awk -v var="${str} _" 'BEGIN { |
3772 | 127 | printf "%s\n" "${line//_/ }" | 560 | line = sprintf("# %-60s", var); |
3773 | 128 | } | 561 | i = index(line, "_"); |
3774 | 129 | 562 | x = substr(line, i); | |
3775 | 130 | # Print a "name | value" line. | 563 | gsub(/[_ \t]/, "#", x); |
3776 | 131 | name_val() { | 564 | printf("%s%s\n", substr(line, 1, i-1), x); |
3777 | 132 | printf "%20s | %s\n" "$1" "$2" | 565 | }' |
3778 | 133 | } | 566 | } |
3779 | 134 | 567 | ||
3780 | 135 | # Converts a value to units of power of 2. Optional precision is $2. | 568 | NAME_VAL_LEN=12 |
3781 | 569 | name_val () { | ||
3782 | 570 | printf "%+*s | %s\n" "${NAME_VAL_LEN}" "$1" "$2" | ||
3783 | 571 | } | ||
3784 | 572 | |||
3785 | 136 | shorten() { | 573 | shorten() { |
3800 | 137 | unit=k | 574 | local num="$1" |
3801 | 138 | size=1024 | 575 | local prec="${2:-2}" |
3802 | 139 | if [ $1 -ge 1099511627776 ] ; then | 576 | local div="${3:-1024}" |
3803 | 140 | size=1099511627776 | 577 | |
3804 | 141 | unit=T | 578 | echo "$num" | awk -v prec="$prec" -v div="$div" ' |
3805 | 142 | elif [ $1 -ge 1073741824 ] ; then | 579 | { |
3806 | 143 | size=1073741824 | 580 | size = 4; |
3807 | 144 | unit=G | 581 | val = $1; |
3808 | 145 | elif [ $1 -ge 1048576 ] ; then | 582 | |
3809 | 146 | size=1048576 | 583 | unit = val >= 1099511627776 ? "T" : val >= 1073741824 ? "G" : val >= 1048576 ? "M" : val >= 1024 ? "k" : ""; |
3810 | 147 | unit=M | 584 | |
3811 | 148 | fi | 585 | while ( int(val) && !(val % 1024) ) { |
3812 | 149 | result=$(echo "$1 $size ${2:-0}" | $AP_AWK '{printf "%." $3 "f", $1 / $2}') | 586 | val /= 1024; |
3813 | 150 | echo "${result}${unit}" | 587 | } |
3814 | 588 | |||
3815 | 589 | while ( val > 1000 ) { | ||
3816 | 590 | val /= div; | ||
3817 | 591 | } | ||
3818 | 592 | |||
3819 | 593 | printf "%.*f%s", prec, val, unit; | ||
3820 | 594 | } | ||
3821 | 595 | ' | ||
3822 | 151 | } | 596 | } |
3823 | 152 | 597 | ||
3824 | 153 | # Collapse a file into an aggregated list; file must be created with 'sort | | ||
3825 | 154 | # uniq -c'. This function is copy-pasted from 'summary' so see there for full | ||
3826 | 155 | # docs and tests. | ||
3827 | 156 | # ############################################################################## | ||
3828 | 157 | group_concat () { | 598 | group_concat () { |
3833 | 158 | sed -e '{H; $!d}' -e 'x' -e 's/\n[[:space:]]*\([[:digit:]]*\)[[:space:]]*/, \1x/g' -e 's/[[:space:]][[:space:]]*/ /g' -e 's/, //' ${1} | 599 | sed -e '{H; $!d;}' -e 'x' -e 's/\n[[:space:]]*\([[:digit:]]*\)[[:space:]]*/, \1x/g' -e 's/[[:space:]][[:space:]]*/ /g' -e 's/, //' "${1}" |
3834 | 159 | } | 600 | } |
3835 | 160 | 601 | ||
3836 | 161 | # Accepts a number of seconds, and outputs a d+h:m:s formatted string | 602 | # ########################################################################### |
3837 | 603 | # End report_formatting package | ||
3838 | 604 | # ########################################################################### | ||
3839 | 605 | |||
3840 | 606 | # ########################################################################### | ||
3841 | 607 | # summary_common package | ||
3842 | 608 | # This package is a copy without comments from the original. The original | ||
3843 | 609 | # with comments and its test file can be found in the Bazaar repository at, | ||
3844 | 610 | # lib/bash/summary_common.sh | ||
3845 | 611 | # t/lib/bash/summary_common.sh | ||
3846 | 612 | # See https://launchpad.net/percona-toolkit for more information. | ||
3847 | 613 | # ########################################################################### | ||
3848 | 614 | |||
3849 | 615 | |||
3850 | 616 | set -u | ||
3851 | 617 | |||
3852 | 618 | CMD_FILE="$( _which file 2>/dev/null )" | ||
3853 | 619 | CMD_NM="$( _which nm 2>/dev/null )" | ||
3854 | 620 | CMD_OBJDUMP="$( _which objdump 2>/dev/null )" | ||
3855 | 621 | |||
3856 | 622 | get_nice_of_pid () { | ||
3857 | 623 | local pid="$1" | ||
3858 | 624 | local niceness="$(ps -p $pid -o nice | awk '$1 !~ /[^0-9]/ {print $1; exit}')" | ||
3859 | 625 | |||
3860 | 626 | if [ -n "${niceness}" ]; then | ||
3861 | 627 | echo $niceness | ||
3862 | 628 | else | ||
3863 | 629 | local tmpfile="$TMPDIR/nice_through_c.tmp.c" | ||
3864 | 630 | _d "Getting the niceness from ps failed, somehow. We are about to try this:" | ||
3865 | 631 | cat <<EOC > "$tmpfile" | ||
3866 | 632 | |||
3867 | 633 | int main(void) { | ||
3868 | 634 | int priority = getpriority(PRIO_PROCESS, $pid); | ||
3869 | 635 | if ( priority == -1 && errno == ESRCH ) { | ||
3870 | 636 | return 1; | ||
3871 | 637 | } | ||
3872 | 638 | else { | ||
3873 | 639 | printf("%d\\n", priority); | ||
3874 | 640 | return 0; | ||
3875 | 641 | } | ||
3876 | 642 | } | ||
3877 | 643 | |||
3878 | 644 | EOC | ||
3879 | 645 | local c_comp=$(_which gcc) | ||
3880 | 646 | if [ -z "${c_comp}" ]; then | ||
3881 | 647 | c_comp=$(_which cc) | ||
3882 | 648 | fi | ||
3883 | 649 | _d "$tmpfile: $( cat "$tmpfile" )" | ||
3884 | 650 | _d "$c_comp -xc \"$tmpfile\" -o \"$tmpfile\" && eval \"$tmpfile\"" | ||
3885 | 651 | $c_comp -xc "$tmpfile" -o "$tmpfile" 2>/dev/null && eval "$tmpfile" 2>/dev/null | ||
3886 | 652 | if [ $? -ne 0 ]; then | ||
3887 | 653 | echo "?" | ||
3888 | 654 | _d "Failed to get a niceness value for $pid" | ||
3889 | 655 | fi | ||
3890 | 656 | fi | ||
3891 | 657 | } | ||
3892 | 658 | |||
3893 | 659 | get_oom_of_pid () { | ||
3894 | 660 | local pid="$1" | ||
3895 | 661 | local oom_adj="" | ||
3896 | 662 | |||
3897 | 663 | if [ -n "${pid}" -a -e /proc/cpuinfo ]; then | ||
3898 | 664 | if [ -s "/proc/$pid/oom_score_adj" ]; then | ||
3899 | 665 | oom_adj=$(cat "/proc/$pid/oom_score_adj" 2>/dev/null) | ||
3900 | 666 | _d "For $pid, the oom value is $oom_adj, retreived from oom_score_adj" | ||
3901 | 667 | else | ||
3902 | 668 | oom_adj=$(cat "/proc/$pid/oom_adj" 2>/dev/null) | ||
3903 | 669 | _d "For $pid, the oom value is $oom_adj, retreived from oom_adj" | ||
3904 | 670 | fi | ||
3905 | 671 | fi | ||
3906 | 672 | |||
3907 | 673 | if [ -n "${oom_adj}" ]; then | ||
3908 | 674 | echo "${oom_adj}" | ||
3909 | 675 | else | ||
3910 | 676 | echo "?" | ||
3911 | 677 | _d "Can't find the oom value for $pid" | ||
3912 | 678 | fi | ||
3913 | 679 | } | ||
3914 | 680 | |||
3915 | 681 | has_symbols () { | ||
3916 | 682 | local executable="$(_which "$1")" | ||
3917 | 683 | local has_symbols="" | ||
3918 | 684 | |||
3919 | 685 | if [ "${CMD_FILE}" ] \ | ||
3920 | 686 | && [ "$($CMD_FILE "${executable}" | grep 'not stripped' )" ]; then | ||
3921 | 687 | has_symbols=1 | ||
3922 | 688 | elif [ "${CMD_NM}" ] \ | ||
3923 | 689 | || [ "${CMD_OBJDMP}" ]; then | ||
3924 | 690 | if [ "${CMD_NM}" ] \ | ||
3925 | 691 | && [ !"$("${CMD_NM}" -- "${executable}" 2>&1 | grep 'File format not recognized' )" ]; then | ||
3926 | 692 | if [ -z "$( $CMD_NM -- "${executable}" 2>&1 | grep ': no symbols' )" ]; then | ||
3927 | 693 | has_symbols=1 | ||
3928 | 694 | fi | ||
3929 | 695 | elif [ -z "$("${CMD_OBJDUMP}" -t -- "${executable}" | grep '^no symbols$' )" ]; then | ||
3930 | 696 | has_symbols=1 | ||
3931 | 697 | fi | ||
3932 | 698 | fi | ||
3933 | 699 | |||
3934 | 700 | if [ "${has_symbols}" ]; then | ||
3935 | 701 | echo "Yes" | ||
3936 | 702 | else | ||
3937 | 703 | echo "No" | ||
3938 | 704 | fi | ||
3939 | 705 | } | ||
3940 | 706 | |||
3941 | 707 | setup_data_dir () { | ||
3942 | 708 | local existing_dir="$1" | ||
3943 | 709 | local data_dir="" | ||
3944 | 710 | if [ -z "$existing_dir" ]; then | ||
3945 | 711 | mkdir "$TMPDIR/data" || die "Cannot mkdir $TMPDIR/data" | ||
3946 | 712 | data_dir="$TMPDIR/data" | ||
3947 | 713 | else | ||
3948 | 714 | if [ ! -d "$existing_dir" ]; then | ||
3949 | 715 | mkdir "$existing_dir" || die "Cannot mkdir $existing_dir" | ||
3950 | 716 | elif [ "$( ls -A "$existing_dir" )" ]; then | ||
3951 | 717 | die "--save-samples directory isn't empty, halting." | ||
3952 | 718 | fi | ||
3953 | 719 | touch "$existing_dir/test" || die "Cannot write to $existing_dir" | ||
3954 | 720 | rm "$existing_dir/test" || die "Cannot rm $existing_dir/test" | ||
3955 | 721 | data_dir="$existing_dir" | ||
3956 | 722 | fi | ||
3957 | 723 | echo "$data_dir" | ||
3958 | 724 | } | ||
3959 | 725 | |||
3960 | 726 | get_var () { | ||
3961 | 727 | local varname="$1" | ||
3962 | 728 | local file="$2" | ||
3963 | 729 | awk -v pattern="${varname}" '$1 == pattern { if (length($2)) { len = length($1); print substr($0, len+index(substr($0, len+1), $2)) } }' "${file}" | ||
3964 | 730 | } | ||
3965 | 731 | |||
3966 | 732 | # ########################################################################### | ||
3967 | 733 | # End summary_common package | ||
3968 | 734 | # ########################################################################### | ||
3969 | 735 | |||
3970 | 736 | # ########################################################################### | ||
3971 | 737 | # collect_mysql_info package | ||
3972 | 738 | # This package is a copy without comments from the original. The original | ||
3973 | 739 | # with comments and its test file can be found in the Bazaar repository at, | ||
3974 | 740 | # lib/bash/collect_mysql_info.sh | ||
3975 | 741 | # t/lib/bash/collect_mysql_info.sh | ||
3976 | 742 | # See https://launchpad.net/percona-toolkit for more information. | ||
3977 | 743 | # ########################################################################### | ||
3978 | 744 | |||
3979 | 745 | |||
3980 | 746 | |||
3981 | 747 | CMD_MYSQL="${CMD_MYSQL:-""}" | ||
3982 | 748 | CMD_MYSQLDUMP="${CMD_MYSQLDUMP:-""}" | ||
3983 | 749 | |||
3984 | 750 | collect_mysqld_instances () { | ||
3985 | 751 | local variables_file="$1" | ||
3986 | 752 | |||
3987 | 753 | local pids="$(_pidof mysqld)" | ||
3988 | 754 | |||
3989 | 755 | if [ -n "$pids" ]; then | ||
3990 | 756 | |||
3991 | 757 | for pid in $pids; do | ||
3992 | 758 | local nice="$( get_nice_of_pid $pid )" | ||
3993 | 759 | local oom="$( get_oom_of_pid $pid )" | ||
3994 | 760 | echo "internal::nice_of_$pid $nice" >> "$variables_file" | ||
3995 | 761 | echo "internal::oom_of_$pid $oom" >> "$variables_file" | ||
3996 | 762 | done | ||
3997 | 763 | |||
3998 | 764 | pids="$(echo $pids | sed -e 's/ /,/g')" | ||
3999 | 765 | ps ww -p "$pids" 2>/dev/null | ||
4000 | 766 | else | ||
4001 | 767 | echo "mysqld doesn't appear to be running" | ||
4002 | 768 | fi | ||
4003 | 769 | |||
4004 | 770 | } | ||
4005 | 771 | |||
4006 | 772 | find_my_cnf_file() { | ||
4007 | 773 | local file="$1" | ||
4008 | 774 | local port="${2:-""}" | ||
4009 | 775 | |||
4010 | 776 | local cnf_file="" | ||
4011 | 777 | if test -n "$port" && grep -- "/mysqld.*--port=$port" "${file}" >/dev/null 2>&1 ; then | ||
4012 | 778 | cnf_file="$(grep -- "/mysqld.*--port=$port" "${file}" \ | ||
4013 | 779 | | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \ | ||
4014 | 780 | | head -n1)" | ||
4015 | 781 | else | ||
4016 | 782 | cnf_file="$(grep '/mysqld' "${file}" \ | ||
4017 | 783 | | awk 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \ | ||
4018 | 784 | | head -n1)" | ||
4019 | 785 | fi | ||
4020 | 786 | |||
4021 | 787 | if [ ! -n "${cnf_file}" ]; then | ||
4022 | 788 | cnf_file="/etc/my.cnf"; | ||
4023 | 789 | if [ ! -e "${cnf_file}" ]; then | ||
4024 | 790 | cnf_file="/etc/mysql/my.cnf"; | ||
4025 | 791 | fi | ||
4026 | 792 | if [ ! -e "${cnf_file}" ]; then | ||
4027 | 793 | cnf_file="/var/db/mysql/my.cnf"; | ||
4028 | 794 | fi | ||
4029 | 795 | fi | ||
4030 | 796 | |||
4031 | 797 | echo "$cnf_file" | ||
4032 | 798 | } | ||
4033 | 799 | |||
4034 | 800 | collect_mysql_variables () { | ||
4035 | 801 | $CMD_MYSQL $EXT_ARGV -ss -e 'SHOW /*!40100 GLOBAL*/ VARIABLES' | ||
4036 | 802 | } | ||
4037 | 803 | |||
4038 | 804 | collect_mysql_status () { | ||
4039 | 805 | $CMD_MYSQL $EXT_ARGV -ss -e 'SHOW /*!50000 GLOBAL*/ STATUS' | ||
4040 | 806 | } | ||
4041 | 807 | |||
4042 | 808 | collect_mysql_databases () { | ||
4043 | 809 | $CMD_MYSQL $EXT_ARGV -ss -e 'SHOW DATABASES' 2>/dev/null | ||
4044 | 810 | } | ||
4045 | 811 | |||
4046 | 812 | collect_mysql_plugins () { | ||
4047 | 813 | $CMD_MYSQL $EXT_ARGV -ss -e 'SHOW PLUGINS' 2>/dev/null | ||
4048 | 814 | } | ||
4049 | 815 | |||
4050 | 816 | collect_mysql_slave_status () { | ||
4051 | 817 | $CMD_MYSQL $EXT_ARGV -ssE -e 'SHOW SLAVE STATUS' 2>/dev/null | ||
4052 | 818 | } | ||
4053 | 819 | |||
4054 | 820 | collect_mysql_innodb_status () { | ||
4055 | 821 | $CMD_MYSQL $EXT_ARGV -ssE -e 'SHOW /*!50000 ENGINE*/ INNODB STATUS' 2>/dev/null | ||
4056 | 822 | } | ||
4057 | 823 | |||
4058 | 824 | collect_mysql_processlist () { | ||
4059 | 825 | $CMD_MYSQL $EXT_ARGV -ssE -e 'SHOW FULL PROCESSLIST' 2>/dev/null | ||
4060 | 826 | } | ||
4061 | 827 | |||
4062 | 828 | collect_mysql_users () { | ||
4063 | 829 | $CMD_MYSQL $EXT_ARGV -ss -e 'SELECT COUNT(*), SUM(user=""), SUM(password=""), SUM(password NOT LIKE "*%") FROM mysql.user' 2>/dev/null | ||
4064 | 830 | } | ||
4065 | 831 | |||
4066 | 832 | collect_master_logs_status () { | ||
4067 | 833 | local master_logs_file="$1" | ||
4068 | 834 | local master_status_file="$2" | ||
4069 | 835 | $CMD_MYSQL $EXT_ARGV -ss -e 'SHOW MASTER LOGS' > "$master_logs_file" 2>/dev/null | ||
4070 | 836 | $CMD_MYSQL $EXT_ARGV -ss -e 'SHOW MASTER STATUS' > "$master_status_file" 2>/dev/null | ||
4071 | 837 | } | ||
4072 | 838 | |||
4073 | 839 | collect_mysql_deferred_status () { | ||
4074 | 840 | local status_file="$1" | ||
4075 | 841 | collect_mysql_status > "$TMPDIR/defer_gatherer" | ||
4076 | 842 | join "$status_file" "$TMPDIR/defer_gatherer" | ||
4077 | 843 | } | ||
4078 | 844 | |||
4079 | 845 | collect_internal_vars () { | ||
4080 | 846 | local mysqld_executables="${1:-""}" | ||
4081 | 847 | |||
4082 | 848 | local FNV_64="" | ||
4083 | 849 | if $CMD_MYSQL $EXT_ARGV -e 'SELECT FNV_64("a")' >/dev/null 2>&1; then | ||
4084 | 850 | FNV_64="Enabled"; | ||
4085 | 851 | else | ||
4086 | 852 | FNV_64="Unknown"; | ||
4087 | 853 | fi | ||
4088 | 854 | |||
4089 | 855 | local now="$($CMD_MYSQL $EXT_ARGV -ss -e 'SELECT NOW()')" | ||
4090 | 856 | local user="$($CMD_MYSQL $EXT_ARGV -ss -e 'SELECT CURRENT_USER()')" | ||
4091 | 857 | local trigger_count=$($CMD_MYSQL $EXT_ARGV -ss -e "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TRIGGERS" 2>/dev/null) | ||
4092 | 858 | |||
4093 | 859 | echo "pt-summary-internal-mysql_executable $CMD_MYSQL" | ||
4094 | 860 | echo "pt-summary-internal-now $now" | ||
4095 | 861 | echo "pt-summary-internal-user $user" | ||
4096 | 862 | echo "pt-summary-internal-FNV_64 $FNV_64" | ||
4097 | 863 | echo "pt-summary-internal-trigger_count $trigger_count" | ||
4098 | 864 | |||
4099 | 865 | if [ -e "$mysqld_executables" ]; then | ||
4100 | 866 | local i=1 | ||
4101 | 867 | while read executable; do | ||
4102 | 868 | echo "pt-summary-internal-mysqld_executable_${i} $(has_symbols "$executable")" | ||
4103 | 869 | i=$(($i + 1)) | ||
4104 | 870 | done < "$mysqld_executables" | ||
4105 | 871 | fi | ||
4106 | 872 | } | ||
4107 | 873 | |||
4108 | 874 | get_mysqldump_for () { | ||
4109 | 875 | local args="$1" | ||
4110 | 876 | local dbtodump="${2:-"--all-databases"}" | ||
4111 | 877 | |||
4112 | 878 | $CMD_MYSQLDUMP $EXT_ARGV --no-data --skip-comments \ | ||
4113 | 879 | --skip-add-locks --skip-add-drop-table --compact \ | ||
4114 | 880 | --skip-lock-all-tables --skip-lock-tables --skip-set-charset \ | ||
4115 | 881 | ${args} --databases $( local IFS=,; echo ${dbtodump}) | ||
4116 | 882 | } | ||
4117 | 883 | |||
4118 | 884 | get_mysqldump_args () { | ||
4119 | 885 | local file="$1" | ||
4120 | 886 | local trg_arg="" | ||
4121 | 887 | |||
4122 | 888 | if $CMD_MYSQLDUMP --help --verbose 2>&1 | grep triggers >/dev/null; then | ||
4123 | 889 | trg_arg="--routines" | ||
4124 | 890 | fi | ||
4125 | 891 | |||
4126 | 892 | if [ "${trg_arg}" ]; then | ||
4127 | 893 | local triggers="--skip-triggers" | ||
4128 | 894 | local trg=$(get_var "pt-summary-internal-trigger_count" "$file" ) | ||
4129 | 895 | if [ -n "${trg}" ] && [ "${trg}" -gt 0 ]; then | ||
4130 | 896 | triggers="--triggers" | ||
4131 | 897 | fi | ||
4132 | 898 | trg_arg="${trg_arg} ${triggers}"; | ||
4133 | 899 | fi | ||
4134 | 900 | echo "${trg_arg}" | ||
4135 | 901 | } | ||
4136 | 902 | |||
4137 | 903 | collect_mysqld_executables () { | ||
4138 | 904 | local mysqld_instances="$1" | ||
4139 | 905 | |||
4140 | 906 | for pid in $( grep '/mysqld' "$mysqld_instances" | awk '/^ .*[0-9]/{print $1}' ); do | ||
4141 | 907 | ps -o cmd -p $pid | sed -e 's/^\(.*mysqld\) .*/\1/' | grep -v '^CMD$' | ||
4142 | 908 | done | sort -u | ||
4143 | 909 | } | ||
4144 | 910 | |||
4145 | 911 | collect_mysql_info () { | ||
4146 | 912 | local dir="$1" | ||
4147 | 913 | |||
4148 | 914 | collect_mysql_variables > "$dir/mysql-variables" | ||
4149 | 915 | collect_mysql_status > "$dir/mysql-status" | ||
4150 | 916 | collect_mysql_databases > "$dir/mysql-databases" | ||
4151 | 917 | collect_mysql_plugins > "$dir/mysql-plugins" | ||
4152 | 918 | collect_mysql_slave_status > "$dir/mysql-slave" | ||
4153 | 919 | collect_mysql_innodb_status > "$dir/innodb-status" | ||
4154 | 920 | collect_mysql_processlist > "$dir/mysql-processlist" | ||
4155 | 921 | collect_mysql_users > "$dir/mysql-users" | ||
4156 | 922 | |||
4157 | 923 | collect_mysqld_instances "$dir/mysql-variables" > "$dir/mysqld-instances" | ||
4158 | 924 | collect_mysqld_executables "$dir/mysqld-instances" > "$dir/mysqld-executables" | ||
4159 | 925 | |||
4160 | 926 | local binlog="$(get_var log_bin "$dir/mysql-variables")" | ||
4161 | 927 | if [ "${binlog}" ]; then | ||
4162 | 928 | collect_master_logs_status "$dir/mysql-master-logs" "$dir/mysql-master-status" | ||
4163 | 929 | fi | ||
4164 | 930 | |||
4165 | 931 | local uptime="$(get_var Uptime "$dir/mysql-status")" | ||
4166 | 932 | local current_time="$($CMD_MYSQL $EXT_ARGV -ss -e \ | ||
4167 | 933 | "SELECT LEFT(NOW() - INTERVAL ${uptime} SECOND, 16)")" | ||
4168 | 934 | |||
4169 | 935 | local port="$(get_var port "$dir/mysql-variables")" | ||
4170 | 936 | local cnf_file="$(find_my_cnf_file "$dir/mysqld-instances" ${port})" | ||
4171 | 937 | |||
4172 | 938 | cat "$cnf_file" > "$dir/mysql-config-file" | ||
4173 | 939 | |||
4174 | 940 | local pid_file="$(get_var "pid_file" "$dir/mysql-variables")" | ||
4175 | 941 | local pid_file_exists="" | ||
4176 | 942 | [ -e "${pid_file}" ] && pid_file_exists=1 | ||
4177 | 943 | echo "pt-summary-internal-pid_file_exists $pid_file_exists" >> "$dir/mysql-variables" | ||
4178 | 944 | |||
4179 | 945 | echo "pt-summary-internal-current_time $current_time" >> "$dir/mysql-variables" | ||
4180 | 946 | echo "pt-summary-internal-Config_File_path $cnf_file" >> "$dir/mysql-variables" | ||
4181 | 947 | collect_internal_vars "$dir/mysqld-executables" >> "$dir/mysql-variables" | ||
4182 | 948 | |||
4183 | 949 | if [ -n "${OPT_DATABASES}" ]; then | ||
4184 | 950 | local trg_arg="$( get_mysqldump_args "$dir/mysql-variables" )" | ||
4185 | 951 | get_mysqldump_for "${trg_arg}" "${OPT_DATABASES}" > "$dir/mysqldump" | ||
4186 | 952 | fi | ||
4187 | 953 | |||
4188 | 954 | ( | ||
4189 | 955 | sleep $OPT_SLEEP | ||
4190 | 956 | collect_mysql_deferred_status "$dir/mysql-status" > "$dir/mysql-status-defer" | ||
4191 | 957 | ) & | ||
4192 | 958 | _d "Forked child is $!" | ||
4193 | 959 | } | ||
4194 | 960 | |||
4195 | 961 | # ########################################################################### | ||
4196 | 962 | # End collect_mysql_info package | ||
4197 | 963 | # ########################################################################### | ||
4198 | 964 | |||
4199 | 965 | # ########################################################################### | ||
4200 | 966 | # report_mysql_info package | ||
4201 | 967 | # This package is a copy without comments from the original. The original | ||
4202 | 968 | # with comments and its test file can be found in the Bazaar repository at, | ||
4203 | 969 | # lib/bash/report_mysql_info.sh | ||
4204 | 970 | # t/lib/bash/report_mysql_info.sh | ||
4205 | 971 | # See https://launchpad.net/percona-toolkit for more information. | ||
4206 | 972 | # ########################################################################### | ||
4207 | 973 | |||
4208 | 974 | |||
4209 | 975 | set -u | ||
4210 | 976 | POSIXLY_CORRECT=1 | ||
4211 | 977 | |||
4212 | 162 | secs_to_time () { | 978 | secs_to_time () { |
4215 | 163 | echo "$1" | $AP_AWK '{ | 979 | awk -v sec="$1" 'BEGIN { |
4216 | 164 | printf( "%d+%02d:%02d:%02d", $1 / 86400, ($1 % 86400) / 3600, ($1 % 3600) / 60, $1 % 60); | 980 | printf( "%d+%02d:%02d:%02d", sec / 86400, (sec % 86400) / 3600, (sec % 3600) / 60, sec % 60); |
4217 | 165 | }' | 981 | }' |
4218 | 166 | } | 982 | } |
4219 | 167 | 983 | ||
4220 | 168 | # gets a value from $TMPDIR/percona-toolkit-mysql-variables. Returns zero if it doesn't | ||
4221 | 169 | # exist. | ||
4222 | 170 | get_var () { | ||
4223 | 171 | v="$($AP_AWK "\$1 ~ /^$1$/ { print \$2 }" $TMPDIR/percona-toolkit-mysql-variables)" | ||
4224 | 172 | echo "${v:-0}" | ||
4225 | 173 | } | ||
4226 | 174 | |||
4227 | 175 | # Returns true if a variable exists | ||
4228 | 176 | var_exists () { | ||
4229 | 177 | $AP_GREP "$1" $TMPDIR/percona-toolkit-mysql-variables >/dev/null 2>&1; | ||
4230 | 178 | } | ||
4231 | 179 | |||
4232 | 180 | # Returns "Enabled", "Disabled", or "Not Supported" depending on whether the | ||
4233 | 181 | # variable exists and is ON or enabled. You can pass 2nd and 3rd variables to | ||
4234 | 182 | # control whether the variable should be 'gt' (numeric greater than) or 'eq' | ||
4235 | 183 | # (string equal) to some value. | ||
4236 | 184 | feat_on() { | 984 | feat_on() { |
4239 | 185 | if var_exists $1 ; then | 985 | local file="$1" |
4240 | 186 | var="$($AP_AWK "\$1 ~ /^$1$/ { print \$2 }" $TMPDIR/percona-toolkit-mysql-variables)" | 986 | local varname="$2" |
4241 | 987 | [ -e "$file" ] || return | ||
4242 | 988 | |||
4243 | 989 | if [ "$( get_var "$varname" "${file}" )" ]; then | ||
4244 | 990 | local var="$(awk "\$1 ~ /^$2$/ { print \$2 }" $file)" | ||
4245 | 187 | if [ "${var}" = "ON" ]; then | 991 | if [ "${var}" = "ON" ]; then |
4246 | 188 | echo "Enabled" | 992 | echo "Enabled" |
4247 | 189 | elif [ "${var}" = "OFF" -o "${var}" = "0" -o -z "${var}" ]; then | 993 | elif [ "${var}" = "OFF" -o "${var}" = "0" -o -z "${var}" ]; then |
4248 | 190 | echo "Disabled" | 994 | echo "Disabled" |
4251 | 191 | elif [ "$2" = "ne" ]; then | 995 | elif [ "$3" = "ne" ]; then |
4252 | 192 | if [ "${var}" != "$3" ]; then | 996 | if [ "${var}" != "$4" ]; then |
4253 | 193 | echo "Enabled" | 997 | echo "Enabled" |
4254 | 194 | else | 998 | else |
4255 | 195 | echo "Disabled" | 999 | echo "Disabled" |
4256 | 196 | fi | 1000 | fi |
4259 | 197 | elif [ "$2" = "gt" ]; then | 1001 | elif [ "$3" = "gt" ]; then |
4260 | 198 | if [ "${var}" -gt "$3" ]; then | 1002 | if [ "${var}" -gt "$4" ]; then |
4261 | 199 | echo "Enabled" | 1003 | echo "Enabled" |
4262 | 200 | else | 1004 | else |
4263 | 201 | echo "Disabled" | 1005 | echo "Disabled" |
4264 | @@ -210,128 +1014,124 @@ | |||
4265 | 210 | fi | 1014 | fi |
4266 | 211 | } | 1015 | } |
4267 | 212 | 1016 | ||
4297 | 213 | # gets a value from $TMPDIR/percona-toolkit-mysql-status. Returns zero if it doesn't | 1017 | get_table_cache () { |
4298 | 214 | # exist. | 1018 | local file="$1" |
4299 | 215 | get_stat () { | 1019 | |
4300 | 216 | v="$($AP_AWK "\$1 ~ /^$1$/ { print \$2 }" $TMPDIR/percona-toolkit-mysql-status)" | 1020 | [ -e "$file" ] || return |
4301 | 217 | echo "${v:-0}" | 1021 | |
4302 | 218 | } | 1022 | local table_cache="" |
4303 | 219 | 1023 | if [ "$( get_var table_open_cache "${file}" )" ]; then | |
4304 | 220 | # Does fuzzy rounding: rounds to nearest interval, but the interval gets larger | 1024 | table_cache="$(get_var table_open_cache "${file}")" |
4305 | 221 | # as the number gets larger. This is to make things easier to diff. | 1025 | else |
4306 | 222 | fuzz () { | 1026 | table_cache="$(get_var table_cache "${file}")" |
4307 | 223 | echo $1 | $AP_AWK "{fuzzy_var=\$1; ${fuzzy_formula} print fuzzy_var;}" | 1027 | fi |
4308 | 224 | } | 1028 | echo ${table_cache:-0} |
4309 | 225 | 1029 | } | |
4310 | 226 | # Fuzzy computes the percent that $1 is of $2 | 1030 | |
4311 | 227 | fuzzy_pct () { | 1031 | get_plugin_status () { |
4312 | 228 | pct=$(echo $1 $2 | $AP_AWK '{ if ($2 > 0) { printf "%d", $1/$2*100; } else {print 0} }'); | 1032 | local file="$1" |
4313 | 229 | echo "$(fuzz ${pct})%" | 1033 | local plugin="$2" |
4314 | 230 | } | 1034 | |
4315 | 231 | 1035 | local status="$(grep -w "$plugin" "$file" | awk '{ print $2 }')" | |
4316 | 232 | # ############################################################################## | 1036 | |
4317 | 233 | # Functions for parsing specific files and getting desired info from them. | 1037 | echo ${status:-"Not found"} |
4318 | 234 | # These are called from within main() and are separated so they can be tested | 1038 | } |
4319 | 235 | # easily. The calling convention is that the data they need to run is prepared | 1039 | |
4320 | 236 | # first by putting it into $TMPDIR/percona-toolkit. Then code that's testing | 1040 | |
4321 | 237 | # just needs to put sample data into $TMPDIR/percona-toolkit and call it. | 1041 | _NO_FALSE_NEGATIVES="" |
4293 | 238 | # ############################################################################## | ||
4294 | 239 | |||
4295 | 240 | # Parses the output of 'ps -e -o args | $AP_GREP mysqld' or 'ps auxww...' | ||
4296 | 241 | # which should be in $TMPDIR/percona-toolkit. | ||
4322 | 242 | parse_mysqld_instances () { | 1042 | parse_mysqld_instances () { |
4324 | 243 | local file=$1 | 1043 | local file="$1" |
4325 | 1044 | local variables_file="$2" | ||
4326 | 1045 | |||
4327 | 244 | local socket=${socket:-""} | 1046 | local socket=${socket:-""} |
4328 | 245 | local port=${port:-""} | 1047 | local port=${port:-""} |
4333 | 246 | local datadir=${datadir:-""} | 1048 | local datadir="${datadir:-""}" |
4334 | 247 | echo " Port Data Directory Socket" | 1049 | |
4335 | 248 | echo " ===== ========================== ======" | 1050 | [ -e "$file" ] || return |
4336 | 249 | $AP_GREP '/mysqld ' $file | while read line; do | 1051 | |
4337 | 1052 | echo " Port Data Directory Nice OOM Socket" | ||
4338 | 1053 | echo " ===== ========================== ==== === ======" | ||
4339 | 1054 | |||
4340 | 1055 | grep '/mysqld ' "$file" | while read line; do | ||
4341 | 1056 | local pid=$(echo "$line" | awk '{print $1;}') | ||
4342 | 250 | for word in ${line}; do | 1057 | for word in ${line}; do |
4346 | 251 | # Some grep doesn't have -o, so I have to pull out the words I want by | 1058 | if echo "${word}" | grep -- "--socket=" > /dev/null; then |
4344 | 252 | # looking at each word | ||
4345 | 253 | if echo "${word}" | $AP_GREP -- "--socket=" > /dev/null; then | ||
4347 | 254 | socket="$(echo "${word}" | cut -d= -f2)" | 1059 | socket="$(echo "${word}" | cut -d= -f2)" |
4348 | 255 | fi | 1060 | fi |
4350 | 256 | if echo "${word}" | $AP_GREP -- "--port=" > /dev/null; then | 1061 | if echo "${word}" | grep -- "--port=" > /dev/null; then |
4351 | 257 | port="$(echo "${word}" | cut -d= -f2)" | 1062 | port="$(echo "${word}" | cut -d= -f2)" |
4352 | 258 | fi | 1063 | fi |
4354 | 259 | if echo "${word}" | $AP_GREP -- "--datadir=" > /dev/null; then | 1064 | if echo "${word}" | grep -- "--datadir=" > /dev/null; then |
4355 | 260 | datadir="$(echo "${word}" | cut -d= -f2)" | 1065 | datadir="$(echo "${word}" | cut -d= -f2)" |
4356 | 261 | fi | 1066 | fi |
4357 | 262 | done | 1067 | done |
4359 | 263 | printf " %5s %-26s %s\n" "${port}" "${datadir}" "${socket}" | 1068 | local nice="$(get_var "internal::nice_of_$pid" "$variables_file")" |
4360 | 1069 | local oom="$(get_var "internal::oom_of_$pid" "$variables_file")" | ||
4361 | 1070 | if [ -n "${_NO_FALSE_NEGATIVES}" ]; then | ||
4362 | 1071 | nice="?" | ||
4363 | 1072 | oom="?" | ||
4364 | 1073 | fi | ||
4365 | 1074 | printf " %5s %-26s %-4s %-3s %s\n" "${port}" "${datadir}" "${nice:-"?"}" "${oom:-"?"}" "${socket}" | ||
4366 | 264 | done | 1075 | done |
4367 | 265 | } | 1076 | } |
4368 | 266 | 1077 | ||
4369 | 267 | # Tries to find the my.cnf file by examining 'ps' output, which should be in | ||
4370 | 268 | # $TMPDIR/percona-toolkit. You have to specify the port for the instance you are | ||
4371 | 269 | # interested in, in case there are multiple instances. | ||
4372 | 270 | find_my_cnf_file() { | ||
4373 | 271 | local file=$1 | ||
4374 | 272 | local port=${2:-""} | ||
4375 | 273 | if test -n "$port" && $AP_GREP -- "/mysqld.*--port=$port" $file >/dev/null 2>&1 ; then | ||
4376 | 274 | $AP_GREP -- "/mysqld.*--port=$port" $file \ | ||
4377 | 275 | | $AP_AWK 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \ | ||
4378 | 276 | | head -n1 | ||
4379 | 277 | else | ||
4380 | 278 | $AP_GREP '/mysqld' $file \ | ||
4381 | 279 | | $AP_AWK 'BEGIN{RS=" "; FS="=";} $1 ~ /--defaults-file/ { print $2; }' \ | ||
4382 | 280 | | head -n1 | ||
4383 | 281 | fi | ||
4384 | 282 | } | ||
4385 | 283 | |||
4386 | 284 | # Gets the MySQL system time. Uses input from $TMPDIR/percona-toolkit-mysql-variables. | ||
4387 | 285 | get_mysql_timezone () { | 1078 | get_mysql_timezone () { |
4389 | 286 | tz="$(get_var time_zone)" | 1079 | local file="$1" |
4390 | 1080 | |||
4391 | 1081 | [ -e "$file" ] || return | ||
4392 | 1082 | |||
4393 | 1083 | local tz="$(get_var time_zone "${file}")" | ||
4394 | 287 | if [ "${tz}" = "SYSTEM" ]; then | 1084 | if [ "${tz}" = "SYSTEM" ]; then |
4396 | 288 | tz="$(get_var system_time_zone)" | 1085 | tz="$(get_var system_time_zone "${file}")" |
4397 | 289 | fi | 1086 | fi |
4398 | 290 | echo "${tz}" | 1087 | echo "${tz}" |
4399 | 291 | } | 1088 | } |
4400 | 292 | 1089 | ||
4401 | 293 | # Gets the MySQL system version. Uses input from $TMPDIR/percona-toolkit-mysql-variables. | ||
4402 | 294 | get_mysql_version () { | 1090 | get_mysql_version () { |
4405 | 295 | name_val Version "$(get_var version) $(get_var version_comment)" | 1091 | local file="$1" |
4406 | 296 | name_val "Built On" "$(get_var version_compile_os) $(get_var version_compile_machine)" | 1092 | |
4407 | 1093 | name_val Version "$(get_var version "${file}") $(get_var version_comment "${file}")" | ||
4408 | 1094 | name_val "Built On" "$(get_var version_compile_os "${file}") $(get_var version_compile_machine "${file}")" | ||
4409 | 297 | } | 1095 | } |
4410 | 298 | 1096 | ||
4411 | 299 | # Gets the system start and uptime in human readable format. Last restart date | ||
4412 | 300 | # should be in $TMPDIR/percona-toolkit. | ||
4413 | 301 | get_mysql_uptime () { | 1097 | get_mysql_uptime () { |
4417 | 302 | local file=$1 | 1098 | local uptime="$1" |
4418 | 303 | restart="$(cat $file)" | 1099 | local restart="$2" |
4416 | 304 | uptime="$(get_stat Uptime)" | ||
4419 | 305 | uptime="$(secs_to_time ${uptime})" | 1100 | uptime="$(secs_to_time ${uptime})" |
4420 | 306 | echo "${restart} (up ${uptime})" | 1101 | echo "${restart} (up ${uptime})" |
4421 | 307 | } | 1102 | } |
4422 | 308 | 1103 | ||
4423 | 309 | # Summarizes the output of SHOW MASTER LOGS, which is in $TMPDIR/percona-toolkit | ||
4424 | 310 | summarize_binlogs () { | 1104 | summarize_binlogs () { |
4429 | 311 | local file=$1 | 1105 | local file="$1" |
4430 | 312 | name_val "Binlogs" $(wc -l $file) | 1106 | |
4431 | 313 | name_val "Zero-Sized" $($AP_GREP -c '\<0$' $file) | 1107 | [ -e "$file" ] || return |
4432 | 314 | size=$($AP_AWK '{t += $2} END{printf "%0.f\n", t}' $file) | 1108 | |
4433 | 1109 | local size="$(awk '{t += $2} END{printf "%0.f\n", t}' "$file")" | ||
4434 | 1110 | name_val "Binlogs" $(wc -l "$file") | ||
4435 | 1111 | name_val "Zero-Sized" $(grep -c '\<0$' "$file") | ||
4436 | 315 | name_val "Total Size" $(shorten ${size} 1) | 1112 | name_val "Total Size" $(shorten ${size} 1) |
4437 | 316 | } | 1113 | } |
4438 | 317 | 1114 | ||
4440 | 318 | # Print out binlog_do_db and binlog_ignore_db | 1115 | format_users () { |
4441 | 1116 | local file="$1" | ||
4442 | 1117 | [ -e "$file" ] || return | ||
4443 | 1118 | awk '{printf "%d users, %d anon, %d w/o pw, %d old pw\n", $1, $2, $3, $4}' "${file}" | ||
4444 | 1119 | } | ||
4445 | 1120 | |||
4446 | 319 | format_binlog_filters () { | 1121 | format_binlog_filters () { |
4450 | 320 | local file=$1 | 1122 | local file="$1" |
4451 | 321 | name_val "binlog_do_db" $(cut -f3 $file) | 1123 | [ -e "$file" ] || return |
4452 | 322 | name_val "binlog_ignore_db" $(cut -f4 $file) | 1124 | name_val "binlog_do_db" "$(cut -f3 "$file")" |
4453 | 1125 | name_val "binlog_ignore_db" "$(cut -f4 "$file")" | ||
4454 | 323 | } | 1126 | } |
4455 | 324 | 1127 | ||
4456 | 325 | # Takes as input a file that has two samples of SHOW STATUS, columnized next to | ||
4457 | 326 | # each other. These should be in $TMPDIR/percona-toolkit. Outputs fuzzy-ed numbers: | ||
4458 | 327 | # absolute, all-time per second, and per-second over the interval between the | ||
4459 | 328 | # samples. Omits any rows that are all zeroes. | ||
4460 | 329 | format_status_variables () { | 1128 | format_status_variables () { |
4466 | 330 | local file=$1 | 1129 | local file="$1" |
4467 | 331 | # First, figure out the intervals. | 1130 | [ -e "$file" ] || return |
4468 | 332 | utime1=$($AP_AWK '/Uptime /{print $2}' $file); | 1131 | |
4469 | 333 | utime2=$($AP_AWK '/Uptime /{print $3}' $file); | 1132 | utime1="$(awk '/Uptime /{print $2}' "$file")"; |
4470 | 334 | ${AP_AWK} " | 1133 | utime2="$(awk '/Uptime /{print $3}' "$file")"; |
4471 | 1134 | awk " | ||
4472 | 335 | BEGIN { | 1135 | BEGIN { |
4473 | 336 | utime1 = ${utime1}; | 1136 | utime1 = ${utime1}; |
4474 | 337 | utime2 = ${utime2}; | 1137 | utime2 = ${utime2}; |
4475 | @@ -367,28 +1167,22 @@ | |||
4476 | 367 | printf(format, \$1, perday, persec, nowsec); | 1167 | printf(format, \$1, perday, persec, nowsec); |
4477 | 368 | } | 1168 | } |
4478 | 369 | } | 1169 | } |
4480 | 370 | }" $file | 1170 | }" "$file" |
4481 | 371 | } | 1171 | } |
4482 | 372 | 1172 | ||
4483 | 373 | # Slices the processlist a bunch of different ways. The processlist should be | ||
4484 | 374 | # created with the \G flag so it's vertical. | ||
4485 | 375 | # The parsing is a bit awkward because different | ||
4486 | 376 | # versions of awk have limitations like "too many fields on line xyz". So we | ||
4487 | 377 | # use 'cut' to shorten the lines. We count all things into temporary variables | ||
4488 | 378 | # for each process in the processlist, and when we hit the Info: line which | ||
4489 | 379 | # ought to be the last line in the process, we decide what to do with the temp | ||
4490 | 380 | # variables. If we're summarizing Command, we count everything; otherwise, only | ||
4491 | 381 | # non-Sleep processes get counted towards the sum and max of Time. | ||
4492 | 382 | summarize_processlist () { | 1173 | summarize_processlist () { |
4494 | 383 | local file=$1 | 1174 | local file="$1" |
4495 | 1175 | |||
4496 | 1176 | [ -e "$file" ] || return | ||
4497 | 1177 | |||
4498 | 384 | for param in Command User Host db State; do | 1178 | for param in Command User Host db State; do |
4499 | 385 | echo | 1179 | echo |
4500 | 386 | printf ' %-30s %8s %7s %9s %9s\n' \ | 1180 | printf ' %-30s %8s %7s %9s %9s\n' \ |
4501 | 387 | "${param}" "COUNT(*)" Working "SUM(Time)" "MAX(Time)" | 1181 | "${param}" "COUNT(*)" Working "SUM(Time)" "MAX(Time)" |
4502 | 388 | echo " ------------------------------" \ | 1182 | echo " ------------------------------" \ |
4503 | 389 | "-------- ------- --------- ---------" | 1183 | "-------- ------- --------- ---------" |
4506 | 390 | cut -c1-80 $file \ | 1184 | cut -c1-80 "$file" \ |
4507 | 391 | | $AP_AWK " | 1185 | | awk " |
4508 | 392 | \$1 == \"${param}:\" { | 1186 | \$1 == \"${param}:\" { |
4509 | 393 | p = substr(\$0, index(\$0, \":\") + 2); | 1187 | p = substr(\$0, index(\$0, \":\") + 2); |
4510 | 394 | if ( index(p, \":\") > 0 ) { | 1188 | if ( index(p, \":\") > 0 ) { |
4511 | @@ -428,22 +1222,21 @@ | |||
4512 | 428 | echo | 1222 | echo |
4513 | 429 | } | 1223 | } |
4514 | 430 | 1224 | ||
4515 | 431 | # Pretty-prints the my.cnf file, which should be in $TMPDIR/percona-toolkit. It's super | ||
4516 | 432 | # annoying, but some *modern* versions of awk don't support POSIX character | ||
4517 | 433 | # sets in regular expressions, like [[:space:]] (looking at you, Debian). So | ||
4518 | 434 | # the below patterns contain [<space><tab>] and must remain that way. | ||
4519 | 435 | pretty_print_cnf_file () { | 1225 | pretty_print_cnf_file () { |
4522 | 436 | local file=$1 | 1226 | local file="$1" |
4523 | 437 | $AP_AWK ' | 1227 | |
4524 | 1228 | [ -e "$file" ] || return | ||
4525 | 1229 | |||
4526 | 1230 | awk ' | ||
4527 | 438 | BEGIN { | 1231 | BEGIN { |
4528 | 439 | FS="=" | 1232 | FS="=" |
4529 | 440 | } | 1233 | } |
4536 | 441 | /^ *[a-zA-Z[]/ { | 1234 | /^[ \t]*[a-zA-Z[]/ { |
4537 | 442 | if ($2) { | 1235 | if (length($2)) { |
4538 | 443 | gsub(/^[ ]*/, "", $1); | 1236 | gsub(/^[ \t]*/, "", $1); |
4539 | 444 | gsub(/^[ ]*/, "", $2); | 1237 | gsub(/^[ \t]*/, "", $2); |
4540 | 445 | gsub(/[ ]*$/, "", $1); | 1238 | gsub(/[ \t]*$/, "", $1); |
4541 | 446 | gsub(/[ ]*$/, "", $2); | 1239 | gsub(/[ \t]*$/, "", $2); |
4542 | 447 | printf("%-35s = %s\n", $1, $2); | 1240 | printf("%-35s = %s\n", $1, $2); |
4543 | 448 | } | 1241 | } |
4544 | 449 | else if ( $0 ~ /\[/ ) { | 1242 | else if ( $0 ~ /\[/ ) { |
4545 | @@ -453,11 +1246,12 @@ | |||
4546 | 453 | else { | 1246 | else { |
4547 | 454 | print $1; | 1247 | print $1; |
4548 | 455 | } | 1248 | } |
4550 | 456 | }' $file | 1249 | }' "$file" |
4551 | 457 | } | 1250 | } |
4552 | 458 | 1251 | ||
4553 | 459 | find_checkpoint_age() { | 1252 | find_checkpoint_age() { |
4555 | 460 | $AP_AWK ' | 1253 | local file="$1" |
4556 | 1254 | awk ' | ||
4557 | 461 | /Log sequence number/{ | 1255 | /Log sequence number/{ |
4558 | 462 | if ( $5 ) { | 1256 | if ( $5 ) { |
4559 | 463 | lsn = $5 + ($4 * 4294967296); | 1257 | lsn = $5 + ($4 * 4294967296); |
4560 | @@ -474,11 +1268,15 @@ | |||
4561 | 474 | print lsn - $4; | 1268 | print lsn - $4; |
4562 | 475 | } | 1269 | } |
4563 | 476 | } | 1270 | } |
4565 | 477 | ' "$@" | 1271 | ' "$file" |
4566 | 478 | } | 1272 | } |
4567 | 479 | 1273 | ||
4568 | 480 | find_pending_io_reads() { | 1274 | find_pending_io_reads() { |
4570 | 481 | $AP_AWK ' | 1275 | local file="$1" |
4571 | 1276 | |||
4572 | 1277 | [ -e "$file" ] || return | ||
4573 | 1278 | |||
4574 | 1279 | awk ' | ||
4575 | 482 | /Pending normal aio reads/ { | 1280 | /Pending normal aio reads/ { |
4576 | 483 | normal_aio_reads = substr($5, 1, index($5, ",")); | 1281 | normal_aio_reads = substr($5, 1, index($5, ",")); |
4577 | 484 | } | 1282 | } |
4578 | @@ -495,11 +1293,15 @@ | |||
4579 | 495 | printf "%d buf pool reads, %d normal AIO", reads, normal_aio_reads; | 1293 | printf "%d buf pool reads, %d normal AIO", reads, normal_aio_reads; |
4580 | 496 | printf ", %d ibuf AIO, %d preads", ibuf_aio_reads, preads; | 1294 | printf ", %d ibuf AIO, %d preads", ibuf_aio_reads, preads; |
4581 | 497 | } | 1295 | } |
4583 | 498 | ' "${1}" | 1296 | ' "${file}" |
4584 | 499 | } | 1297 | } |
4585 | 500 | 1298 | ||
4586 | 501 | find_pending_io_writes() { | 1299 | find_pending_io_writes() { |
4588 | 502 | $AP_AWK ' | 1300 | local file="$1" |
4589 | 1301 | |||
4590 | 1302 | [ -e "$file" ] || return | ||
4591 | 1303 | |||
4592 | 1304 | awk ' | ||
4593 | 503 | /aio writes/ { | 1305 | /aio writes/ { |
4594 | 504 | aio_writes = substr($NF, 1, index($NF, ",")); | 1306 | aio_writes = substr($NF, 1, index($NF, ",")); |
4595 | 505 | } | 1307 | } |
4596 | @@ -522,11 +1324,15 @@ | |||
4597 | 522 | END { | 1324 | END { |
4598 | 523 | printf "%d buf pool (%d LRU, %d flush list, %d page); %d AIO, %d sync, %d log IO (%d log, %d chkp); %d pwrites", lru + flush_list + single_page, lru, flush_list, single_page, aio_writes, sync_ios, log_ios, log_writes, chkp_writes, pwrites; | 1325 | printf "%d buf pool (%d LRU, %d flush list, %d page); %d AIO, %d sync, %d log IO (%d log, %d chkp); %d pwrites", lru + flush_list + single_page, lru, flush_list, single_page, aio_writes, sync_ios, log_ios, log_writes, chkp_writes, pwrites; |
4599 | 524 | } | 1326 | } |
4601 | 525 | ' "${1}" | 1327 | ' "${file}" |
4602 | 526 | } | 1328 | } |
4603 | 527 | 1329 | ||
4604 | 528 | find_pending_io_flushes() { | 1330 | find_pending_io_flushes() { |
4606 | 529 | $AP_AWK ' | 1331 | local file="$1" |
4607 | 1332 | |||
4608 | 1333 | [ -e "$file" ] || return | ||
4609 | 1334 | |||
4610 | 1335 | awk ' | ||
4611 | 530 | /Pending flushes/ { | 1336 | /Pending flushes/ { |
4612 | 531 | log_flushes = substr($5, 1, index($5, ";")); | 1337 | log_flushes = substr($5, 1, index($5, ";")); |
4613 | 532 | buf_pool = $NF; | 1338 | buf_pool = $NF; |
4614 | @@ -534,13 +1340,17 @@ | |||
4615 | 534 | END { | 1340 | END { |
4616 | 535 | printf "%d buf pool, %d log", buf_pool, log_flushes; | 1341 | printf "%d buf pool, %d log", buf_pool, log_flushes; |
4617 | 536 | } | 1342 | } |
4619 | 537 | ' "${1}" | 1343 | ' "${file}" |
4620 | 538 | } | 1344 | } |
4621 | 539 | 1345 | ||
4622 | 540 | summarize_undo_log_entries() { | 1346 | summarize_undo_log_entries() { |
4626 | 541 | $AP_GREP 'undo log entries' "$1" \ | 1347 | local file="$1" |
4627 | 542 | | $AP_SED -e 's/^.*undo log entries \([0-9]*\)/\1/' \ | 1348 | |
4628 | 543 | | $AP_AWK ' | 1349 | [ -e "$file" ] || return |
4629 | 1350 | |||
4630 | 1351 | grep 'undo log entries' "${file}" \ | ||
4631 | 1352 | | sed -e 's/^.*undo log entries \([0-9]*\)/\1/' \ | ||
4632 | 1353 | | awk ' | ||
4633 | 544 | { | 1354 | { |
4634 | 545 | count++; | 1355 | count++; |
4635 | 546 | sum += $1; | 1356 | sum += $1; |
4636 | @@ -554,7 +1364,11 @@ | |||
4637 | 554 | } | 1364 | } |
4638 | 555 | 1365 | ||
4639 | 556 | find_max_trx_time() { | 1366 | find_max_trx_time() { |
4641 | 557 | $AP_AWK ' | 1367 | local file="$1" |
4642 | 1368 | |||
4643 | 1369 | [ -e "$file" ] || return | ||
4644 | 1370 | |||
4645 | 1371 | awk ' | ||
4646 | 558 | BEGIN { | 1372 | BEGIN { |
4647 | 559 | max = 0; | 1373 | max = 0; |
4648 | 560 | } | 1374 | } |
4649 | @@ -570,59 +1384,69 @@ | |||
4650 | 570 | } | 1384 | } |
4651 | 571 | END { | 1385 | END { |
4652 | 572 | print max; | 1386 | print max; |
4657 | 573 | }' "$@" | 1387 | }' "${file}" |
4658 | 574 | } | 1388 | } |
4659 | 575 | 1389 | ||
4660 | 576 | # Summarizes various things about InnoDB status that are not easy to see by eye. | 1390 | find_transation_states () { |
4661 | 1391 | local file="$1" | ||
4662 | 1392 | local tmpfile="$TMPDIR/find_transation_states.tmp" | ||
4663 | 1393 | |||
4664 | 1394 | [ -e "$file" ] || return | ||
4665 | 1395 | |||
4666 | 1396 | awk -F, '/^---TRANSACTION/{print $2}' "${file}" \ | ||
4667 | 1397 | | sed -e 's/ [0-9]* sec.*//' \ | ||
4668 | 1398 | | sort \ | ||
4669 | 1399 | | uniq -c > "${tmpfile}" | ||
4670 | 1400 | group_concat "${tmpfile}" | ||
4671 | 1401 | } | ||
4672 | 1402 | |||
4673 | 577 | format_innodb_status () { | 1403 | format_innodb_status () { |
4674 | 578 | local file=$1 | 1404 | local file=$1 |
4676 | 579 | name_val "Checkpoint Age" $(shorten $(find_checkpoint_age "${file}")) | 1405 | |
4677 | 1406 | [ -e "$file" ] || return | ||
4678 | 1407 | |||
4679 | 1408 | name_val "Checkpoint Age" "$(shorten $(find_checkpoint_age "${file}") 0)" | ||
4680 | 580 | name_val "InnoDB Queue" "$(awk '/queries inside/{print}' "${file}")" | 1409 | name_val "InnoDB Queue" "$(awk '/queries inside/{print}' "${file}")" |
4681 | 581 | name_val "Oldest Transaction" "$(find_max_trx_time "${file}") Seconds"; | 1410 | name_val "Oldest Transaction" "$(find_max_trx_time "${file}") Seconds"; |
4684 | 582 | name_val "History List Len" $(awk '/History list length/{print $4}' "${file}") | 1411 | name_val "History List Len" "$(awk '/History list length/{print $4}' "${file}")" |
4685 | 583 | name_val "Read Views" $(awk '/read views open inside/{print $1}' "${file}") | 1412 | name_val "Read Views" "$(awk '/read views open inside/{print $1}' "${file}")" |
4686 | 584 | name_val "Undo Log Entries" "$(summarize_undo_log_entries "${file}")" | 1413 | name_val "Undo Log Entries" "$(summarize_undo_log_entries "${file}")" |
4687 | 585 | name_val "Pending I/O Reads" "$(find_pending_io_reads "${file}")" | 1414 | name_val "Pending I/O Reads" "$(find_pending_io_reads "${file}")" |
4688 | 586 | name_val "Pending I/O Writes" "$(find_pending_io_writes "${file}")" | 1415 | name_val "Pending I/O Writes" "$(find_pending_io_writes "${file}")" |
4689 | 587 | name_val "Pending I/O Flushes" "$(find_pending_io_flushes "${file}")" | 1416 | name_val "Pending I/O Flushes" "$(find_pending_io_flushes "${file}")" |
4694 | 588 | $AP_AWK -F, '/^---TRANSACTION/{print $2}' "${file}" \ | 1417 | name_val "Transaction States" "$(find_transation_states "${file}" )" |
4695 | 589 | | $AP_SED -e 's/ [0-9]* sec.*//' | sort | uniq -c > $TMPDIR/percona-toolkit2 | 1418 | if grep 'TABLE LOCK table' "${file}" >/dev/null ; then |
4692 | 590 | name_val "Transaction States" "$(group_concat $TMPDIR/percona-toolkit2)" | ||
4693 | 591 | if $AP_GREP 'TABLE LOCK table' "${file}" >/dev/null ; then | ||
4696 | 592 | echo "Tables Locked" | 1419 | echo "Tables Locked" |
4698 | 593 | $AP_AWK '/^TABLE LOCK table/{print $4}' "${file}" \ | 1420 | awk '/^TABLE LOCK table/{print $4}' "${file}" \ |
4699 | 594 | | sort | uniq -c | sort -rn | 1421 | | sort | uniq -c | sort -rn |
4700 | 595 | fi | 1422 | fi |
4702 | 596 | if $AP_GREP 'has waited at' "${file}" > /dev/null ; then | 1423 | if grep 'has waited at' "${file}" > /dev/null ; then |
4703 | 597 | echo "Semaphore Waits" | 1424 | echo "Semaphore Waits" |
4705 | 598 | $AP_GREP 'has waited at' "${file}" | cut -d' ' -f6-8 \ | 1425 | grep 'has waited at' "${file}" | cut -d' ' -f6-8 \ |
4706 | 599 | | sort | uniq -c | sort -rn | 1426 | | sort | uniq -c | sort -rn |
4707 | 600 | fi | 1427 | fi |
4709 | 601 | if $AP_GREP 'reserved it in mode' "${file}" > /dev/null; then | 1428 | if grep 'reserved it in mode' "${file}" > /dev/null; then |
4710 | 602 | echo "Semaphore Holders" | 1429 | echo "Semaphore Holders" |
4712 | 603 | $AP_AWK '/has reserved it in mode/{ | 1430 | awk '/has reserved it in mode/{ |
4713 | 604 | print substr($0, 1 + index($0, "("), index($0, ")") - index($0, "(") - 1); | 1431 | print substr($0, 1 + index($0, "("), index($0, ")") - index($0, "(") - 1); |
4714 | 605 | }' "${file}" | sort | uniq -c | sort -rn | 1432 | }' "${file}" | sort | uniq -c | sort -rn |
4715 | 606 | fi | 1433 | fi |
4717 | 607 | if $AP_GREP -e 'Mutex at' -e 'lock on' "${file}" >/dev/null 2>&1; then | 1434 | if grep -e 'Mutex at' -e 'lock on' "${file}" >/dev/null 2>&1; then |
4718 | 608 | echo "Mutexes/Locks Waited For" | 1435 | echo "Mutexes/Locks Waited For" |
4720 | 609 | $AP_GREP -e 'Mutex at' -e 'lock on' "${file}" | $AP_SED -e 's/^[XS]-//' -e 's/,.*$//' \ | 1436 | grep -e 'Mutex at' -e 'lock on' "${file}" | sed -e 's/^[XS]-//' -e 's/,.*$//' \ |
4721 | 610 | | sort | uniq -c | sort -rn | 1437 | | sort | uniq -c | sort -rn |
4722 | 611 | fi | 1438 | fi |
4723 | 612 | } | 1439 | } |
4724 | 613 | 1440 | ||
4725 | 614 | # Summarizes per-database statistics for a bunch of different things: count of | ||
4726 | 615 | # tables, views, etc. $1 is the file name. $2 is the database name; if none, | ||
4727 | 616 | # then there should be multiple databases. | ||
4728 | 617 | format_overall_db_stats () { | 1441 | format_overall_db_stats () { |
4730 | 618 | local file=$1 | 1442 | local file="$1" |
4731 | 1443 | local tmpfile="$TMPDIR/format_overall_db_stats.tmp" | ||
4732 | 1444 | |||
4733 | 1445 | [ -e "$file" ] || return | ||
4734 | 1446 | |||
4735 | 619 | echo | 1447 | echo |
4740 | 620 | # We keep counts of everything in an associative array keyed by db name, and | 1448 | awk ' |
4737 | 621 | # what it is. The num_dbs counter is to ensure sort order is consistent when | ||
4738 | 622 | # we run the awk commands following this one. | ||
4739 | 623 | $AP_AWK ' | ||
4741 | 624 | BEGIN { | 1449 | BEGIN { |
4742 | 625 | # In case there is no USE statement in the file. | ||
4743 | 626 | db = "{chosen}"; | 1450 | db = "{chosen}"; |
4744 | 627 | num_dbs = 0; | 1451 | num_dbs = 0; |
4745 | 628 | } | 1452 | } |
4746 | @@ -634,7 +1458,6 @@ | |||
4747 | 634 | } | 1458 | } |
4748 | 635 | } | 1459 | } |
4749 | 636 | /^CREATE TABLE/ { | 1460 | /^CREATE TABLE/ { |
4750 | 637 | # Handle single-DB dumps, where there is no USE statement. | ||
4751 | 638 | if (num_dbs == 0) { | 1461 | if (num_dbs == 0) { |
4752 | 639 | num_dbs = 1; | 1462 | num_dbs = 1; |
4753 | 640 | db_seen[db] = 1; | 1463 | db_seen[db] = 1; |
4754 | @@ -674,15 +1497,13 @@ | |||
4755 | 674 | printf fmt, db, counts[db ",tables"], counts[db ",views"], counts[db ",sps"], counts[db ",trg"], counts[db ",func"], counts[db ",fk"], counts[db ",partn"]; | 1497 | printf fmt, db, counts[db ",tables"], counts[db ",views"], counts[db ",sps"], counts[db ",trg"], counts[db ",func"], counts[db ",fk"], counts[db ",partn"]; |
4756 | 675 | } | 1498 | } |
4757 | 676 | } | 1499 | } |
4761 | 677 | ' $file > $TMPDIR/percona-toolkit | 1500 | ' "$file" > "$tmpfile" |
4762 | 678 | head -n2 $TMPDIR/percona-toolkit | 1501 | head -n2 "$tmpfile" |
4763 | 679 | tail -n +3 $TMPDIR/percona-toolkit | sort | 1502 | tail -n +3 "$tmpfile" | sort |
4764 | 680 | 1503 | ||
4765 | 681 | echo | 1504 | echo |
4768 | 682 | # Now do the summary of engines per DB | 1505 | awk ' |
4767 | 683 | $AP_AWK ' | ||
4769 | 684 | BEGIN { | 1506 | BEGIN { |
4770 | 685 | # In case there is no USE statement in the file. | ||
4771 | 686 | db = "{chosen}"; | 1507 | db = "{chosen}"; |
4772 | 687 | num_dbs = 0; | 1508 | num_dbs = 0; |
4773 | 688 | num_engines = 0; | 1509 | num_engines = 0; |
4774 | @@ -695,7 +1516,6 @@ | |||
4775 | 695 | } | 1516 | } |
4776 | 696 | } | 1517 | } |
4777 | 697 | /^\) ENGINE=/ { | 1518 | /^\) ENGINE=/ { |
4778 | 698 | # Handle single-DB dumps, where there is no USE statement. | ||
4779 | 699 | if (num_dbs == 0) { | 1519 | if (num_dbs == 0) { |
4780 | 700 | num_dbs = 1; | 1520 | num_dbs = 1; |
4781 | 701 | db_seen[db] = 1; | 1521 | db_seen[db] = 1; |
4782 | @@ -734,16 +1554,13 @@ | |||
4783 | 734 | print ""; | 1554 | print ""; |
4784 | 735 | } | 1555 | } |
4785 | 736 | } | 1556 | } |
4789 | 737 | ' $file > $TMPDIR/percona-toolkit | 1557 | ' "$file" > "$tmpfile" |
4790 | 738 | head -n1 $TMPDIR/percona-toolkit | 1558 | head -n1 "$tmpfile" |
4791 | 739 | tail -n +2 $TMPDIR/percona-toolkit | sort | 1559 | tail -n +2 "$tmpfile" | sort |
4792 | 740 | 1560 | ||
4793 | 741 | echo | 1561 | echo |
4797 | 742 | # Now do the summary of index types per DB. Careful -- index is a reserved | 1562 | awk ' |
4795 | 743 | # word in awk. | ||
4796 | 744 | $AP_AWK ' | ||
4798 | 745 | BEGIN { | 1563 | BEGIN { |
4799 | 746 | # In case there is no USE statement in the file. | ||
4800 | 747 | db = "{chosen}"; | 1564 | db = "{chosen}"; |
4801 | 748 | num_dbs = 0; | 1565 | num_dbs = 0; |
4802 | 749 | num_idxes = 0; | 1566 | num_idxes = 0; |
4803 | @@ -756,7 +1573,6 @@ | |||
4804 | 756 | } | 1573 | } |
4805 | 757 | } | 1574 | } |
4806 | 758 | /KEY/ { | 1575 | /KEY/ { |
4807 | 759 | # Handle single-DB dumps, where there is no USE statement. | ||
4808 | 760 | if (num_dbs == 0) { | 1576 | if (num_dbs == 0) { |
4809 | 761 | num_dbs = 1; | 1577 | num_dbs = 1; |
4810 | 762 | db_seen[db] = 1; | 1578 | db_seen[db] = 1; |
4811 | @@ -807,15 +1623,13 @@ | |||
4812 | 807 | print ""; | 1623 | print ""; |
4813 | 808 | } | 1624 | } |
4814 | 809 | } | 1625 | } |
4818 | 810 | ' $file > $TMPDIR/percona-toolkit | 1626 | ' "$file" > "$tmpfile" |
4819 | 811 | head -n1 $TMPDIR/percona-toolkit | 1627 | head -n1 "$tmpfile" |
4820 | 812 | tail -n +2 $TMPDIR/percona-toolkit | sort | 1628 | tail -n +2 "$tmpfile" | sort |
4821 | 813 | 1629 | ||
4822 | 814 | echo | 1630 | echo |
4825 | 815 | # Now do the summary of datatypes per DB | 1631 | awk ' |
4824 | 816 | $AP_AWK ' | ||
4826 | 817 | BEGIN { | 1632 | BEGIN { |
4827 | 818 | # In case there is no USE statement in the file. | ||
4828 | 819 | db = "{chosen}"; | 1633 | db = "{chosen}"; |
4829 | 820 | num_dbs = 0; | 1634 | num_dbs = 0; |
4830 | 821 | num_types = 0; | 1635 | num_types = 0; |
4831 | @@ -828,7 +1642,6 @@ | |||
4832 | 828 | } | 1642 | } |
4833 | 829 | } | 1643 | } |
4834 | 830 | /^ `/ { | 1644 | /^ `/ { |
4835 | 831 | # Handle single-DB dumps, where there is no USE statement. | ||
4836 | 832 | if (num_dbs == 0) { | 1645 | if (num_dbs == 0) { |
4837 | 833 | num_dbs = 1; | 1646 | num_dbs = 1; |
4838 | 834 | db_seen[db] = 1; | 1647 | db_seen[db] = 1; |
4839 | @@ -898,106 +1711,195 @@ | |||
4840 | 898 | print ""; | 1711 | print ""; |
4841 | 899 | } | 1712 | } |
4842 | 900 | } | 1713 | } |
4847 | 901 | ' $file > $TMPDIR/percona-toolkit | 1714 | ' "$file" > "$tmpfile" |
4848 | 902 | hdr=$($AP_GREP -n Database $TMPDIR/percona-toolkit | cut -d: -f1); | 1715 | local hdr=$(grep -n Database "$tmpfile" | cut -d: -f1); |
4849 | 903 | head -n${hdr} $TMPDIR/percona-toolkit | 1716 | head -n${hdr} "$tmpfile" |
4850 | 904 | tail -n +$((${hdr} + 1)) $TMPDIR/percona-toolkit | sort | 1717 | tail -n +$((${hdr} + 1)) "$tmpfile" | sort |
4851 | 905 | echo | 1718 | echo |
4852 | 906 | } | 1719 | } |
4853 | 907 | 1720 | ||
4947 | 908 | # ############################################################################## | 1721 | section_percona_server_features () { |
4948 | 909 | # The main() function is called at the end of the script. This makes it | 1722 | local file="$1" |
4949 | 910 | # testable. Major bits of parsing are separated into functions for testability. | 1723 | |
4950 | 911 | # ############################################################################## | 1724 | [ -e "$file" ] || return |
4951 | 912 | main() { | 1725 | |
4952 | 913 | 1726 | name_val "Table & Index Stats" \ | |
4953 | 914 | # Begin by setting the $PATH to include some common locations that are not | 1727 | "$(feat_on "$file" userstat_running)" |
4954 | 915 | # always in the $PATH, including the "sbin" locations. On SunOS systems, | 1728 | name_val "Multiple I/O Threads" \ |
4955 | 916 | # prefix the path with the location of more sophisticated utilities. | 1729 | "$(feat_on "$file" innodb_read_io_threads gt 1)" |
4956 | 917 | export PATH="${PATH}:/usr/local/bin:/usr/bin:/bin:/usr/libexec" | 1730 | name_val "Corruption Resilient" \ |
4957 | 918 | export PATH="${PATH}:/usr/mysql/bin/:/usr/local/sbin:/usr/sbin:/sbin" | 1731 | "$(feat_on "$file" innodb_pass_corrupt_table)" |
4958 | 919 | export PATH="/usr/gnu/bin/:/usr/xpg4/bin/:${PATH}" | 1732 | name_val "Durable Replication" \ |
4959 | 920 | 1733 | "$(feat_on "$file" innodb_overwrite_relay_log_info)" | |
4960 | 921 | # Set up temporary files. | 1734 | name_val "Import InnoDB Tables" \ |
4961 | 922 | mk_tmpdir | 1735 | "$(feat_on "$file" innodb_expand_import)" |
4962 | 923 | temp_files "rm" | 1736 | name_val "Fast Server Restarts" \ |
4963 | 924 | temp_files "touch" | 1737 | "$(feat_on "$file" innodb_auto_lru_dump)" |
4964 | 925 | 1738 | name_val "Enhanced Logging" \ | |
4965 | 926 | # ######################################################################## | 1739 | "$(feat_on "$file" log_slow_verbosity ne microtime)" |
4966 | 927 | # Header for the whole thing, table of discovered instances | 1740 | name_val "Replica Perf Logging" \ |
4967 | 928 | # ######################################################################## | 1741 | "$(feat_on "$file" log_slow_slave_statements)" |
4968 | 929 | section Percona_Toolkit_MySQL_Summary_Report | 1742 | name_val "Response Time Hist." \ |
4969 | 930 | name_val "System time" "`date -u +'%F %T UTC'` (local TZ: `date +'%Z %z'`)" | 1743 | "$(feat_on "$file" enable_query_response_time_stats)" |
4970 | 931 | section Instances | 1744 | name_val "Smooth Flushing" \ |
4971 | 932 | ps auxww 2>/dev/null | $AP_GREP mysqld > $TMPDIR/percona-toolkit | 1745 | "$(feat_on "$file" innodb_adaptive_checkpoint ne none)" |
4972 | 933 | parse_mysqld_instances $TMPDIR/percona-toolkit | 1746 | name_val "HandlerSocket NoSQL" \ |
4973 | 934 | 1747 | "$(feat_on "$file" handlersocket_port)" | |
4974 | 935 | # ######################################################################## | 1748 | name_val "Fast Hash UDFs" \ |
4975 | 936 | # Fetch some basic info so we can start | 1749 | "$(get_var "pt-summary-internal-FNV_64" "$file")" |
4976 | 937 | # ######################################################################## | 1750 | } |
4977 | 938 | mysql "$@" -ss -e 'SELECT CURRENT_USER()' > $TMPDIR/percona-toolkit | 1751 | |
4978 | 939 | if [ "$?" != "0" ]; then | 1752 | section_myisam () { |
4979 | 940 | echo "Cannot connect to mysql, please specify command-line options." | 1753 | local variables_file="$1" |
4980 | 941 | temp_files "rm" | 1754 | local status_file="$2" |
4981 | 942 | rm_tmpdir | 1755 | |
4982 | 943 | exit 1 | 1756 | [ -e "$variables_file" -a -e "$status_file" ] || return |
4983 | 944 | fi | 1757 | |
4984 | 945 | user="$(cat $TMPDIR/percona-toolkit)"; | 1758 | local buf_size="$(get_var key_buffer_size "$variables_file")" |
4985 | 946 | mysql "$@" -ss -e 'SHOW /*!40100 GLOBAL*/ VARIABLES' > $TMPDIR/percona-toolkit-mysql-variables | 1759 | local blk_size="$(get_var key_cache_block_size "$variables_file")" |
4986 | 947 | mysql "$@" -ss -e 'SHOW /*!50000 GLOBAL*/ STATUS' > $TMPDIR/percona-toolkit-mysql-status | 1760 | local blk_unus="$(get_var Key_blocks_unused "$status_file")" |
4987 | 948 | mysql "$@" -ss -e 'SHOW DATABASES' > $TMPDIR/percona-toolkit-mysql-databases 2>/dev/null | 1761 | local blk_unfl="$(get_var Key_blocks_not_flushed "$variables_file")" |
4988 | 949 | mysql "$@" -ssE -e 'SHOW SLAVE STATUS' > $TMPDIR/percona-toolkit-mysql-slave 2>/dev/null | 1762 | local unus=$((${blk_unus:-0} * ${blk_size:-0})) |
4989 | 950 | mysql "$@" -ssE -e 'SHOW /*!50000 ENGINE*/ INNODB STATUS' > $TMPDIR/percona-toolkit-innodb-status 2>/dev/null | 1763 | local unfl=$((${blk_unfl:-0} * ${blk_size:-0})) |
4990 | 951 | mysql "$@" -ssE -e 'SHOW FULL PROCESSLIST' > $TMPDIR/percona-toolkit-mysql-processlist 2>/dev/null | 1764 | local used=$((${buf_size:-0} - ${unus})) |
4991 | 952 | now="$(mysql "$@" -ss -e 'SELECT NOW()')" | 1765 | |
4992 | 953 | port="$(get_var port)" | 1766 | name_val "Key Cache" "$(shorten ${buf_size} 1)" |
4993 | 954 | 1767 | name_val "Pct Used" "$(fuzzy_pct ${used} ${buf_size})" | |
4994 | 955 | # ######################################################################## | 1768 | name_val "Unflushed" "$(fuzzy_pct ${unfl} ${buf_size})" |
4995 | 956 | # General date, hostname, etc | 1769 | } |
4996 | 957 | # ######################################################################## | 1770 | |
4997 | 958 | section "Report_On_Port_${port}" | 1771 | section_innodb () { |
4998 | 959 | name_val User "${user}" | 1772 | local variables_file="$1" |
4999 | 960 | name_val Time "${now} ($(get_mysql_timezone))" | 1773 | local status_file="$2" |
5000 | 961 | name_val Hostname "$(get_var hostname)" | 1774 | |
4908 | 962 | get_mysql_version | ||
4909 | 963 | |||
4910 | 964 | uptime="$(get_stat Uptime)" | ||
4911 | 965 | mysql "$@" -ss -e "SELECT LEFT(NOW() - INTERVAL ${uptime} SECOND, 16)" \ | ||
4912 | 966 | > $TMPDIR/percona-toolkit | ||
4913 | 967 | name_val Started "$(get_mysql_uptime $TMPDIR/percona-toolkit)" | ||
4914 | 968 | |||
4915 | 969 | name_val Databases "$($AP_GREP -c . $TMPDIR/percona-toolkit-mysql-databases)" | ||
4916 | 970 | name_val Datadir "$(get_var datadir)" | ||
4917 | 971 | procs="$(get_stat Threads_connected)" | ||
4918 | 972 | procr="$(get_stat Threads_running)" | ||
4919 | 973 | name_val Processes "$(fuzz ${procs}) connected, $(fuzz ${procr}) running" | ||
4920 | 974 | if [ -s $TMPDIR/percona-toolkit-mysql-slave ]; then slave=""; else slave="not "; fi | ||
4921 | 975 | slavecount=$($AP_GREP -c 'Binlog Dump' $TMPDIR/percona-toolkit-mysql-processlist) | ||
4922 | 976 | name_val Replication "Is ${slave}a slave, has ${slavecount} slaves connected" | ||
4923 | 977 | |||
4924 | 978 | # TODO move this into a section with other files: error log, slow log and | ||
4925 | 979 | # show the sizes | ||
4926 | 980 | pid_file="$(get_var pid_file)" | ||
4927 | 981 | [ -e "${pid_file}" ] && PID_EXISTS="(exists)" | ||
4928 | 982 | name_val Pidfile "${pid_file} ${PID_EXISTS:-(does not exist)}" | ||
4929 | 983 | |||
4930 | 984 | # ######################################################################## | ||
4931 | 985 | # Processlist, sliced several different ways | ||
4932 | 986 | # ######################################################################## | ||
4933 | 987 | section Processlist | ||
4934 | 988 | summarize_processlist $TMPDIR/percona-toolkit-mysql-processlist | ||
4935 | 989 | |||
4936 | 990 | # ######################################################################## | ||
4937 | 991 | # Queries and query plans | ||
4938 | 992 | # ######################################################################## | ||
4939 | 993 | section "Status_Counters_(Wait_10_Seconds)" | ||
4940 | 994 | sleep 10 | ||
4941 | 995 | # TODO: gather this data in the same format as normal: stats, TS line | ||
4942 | 996 | mysql "$@" -ss -e 'SHOW /*!50000 GLOBAL*/ STATUS' \ | ||
4943 | 997 | | join $TMPDIR/percona-toolkit-mysql-status - > $TMPDIR/percona-toolkit | ||
4944 | 998 | # Make a file with a list of things we want to omit because they aren't | ||
4945 | 999 | # counters, they are gauges (in RRDTool terminology). Gauges are shown | ||
4946 | 1000 | # elsewhere in the output. |
The diff has been truncated for viewing.