DZone
Performance Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > The Poor Man's Monitoring and Recovery

The Poor Man's Monitoring and Recovery

You have a Tomcat web server running. You want to take action if it dies and notify a group of people about the action. What do you do?

Rodrigo Asensio user avatar by
Rodrigo Asensio
·
Jan. 19, 17 · Performance Zone · Opinion
Like (1)
Save
Tweet
3.19K Views

Join the DZone community and get the full member experience.

Join For Free

When you are not willing to pay for monitoring services like AppDynamics or UptimeMonitor, you still have a cheap Shell script-based solution.

Shells scripts and Slack are your friends.

Scenario

You have a Tomcat web server running. You want to take action if it dies and notify a group of people about the action.

Tools

Crontab, Shell script, and Slack.

1. Set Up Crontab

In your Linux server, add a new crontab crontab -e and edit the following check every minute:

*/1 * * * * /opt/scripts/check.sh

Now, restart your cron service service crond restart.

2. Create the Check

Create a new Shell script in /opt/scripts/check.sh with the following code:

#! /bin/sh
SERVICE=/etc/init.d/tomcat8

if $SERVICE status | grep -q "not running"; then
    $SERVICE start
    /opt/scripts/slackpost.sh "https://hooks.slack.com/services/your-hook" "#monitor" "Tomcat Automatic Restart"
fi
if $SERVICE status | grep -q "stopped"; then
    $SERVICE start
    /opt/scripts/slackpost.sh "https://hooks.slack.com/services/your-hook" "#monitor" "Tomcat Automatic Restart"
fi

This will find out if the service crashed or was not started, and it will start it. After staring the service, it will send a notification to your Slack channel (you can strip this piece if no slack desired).

3. Notify Slack

Create another script in /opt/scripts/slackpost.sh with the following content:

#!/bin/bash

# Usage: slackpost "" "" ""

webhook_url=$1
if [[ $webhook_url == "" ]]
then
        echo "No webhook_url specified"
        exit 1
fi

shift
channel=$1
if [[ $channel == "" ]]
then
        echo "No channel specified"
        exit 1
fi

shift

text=$*

if [[ $text == "" ]]
then
        echo "No text specified"
        exit 1
fi

escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"channel\": \"$channel\", \"text\": \"$escapedText\"}"

curl -s -d "payload=$json" "$webhook_url"

Finally, use chmod +x /opt/scripts/*sh to make both scripts runnable. 

Note: This code can be found here. 

Happy coding!

Slack (software) Web Service shell Web server Shell script Crons Die (manufacturing) Apache Tomcat Stripes (framework)

Published at DZone with permission of Rodrigo Asensio. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • No Code Expectations vs Reality
  • The Developer's Guide to SaaS Compliance
  • 5 Ways to Optimize Your CQL Queries for Performance
  • Building a QR Code Generator with Azure Functions

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo