#! /bin/sh
# Copyright 2016, 2017, 2018, Cumulus Networks, Inc.  All rights reserved.

# for services we report that can run in a vrf, determine if they are.
# Can't simply use use servicename\* because if not enabled, that's empty
x=$(systemctl show -p Names ssh@\* 2>/dev/null)
[ -n "$x" ] && ssh="ssh ssh@*" || ssh=ssh
x=$(systemctl show -p Names netq-agent@\* 2>/dev/null)
[ -n "$x" ] && netq_agent="netq-agent netq-agent@*" || netq_agent=netq-agent
x=$(systemctl show -p Names ntp@\* 2>/dev/null)
[ -n "$x" ] && ntp="ntp ntp@*" || ntp=ntp

sys="cron $ssh rsyslog" # rsyslog shows as syslog, so after ssh
cumulus="asic-monitor clagd cumulus-poe lldpd mstpd neighmgrd netd $netq_agent $ntp portwd ptmd pwmd smond switchd sysmonitor vxrd vxsnd"
routing="rdnbrd frr"

systemctl show -p Names,ActiveState,UnitFileState ${sys} ${cumulus} ${routing} | \
  awk 'BEGIN { FS="[=.]" }
/^Names/ {service=$2 }
/^ActiveState/ { active=$2 }
/^UnitFileState/ {
  state=$2
  printf "Service %-18s %-10s %-9s\n", service, state, active
  }
'

[ -f /etc/frr/daemons -a -r /etc/frr/daemons ] || exit 0

for srvc in bgpd eigrpd isisd ldpd nhrpd ospf6d ospfd pbrd pimd ripd ripngd zebra; do
  if grep -q "^$srvc=yes" /etc/frr/daemons
    then state=enabled
    else state=disabled
  fi
  if pidof $srvc > /dev/null
    then active=active
    else active=inactive
  fi
  echo $srvc $state $active
done | awk '{printf "Service %-18s %-10s %-9s\n", $1, $2, $3}'
