Tomcat 7 monitoring with e-mail notifications - tomcat7

I've been looking for a bit but can't find a free/open-source tomcat 7 monitoring tool that will send out e-mails or notifications when certain situations occur. For example when CPU utilization spikes or RAM is consistently full. Things Like that.
I've looked at JMelody and Psi-Probe and neither of them have the ability to send e-mails when some event occurs.

You can take a look at jboss RHQ
https://docs.jboss.org/author/display/RHQ/Alerts

This might help someone!!
If one do not want to use any monitoring tool, then set up an email configurtion in Ubuntu server using mailutils package.
https://rianjs.net/2013/08/send-email-from-linux-server-using-gmail-and-ubuntu-two-factor-authentication
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-14-04
To monitor Tomcat status you can use below script and set up cron job which runs every minute/hour/day according to your needs.
#!/bin/bash
TOMCAT_HOME=/opt/tomcat
PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
EMAIL_BODY="Hi Admin,\n\n$PUBLIC_IP Tomcat is down at $(date -d "+330 minutes" +"%Y-%m-%d %T") IST, Please take necessary action.\n\n\nDo not reply to this email as it is auto generated by Ubuntu system\n"
tomcat_pid() {
echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is already running (pid: $pid)"
else
# Start tomcat
echo "Starting tomcat"
/bin/sh $TOMCAT_HOME/bin/startup.sh
fi
return 0
}
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
#stop
else
echo "Tomcat is not running"
# send an email alert then start
echo -e $EMAIL_BODY | mail -s "$PUBLIC_IP Tomcat is down" user#email.com
echo "Mail sent"
#remove cache and release memory occupied by heavy processes
start
fi
exit 0

Related

Bash - start multiple services if any is running CentOS 6.8

I want to check if one, (or all) services are running, if yes, stop it
#!/bin/bash
# Define an array of processes to be checked.
# If properly quoted, these may contain spaces
check_process=( "nagios" "httpd" )
for p in "${check_process[#]}"; do
if pgrep "$p" > /dev/null; then
echo "Process \`$p' is running, stopping it"
service $p stop
else
echo "Process \`$p' is not running"
fi
done
For httpd service all works fine, script detects correctly httpd service state.
I have issues detecting nagios service state.
But although nagios service is not running, script shows it's running
Process `nagios' is running, stopping it
Stopping nagios:No lock file found in /usr/local/nagios/var/nagios.lock
Process `httpd' is not running
Is there any more elegant way of detecting if nagios service is running without checking if nagios.lock file exists ?
pgrep nagios shows no output when service is not tunning.
I gave up, this works fine for me:
although ps -ef | grep -v grep | grep $service | wc -l shows 0 for nagios, script reports that nagios service is running
#!/bin/bash
logfile=/tmp/stop_nagios.txt
exec >> $logfile
exec 2>&1
service=httpd
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running, stopping it"
date
sudo service $service stop
else
echo "$service is not running"
fi
# check nagios service
FILE=/usr/local/nagios/var/nagios.lock
if test -f "$FILE"; then
echo "nagios service is running, stopping it"
date
sudo service nagios stop
else
echo "nagios is not running..."
fi

bash script: to check if Apache server is up and running

I am new to bash scripting and trying to figure out why the below script is outputting that Apache server is not running whereas it is running properly.
ps cax | grep httpd
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
I'm running it on Ubuntu 14.04.2 LTS
Also, how do I make changes to the script that this can test apache server installed on another machine.
Kindly help
This is a working sample of bash script which check the apache status, restart it automatically if down, and alert by telegram bot within unicode emoji.
#!/bin/bash
telegram=(xxxxx, yyyyyy)
if ! pidof apache2 > /dev/null
then
# web server down, restart the server
echo "Server down"
/etc/init.d/apache2 restart > /dev/null
sleep 10
#checking if apache restarted or not
if pidof apache2 > /dev/null
then
for i in "${telegram[#]}"
do
curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restarted succesfully."
done
else
for i in "${telegram[#]}"
do
curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restart failed. Please check manually."
done
fi
fi
Use this:
service apache2 status
Or this:
service --status-all | grep apache2
Instead of httpd try to grep "apache2". To be sure try to check services with the next command and decide the registered name of the apache webserver:
service --status-all
Try and see - simply simplest, most didactic here and well working on Ubuntu 20.04:
catching output of status to bash variable
"if" status includes substring (from "Active:" statement) - do job you wanted
"else" - do another job you defined
#!/bin/bash
servstat=$(service apache2 status)
if [[ $servstat == *"active (running)"* ]]; then
echo "process is running"
else echo "process is not running"
fi
This work perfect in an old Debian. Remember to run with bash and not with sh.
In Centos replace with httpd.
#!/bin/bash
if [ $(/etc/init.d/apache2 status | grep -v grep | grep 'apache2 is running' | wc -l) > 0 ]
then
echo "Process is running."
else
echo "Process is not running."
fi
## Plz run this script .. its working
------------------------------------------------
ps cax | grep httpd
if [ $? -eq 1 ]
then
echo "Process is running."
else if [ $? -eq 0 ]
echo "Process is not running."
fi
fi
----------------------------------------------
This is menu driven one stop shell script in which you can check the firewall,apache or any other webservices ,you can start or stop the services just by choosing the option in below script
echo "welcome please select your options"
read choice
firewall=`sudo systemctl status firewalld`
apache=`sudo systemctl status apache2`
firewall1=`sudo systemctl stop firewalld`
apache1=`sudo systemctl stop apache2`
startrfirewall=`sudo systemctl start firewalld`
startapache=`sudo systemctl start apache2`
case $choice in
1) status of the firewall is $firewall
;;
2) status of apache is $apache
;;
3) echo stop firewall by $firewall1
;;
4) echo stop apache by $apache1
;;
5) echo start firewall by $startrfirewall
;;
6) echo start apache by $startapache
;;
*) echo exit
esac
I put this together based on the above and made so can use other services.
Hope this helps.
#!/bin/bash
# Must be running as root or via sudo permissions to be able to restart
# Put your process name restart command line in
PROCESS_NAME=httpd
if ! pidof $PROCESS_NAME > /dev/null
then
# web server down, restart the server
echo "Server $PROCESS_NAME down"
/usr/sbin/apachectl restart > /dev/null
echo "Tried restart of $PROCESS_NAME. Waiting 10 seconds to settle."
# wait ten
sleep 10
#checking if process restarted or not
if pidof $PROCESS_NAME > /dev/null
then
echo "$PROCESS_NAME was down but is now up."
else
echo "$PROCESS_NAME is still down. Please take some action."
fi
else
echo "Server $PROCESS_NAME up."
fi

To check whether nohup service is running or not using shell script?

I had created a nohup service using the below command in putty.
nohup php /var/www/html/XYZ/sample.php &
This command executes the sample.php file in background.
Now what i need is i want a shell script which checks whether this service is running or not.Incase if the service is not running i want that shell script to create a service by its own. Below is the code what i tried.
#!/bin/bash
email_to="xyz#gmail.com";
export DISPLAY=:0.0
PIDS=`ps -aux | grep sample.php|awk '{print $2}'`
if [ -z "$PIDS" ]; then
echo "$(date) - The service is not running. Sending email to :$email_to" >> /var/www/html/XYZ/sample.php;
echo "SERVICE is not running - $(date)" | mail -s "service is not running - $(date)" $email_to
echo "" >> /var/www/html/XYZ/sample.php;
exit 1
else
echo "$(date) - Service already running. Sending email to : $email_to" >> /var/www/html/XYZ/sample.php;
echo "SERVICE is running - $(date)" | mail -s "SERVICE is running - $(date)" $email_to
fi
when i execute the file i get the mail as service is running ,and once i kill the sample.php and when i get execute this file i get the same mail "as service is running" but its wrong ,so can anyone direct me where have i gone wrong?
where have i gone wrong?
With ps -aux | grep sample.php, the grep is finding sample.php in its own process command line grep sample.php, also output by ps. This can be avoided by modifying the grep command so that it doesn't contain sample.php literally, e. g. grep 'sample\.php' (which by the way averts the risk of matching another character instead of the .). You'll probably also need wide output from ps to not truncate the command, so change the above pipeline to ps waux | grep 'sample\.php'.

Ping Tool to check if server is online

Is this tool that I created from various SOF threads valid? Will it work? I want to have a ping test done to a server every minute. If it fails 5 times in a row it sends an email out. It then flushes and resets the script pretty much to check again.
#!/bin/bash
# ping checker tool
numOfFails=0
incrememnt=1
EMAILMESSAGE="/tmp/emailmessage.txt"
while true; do
if ! ping -c 1 google.com ; then #if ping exits nonzero...
numOfFails=$(($num + $increment))
else
numOfFails=0
fi
if ((numOfFails > 4)); then
numOfFails=0
echo "SAN is offline!" > $EMAILMESSAGE
mail -s "SAN offline" "test#test.com" < $EMAILMESSAGE
fi
sleep 60 #check again in one minute
done
Your code won't work at all, this is a revised version:
#!/bin/bash
# ping checker tool
FAILS=0
EMAIL_ADDRESS="example#example.com"
SERVER="192.168.1.1"
SLEEP=60
while true; do
ping -c 1 $SERVER >/dev/null 2>&1
if [ $? -ne 0 ] ; then #if ping exits nonzero...
FAILS=$[FAILS + 1]
else
FAILS=0
fi
if [ $FAILS -gt 4 ]; then
FAILS=0
echo "Server $SERVER is offline!" \
| mail -s "Server offline" "$EMAIL_ADDRESS"
fi
sleep $SLEEP #check again in SLEEP seconds
done
Change example#example.com and 192.168.1.1 for your email address and the IP address of the server you are testing. I recommend using and IP address instead of a hostname to prevent mixing name resolution errors with connection errors.
Please be advised that although this will work I would recommend running a slightly different script from cron instead of having it running continuously like you seem to want, when running from cron you would not need to monitor that the script is running since if it stops for some reason the monitoring of the server stops as well.
Something like this run from crontab every minute.
#!/bin/bash
# ping checker tool
TMP_FILE="/tmp/ping_checker_tool.tmp"
if [ -r $TMP_FILE ]; then
FAILS=`cat $TMP_FILE`
else
FAILS=0
fi
EMAIL_ADDRESS="example#example.com"
SERVER="192.168.1.1"
ping -c 1 $SERVER >/dev/null 2>&1
if [ $? -ne 0 ] ; then #if ping exits nonzero...
FAILS=$[FAILS + 1]
else
FAILS=0
fi
if [ $FAILS -gt 4 ]; then
FAILS=0
echo "Server $SERVER is offline!" \
| mail -s "Server offline" "$EMAIL_ADDRESS"
fi
echo $FAILS > $TMP_FILE
Consider using Pingdom. It provides this service for you.
One thing you have not considered is once your site goes down, you will continue to get email messages every minute, until the site is up again, or until you stop this script.
A good approach is to switch states from reporting when the site is down to reporting when the site is up, once you have detected that it is down. And then back again, once it is back up.
Essentially you one receive an email reporting 'site down', then another later on, hopefully, reporting 'site is up'.
Pingdom does this for you, very nicely.
I have been investigating how to do this so that I can activate/deactivate services depending on whether my phone is at home. I have come up with the following:
#!/bin/bash
HOST_TO_CHECK=<hostname>
if ping -qc 20 $HOST_TO_CHECK >/dev/null; then
echo "Host $HOST_TO_CHECK is up"
else
echo "Host $HOST_TO_CHECK is down"
fi
Replace <hostname> with the host you wish to check.
The script will ping the host 20 times.
The reason it does this is that my mobile doesn't always respond to pings immediately.
Obviously you can replace the echo commands with something to actually do something useful :-)
You can then schedule the script to check every 5 minutes by adding it to your crontab:
*/5 * * * * /opt/pingcheck.sh

bash script to monitor myself

I need to develop a shell script that would not be started if another instance of them self is running.
If I build a test.sh that monitors itself I need to know if it is already running and then abort, otherwise (if it not previously running) I can run
#!/bin/bash
loop() {
while [ 1 ]; do
echo "run";
#-- (... omissis ...)
sleep 30
done
}
daemon="`/bin/basename $0`"
pidlist=`/usr/bin/pgrep $daemon | grep -v $$`
echo "1:[ $pidlist ]"
pidlist=$(/usr/bin/pgrep $daemon | grep -v $$)
echo "2:[ $pidlist ]"
echo "3:[ `/usr/bin/pgrep $daemon | grep -v $$` ]"
echo "4:["
/usr/bin/pgrep $daemon | grep -v $$
echo "]"
if [ -z "$pidlist" ]; then
loop &
else
echo "Process $daemon is already running with pid [ $pidlist ]"
fi
exit 0;
When I run the above script for the first time (no previous instances running) I get this output:
1:[ 20341 ]
2:[ 20344 ]
3:[ 20347 ]
4:[
]
I cannot understand why only 4th attempt does not return anything (as expected). What's wrong in my script?
Do I have to redirect output of 4th command on a temporary file and then query that file in order to decide if I can run (or not) the loop function?
Thanks anyone would help me!
Sub-shells...the first three are run in sub-shells and hence $$ has changed to the PID of the sub-shell.
Try using:
PID=$$
pidlist=`/usr/bin/pgrep $daemon | grep -v $PID`
echo "1:[ $pidlist ]"
Etc. Since the value of $PID is established before the sub-shell is run, it should be the same for all of the commands.
Is this process going to be popular enough that other people want to run the same daemon on the machine? Maybe you never have multiple users on the machine, but remember that someone else might be wanting to run the command too.

Resources