Re: Server Online Status Watchdog for MacOS

From NAT, 6 Years ago, written in Bash, viewed 825 times. This paste is a reply to Server Online Status Watchdog for MacOS from NAT - view diff
URL https://code.nat.moe/view/c3c62485 Embed
Download Paste or View Raw
  1. #!/bin/bash
  2. function init_interactive {
  3.         rm watchdogjobs.pid > /dev/null 2> /dev/null
  4.         touch watchdogjobs.pid > /dev/null 2> /dev/null
  5.         INTERACTIVE=true
  6. }
  7. function wrapthekill {
  8. trap 'exit' 1 2 3 15
  9. }
  10. function processor {
  11.         HOSTUP=false
  12.         echo "$$,$2" >> watchdogjobs.pid
  13.         showstatus $2 offline
  14.         while true
  15.         do
  16.                 RAWREPLY=$(ping -c 1 $1 2> /dev/null)
  17.                 REPLY=$(echo $RAWREPLY | grep from)
  18.                 if [[ ! -z $REPLY ]]
  19.                 then
  20.                         if [[ $HOSTUP == "false" ]]
  21.                         then
  22.                                 echo -ne "\a"
  23.                                 showstatus $2 online
  24.                                 HOSTUP=true
  25.                         fi
  26.                 else
  27.                         if [[ $HOSTUP == "true" ]]
  28.                         then
  29.                                 echo -ne "\a"
  30.                                 showstatus $2 offline
  31.                                 HOSTUP=false
  32.                         fi     
  33.                 fi
  34.         done
  35. }
  36. function smbsolver {
  37.         smbutil lookup $1 | grep IP | awk -F": " '{print $2}'
  38. }
  39. function showstatus {
  40.         echo "[$(date) $$] Host $1 $2"
  41. }
  42. function action {
  43.         processor $1 $1
  44. }
  45. function smb_action {
  46.         processor $(smbsolver $1) $1
  47. }
  48. function this {
  49.         case $1 in
  50.                 -s|--smb|--netbios|--nb)
  51.                         smb_action $2
  52.                         ;;
  53.                 *)
  54.                         action $*
  55.                         ;;
  56.         esac
  57. }
  58. function new {
  59.         if [[ $INTERACTIVE != "true" ]]
  60.         then
  61.                 echo "[$(date) $$] Can't use new in non-interactive mode"
  62.         else
  63.                 if [[ ! -z $*  ]]
  64.                 then
  65.                         $0 $* &
  66.                 fi
  67.         fi
  68. }
  69. function showjobs {
  70.         if [[ $INTERACTIVE != "true" ]]
  71.         then
  72.                 echo "[$(date) $$] Can't show jobs in non-interactive mode"
  73.         else
  74.                 for pid in $(cat watchdogjobs.pid)
  75.                 do
  76.                         echo "Task watching $(echo $pid | awk -F, '{print $2}'), pid $(echo $pid | awk -F, '{print $1}')"
  77.                 done
  78.         fi
  79. }
  80. function killjobs {
  81.         if [[ $INTERACTIVE != "true" ]]
  82.         then
  83.                 echo "[$(date) $$] Can't kill jobs in non-interactive mode"
  84.         else
  85.                 echo "[$(date) $$] Send SIGKILL to all jobs..."
  86.                 for pid in $(cat watchdogjobs.pid | awk -F, '{print $1}')
  87.                 do
  88.                         kill -9 $pid
  89.                 done
  90.                 init_interactive
  91.         fi
  92. }
  93. function help {
  94.         case $1 in
  95.                 action)
  96.                         echo "Usage: action IP"
  97.                         echo "Start a new watchdog job using given IP address. It it not recommand to start job using this. You should use this action command. see help this for more informations. "
  98.                 ;;
  99.                 exit)
  100.                         echo "Usage: exit [-k|--keep-jobs]"
  101.                         echo "Exit watchdog. When -k or --keep-jobs is used, the process watchdog start will not be terminate. That might cause zombie process."
  102.                 ;;
  103.                 init_interactive)
  104.                         echo "Usage: init_interactive"
  105.                         echo "This is an internal function, you should not use this directly. Set the environment for interactive mode to run. If you run this command, the watchdog jobs list will be clear, without killing the running jobs. That might cause zombie process."
  106.                 ;;
  107.                 killjobs)
  108.                         echo "Usage: killjobs"
  109.                         echo "Kill all running watchdog process"
  110.                 ;;
  111.                 new)
  112.                         echo "Usage: new arguement [options]"
  113.                         echo "Start a new watchdog process and process given argument, argument can be any one of avaliable command"
  114.                 ;;
  115.                 processor)
  116.                         echo "Usage: processor IP DISPLAY_NAME"
  117.                         echo "Start a new watchdog job using given IP addesss and Display name. It it not recommand to start job using this. You should use this action command. see help this for more informations."
  118.                 ;;
  119.                 showjobs)
  120.                         echo "Usage: showjobs"
  121.                         echo "Show all jobs start by current watchdog process"
  122.                 ;;
  123.                 showstatus)
  124.                         echo "Usage: showstatus HOST STATUS"
  125.                         echo "This is an internal function, you should not use this directly. Using to display online/offline of a host."
  126.                 ;;
  127.                 this)
  128.                         echo "Usage: this [-s|--smb|--netbios|--nb] HOST"
  129.                         echo "Call current watchdog process to preform given job. It is not recommand to directly using this command. If you can, use new to start the job."
  130.                         echo "When option -s, --smb, --netbios or --nb is used, watchdog will first lookup IP address using smbutil."
  131.                 ;;
  132.                 smb_action)
  133.                         echo "Usage: smb_action NETBIOS_NAME"
  134.                         echo "Start a new watchdog job using given NETBIOS/Samba hostname. It it not recommand to start job using this. You should use this action command. see help this for more informations."
  135.                 ;;
  136.                 smbsolver)
  137.                         echo "Usage: smbsolver NETBIOS_NAME"
  138.                         echo "Resolve NETBIOS/Samba hostname to IP address."
  139.                 ;;
  140.                 *)
  141.                         echo "No such help topic found."
  142.                         echo "Avaliable helps are: action, exit, help, init_interactive, killjobs, new, processor, showstatus, this, smb_action, smbslover"
  143.                 ;;
  144.         esac
  145. }
  146. function exit {
  147.         case $1 in
  148.         -k|--keep-jobs)
  149.                 echo "[$(date) $$] Send SIGKILL to self..."
  150.                 kill -9 $$
  151.         ;;
  152.         *)
  153.                 echo "[$(date) $$] Send SIGKILL to all jobs..."
  154.                 for pid in $(cat watchdogjobs.pid | awk -F, '{print $1}')
  155.                 do
  156.                         kill -9 $pid
  157.                 done
  158.                 rm watchdogjobs.pid
  159.                 echo "[$(date) $$] Send SIGKILL to self..."
  160.                 kill -9 $$
  161.         ;;
  162.         esac
  163. }
  164. wrapthekill
  165. if [[ ! -z $* ]]
  166. then
  167.         $*
  168. else
  169.         init_interactive
  170.         echo "Watchdog 0.1 (interactive mode) - (C) MagicNAT 2014"
  171.         echo "Available commands: action, exit, help, init_interactive, killjobs, new, processor, showstatus, this, smb_action, smbslover"
  172.         while true
  173.         do
  174.                 echo -n "[$(pwd)] watchdog-$$> "
  175.                 read cmd;$cmd;
  176.         done
  177. fi

Reply to "Re: Server Online Status Watchdog for MacOS"

Here you can reply to the paste above

captcha