#!/bin/sh
#####################################################################################################
##
#	Extract information from Nagios-config and write nice Graphs
#
# This script takes the objects.cache file ifrom / and then writes the files Hosts.dot
# and ContactsAndGroups.dot in the  current directory. (Existing files will be overwritten)
# Theese files can be used with the Graphviz-Tool dot to create a graphical overview about some
# of the many aspects of the Nagios-Configuration.
# Tested with Nagios 2.4
#
# One graph shows [Parent] ---> Hosts ---> Services ---> Contactgroups
#                                      +-> Contactgroups
#
# the other Contactgroups ---> Contacts ---> Service-Notification-Command
#                                        +-> Host-Notification-Command
#
#
# Possible ToDos:
#  - Help function
#  - Extract colors and labels from awk to a common position
#  - More control of dot from this script
#
#
#




######################################################################################################
##
#	Graph for Hosts/Services and Contacts

echo "digraph \"Nagios Hosts und Services\" {" >Hosts.dot
awk '	{
		# First we look at the config-object as Row and at the lines as fields
		RS="define"
		FS="\n"
		HCOLOR="lightyellow2"		# Color for Hosts
		SCOLOR="lightblue2"		# for Services
		CCOLOR="salmon2"		# and for Contactgroups
	}

	{
##### Step 1: Get information from the Serviceentrys
		if ($1 == " service {") {     #}
			# Init some vars
			hostname = ""
#			servicename = ""

			# Now for every line in the config-object ...
			split($0,conf,"\n")
			for (i in conf) {
				# ... The line is split in Fieldname and Value ...
				split(conf[i],confline,"\t")
				# ... and if it is a line starting with tab (not the starting line) and not empty
				# like the last line with the final brace ...
				if (and((length(confline[1]) == 0),(length(confline[3]) != 0))) {
					# ... and the fieldname is correct ...
#					if (confline[2] == "service_description") {
						# ... the variable is filled.
#						servicename = confline[3]
#					}
					if (confline[2] == "host_name") {
						hostname = confline[3]
					}
				}
			}
			# When finished we can start printing the information for the formater:
			# First the simple edge-definition, where we want a Serviceentry for each host and no two servers ponting to one http-service
#			print "\""hostname "\" -> \""hostname"+"servicename"\";"
			# The service should be labeld with name, checkperiod and a niche color
#			print "\""hostname"+"servicename"\" [label=\""servicename"\\n\",color="SCOLOR",style=filled];"
			# There may be multiple contactsgroups in one field so split them
			for (grp in carray) {
				# The each service-bubble gets a notification group, the edges are labeled with period and options
#				print "\""hostname"+"servicename"\" -> \""hostname"+"servicename"+"carray[grp]"\";"
#				print "\""hostname"+"servicename"+"carray[grp]"\" [label=\""carray[grp]"\"color="CCOLOR",style=filled];"
			}
		}

##### Step 2: Add information from the Hostentrys
		if ($1 == " host {") {     #}
			hostname = ""
			haddress = ""
			parents = ""

			split($0,conf,"\n")
			for (i in conf) {
				split(conf[i],confline,"\t")
				if (and((length(confline[1]) == 0),(length(confline[3]) != 0))) {
					if (confline[2] == "host_name") {
						hostname = confline[3]
					}
					if (confline[2] == "address") {
						haddress = confline[3]
					}
					if (confline[2] == "parents") {
						parents = confline[3]
					}
				}
			}
			# Write the contactgroups for the hosts and label the edges
			# Again with an extra split for multiple groups on one host
#			split(carray,",")
			for (grp in carray) {
                                print "\""hostname"\" -> \""hostname"+"carray[grp]"\";"
				print "\""hostname"+"carray[grp]"\" [label=\""carray[grp]"\"color="CCOLOR",style=filled];"
			}
			# If a parent is given take that into account
			if (length(parents) > 0) {
				split(parents,parray,",")
				for (prnt in parray) {
					print "\""parray[prnt]"\" -> \""hostname"\";"
				}
			}
			# Finally give a nice label and color
			print "\""hostname"\" [label=\""hostname"\\n"haddress"\",color="HCOLOR",style=filled];"
		}
	}' objects.cache >>Hosts.dot
echo "}" >>Hosts.dot
dot -Glabel="Generated on: `date`" -Glabeljust=l -Glabelloc=b -Tpng -o./Hosts.png ./Hosts.dot
