List zones, hosts and endpoins via shell script and 'tree'

api
bash
icinga2

(qubit) #1

Hello together,

wrote a simple bash script to visualize the icinga structure about hosts, zones and endpoints. First outputs a simple table and second plots a tree (ab)using the command ‘tree’ ^^

#!/bin/bash

# Copyright [2018] [Daniel Buelow]
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

MASTER="$1"

_config_() {
  # possible values: host, zone, endpoint, service
  icinga_hosts=$(icinga2 object list --type host)
  icinga_zones=$(icinga2 object list --type zone)
  icinga_endpo=$(icinga2 object list --type endpoint)
  
  TMPDIR=$(mktemp -d /tmp/output.XXXXXXXXXX)
  mkdir -p "${TMPDIR}/zones/master"
  touch "${TMPDIR}/zones/master/${MASTER}"
}

_loop_() {
  for host_name in $(echo "$icinga_hosts"|grep -w name|cut -d '"' -f2|sort); do

    host_zone=$(echo "$icinga_zones"|sed -n -e "/Object '$host_name' of type 'Zone':/,/zone =/ p"|grep -w zone|cut -d '"' -f2)
    host_pare=$(echo "$icinga_zones"|sed -n -e "/Object '$host_name' of type 'Zone':/,/parent =/ p"|grep -w parent|cut -d '"' -f2)

    if [ -z "$host_zone" ]; then
      host_zone=$(echo "$icinga_hosts"|sed -n -e "/Object '$host_name' of type 'Host':/,/zone =/ p"|grep -w zone|cut -d '"' -f2)
      if [ -z "$host_zone" ]; then
        host_zone="null"
      fi
    fi
    if [ -z "$host_pare" ]; then
      host_pare=$(echo "$icinga_endpo"|sed -n -e "/Object '$host_name' of type 'Endpoint':/,/zone =/ p"|grep -w zone|cut -d '"' -f2)
      if [ -z "$host_pare" ]; then
        host_pare="null"
      fi
    fi

    hosts+=" $host_name $host_zone $host_pare\n"
    if [ "$host_name" != "$MASTER" ]; then
      mkdir -p "${TMPDIR}/zones/${host_zone}"
      if [ "$host_zone" != "$host_pare" -a "$host_pare" != "null" ]; then
        cd "${TMPDIR}/zones/${host_zone}" && ln -s "../${host_zone}" "${TMPDIR}/zones/${host_zone}/${host_name}"
      else
        touch "${TMPDIR}/zones/${host_zone}/${host_name}"
      fi
    fi

  done 
}

_output_() {
  echo -e "Hostname Zone Parent\n${hosts}\n"|column -t
  echo -e "\n'tree' graph:\n"
  cd "$TMPDIR" && tree -F zones|sed -e 's/directories/Zone(s)/' -e 's/files/Host(s) Object(s)/' -e '/->.*$/ s/$/ \(Satelite\/Endpoint\)/' -e "s/$MASTER/$MASTER \(MASTER\)/"
  rm -rf "$TMPDIR"
}

_main_() {
  if [ $# -eq 0 ]; then
    echo "$0 <master-fqdn>"
    exit 1
  else
    _config_
    _loop_
    _output_
  fi
}

_main_ "$@"

visualiZEH.sh.tgz (1.2 KB)

Just an experimental thing, would be great if there is someone with php skills to implement something similar inside the icingaweb2 directly.

Daniel