Tidify code

This commit is contained in:
root 2016-04-19 16:19:31 +02:00
parent 5b55c92ac5
commit fa1639e418

View file

@ -218,6 +218,7 @@ $opt{nocolor} = 1 if defined($outputfile);
# Setting up the colors for the print styles
my $me = `whoami`;
$me =~ s/\n//g;
# Setting up the colors for the print styles
my $good = ( $opt{nocolor} == 0 ) ? "[\e[0;32mOK\e[0m]" : "[OK]";
my $bad = ( $opt{nocolor} == 0 ) ? "[\e[0;31m!!\e[0m]" : "[!!]";
@ -247,19 +248,31 @@ sub greenwrap {
return ( $opt{nocolor} == 0 ) ? "\e[0;32m" . $_[0] . "\e[0m" : $_[0];
}
sub cmdprint { prettyprint $cmd. " " . $_[0] . $end; }
sub infoprintml { for my $ln(@_) { $ln =~s/\n//g; infoprint "\t$ln"; } }
sub infoprintcmd { cmdprint "@_"; infoprintml grep { $_ ne '' and $_ !~ /^\s*$/ } `@_ 2>&1`; }
sub infoprintml {
for my $ln (@_) { $ln =~ s/\n//g; infoprint "\t$ln"; }
}
sub infoprintcmd {
cmdprint "@_";
infoprintml grep { $_ ne '' and $_ !~ /^\s*$/ } `@_ 2>&1`;
}
sub subheaderprint {
my $tln = 100;
my $sln = 8;
my $ln = length("@_") + 2;
prettyprint " ";
#prettyprint "-"x$tln;
prettyprint "-" x $sln . " @_ " . "-" x ( $tln - $ln - $sln );
#prettyprint "-"x$tln;
}
sub infoprinthcmd {
# print Dumper @_;
subheaderprint "$_[0]";
infoprintcmd "$_[1]";
@ -483,7 +496,8 @@ sub validate_tuner_version {
compare_tuner_version($update);
return;
} else {
}
else {
}
@ -942,6 +956,7 @@ sub arr2hash {
}
sub get_all_vars {
# We need to initiate at least one query so that our data is useable
$dummyselect = select_one "SELECT VERSION()";
debugprint "VERSION: " . $dummyselect . "";
@ -958,7 +973,9 @@ sub get_all_vars {
$result{'Status'} = %mystat;
$myvar{'have_galera'} = "NO";
if ( defined($myvar{'wsrep_provider_options'}) && $myvar{'wsrep_provider_options'} ne "") {
if ( defined( $myvar{'wsrep_provider_options'} )
&& $myvar{'wsrep_provider_options'} ne "" )
{
$myvar{'have_galera'} = "YES";
debugprint "Galera options: " . $myvar{'wsrep_provider_options'};
}
@ -1174,8 +1191,7 @@ sub get_fs_info() {
}
}
sub merge_hash
{
sub merge_hash {
my $h1 = shift;
my $h2 = shift;
my %result = {};
@ -1220,44 +1236,51 @@ sub infocmd_one {
return join ', ', @result;
}
sub get_kernel_info()
{
my @params=('fs.aio-max-nr', 'fs.aio-nr', 'fs.file-max', 'sunrpc.tcp_fin_timeout',
sub get_kernel_info() {
my @params = (
'fs.aio-max-nr', 'fs.aio-nr',
'fs.file-max', 'sunrpc.tcp_fin_timeout',
'sunrpc.tcp_max_slot_table_entries', 'sunrpc.tcp_slot_table_entries',
'vm.swappiness');
'vm.swappiness'
);
infoprint "Informations about kernel tuning:";
foreach my $param (@params) {
infocmd_tab("sysctl $param");
}
if ( `sysctl -n vm.swappiness` > 10 ) {
badprint "Swappiness is > 10, please consider having a value lower than 10";
badprint
"Swappiness is > 10, please consider having a value lower than 10";
push @generalrec, "setup swappiness lower or equals to 10";
push @adjvars, 'vm.swappiness <= 10 (echo 0 > /proc/sys/vm/swappiness)';
} else {
}
else {
infoprint "Swappiness is < 10.";
}
if ( `sysctl -n sunrpc.tcp_slot_table_entries` < 100 ) {
badprint "Initial TCP slot entries is < 1M, please consider having a value greater than 100";
badprint
"Initial TCP slot entries is < 1M, please consider having a value greater than 100";
push @generalrec, "setup Initial TCP slot entries greater than 100";
push @adjvars, 'sunrpc.tcp_slot_table_entries > 100 (echo 128 > /proc/sys/sunrpc/tcp_slot_table_entries)';
} else {
push @adjvars,
'sunrpc.tcp_slot_table_entries > 100 (echo 128 > /proc/sys/sunrpc/tcp_slot_table_entries)';
}
else {
infoprint "TCP slot entries is > 100.";
}
if ( `sysctl -n fs.aio-max-nr` < 1000000 ) {
badprint "Max running total of the number of events is < 1M, please consider having a value greater than 1M";
badprint
"Max running total of the number of events is < 1M, please consider having a value greater than 1M";
push @generalrec, "setup Max running number events greater than 1M";
push @adjvars, 'fs.aio-max-nr > 1M (echo 1048576 > /proc/sys/fs/aio-max-nr)';
} else {
push @adjvars,
'fs.aio-max-nr > 1M (echo 1048576 > /proc/sys/fs/aio-max-nr)';
}
else {
infoprint "Max Number of AIO events is > 1M.";
}
}
sub get_system_info() {
infoprint get_os_release;
if (is_virtual_machine) {
@ -1283,6 +1306,7 @@ sub get_system_info() {
infoprint "Internal IP : " . infocmd_one "hostname -I";
my $httpcli = get_http_cli();
infoprint "HTTP client found: $httpcli" if defined $httpcli;
if ( $httpcli =~ /curl$/ ) {
infoprint "External IP : "
. infocmd_one "$httpcli ipecho.net/plain";
@ -1292,7 +1316,8 @@ sub get_system_info() {
. infocmd_one "$httpcli -q -O - ipecho.net/plain";
}
badprint
"External IP : Can't check because of Internet connectivity" unless defined($httpcli);
"External IP : Can't check because of Internet connectivity"
unless defined($httpcli);
infoprint "Name Servers : "
. infocmd_one "grep 'nameserver' /etc/resolv.conf \| awk '{print \$2}'";
infoprint "Logged In users : ";
@ -1965,14 +1990,20 @@ sub calculations {
# Max used memory is memory used by MySQL based on Max_used_connections
# This is the max memory used theorically calculated with the max concurrent connection number reached by mysql
$mycalc{'max_used_memory'} =
$mycalc{'server_buffers'} + $mycalc{"max_total_per_thread_buffers"} +get_pf_memory() + get_gcache_memory();
$mycalc{'server_buffers'} +
$mycalc{"max_total_per_thread_buffers"} +
get_pf_memory() +
get_gcache_memory();
$mycalc{'pct_max_used_memory'} =
percentage( $mycalc{'max_used_memory'}, $physical_memory );
# Total possible memory is memory needed by MySQL based on max_connections
# This is the max memory MySQL can theorically used if all connections allowed has opened by mysql
$mycalc{'max_peak_memory'} =
$mycalc{'server_buffers'} + $mycalc{'total_per_thread_buffers'} + get_pf_memory()+ get_gcache_memory();
$mycalc{'server_buffers'} +
$mycalc{'total_per_thread_buffers'} +
get_pf_memory() +
get_gcache_memory();
$mycalc{'pct_max_physical_memory'} =
percentage( $mycalc{'max_peak_memory'}, $physical_memory );
@ -2322,7 +2353,8 @@ sub mysql_stats {
. hr_bytes( $mycalc{'per_thread_buffers'} )
. " per thread ($myvar{'max_connections'} max threads)";
infoprint "P_S Max memory usage: " . hr_bytes_rnd( get_pf_memory() );
infoprint "Galera GCache Max memory usage: ".hr_bytes_rnd(get_gcache_memory());
infoprint "Galera GCache Max memory usage: "
. hr_bytes_rnd( get_gcache_memory() );
if ( $opt{buffers} ne 0 ) {
infoprint "Global Buffers";
infoprint " +-- Key Buffer: "
@ -2398,11 +2430,17 @@ sub mysql_stats {
. " ($mycalc{'pct_max_physical_memory'}% of installed RAM)";
}
if ($physical_memory < ($mycalc{'max_peak_memory'}+get_other_process_memory())) {
badprint "Overall possible memory usage with other process exceeded memory";
push( @generalrec, "Dedicated this server to your database for highest performance." );
} else {
goodprint "Overall possible memory usage with other process is compatible with memory available";
if ( $physical_memory <
( $mycalc{'max_peak_memory'} + get_other_process_memory() ) )
{
badprint
"Overall possible memory usage with other process exceeded memory";
push( @generalrec,
"Dedicated this server to your database for highest performance." );
}
else {
goodprint
"Overall possible memory usage with other process is compatible with memory available";
}
# Slow queries
@ -2574,6 +2612,7 @@ sub mysql_stats {
}
else {
goodprint "No joins without indexes";
# No joins have run without indexes
}
@ -2923,11 +2962,11 @@ sub mariadb_threadpool {
infoprint "Thread Pool Size: " . $myvar{'thread_pool_size'} . " thread(s).";
if ( $myvar{'version'} =~ /mariadb|percona/i ) {
infoprint "Using default value is good enougth for your version (".$myvar{'version'}.")";
infoprint "Using default value is good enougth for your version ("
. $myvar{'version'} . ")";
return;
}
if ( $myvar{'have_innodb'} eq 'YES' ) {
if ( $myvar{'thread_pool_size'} < 16
or $myvar{'thread_pool_size'} > 36 )
@ -2966,17 +3005,19 @@ sub mariadb_threadpool {
}
}
sub get_pf_memory
{
sub get_pf_memory {
# Performance Schema
return 0 unless defined $myvar{'performance_schema'};
return 0 if $myvar{'performance_schema'} eq 'OFF';
my @infoPFSMemory=grep /performance_schema.memory/, select_array("SHOW ENGINE PERFORMANCE_SCHEMA STATUS");
my @infoPFSMemory = grep /performance_schema.memory/,
select_array("SHOW ENGINE PERFORMANCE_SCHEMA STATUS");
return 0 if scalar(@infoPFSMemory) == 0;
$infoPFSMemory[0] =~ s/.*\s+(\d+)$/$1/g;
return $infoPFSMemory[0];
}
# Recommendations for Performance Schema
sub mysqsl_pfs {
subheaderprint "Performance schema";
@ -3096,12 +3137,14 @@ sub get_wsrep_options {
debugprint Dumper( \@galera_options );
return @galera_options;
}
sub get_gcache_memory {
my $gCacheMem = get_wsrep_option('gcache.mem_size');
return 0 unless defined $gCacheMem and $gCacheMem ne '';
return $gCacheMem;
}
sub get_wsrep_option {
my $key = shift;
return '' unless defined $myvar{'wsrep_provider_options'};
@ -3113,7 +3156,6 @@ sub get_wsrep_option {
return $memValue;
}
# Recommendations for Galera
sub mariadb_galera {
subheaderprint "Galera Metrics";
@ -3143,137 +3185,190 @@ sub mariadb_galera {
next unless $gstatus =~ /^wsrep.*/;
debugprint "\t" . trim($gstatus) . " = " . $mystat{$gstatus};
}
infoprint "GCache is using ".hr_bytes_rnd(get_wsrep_option('gcache.mem_size'));
my @primaryKeysNbTables=select_array("select CONCAT(table_schema,CONCAT('.', table_name)) from information_schema.columns where table_schema not in ('mysql', 'information_schema', 'performance_schema') group by table_schema,table_name having sum(if(column_key in ('PRI','UNI'), 1,0)) = 0");
infoprint "GCache is using "
. hr_bytes_rnd( get_wsrep_option('gcache.mem_size') );
my @primaryKeysNbTables = select_array(
"select CONCAT(table_schema,CONCAT('.', table_name)) from information_schema.columns where table_schema not in ('mysql', 'information_schema', 'performance_schema') group by table_schema,table_name having sum(if(column_key in ('PRI','UNI'), 1,0)) = 0"
);
if ( scalar(@primaryKeysNbTables) > 0 ) {
badprint "Following table(s) don't have primary key:";
foreach my $badtable (@primaryKeysNbTables) {
badprint "\t$badtable";
}
} else {
}
else {
goodprint "All tables get a primary key";
}
my @nonInnoDbTables=select_array("select CONCAT(table_schema,CONCAT('.', table_name)) from information_schema.tables where ENGINE <> 'InnoDb' and table_schema not in ('mysql', 'performance_schema', 'information_schema')");
my @nonInnoDbTables = select_array(
"select CONCAT(table_schema,CONCAT('.', table_name)) from information_schema.tables where ENGINE <> 'InnoDb' and table_schema not in ('mysql', 'performance_schema', 'information_schema')"
);
if ( scalar(@nonInnoDbTables) > 0 ) {
badprint "Following table(s) are not InnoDB table:";
push @generalrec, "Ensure that all table(s) are InnoDB tabls for Galera replication";
push @generalrec,
"Ensure that all table(s) are InnoDB tabls for Galera replication";
foreach my $badtable (@nonInnoDbTables) {
badprint "\t$badtable";
}
} else {
}
else {
goodprint "All tables are InnoDB tables";
}
if ( $myvar{'binlog_format'} ne 'ROW' ) {
badprint "Binlog format should be in ROW mode.";
push @adjvars, "binlog_format = ROW";
} else {
}
else {
goodprint "Binlog format is in ROW mode.";
}
if ( $myvar{'innodb_flush_log_at_trx_commit'} != 0 ) {
badprint "Innodb flush log at each commit should be disabled.";
push @adjvars, "innodb_flush_log_at_trx_commit = 0";
} else {
}
else {
goodprint "Innodb flush log at each commit is disabled for Galera.";
}
infoprint "Read consistency mode :" . $myvar{'wsrep_causal_reads'};
if ( defined($myvar{'wsrep_cluster_name'}) and $myvar{'wsrep_on'} eq "ON" ) {
if ( defined( $myvar{'wsrep_cluster_name'} )
and $myvar{'wsrep_on'} eq "ON" )
{
goodprint "Galera WsREP is enabled.";
if ( defined($myvar{'wsrep_cluster_address'}) and trim("$myvar{'wsrep_cluster_address'}") ne "") {
goodprint "Galera Cluster address is defined: ".$myvar{'wsrep_cluster_address'};
if ( defined( $myvar{'wsrep_cluster_address'} )
and trim("$myvar{'wsrep_cluster_address'}") ne "" )
{
goodprint "Galera Cluster address is defined: "
. $myvar{'wsrep_cluster_address'};
my $nbNodes = scalar( split /,/, $myvar{'wsrep_cluster_address'} );
if ( $nbNodes != 3 or $nbNodes != 5 ) {
goodprint "There is $nbNodes nodes in wsrep_cluster_address.";
} else {
badprint "There is $nbNodes nodes in wsrep_cluster_address. Prefer 3 or 5 nodes achitecture.";
}
else {
badprint
"There is $nbNodes nodes in wsrep_cluster_address. Prefer 3 or 5 nodes achitecture.";
}
my $nbNodesSize = trim( $mystat{'wsrep_cluster_size'} );
if ( $nbNodesSize != 3 or $nbNodesSize != 5 ) {
goodprint "There is $nbNodes nodes in wsrep_cluster_size.";
} else {
badprint "There is $nbNodes nodes in wsrep_cluster_size. Prefer 3 or 5 nodes achitecture.";
}
else {
badprint
"There is $nbNodes nodes in wsrep_cluster_size. Prefer 3 or 5 nodes achitecture.";
}
if ( $nbNodes != trim( $mystat{'wsrep_cluster_size'} ) ) {
badprint "All cluster nodes dre not detected. wsrep_cluster_size != informations in wsrep_cluster_adress";
} else {
badprint
"All cluster nodes dre not detected. wsrep_cluster_size != informations in wsrep_cluster_adress";
}
else {
badprint "All cluster nodes detected.";
}
} else {
}
else {
badprint "Galera Cluster address is undefined";
push @adjvars, "set up wsrep_cluster_address variable for Galera replication";
push @adjvars,
"set up wsrep_cluster_address variable for Galera replication";
}
if ( defined($myvar{'wsrep_cluster_name'}) and trim($myvar{'wsrep_cluster_name'}) ne "") {
goodprint "Galera Cluster name is defined: ".$myvar{'wsrep_cluster_name'};
} else {
if ( defined( $myvar{'wsrep_cluster_name'} )
and trim( $myvar{'wsrep_cluster_name'} ) ne "" )
{
goodprint "Galera Cluster name is defined: "
. $myvar{'wsrep_cluster_name'};
}
else {
badprint "Galera Cluster name is undefined";
push @adjvars, "set up wsrep_cluster_name variable for Galera replication";
push @adjvars,
"set up wsrep_cluster_name variable for Galera replication";
}
if ( defined($myvar{'wsrep_node_name'}) and trim($myvar{'wsrep_node_name'}) ne "") {
goodprint "Galera Node name is defined: ".$myvar{'wsrep_node_name'};
} else {
if ( defined( $myvar{'wsrep_node_name'} )
and trim( $myvar{'wsrep_node_name'} ) ne "" )
{
goodprint "Galera Node name is defined: "
. $myvar{'wsrep_node_name'};
}
else {
badprint "Galera node name is undefined";
push @adjvars, "set up wsrep_node_name variable for Galera replication";
push @adjvars,
"set up wsrep_node_name variable for Galera replication";
}
if ( trim( $myvar{'wsrep_notify_cmd'} ) ne "" ) {
goodprint "Galera Notify command is defined.";
} else {
}
else {
badprint "Galera Notify command is not defined.";
push( @adjvars, "set up parameter wsrep_notify_cmd to be notify" );
}
if ( trim( $myvar{'wsrep_sst_method'} ) ne "xtrabackup" ) {
badprint "Galera SST method is xtrabackup.";
push( @adjvars, "set up parameter wsrep_sst_method to xtrabackup" );
} else {
}
else {
goodprint "SST Method is inot based on xtrabackup.";
}
if ( trim( $myvar{'wsrep_OSU_method'} ) eq "TOI" ) {
goodprint "TOI is default mode for upgrade.";
} else {
}
else {
badprint "Schema upgrade are not replicated automatically";
push( @adjvars, "set up parameter wsrep_OSU_method to TOI" );
}
infoprint "Max WsRep message : " .hr_bytes( $myvar{'wsrep_max_ws_size'});
} else {
infoprint "Max WsRep message : "
. hr_bytes( $myvar{'wsrep_max_ws_size'} );
}
else {
badprint "Galera WsREP is disabled";
}
if (defined($mystat{'wsrep_connected'}) and $mystat{'wsrep_connected'} eq "ON") {
if ( defined( $mystat{'wsrep_connected'} )
and $mystat{'wsrep_connected'} eq "ON" )
{
goodprint "Node is connected";
} else {
}
else {
badprint "Node is disconnected";
}
if (defined($mystat{'wsrep_ready'}) and $mystat{'wsrep_ready'} eq "ON") {
if ( defined( $mystat{'wsrep_ready'} ) and $mystat{'wsrep_ready'} eq "ON" )
{
goodprint "Node is ready";
} else {
}
else {
badprint "Node is not ready";
}
infoprint "Cluster status :" . $mystat{'wsrep_cluster_status'};
if (defined($mystat{'wsrep_cluster_status'}) and $mystat{'wsrep_cluster_status'} eq "Primary") {
if ( defined( $mystat{'wsrep_cluster_status'} )
and $mystat{'wsrep_cluster_status'} eq "Primary" )
{
goodprint "Galera cluster is consistent and ready for operations";
} else {
}
else {
badprint "Cluster is not consistent and ready";
}
if ($mystat{'wsrep_local_state_uuid'} eq $mystat{'wsrep_cluster_state_uuid'}) {
goodprint "Node and whole cluster at the same level: ".$mystat{'wsrep_cluster_state_uuid'};
} else {
if ( $mystat{'wsrep_local_state_uuid'} eq
$mystat{'wsrep_cluster_state_uuid'} )
{
goodprint "Node and whole cluster at the same level: "
. $mystat{'wsrep_cluster_state_uuid'};
}
else {
badprint "Node and whole cluster not the same level";
infoprint "Node state uuid: " . $mystat{'wsrep_local_state_uuid'};
infoprint "Cluster state uuid: " . $mystat{'wsrep_cluster_state_uuid'};
}
if ( $mystat{'wsrep_local_state_comment'} eq 'Synced' ) {
goodprint "Node is synced with whole cluster.";
} else {
}
else {
badprint "Node is not synced";
infoprint "Node State : " . $mystat{'wsrep_local_state_comment'};
}
if ( $mystat{'wsrep_local_cert_failures'} == 0 ) {
goodprint "There is no certification failures detected.";
} else {
badprint "There is ".$mystat{'wsrep_local_cert_failures'}." certification failure(s)detected.";
}
else {
badprint "There is "
. $mystat{'wsrep_local_cert_failures'}
. " certification failure(s)detected.";
}
for my $key ( keys %mystat ) {
@ -3549,12 +3644,10 @@ sub mysql_databases {
foreach (@dblist) {
chomp($_);
if (
$_ eq "information_schema"
if ( $_ eq "information_schema"
or $_ eq "performance_schema"
or $_ eq "mysql"
or $_ eq ""
)
or $_ eq "" )
{
next;
}
@ -3916,10 +4009,9 @@ sub which {
}
}
return 0
return 0;
}
# ---------------------------------------------------------------------------
# BEGIN 'MAIN'
# ---------------------------------------------------------------------------