# -*- perl -*- package Smokeping::Examples; use strict; use Smokeping; =head1 NAME Smokeping::Examples - A module for generating the smokeping_examples document =head1 OVERVIEW This module generates L and the example configuration files distributed with Smokeping. It is supposed to be invoked from the smokeping distribution top directory, as it will need the C template configuration file and will create files in the directories C and C. =head1 DESCRIPTION The entry point to the module is the C subroutine. It takes one optional parameter, C, that makes the module run a syntax check for all the created example configuration files. =head1 BUGS This module uses more or less internal functions from L. It's a separate module only because the latter is much too big already. It should be possible to include POD markup in the configuration explanations and have this module filter them away for the config files. It might be nice for the probe module authors to be able to provide an example configuration as part of the probe module instead of having to modify Smokeping::Examples too. =head1 COPYRIGHT Copyright 2005 by Niko Tyni. =head1 LICENSE This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. =head1 AUTHOR Niko Tyni =cut use strict; sub read_config_template { my $file = "../etc/config.dist"; my $h = { common => "", # everything up to the Probes section probes => "", # the Probes section, without the *** Probes *** line targets => "", # the Targets section, without the *** Targets *** line }; open(F, "<$file") or die("open template configuration file $file for reading: $!"); my %found; while () { /\*\*\*\s*(Probes|Targets)\s*\*\*\*/ and $found{$1} = 1, next; $h->{common} .= $_ and next unless $found{Probes}; $h->{probes} .= $_ and next unless $found{Targets}; $h->{targets} .= $_; } close F; return $h; } sub prologue { my $e = "="; return < directory in the Smokeping documentation. Note that the DNS names in the examples are non-functional. Details of the syntax and all the variables are found in L and in the documentation of the corresponding probe, if applicable. This manual is automatically generated from the Smokeping source code, specifically the L module. ${e}head1 DESCRIPTION Currently the examples differ only in the C and C sections. The other sections are taken from the C configuration template in the Smokeping distribution so that the example files are complete. If you would like to provide more examples, document the other sections or enhance the existing examples, please do so, preferably by sending the proposed changes to the smokeping-users mailing list. DOC } sub epilogue { my $e = "="; return < ${e}head1 SEE ALSO The other Smokeping documents, especially L. DOC } sub make { print "Generating example files...\n"; my $check = shift; # check the syntax of the generated config files my $template = read_config_template(); my $examples = examples($template); my $manual = prologue(); for my $ex (sort { $examples->{$a}{order} <=> $examples->{$b}{order} } keys %$examples) { my $h = $examples->{$ex}; $manual .= "\n=head2 Example $h->{order}: config.$ex\n\n" . genpod($h); my $cfgfile = "examples/config.$ex"; print "\t$cfgfile ...\n"; writecfg($cfgfile, $template, $h); if ($check) { local $Smokeping::cfg = undef; eval { Smokeping::verify_cfg($cfgfile); }; die("Syntax check for $cfgfile failed: $@") if $@; } } $manual .= epilogue(); writemanual($manual); print "done.\n"; } sub writemanual { my $text = shift; my $filename = "smokeping_examples.pod"; print "\t$filename ...\n"; open(F, ">$filename") or die("open $filename for writing: $!"); print F $text; close F; } sub genpod { my $h = shift; my $text = ""; $text .= "=over\n\n"; $text .= "=item Probe configuration\n\n"; $text .= " *** Probes ***\n"; $text .= join("\n", map { " $_" } split(/\n/, $h->{probes})); $text .= "\n\n=item Probe explanation\n\n"; $text .= $h->{probedoc} || "No probedoc found !"; $text .= "\n\n=item Target configuration\n\n"; $text .= " *** Targets ***\n"; $text .= join("\n", map { " $_" } split(/\n/, $h->{targets})); $text .= "\n\n=item Target explanation\n\n"; $text .= $h->{targetdoc} || "No targetdoc found !"; $text .= "\n\n=back\n\n"; return $text; } sub writecfg { my $file = shift; my $template = shift; my $h = shift; open(F, ">$file") or die("open $file for writing: $!"); print F <{common}; print F "# (The actual example starts here.)\n"; print F "\n*** Probes ***\n\n"; print F join("\n", map { "# $_" } split(/\n/, $h->{probedoc} || 'No probedoc found!')); print F "\n\n"; print F $h->{probes}; print F "\n*** Targets ***\n\n"; print F join("\n", map { "# $_" } split(/\n/, $h->{targetdoc} || 'No targetdoc found')); print F "\n\n"; print F $h->{targets}; close F; } sub examples { my $template = shift; return { simple => { order => 1, probes => < < < < { order => 2, probes => < < < < { order => 3, probes => < < < < { order => 4, probes => < < < < { order => 5, probes => < < < < { order => 6, # last probes => $template->{probes}, targets => $template->{targets}, probedoc => < <