LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2019, 09:00 PM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Rep: Reputation: Disabled
Question Send by email the name of the database that has MyISAM tables


Hello

I am trying to create a script that will check all databases and if any of them has MyISAM tables to send me an email with the name of the database(s).

Now i have that:
Code:
#!/bin/bash
EMAILALERT="email@gmail.com"
HOSTNAME="$(hostname)"
MYSQLDATADIR=$(mysqladmin var | grep 'datadir' | awk '{ print $4}')


if [[ "$(find ${MYSQLDATADIR} -type f -not -path "${MYSQLDATADIR}mysql/*" -name "*.MYD")" ]]; then
  echo "Server has MyISAM tables in database" | mail -s "$HOSTNAME has MyISAM tables in database" $EMAILALERT
fi
It works as expected but i don't know how i can get the name(s) of the folder(s) of the database(s) on my email so i will know exactly which database has the MyISAM tables.

Need some help please.

Thank you
 
Old 11-05-2019, 11:40 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
awk can do the grep bit

Code:
#!/bin/bash
EMAILALERT="email@gmail.com"
HOSTNAME="$(hostname)"
MYSQLDATADIR=$(mysqladmin var | awk '/datadir/{ print $4}')


MYDs=( "$(find ${MYSQLDATADIR} -type f -not -path "${MYSQLDATADIR}mysql/*" -name "*.MYD")" )
# put path/filenames in array

email_body () {
# this just prints things
  printf "%s\n" "Server has MyISAM tables in database"
  printf "%s\n" "${MYDs[@]}"
}


if [[ ${#MYDs[@]} -gt 0 ]]
then
  email_body #removeme | mail -s "$HOSTNAME has MyISAM tables in database" $EMAILALERT 
else
  echo "nothing in the array"
fi
once you are happy with the email_body output remove the #removeme to go live with the pipe to mail

Edit, the array is probably overkill
you might get away with setting -E on mail
from man mail
-E Don't send messages with an empty body.

Code:
#!/bin/bash
EMAILALERT="email@gmail.com"
HOSTNAME="$(hostname)"
MYSQLDATADIR=$(mysqladmin var | awk '/datadir/{ print $4}')

find ${MYSQLDATADIR} \
  -type f \
  -not -path "${MYSQLDATADIR}mysql/*" \
  -name "*.MYD" \
  | mail -E -s "$HOSTNAME has MyISAM tables in database" $EMAILALERT
but I'm not 100% certain of what you actually want

Last edited by Firerat; 11-05-2019 at 11:47 PM.
 
Old 11-06-2019, 12:52 AM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Code:
find ${MYSQLDATADIR} -type f -not -path "${MYSQLDATADIR}mysql/*" -name "*.MYD"
gives the entire path to each MYD file:
Code:
/var/lib/mysql/STG/Characters.MYD
/var/lib/mysql/STG/Conflicts.MYD
And you know the value of MYSQLDATADIR (/var/lib/mysql/ in this example) so the directory you're looking for follows that in the results of the find.

Seems you only need to parse that result -- which you're not now doing.
 
1 members found this post helpful.
Old 11-06-2019, 02:54 AM   #4
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Quote:
Seems you only need to parse that result -- which you're not now doing.
Got it working !

Thank you

@Firerat

Thanks for your reply also !

Last edited by bmxakias; 11-06-2019 at 03:06 AM.
 
Old 11-06-2019, 08:15 AM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by bmxakias View Post
Got it working !

Thank you

@Firerat

Thanks for your reply also !
You're welcome.
Share your final script here for the next person?
 
Old 11-06-2019, 08:53 AM   #6
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Sure:

Code:
#!/bin/bash
###########
# variables
###########
EMAILALERT="email@gmail.com"
HOSTNAME="$(hostname)"
MYSQLDATADIR=$(mysqladmin var | grep 'datadir' | awk '{ print $4}')
MYDs="$(find ${MYSQLDATADIR} -type f -not -path "${MYSQLDATADIR}mysql/*" -name "*.MYD")"
########################################################################################

if [[ "$(find ${MYSQLDATADIR} -type f -not -path "${MYSQLDATADIR}mysql/*" -name "*.MYD")" ]]; then
  echo "Server has MyISAM tables in database(s): $MYDs" | mail -s "MyISAM tables in database(s) at $HOSTNAME" $EMAILALERT
  echo ""
  echo "MyISAM tables in database(s):"
  echo "$MYDs"
  echo ""
fi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to list all databases that have inside tables with MyIsam engine? bmxakias Programming 4 09-14-2019 08:23 AM
How to convert/changes MySQL Tables from MyISAM to NDBCLUSTER cparapat Linux - Software 3 02-04-2011 03:05 AM
Overhead tables MyISAM in MySQL Stephan_Craft Linux - Software 2 07-20-2008 04:38 PM
Innodb & MyISAM please help Stephan_Craft Linux - Software 8 07-19-2008 06:46 AM
Tutorial: Setting up MySQL quotas for users (MyISAM) nileshgr Linux - Server 4 02-04-2008 06:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration