#!/bin/bash function init_interactive { rm watchdogjobs.pid > /dev/null 2> /dev/null touch watchdogjobs.pid > /dev/null 2> /dev/null INTERACTIVE=true } function wrapthekill { trap 'exit' 1 2 3 15 } function processor { HOSTUP=false echo "$$,$2" >> watchdogjobs.pid showstatus $2 offline while true do RAWREPLY=$(ping -c 1 $1 2> /dev/null) REPLY=$(echo $RAWREPLY | grep from) if [[ ! -z $REPLY ]] then if [[ $HOSTUP == "false" ]] then echo -ne "\a" showstatus $2 online HOSTUP=true fi else if [[ $HOSTUP == "true" ]] then echo -ne "\a" showstatus $2 offline HOSTUP=false fi fi done } function smbsolver { smbutil lookup $1 | grep IP | awk -F": " '{print $2}' } function showstatus { echo "[$(date) $$] Host $1 $2" } function action { processor $1 $1 } function smb_action { processor $(smbsolver $1) $1 } function this { case $1 in -s|--smb|--netbios|--nb) smb_action $2 ;; *) action $* ;; esac } function new { if [[ $INTERACTIVE != "true" ]] then echo "[$(date) $$] Can't use new in non-interactive mode" else if [[ ! -z $* ]] then $0 $* & fi fi } function showjobs { if [[ $INTERACTIVE != "true" ]] then echo "[$(date) $$] Can't show jobs in non-interactive mode" else for pid in $(cat watchdogjobs.pid) do echo "Task watching $(echo $pid | awk -F, '{print $2}'), pid $(echo $pid | awk -F, '{print $1}')" done fi } function killjobs { if [[ $INTERACTIVE != "true" ]] then echo "[$(date) $$] Can't kill jobs in non-interactive mode" else echo "[$(date) $$] Send SIGKILL to all jobs..." for pid in $(cat watchdogjobs.pid | awk -F, '{print $1}') do kill -9 $pid done init_interactive fi } function help { case $1 in action) echo "Usage: action IP" 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. " ;; exit) echo "Usage: exit [-k|--keep-jobs]" echo "Exit watchdog. When -k or --keep-jobs is used, the process watchdog start will not be terminate. That might cause zombie process." ;; init_interactive) echo "Usage: init_interactive" 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." ;; killjobs) echo "Usage: killjobs" echo "Kill all running watchdog process" ;; new) echo "Usage: new arguement [options]" echo "Start a new watchdog process and process given argument, argument can be any one of avaliable command" ;; processor) echo "Usage: processor IP DISPLAY_NAME" 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." ;; showjobs) echo "Usage: showjobs" echo "Show all jobs start by current watchdog process" ;; showstatus) echo "Usage: showstatus HOST STATUS" echo "This is an internal function, you should not use this directly. Using to display online/offline of a host." ;; this) echo "Usage: this [-s|--smb|--netbios|--nb] HOST" 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." echo "When option -s, --smb, --netbios or --nb is used, watchdog will first lookup IP address using smbutil." ;; smb_action) echo "Usage: smb_action NETBIOS_NAME" 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." ;; smbsolver) echo "Usage: smbsolver NETBIOS_NAME" echo "Resolve NETBIOS/Samba hostname to IP address." ;; *) echo "No such help topic found." echo "Avaliable helps are: action, exit, help, init_interactive, killjobs, new, processor, showstatus, this, smb_action, smbslover" ;; esac } function exit { case $1 in -k|--keep-jobs) echo "[$(date) $$] Send SIGKILL to self..." kill -9 $$ ;; *) echo "[$(date) $$] Send SIGKILL to all jobs..." for pid in $(cat watchdogjobs.pid | awk -F, '{print $1}') do kill -9 $pid done rm watchdogjobs.pid echo "[$(date) $$] Send SIGKILL to self..." kill -9 $$ ;; esac } wrapthekill if [[ ! -z $* ]] then $* else init_interactive echo "Watchdog 0.1 (interactive mode) - (C) MagicNAT 2014" echo "Available commands: action, exit, help, init_interactive, killjobs, new, processor, showstatus, this, smb_action, smbslover" while true do echo -n "[$(pwd)] watchdog-$$> " read cmd;$cmd; done fi