Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Visualize Nagios Configuration using GraphViz and Perl Usage: recursive copy your /etc/nagios or /etc/icinga to a box of your choosing. Make sure : - GraphViz2, - Nagios::Object::Config, - String::Random from CPAN are installed. From within your copy of the nagios configuration run: perl script.pl -n nagios.cfg -o output.svg Be *amazed* by the s…
#!/usr/bin/perl
=head1 NAME
nagios-view - View config of nagios in SVG/PNG
=head1 SYNOPSIS
nagios-view --nagioscfg nagios.cfg --output nagios-config-visualized.svg
=head1 DESCRIPTION
This script is meant as a visualization of your nagios or icinga configuration.
It uses GraphViz2 and Nagios::Object::Config to parse and create this graph.
=cut
use 5.010_000;
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use IO::File;
use Pod::Usage;
use Nagios::Object::Config;
use GraphViz2;
use String::Random;
our $VERSION = '0.1';
my ($nagioscfg,$output);
my $result = GetOptions (
"o|output=s" => \$output,
"n|nagioscfg=s" => \$nagioscfg,
"h|help" => sub { pod2usage(-exitval => 0,
-verbose => 99,
-noperldoc => 1) });
my $config = Nagios::Object::Config->new();
if (defined $nagioscfg) {
$config->parse($nagioscfg);
} else{
die pod2usage(-message => "ERROR: No nagios configuration!\n",
-exitval => 1,
-verbose => 99,
-noperldoc => 1) ;
}
my $rand_color = new String::Random;
my $graph = GraphViz2->new(
edge => {color => 'grey'},
global => {directed => 1},
graph => {label => 'Nagios Configuration Visualization',
splines => "spline",
packmode => "node",
pack => "true"
},
node => {shape => 'rectangle'});
my @hosts = $config->list_hosts;
my @hostgroups = $config->list_hostgroups;
my @services = $config->list_services;
my @servicegroups = $config->list_servicegroups;
my @contacts = $config->list_contacts;
my @contactgroups = $config->list_contactgroups;
#my @hostdependencies = $config->list_hostdependencies;
#my @servicedependencies = $config->list_servicedependencies;
foreach (@servicegroups) {
$graph -> add_node(name => $_->name, shape => "circle", color => $rand_color->randregex("\#[0-9a-f]{6}"));
}
foreach my $service (@services) {
$graph -> add_node(name => $service->name, shape => "trapezium", color => $rand_color->randregex("\#[0-9a-f]{6}"));
foreach my $service_servicegroups ($service->servicegroups()) {
if (defined $service_servicegroups) {
if (ref($service_servicegroups) eq "ARRAY") {
foreach my $servicegroup (@$service_servicegroups) {
add_edge($service->name,$servicegroup, "(Service)", "(Service Group)", " part of ");
}
} elsif (ref($service_servicegroups eq "SCALAR")) {
add_edge($service->name,$service_servicegroups, "(Service)", "(Service Group)", " part of ");
}
}
}
foreach my $service_hostgroups ($service->hostgroup_name()) {
if (defined $service_hostgroups) {
if (ref($service_hostgroups) eq "ARRAY") {
foreach my $service_hostgroup (@$service_hostgroups) {
add_edge($service->name,$service_hostgroup, "(Service)", "(Host Group)", " part of ");
}
} elsif (ref($service_hostgroups eq "SCALAR")) {
add_edge($service->name,$service_hostgroups, "(Service)", "(Host Group)", " part of ");
}
}
}
if (defined $service->host_name() ) {
add_edge($service->name,$service->host_name(), "(Service)", "(Host)", " used for ");
}
if (defined $service->use()) {
add_edge($service->name,$service->use(),"(Service)","(Service Template)", " inherits from ");
}
}
foreach (@hostgroups) {
$graph -> add_node(name => $_->name, shape => "circle", color => $rand_color->randregex("\#[0-9a-f]{6}"));
}
foreach my $host(@hosts) {
$graph->add_node(name => $host->name, shape => 'rectangle');
foreach my $host_hostgroups ($host->hostgroups()) {
if (defined $host_hostgroups) {
if (ref($host_hostgroups) eq "ARRAY") {
foreach my $hostgroup (@$host_hostgroups) {
add_edge($host->name,$hostgroup,"(Host)","(Host Group)"," part of ");
}
} elsif (ref($host_hostgroups eq "SCALAR")) {
add_edge($host->name,$host_hostgroups,"(Host)","(Host Group)"," part of ");
}
}
}
if (defined $host->use()) {
add_edge($host->name,$host->use(),"(Host)","(Host Template)"," inherits from ");
}
}
if (defined $output) {
$graph -> run(format => "svg", output_file => $output);
} else {
$graph -> run(format => "svg", output_file => "./nagios.cfg.svg");
}
sub add_edge {
my ($from,$to,$type_from,$type_to,$verb) = @_;
$graph -> add_edge(from => $from,
to => $to,
arrowsize => 2.0,
label => $from.$type_from.$verb.$to.$type_to,
label => $from.$type_from.$verb.$to.$type_to,
tooltip => $from.$type_from.$verb.$to.$type_to,
comment => $from.$type_from.$verb.$to.$type_to,
color => $rand_color->randregex("\#[0-9a-f]{6}"));
}
@owenBoyle

This comment has been minimized.

Copy link

owenBoyle commented Nov 19, 2015

Greetings. I am looking for something to help us manage the object hierarchies in our Nagios configuration. I tried your script against our config and it ran without error message and produced an output.svg containing only the headline text (Nagios Configuration Visualization) but no images.

I'm guessing it failed to make sense of our top-level config file. Are there any pre-requisites on this file? Any special data it is expecting to find?

Best regards,
Owen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.