#! /usr/bin/perl

# Convert NagVis map cfg file to SVG
# ==================================
# by Hans Schou 2011
#
# This script can convert a simple line based NagVis
# map configuration to a SVG file.
#
# Syntax:
#  nagvis2svg.pl < <nagvis-map> > <output-svg>
# Example:
#  nagvis2svg.pl < MyMap.cfg > index.svg

$font_size = 12;
$radius = 10;
$NodeBoxSize = $radius * 5 / 2;
$NodeBoxHalf = $NodeBoxSize / 2;

print << "EOF";
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg viewBox="0 0 1300 800"
     xmlns="http://www.w3.org/2000/svg" version="1.1"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     >
  <title>Nagios/Icinga SVG demo</title>
  <desc>Example of how data from nagios/icinga can be displayed in SVG</desc>
  <script xlink:href="load-status-nagios-json.js" />
  <script type="application/ecmascript"> <![CDATA[
    function changeColor(evt) {
      var circle = evt.target;
      var currentRadius = circle.getAttribute("fill");
      if (currentRadius == "red")
        circle.setAttribute("fill", "green");
      else
        circle.setAttribute("fill", "red");
    }
  ]]> </script>

  <!-- Outline the drawing area with a blue line -->
  <rect x="1" y="1" width="1100" height="800" fill="none" stroke="blue"/>

  <!-- Test of 'use' -->
  <defs>
     <rect id="NodeBox" width="$NodeBoxSize" height="$NodeBoxSize" onclick="changeColor(evt)"/>
  </defs>
EOF

#define host {
#label_show=1
#label_x=-2
#label_y=+30
#label_text=Dirac
#only_hard_states=0
#recognize_services=1
#host_name=dirac.acme.com
#x=335
#y=600
#z=2
#}

#define textbox {
#text=<font size="3px"><b>Kontor</b></font>
#x=40
#y=70
#w=156
#background_color=#ffffff
#border_color=#ffffff
#}

my $in_host = 0;
while (<>) {
	chomp;
	if (/^\s*define\s+host\s*[{]/) {
		$in_host = 1;
	}
	if (/^\s*label_text=(.+)$/) {
		$label_text = $1;
	}
	if (/^\s*host_name=([^.]+)/) {
		$host_name = $1;
	}
	if (/^\s*x=(\d+)/) {
		$x = $1;
	}
	if (/^\s*y=(\d+)/) {
		$y = $1;
	}
	if (/^\s*[}]/) {
		if ($in_host) {
			print "<use xlink:href=\"#NodeBox\" x=\"".($x-$NodeBoxHalf)."\" y=\"".($y-$NodeBoxHalf)."\" />\n";
			print "<circle cx=\"$x\" cy=\"$y\" id=\"$host_name\" title=\"$label_text\" onclick=\"changeColor(evt)\" r=\"$radius\" fill=\"grey\"/>\n";
			#print "\t<animate attributeName=\"r\"  attributeType=\"XML\" begin=\"0s\" dur=\"1s\" fill=\"freeze\" from=\"10\" to=\"100\" repeatCount=\"indefinite\" />\n";
			#print "</circle>\n";
			print "<text x=\"$x\" y=\"".($y+$font_size+$radius)."\" font-family=\"Verdana\" font-size=\"$font_size\" text-anchor=\"middle\">$label_text</text>\n";
			$in_host = 0;
		}
	}
}

print << "EOF";
  <text onclick="loadStatus()" x="10" y="35" font-family="Verdana" font-size="35" text-anchor="left">
    Click at me
  </text>
</svg>
EOF
