#! /usr/bin/env sh

#############################################################################
#
# Usage:
#
#############################################################################

#### Colors definition
BLACK='\033[30;40m'
RED='\033[0;31m'
REDB='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[34;40m'
MAGENTA='\033[0;35m'
CYAN='\033[36;40m'
WHITE='\033[0;37m'
WHITEB='\033[1;37m'
RESET='\033[0m'

## Return the state of processes passed in parameters
# process_info $PROCESS_LIST_TO_MONITOR $MESSAGE
process_info() {
  local PROCESS_LIST="${1}"
  local MSG="${2}"

  for PROCESS in ${PROCESS_LIST}; do
    MSG="${MSG}${MAGENTA}+ "
    if (ps ax | grep -v grep | grep ${PROCESS} > /dev/null); then
      MSG="${MSG}${WHITEB}${PROCESS}${RESET} [ ${GREEN}RUNNING${RESET} ] "
    else
      MSG="${MSG}${WHITEB}${PROCESS}${RESET} [ ${REDB}NOT RUNNING${RESET} ] "
    fi
  done

  printf '%b' "${MSG}"
}

## Return the listening socket
# service_info $PORT_LIST_TO_MONITOR $MESSAGE
service_info() {
  local PORT_LIST="${1}"
  local MSG="${2}"

  for PORT in ${PORT_LIST}; do
    MSG="${MSG}${MAGENTA}+ "
    # If a port listen
    if (ss -lutn|grep -m1 ":${PORT}" > /dev/null); then
      # Example: "tcp/127.0.0.1:25"
      #MSG="${MSG}${GREEN}$(ss -lutn|grep -m1 ":${PORT}"|awk '{print $1"/"$5}')${RESET} "
      MSG="${MSG}${GREEN}$(ss -lutn|grep ${PORT}|sort|head -n1|awk '{print $1"/"$5}')${RESET} "
    else
      # Example: "22: NOT LISTENING"
      MSG="${MSG}${REDB}${PORT}: NOT LISTENING${RESET} "
    fi
  done

  printf '%b' "${MSG}"
}

printf '%b' "${MAGENTA}+++++++++++++++++++: ${WHITE}Service Info${MAGENTA} :++++++++++++++++++++${RESET}"

## IF POSTFIX
if [ $(command -v postfix) ]; then
  MSG=$(process_info "postfix" '')
  MSG=$(service_info "25" "${MSG}")
  printf '%b' "\n${MSG}"
fi
## FI POSTFIX

### IF APACHE2
if [ $(command -v apache2) ]; then
  MSG=$(process_info "apache2" '')
  MSG=$(service_info "80 443" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI APACHE2

### IF NGINX
if [ $(command -v nginx) ]; then
  MSG=$(process_info "nginx" '')
  MSG=$(service_info "80 443" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI NGINX

### IF MYSQLD
if [ $(command -v mysqld) ]; then
  MSG=$(process_info "mysqld" '')
  MSG=$(service_info "3306" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI MYSQLD

### IF SLAPD
if [ $(command -v slapd) ]; then
  MSG=$(process_info "slapd" '')
  MSG=$(service_info "389 636" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI SLAPD

### IF ZFS
if [ $(command -v zfs) ]; then
  MSG=$(process_info "zfs" '')
  printf '%b' "\n${MSG}"
fi
### FI ZFS

### IF PUPPETMASTER
if [ $(command -v puppetmaster) ]; then
  MSG=$(process_info "puppetmaster" '')
  MSG=$(service_info "8140" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### IF PUPPETMASTER

### IF NFS Server
if [ $(command -v nfsd) ]; then
  MSG=$(process_info "nfsd" '')
  MSG=$(service_info "111 2049" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI NFS Server

### IF TFTPD
if [ $(command -v in.tftpd) ]; then
  MSG=$(process_info "in.tftpd" '')
  #MSG=$(service_info "69" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI TFTPD

### IF SQUID3
if [ $(command -v squid3) ]; then
  MSG=$(process_info "squid3" '')
  MSG=$(service_info "3128" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI SQUID3

### IF APT-CACHER-NG
if [ $(command -v apt-cacher-ng) ]; then
  MSG=$(process_info "apt-cacher-ng" '')
  MSG=$(service_info "3142" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI APT-CACHER-NG

### IF SSHD
if [ $(command -v sshd) ]; then
  MSG=$(process_info "sshd" '')
  MSG=$(service_info "22" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI SSHD

### IF DHCPD
if [ $(command -v dhcpd) ]; then
  MSG=$(process_info "dhcpd" '')
  printf '%b' "\n${MSG}"
fi
### FI DHCPD

### IF FAIL2BAN
if [ $(command -v fail2ban-server) ]; then
  MSG=$(process_info "fail2ban" '')
  printf '%b' "\n${MSG}"
fi
### FI FAIL2BAN

### IF ETHERPAD
if [ $(command -v etherpad) ]; then
  MSG=$(process_info "etherpad" '')
  MSG=$(service_info "9001" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI ETHERPAD

### IF ETHERCALC
if [ $(command -v ethercalc) ]; then
  MSG=$(process_info "ethercalc" '')
  MSG=$(service_info "8000" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI ETHERCALC

### IF GITLAB
if [ $(command -v gitlab-ctl) ]; then
  MSG=$(process_info "nginx" '')
  MSG=$(service_info "80 443" "${MSG}")
  printf '%b' "\n${MSG}"
  MSG=$(process_info "gitlab" '')
  printf '%b' "\n${MSG}"
fi
### FI GITLAB

### IF XYMONSERVER
if [ $(command -v xymond) ]; then
  MSG=$(process_info "xymond" '')
  MSG=$(service_info "1984" "${MSG}")
  printf '%b' "\n${MSG}"
fi
### FI XYMONSERVER

### IF XYMONCLIENT
if [ $(command -v xymon) ]; then
  MSG=$(process_info "xymonlaunch" '')
  printf '%b' "\n${MSG}"
fi
### FI XYMONCLIENT


## Service need a warning if running
YELLOW_SERVICE='tmux screen glances htop automysqlbackup vzdump puppet aptitude'
for SERVICE in ${YELLOW_SERVICE}; do
  if (ps ax | grep -v grep | grep ${SERVICE} > /dev/null); then
    printf '%b' "\n${MAGENTA}+ ${WHITEB}${SERVICE}${RESET} [ ${YELLOW}RUNNING${RESET} ]"
  fi
done

printf '%b' "${RESET}\n"

#echo -e "${MAGENTA}+++++++++++++: ${RED}Maintenance Information${MAGENTA} :+++++++++++++++${RESET}
#${MAGENTA}+${RED}""
#echo -e "${MAGENTA}+++++++++++++++++++++++++++++++++++++++++++++++++++++++${RESET}"