• Home
  • Services
    • DBA Support
    • DBA Consultancy Services
    • PostgreSQL Support
    • Website Maintenance
  • Courses

    About Courses

    • List Of Courses
    • Become an Instructor
    Greenplum Database

    Greenplum Database

    $2,000.00 $1,500.00
    Read More
  • Company
    • FAQs
    • About Us
    • Contact
  • Events
  • Portfolio
  • Blogs
    • Blog – RayaFeeL
    • Blog – PostgreSQL Support
    • Blog – PostgreSQL Migration
    • Blog – All DB’s
    • Blog – Linux
    • Blog – Medical Coding
      • Cart

        0

    Have any question?
    (+91)8838953252
    ITsupport@rayafeel.com
    RegisterLogin
    RayaFeeL
    • Home
    • Services
      • DBA Support
      • DBA Consultancy Services
      • PostgreSQL Support
      • Website Maintenance
    • Courses

      About Courses

      • List Of Courses
      • Become an Instructor
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00
      Read More
    • Company
      • FAQs
      • About Us
      • Contact
    • Events
    • Portfolio
    • Blogs
      • Blog – RayaFeeL
      • Blog – PostgreSQL Support
      • Blog – PostgreSQL Migration
      • Blog – All DB’s
      • Blog – Linux
      • Blog – Medical Coding
        • Cart

          0

      Blog

      • Home
      • Blog
      • Blog
      • PostgreSQL Health checkup script with mail notification

      PostgreSQL Health checkup script with mail notification

      • Posted by 2ndnijam
      • Categories Blog
      • Date May 15, 2019
      • Comments 0 comment

      This script can be used to check on postgres apache,mysql, or whatever service you want. This is not meant to be a SOLUTION to services that are crashing, but rather a notification and a temporary restart until you can solve the real issue.

      The script will check the status of each service. If the service is stopped, it tries to restart the service. If the service starts, it sends you an email saying the service stopped but was restarted.

      If the service does not start for some reason, it sends you an email telling you it was not started.

      After that, it will continue to try and start, but not send any more emails until the service is finally started.

      NOTE*: This script should be run as your root user, so it would be added to the crontab like so:

      1. put it into your scripts folder
      2. set your email address
      3. set the services you want to keep an eye on (by default it has MariaDB and apache2..you can add or take away whatever you need)
      4. save your changes
      5. create a cronjob as root (sudo crontab -e) and add something like this, which runs every minute (adjust to your needs):
      #check on services
      */1 *  * * * sh /root/script/dbalert.sh
      ################### DOWN DB server######################
      #!/bin/bash
      #ver. 2
      
      ##this script will check whatever services
      ##you want to keep an eye on
      ##if that service is not running
      ##it will (try to) start the service and send
      ##an email to you
      
      ##set your email address
      EMAIL="nijamutheen@gmail.com"
      
      ##list your services you want to check
      ##you can add as many as you like
      SERVICES=('postgresql')
      logdate=$(date +%a)  # our log file format is postgresql-Fri.log choose depend on logfile format
      #### DO NOT CHANGE anything BELOW ####
      
      
       for i in "${SERVICES[@]}"
        do
          ###IF SERVICE IS NOT RUNNING####
      if ([[ "$(service $i status)" =~ "not running" ]] || [[ "$(service $i status)" =~ "is stopped" ]] || [[ $"$(service $i status)" =~ "stop/waiting/down" ]] || [[ $"$(service $i status)" =~ "no server running"]] )
      
          then
             ##TRY TO RESTART THAT SERVICE###
          service $i start  
      
      
              ##IF RESTART WORKED###
      if ([[ "$(service $i status)" =~ "server is running" ]] || [[ "$(service $i status)" =~ "is started" ]] || [[ $"$(service $i status)" =~ "start" ]])
      then
              ##SEND AN EMAIL###   
          MESSAGE="$(tail -15 /$PGDATA/pg_log/postgresql-$logdate.log)"
          SUBJECT="PostgreSQL Down But restarted Successfully on $(date) "
          echo -e "   LOGS :  \n$MESSAGE "  | mail -s "$SUBJECT" "$EMAIL"
             else
      
              ##IF RESTART DID NOT WORK SEND A DIFFERENT EMAIL###
          MESSAGE="$(tail -15 /$PGDATA/pg_log/postgresql-$logdate.log)"
          SUBJECT="PostgreSQL down, Restarted did not work on $(date) "
          echo -e "   LOGS :  \n$MESSAGE . \n>>>>> Script tried to restart the Postgres Server but it did not work"  | mail -s "$SUBJECT" "$EMAIL"
      
             fi
          fi
      
      done
      
      

      #  Better schedule  it as root user  because root user only having mail service access in some environment if postgres user having  mail services access then you can schedule it as postgres user and change pg_ctl instead of service command

      • Share:
      2ndnijam

      Previous post

      MariaDB Backup Script Mail Notifications Alert
      May 15, 2019

      Next post

      Migrating Oracle to PostgreSQL using ora2pg
      July 7, 2019

      Leave A Reply Cancel reply

      You must be logged in to post a comment.

      Login with:

      Login with Google Login with Twitter Login with LinkedIn Login with Microsoft


      Search

      ADVERTISEMENT

      Latest Posts

      PostgreSQL Patching version 9, 10,11
      10Oct2019
      Tools for PostgreSQL
      16Sep2019
      Postgres user creation and restrict DDL & database access
      13Sep2019
      PostgreSQL SSL Setup
      07Sep2019
      How to DELETE current XLOG / WAL LOG in postgresql database ?
      19Aug2019

      Latest Courses

      PostgreSQL Database

      PostgreSQL Database

      $600.00 $500.00
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00

      Preview Course

      Free

      Recent Forum Topics

      • thought behind whiteboard activity
      • Are you going to take your first ste
      • How to start working on an application?
      • please let me know pre requirements to increase work_mem
      • how to copy some data in one table to another table in postgres

      2ndquadrant.in

      (+91) 8838953252

      ITsupport@rayafeel.com

      Company

      • About Us
      • Contact
      • Our Team
      • Blog

      COURSES

      • List Of Course
      • Become An Instructor
      • Events
      • Postgres Support Blog

      Support

      • DBA Support
      • Consultancy Services
      • Postgres Migration Blogs
      • Forum

      Recommend

      • Groups
      • Login
      • FAQs
      • SignUp

      IT Services by rayafeel.com. Powered by Rayafeel Technologies Pvt Ltd.

      • Privacy
      • Terms

      Become An Instructor?

      Join thousand of instructors and earn money hassle free!

      Get Started Now

      Login with:

      Login with Google Login with Twitter Login with LinkedIn Login with Microsoft

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now