Help-Site Computer Manuals
  Algorithms & Data Structures   Programming Languages   Revision Control
  Cameras   Computers   Displays   Keyboards & Mice   Motherboards   Networking   Printers & Scanners   Storage
  Windows   Linux & Unix   Mac

Perl module for parsing MRTG configuration files

MRTG::Config - Perl module for parsing MRTG configuration files


MRTG::Config - Perl module for parsing MRTG configuration files


I plan on adding to this documentation and making it better organized soon, but I'm willing to answer questions directly in the mean time.

  use MRTG::Config;

  my $cfgFile = 'mrtg.cfg';

  my $persist_file = 'mrtg.cfg.db'; 


  my $mrtgCfg = new MRTG::Config;






  foreach my $tgtName (@{$mrtgCfg->targets()}) {

    my $tgtCfg = $mrtgCfg->target($tgtName);

        print $tgtCfg->{Title} . "\n"; # Let's assume every target has a Title.



  my $globalCfg = $mrtgCfg->globals();

  print $globalCfg->{WorkDir} . "\n"; # Let's assume WorkDir is set.


I couldn't find any modules on CPAN that would parse MRTG config files, and Tobi's code in MRTG_lib is too slow and too complicated for my needs.

This module will load a given MRTG configuration file, following Include statements, and build a set of hashes representing the confiration keywords, targets, and values.

It's _much_ faster than Tobi's code, but it also does not build a data structure _nearly_ as deep and complex.

It *does*, however, properly handle a number of facilities of the MRTG configuration file format that are specified in the MRTG documentation.

The parsing code correctly handles directives where the value spans multiple lines (sucessive lines after the first begin with whitespace). Each line of the value is contatenated together, including newlines.

Include directives are also handled. When an Include is encountered, the value is used as the name of another MRTG configuration file to parse. Like in MRTG_lib, if the path is not absolute (beginning with / or C:\ or whatever your system uses) this file is looked for first in the same directory as the original configuration file, and then in the current working directory.

When an Included file is loaded, it's lines are inserted into the current position in the parsing buffer and then parsing continues, as if the contents of the included file were simply copied into that position in the original file.

While I have not yet tested it, I believe 'nested' includes are followed, and the same search and loading rules apply. The path of the _first_ config file is _always_ used when looking for included files.

WARNING: There is *no* loop-checking code. If File A includes File B and File B includes File A, the parser will run until your system goes p00f, eating up memory the whole way.

This module understands directives for the [_] (default) target and will interpolate these directives into all the targets that follow the definition of a [_] directive and do not explicitly define the given directive.

From what I can tell, in Tobi's implementation, [_] directives are only applied to targets that follow the definition of that particular directive. This module does likewise. Also, if a [_] directive is redefined later in the configuration, it's new value is used for all future targets. Targets that have already had that directive interpolated are *not* updated.

Also, if a particular target has a directive or directives defined more than once, the last definition in the file 'wins'. The same applies to the [_] target, and also to global directives.

This module is capable of some degree of persistience, by way of DBM::Deep. Using persistience will allow you to do all sorts of interesting things, which I will not get into right now, but if you're creative I'll bet you've already thought of some! Right now, only Global and target-specific directives are persisted.

Please note - I've found that performance with DBM::Deep varies WIDELY depending on what version of DBM::Deep you are using, and wether or not you allow cpan to upgrade it's dependencies - When I allowed cpan to update everything, performance dropped by AN ORDER OF MAGNITUDE.

For best performance, I suggest using DBM::Deep .94 and whatever versions of various core modules that come with Perl 5.8.8.

Most of my testing has been done on a stock Ubuntu 7.04. Some testing has been done on Windows XP SP2 with ActiveState Perl 5.8.8


Please email me if you have *any* questions, complaints, comments, compliments, suggestions, requests, patches, or alcoholic beverages you'd like to share. The more feedback I can get, the better I can make this module!


None by default.



Stephen R. Scaffidi


Copyright (C) 2007 by Stephen R. Scaffidi

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.