#!/bin/sh
# .. vim: foldmarker=[[[,]]]:foldmethod=marker

# {{ ansible_managed }}

# Colors definition [[[
BLACK='\033[49;30m'
BLACKB='\033[49;90m'
RED='\033[0;31m'
REDB='\033[1;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[94;49m'
MAGENTA='\033[0;35m'
CYAN='\033[36;49m'
WHITE='\033[0;37m'
BOLD='\033[1m'
RESET='\033[0m'
# ]]]
# Function definition [[[
## process_info() [[[
### Return the state of processes passed in parameters
###		process_info $PROCESS_LIST_TO_MONITOR $MESSAGE
process_info() {
	PROCESS_LIST="${1}"
	MSG="${2}"

	for PROCESS in ${PROCESS_LIST}; do
		MSG="${MSG}${BLACKB}+ "
		PRINTF_PROCESS=$(printf '%-22s' "${PROCESS}")
		if (ps ax | grep -v grep | grep -E "${PROCESS}" > /dev/null); then
			MSG="${MSG}${WHITE}${PRINTF_PROCESS}${RESET}${BLACKB}= ${GREEN}RUNNING${RESET}"
		else
			MSG="${MSG}${WHITE}${PRINTF_PROCESS}${RESET}${BLACKB}= ${REDB}NOT RUNNING${RESET}"
		fi
	done

	printf '%b' "${MSG}"
}
## ]]]
## service_info() [[[
### Return the listening socket
###		service_info $PORT_LIST_TO_MONITOR $MESSAGE
service_info() {
	PORT_LIST="${1}"
	MSG="${2}"

	for PORT in ${PORT_LIST}; do
		MSG="${MSG}\\t${BLACKB}+ "
		# If a port listen
		if (ss -lutn|grep -m1 -E ":${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 -E "${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}"
}
## ]]]
# ]]]

#+++++++++++++++++++: Service Info :+++++++++++++++++++
printf '%b' "${RESET}"
printf "${BLACKB}%33s${RESET}" | tr ' ' -
printf '%b' " ${CYAN}Service Info${RESET} "
printf "${BLACKB}%33s${RESET}" | tr ' ' -

# Monitore some service if availabe [[[
## WEB SERVER [[[
### IF APACHE2 [[[
if [ "$(command -v apache2)" ]; then
  MSG=$(process_info "apache2" '')
  MSG=$(service_info "80 443" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF NGINX [[[
if [ "$(command -v nginx)" ]; then
  MSG=$(process_info "nginx" '')
  MSG=$(service_info "80 443" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
## ]]]
## WEB APPS [[[
### IF BACKUPPC [[[
if [ -d "/var/lib/backuppc/pc" ]; then
  MSG=$(process_info "backuppc" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF BITWARDEN [[[
if [ -d "/opt/bitwarden_rs" ]; then
  MSG=$(process_info "bitwarden_rs" '')
  MSG=$(service_info "3012 8000" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF ETHERPAD [[[
if [ "$(command -v etherpad)" ]; then
  MSG=$(process_info "etherpad" '')
  MSG=$(service_info "9001" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF ETHERCALC [[[
if [ "$(command -v ethercalc)" ]; then
  MSG=$(process_info "ethercalc" '')
  MSG=$(service_info "8000" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF GITEA [[[
if [ "$(command -v gitea)" ]; then
  MSG=$(process_info "gitea" '')
  MSG=$(service_info "3000" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF GOGS [[[
if [ "$(command -v gogs)" ]; then
  MSG=$(process_info "gogs" '')
  MSG=$(service_info "3000" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### 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
### ]]]
### IF Jenkins [[[
if [ -d "/var/lib/jenkins" ]; then
  MSG=$(process_info "java" '')
  MSG=$(service_info "8080" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF MUMBLE [[[
if [ "$(command -v murmurd)" ]; then
  MSG=$(process_info "murmurd" '')
  MSG=$(service_info "64738" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF XYMONSERVER [[[
if [ -d "/var/lib/xymon/www" ]; then
  MSG=$(process_info "xymond" '')
  MSG=$(service_info "1984" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
## ]]]
## System [[[
### IF POSTFIX [[[
if [ "$(command -v postfix)" ]; then
  MSG=$(process_info "postfix" '')
  MSG=$(service_info "25" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF SLAPD [[[
if [ "$(command -v slapd)" ]; then
  MSG=$(process_info "slapd" '')
  MSG=$(service_info "389 636" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF SASLAUTHD [[[
if [ "$(command -v saslauthd)" ]; then
  MSG=$(process_info "saslauthd" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF SSHD [[[
if [ "$(command -v sshd)" ]; then
  MSG=$(process_info "sshd" '')
  MSG=$(service_info "22" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF DHCPD [[[
if [ "$(command -v dhcpd)" ]; then
  MSG=$(process_info "dhcpd" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
## ]]]
## IF MYSQLD [[[
if [ "$(command -v mysqld)" ]; then
  MSG=$(process_info "mysqld" '')
  MSG=$(service_info "3306" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
## ]]]
## IF UWSGI [[[
if [ "$(command -v uwsgi)" ]; then
  MSG=$(process_info "uwsgi" '')
  MSG=$(service_info "3031|8888" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
## ]]]
## IF ZFS [[[
if [ "$(command -v zfs)" ]; then
  MSG=$(process_info "zed" '')
  printf '%b' "\\n${MSG}"
fi
## ]]]
## IF PUPPETMASTER [[[
if [ "$(command -v puppetmaster)" ]; then
  MSG=$(process_info "puppetmaster" '')
  MSG=$(service_info "8140" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
## ]]]
## NFS [[[
### IF NFS Server [[[
if [ "$(command -v nfsd)" ]; then
  MSG=$(process_info "nfsd" '')
  MSG=$(service_info "111 2049" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF RPCBIND [[[
if [ "$(command -v rpcbind)" ]; then
  MSG=$(process_info "rpcbind" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF RPC.IDMAPD [[[
if [ "$(command -v rpc.idmapd)" ]; then
  MSG=$(process_info "rpc.idmapd" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF RPC.MOUNTD [[[
if [ "$(command -v rpc.mountd)" ]; then
  MSG=$(process_info "rpc.mountd" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
## ]]]
## IF TFTPD [[[
if [ "$(command -v in.tftpd)" ]; then
  MSG=$(process_info "in.tftpd" '')
  #MSG=$(service_info "69" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
## ]]]
## IF SQUID3 [[[
if [ "$(command -v squid3)" ]; then
  MSG=$(process_info "squid3" '')
  MSG=$(service_info "3128" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
## ]]]
## 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
## ]]]
## IF REDIS-SERVER [[[
if [ "$(command -v redis-server)" ]; then
  MSG=$(process_info "redis-server" '')
  MSG=$(service_info "6379" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
## ]]]
## IF ZNC [[[
if [ "$(command -v znc)" ]; then
  MSG=$(process_info "znc" '')
  MSG=$(service_info "1334 1335" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
## ]]]
## Security [[[
### IF FAIL2BAN [[[
if [ "$(command -v fail2ban-server)" ]; then
  MSG=$(process_info "fail2ban" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF XYMONCLIENT [[[
if [ "$(command -v xymon)" ]; then
  MSG=$(process_info "xymonlaunch" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF ARPWATCH [[[
if [ "$(command -v arpwatch)" ]; then
  MSG=$(process_info "arpwatch" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
## ]]]
## Proxmox [[[
### IF PVEDAEMON [[[
if [ "$(command -v pvedaemon)" ]; then
  MSG=$(process_info "pvedaemon" '')
  MSG=$(service_info "85" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF PVEPROXY [[[
if [ "$(command -v pveproxy)" ]; then
  MSG=$(process_info "pveproxy" '')
  MSG=$(service_info "8006" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF PVESTATD [[[
if [ "$(command -v pvestatd)" ]; then
  MSG=$(process_info "pvestatd" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
## ]]]
## Docker [[[
### IF DOCKERD [[[
if [ "$(command -v dockerd)" ]; then
  MSG=$(process_info "dockerd" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF DOCKER-PROXY [[[
if [ "$(command -v docker-proxy)" ]; then
  MSG=$(process_info "docker-proxy" '')
  MSG=$(service_info "3000|5000" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
## ]]]
## Licence Manager [[[
### IF LMGRD [[[
if [ "$(command -v lmgrd)" ]; then
  MSG=$(process_info "lmgrd" '')
  MSG=$(service_info "1718|1719|27000|33188|57227" "${MSG}")
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF INTEL-LM [[[
if [ -f "/opt/intel/etc/license.lic" ]; then
  MSG=$(process_info "INTEL..T" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF ABAQUS-LM [[[
if [ -f "/opt/origin/etc/license.lic" ]; then
  MSG=$(process_info "orglab" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF ORIGIN-LM [[[
if [ -f "/opt/origin/etc/license.lic" ]; then
  MSG=$(process_info "orglab" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
### IF MATLAB-LM [[[
if [ -f "/opt/matlab/etc/license.lic" ]; then
  MSG=$(process_info "MLM" '')
  printf '%b' "\\n${MSG}"
fi
### ]]]
## ]]]
# ]]]

# Process need a warning if running [[[
YELLOW_PROCESS='tmux screen glances htop automysqlbackup vzdump puppet aptitude'
for PROCESS in ${YELLOW_PROCESS}; do
	if (ps ax | grep -v grep | grep "${PROCESS}" > /dev/null); then
		PRINTF_PROCESS=$(printf '%-22s' "${PROCESS}")
		printf '%b' "\\n${BLACKB}+ ${WHITE}${PRINTF_PROCESS}${RESET}${BLACKB}= ${YELLOW}RUNNING${RESET}"
	fi
done
# ]]]

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

