I'm trying to monitor a specific port of a switch. I have to send email if my query returns a number >= 2.
I have tested my query and it works in Mysql CLI :
SELECT COUNT(message)
FROM devices, eventlog WHERE sysName = 'sysName'
AND message = 'ifOperStatus: up -> down'
AND datetime >= now() - INTERVAL 15 MINUTE
This Query returns a correct value. So, I have decided to create a script that executes the query and get infos :
#!/usr/bin/bash
mysql -u DB_USER -p PASSWORD DB_NAME <<MY_QUERY
SELECT COUNT(message)
FROM devices, eventlog WHERE sysName = 'swysName'
AND message = 'ifOperStatus: up -> down'
AND datetime >= now() - INTERVAL 15 MINUTE
MY_QUERY
When I execute this script in terminal, I got this:
librenms#librenms:/opt/librenms/scripts$ bash -x test1.sh
+ mysql -u DB_USER '-p PASSWORD' DB_NAME
COUNT(message)
0
Now I have to get query's value in a variable and test variable with an "if" in the script as :
....
if ($nb >= 2)
Then, if condition is true, send mail to alert admin.
I don't know how to get value in a variable and send by mail in bash script. If someone can help me...
Thanks.
You should get results of mysql query first:
RES = $(mysql -u DB_USER -p PASSWORD DB_NAME <<$MY_QUERY)
and then send it using:
echo $RES | mail -s "Information" email#email.com
Related
I am trying to run MySQL query through bash script. But, when I run SELECT * FROM EXAMPLE_DB; inside bash scripting, it is translated to SELECT files1 files2 files3 where I run the script.
Example :
read -d '' SQL_QUERY << EOF
SET #var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT * FROM tassta.vontista_messages WHERE date(sent_date) >= date(#var_name);
EOF
echo ${SQL_QUERY} | mysql
What I want to run the mysql query as it is. What happened now that this is translated to
read -d '' SQL_QUERY << EOF
SET #var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT file1 file2 file3 [files from where I run the script.] FROM tassta.vontista_messages WHERE date(sent_date) >= date(#var_name);
EOF
echo ${SQL_QUERY} | mysql
SQL_QUERY="SET #var_name = NOW() - INTERVAL 30 DAY;
CREATE TABLE tassta.temp_vontista_messages AS SELECT * FROM tassta.vontista_messages WHERE date(sent_date) >= date(#var_name);"
mysql -Be "$SQL_QUERY"
or:
echo "$SQL_QUERY" | mysql
NOTE: Do not put spaces before, or after the =.
see: How do I escape the wildcard/asterisk character in bash?
I am trying to run MYSQL command using SSH on the remote server. Getting the below error
ssh -p 22 root#eseemon63 "mysql --compress --secure-auth --database nacoma --execute 'SELECT time,object_name FROM changelog_history WHERE object_type = 'host' AND oldname = 'New Host' AND time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY time;'"
Error is:
ERROR 1102 (42000): Incorrect database name 'Host AND time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY time;'
You will have to escape the extra single quotes like below using \ or using another '
ssh -p 22 root#eseemon63 "mysql --compress --secure-auth --database nacoma --execute 'SELECT time,object_name FROM changelog_history WHERE object_type = \'host\' AND oldname = \'New Host\' AND time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY time;'"
Its fixed by entering "'"
ssh -p 22 root#eseemon63 "mysql --compress --secure-auth --database nacoma --execute 'SELECT time,object_name FROM changelog_history WHERE object_type = 'host' AND oldname = 'New Host' AND time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY time;'"
I have below function to read the data from mysql database:
get_all_server_ip_address(){
mysql -h$MYSQL_HOST -u$DB_USER_NAME -p$DB_PASSWORD << EOF
use $DB_SCHEMA;
select ip_address from server;
EOF
}
I am calling this function as below:
server_ip=get_all_server_ip_address
for ip in $server_ip
do
echo $ip
done
Output:
ip_address
54.206.76.6
I have only "54.206.76.6" in my database and it returns ip_address i.e column name
Database screenshot
use "--skip-column-names" option to login into mysql and then query , for example :
mysql -uroot probe --skip-column-names -e 'select ip_address from TABLE_NAME limit 1'
I have a linux script that gets a variable and I store it to var JOB_EXEC_ID
I am trying to pass the value of this to a MySQL query
Here is MySQL query set-up
print "JOB EXEC ID value for DataMeer Job ${LOADJOB} is : ${JobExecId} " |
tee -a ${LOGDIR}/${LOGFILE}
#Log on to MySQL to get the DataId
#Remove first the output file that would house the dataid
rm -f ${SCRDIR}/list_dataid.csv
mysql -u root -pmonday1 ${DAPDBNAME} < ${SCRDIR}/dataid_query.nosql
SQLRTN=$?
if [[ ${SQLRTN} != 0 ]]
then
print "Return code from sqlcall - DAP : ${SQLRTN}" |
tee -a ${LOGDIR}/${LOGFILE}
print "Exiting $Script - 55 " |
tee -a ${LOGDIR}/${LOGFILE}
exit 55
fi
The file dataid_query.nosql looks like this:
set #job_exec_id=10151
select d.id DataId
from data d inner join dap_job_configuration djc on d.dap_job_configuration__id = djc.id
left outer join dap_job_execution dje on djc.id = dje.dap_job_configuration__id and dje.created_data__id = d.id
where dje.id=#job_exec_id
into OUTFILE "/home/app1ebb/cs/list_dataid.csv"
I want to pass the value of JOB_EXEC_ID to the set command that is currently hardcoded right now with a value of 10151
in place of
mysql -u root -pmonday1 ${DAPDBNAME} < ${SCRDIR}/dataid_query.nosql
SQLRTN=$?
this lines
sed "1 s/[0-9]*$/${JOB_EXEC_ID}/" > /tmp/dataid_query.nosql
mysql -u root -pmonday1 ${DAPDBNAME} < /tmp/dataid_query.nosql
SQLRTN=$?
rm /tmp/dataid_query.nosql
This is the normal output:
mysql> select module_id from Modules where Module_name = 'STP_XENA';
+-----------+
| module_id |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
Can I get answer for the query as only "3"
I need something like,
mysql> select module_id from Modules where Module_name = 'STP_XENA';
3
mysql>
But not from bash or console. Is there any option to do this ?
You cannot do it inside MySQL editor, AFAIK. If you execute the script from console, then adding -B switch can get you desired result.
> mysql -B -u username -p password -e "select module_id from Modules where Module_name = 'STP_XENA';" DBNAME
will yield value with column name:
module_id
3
Also, if you add --skip-column-names
> mysql -B --skip-column-names -u username -p password -e "select module_id from Modules where Module_name = 'STP_XENA';" DBNAME
will yield only value (minus column name):
3
HTH
EDIT: You may start mysql with --skip-column-names switch. I am not sure about -B though. If you are able to start with -B, then great.
You want to use the query result in something like a bash script?
If so, you could do with:
mysql -u{user} -p{password} -s -N -e "select module_id from Modules where Module_name = 'STP_XENA'" database_name
Example:
module_id = `mysql -u{user} -p{password} -s -N -e "select module_id from Modules where Module_name = 'STP_XENA'" database_name`
echo $module_id