Check to see if a list of applications are running by testing host and port


/ Published in: Bash
Save to your folder(s)

This script allows you to monitor a list of applications each running on servers in different environments. For example, these can be webservers: apache/nginx/etc., application servers: tomcat/nodejs/IIS/etc., database servers: mysql/oracle/etc., or anything else that listens on a port.

Instructions:
1) Edit the key/value pairs in the arrays: ENV_LIST, PORT_LIST, SERVERS, APPS with the data for your own applications
2) Run: ./scriptname.sh

Requirements:
This script does require "nc" to be installed.

Note:
This works both on linux and cygwin.


Copy this code and paste it in your HTML
  1. #!/bin/bash
  2.  
  3. declare -A SERVERS
  4. declare -A APPS
  5.  
  6. # Dumb trick to retain order in an associate array
  7. ENV_LIST=(Dev Test Stage1 Stage2 Prod1 Prod2)
  8. PORT_LIST=(3000 3001 3002 3003 3004 3005)
  9.  
  10. # Server list
  11. SERVERS=(
  12. ["Dev"]="dev_hostname"
  13. ["Test"]="test_hostname"
  14. ["Stage1"]="stage1_hostname"
  15. ["Stage2"]="stage2_hostname"
  16. ["Prod1"]="prod1_hostname"
  17. ["Prod2"]="prod2_hostname"
  18. )
  19.  
  20. # App list
  21. APPS=(
  22. [3000]="App Name 1"
  23. [3001]="App Name 2"
  24. [3002]="App Name 3"
  25. [3003]="App Name 4"
  26. [3004]="App Name 5"
  27. [3005]="App Name 6"
  28. )
  29.  
  30. echo
  31. echo "####"
  32. echo "#### Checking application servers..."
  33. echo "####"
  34.  
  35. for ENVIRONMENT in ${ENV_LIST[@]}; do
  36. echo
  37. echo "$ENVIRONMENT (${SERVERS[$ENVIRONMENT]})"
  38. echo ---------------------
  39. for PORT in ${PORT_LIST[@]}; do
  40. RSLT=$(nc -w4 -vz ${SERVERS[$ENVIRONMENT]} $PORT 2>&1)
  41. if [ $? = 0 ]; then
  42. printf "%-25s %s\n" "${APPS[$PORT]}" " on $PORT is UP"
  43. else
  44. echo "$RSLT"
  45. fi
  46. done
  47. done

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.