doc::mrtglib(3)
π doc::mrtglib β the MRTG library page, bestie edition π
okay so this is a page from the mrtg manual β yes, that mrtg. it's a section 3 page, so it's all about a library. sit down, grab a strawberry π, we're doing this.
β¨ NAME β¨
MRTG_lib.pm β a whole library for MRTG and the lil support scripts that hang out with it π«Ά
πΈ SYNOPSIS πΈ
use MRTG_lib;
my ($configfile, @target_names, %globalcfg, %targetcfg);
readcfg($configfile, \@target_names, \%globalcfg, \%targetcfg);
my (@parsed_targets);
cfgcheck(\@target_names, \%globalcfg, \%targetcfg, \@parsed_targets);
like. that's the whole vibe. pull it in, read the config, check the config. iconic behavior fr fr.
π¦ DESCRIPTION π¦
MRTG_lib is part of MRTG, the Multi Router Traffic Grapher. the way it got split off from MRTG is so other programs could just... easily use the same config files?? like sharing is caring π
. the main character of MRTG_lib is the config file parser, but there are a few other functions in there too, no cap.
π $MRTG_lib::OS
what kind of OS are we on: WIN, UNIX, VMS
π $MRTG_lib::SL
the slash in the current OS. yes it changes. it's giving adaptable β¨
π $MRTG_lib::PS
the path separator in the PATH variable
π readcfg
readcfg($file, \@targets, \%globalcfg, \%targetcfg [, $prefix, \%extrules])
this one reads a config file, parses it, and fills some arrays and hashes. slay.
the mandatory arguments are:
- the name of the config file
- a ref to an array which will be filled with a list of the target names
- a hashref for the global configuration
- a hashref for the target configuration
the configuration file syntax is:
globaloption: value
targetoption[targetname]: value
aprefix*extglobal: value
aprefix*exttarget[target2]: value
E.g.
workdir: /var/stat/mrtg
target[router1]: 2:public@router1.local.net
14all*columns: 2
the global config hash has the structure
$globalcfg{configoption} = 'value'
the target config hash has the structure
$targetcfg{configoption}{targetname} = 'value'
see mrtg-reference for more info about the MRTG configuration syntax π
now β readcfg can take two additional arguments to extend the config file syntax. this is the glow-up that lets programs put their own configuration into the mrtg config file. the fifth argument is the prefix of the extension, and the sixth argument is a hash with the checkrules for these extension settings. E.g. if the prefix is 14all, readcfg will check config lines that begin with 14all*, i.e. all lines like
14all*columns: 2
14all*graphsize[target3]: 500 200
against the rules in %extrules. the format of this hash is:
$extrules{option} = [sub{$_[0] =~ m/^\d+$/}, sub{"Error message for $_[0]"}]
i.e.
$extrules{option}[0] -> a test expression
$extrules{option}[1] -> error message if test fails
the first part of the array is a perl expression to test the value of the option. the test can access this value in the variable $arg. the second part of the array is an error message to display when the test fails. the failed value can be integrated by using the variable $arg.
config settings with a different prefix than the one given in the readcfg call are not checked β they just get inserted into %globalcfg and %targetcfg. prefixed settings keep their prefix in the config hashes:
$targetcfg{'14all*graphsize'}{'target3'} = '500 200'
π cfgcheck
cfgcheck(\@target_names, \%globalcfg, \%targetcfg, \@parsed_targets)
checks the configuration read by readcfg. it checks the values in the config for syntactical and/or semantical errors. it sets defaults for some options. and it parses the target[...] options and files the array @parsed_targets ready for mrtg functions. multitasking queen π
the first three arguments are the same as for readcfg. the fourth argument is an arrayref which will be filled with the parsed target defs.
cfgcheck converts the values of target settings options, e.g.
options[router1]: bits, growright
to a hash:
$targetcfg{'option'}{'bits'}{'router1'} = 1
$targetcfg{'option'}{'growright'}{'router1'} = 1
this is not done by readcfg, so if you don't use cfgcheck you have to check the scalar variable $targetcfg{'option'}{'router1'} (MRTG allows options to be separated by space or ,). iykyk.
π ensureSL
ensureSL(\$pathname)
checks that the pathname does not contain double path separators and ends with a path separator. it uses $MRTG_lib::SL as path separator, which will be / or \ depending on the OS. flexible icon.
π log2rrd
log2rrd ($router,\%globalcfg,\%targetcfg)
convert log file to rrd format. needs rrdtool.
π datestr
datestr(time)
returns the time given in the argument as a nicely formatted date string. the argument has to be in UNIX time format (seconds since 1970-1-1). cute AND precise β¨
π timestamp
timestamp()
return a string representing the current time. it's giving right now β°
π setup_loghandlers
setup_loghandlers(filename)
install signalhandlers for __DIE__ and __WARN__ making the errors go the the specified destination. if filename is 'eventlog' mrtg will log to the windows event logger.
π expistr
expistr(time)
returns the time given in the argument formatted suitable for HTTP Expire-Headers.
π create_pid
create_pid()
creates a pid file for the mrtg daemon
π demonize_me
demonize_me()
puts the running program into background, detaching it from the terminal. going ghost mode π«₯ (yes, the function is spelled demonize_me, that's just the name, don't @ me)
π§ populatecache
populatecache(\%confcache, $host, $reread, $snmpoptshash)
reads the SNMP variables ifDescr, ipAdEntIfIndex, ifPhysAddress, ifName from the host and stores the values in %confcache as follows:
$confcache{$host}{'Descr'}{ifDescr}{oid} = (ifDescr or 'Dup')
$confcache{$host}{'IP'}{ipAdEntIfIndex}{oid} = (ipAdEntIfIndex or 'Dup')
$confcache{$host}{'Eth'}{ifPhysAddress}{oid} = (ifPhysAddress or 'Dup')
$confcache{$host}{'Name'}{ifName}{oid} = (ifName or 'Dup')
$confcache{$host}{'Type'}{ifType}{oid} = (ifType or 'Dup')
the value (at the right side of =) is 'Dup' if a value was retrieved multiple times, the retrieved value else.
π§ readconfcache
my $confcache = readconfcache($file)
preload the confcache from a file.
π§ readfromconfcache
writeconfcache($confcache,$file)
store the current confcache into a file.
bestie. bestie look at me. yes, the page really does print writeconfcache under this heading. i'm just the messenger π keeping it exactly as it's written.
π§ writeconfcache
writeconfcache($confcache,$file)
store the current confcache into a file.
π§ storeincache
storeincache($confcache,$host,$method,$key,$value)
π§ readfromcache
readfromcache($confcache,$host,$method,$key)
π§ clearfromcache
clearfromcache($confcache,$host)
π debug
debug($type, $message)
prints the message on STDERR if debugging is enabled for type type. a debug type is enabled if type is in array @main::DEBUG.
π AUTHORS π
Rainer Bawidamann <Rainer.Bawidamann@rz.uni-ulm.de>
(This Manpage)
and that's the whole thing, bestie, we ate and left no crumbs π the page is from version 2.17.10, dated 2022-01-19 β shoutout to that exact moment in time, you were everything β¨πΈπ¦
π¦ where this page comes from
- alpine 3.22: mrtg-doc 2.17.10-r1 Β· GPL-2.0-or-later
- alpine 3.23: mrtg-doc 2.17.10-r1 Β· GPL-2.0-or-later
- alpine 3.24: mrtg-doc 2.17.10-r2 Β· GPL-2.0-or-later
- centos-stream 10: mrtg 2.17.10-13.el10 Β· GPL-2.0-or-later
- debian bookworm: mrtg 2.17.10-5+deb12u2 Β· GPL-2.0-or-later
- debian trixie: mrtg 2.17.10-13+deb13u2 Β· GPL-2.0-or-later
- fedora 43: mrtg 2.17.10-12.fc43 Β· GPL-2.0-or-later
- fedora 43: mrtg 2.17.10-13.fc43 Β· GPL-2.0-or-later
- fedora 44: mrtg 2.17.10-13.fc44 Β· GPL-2.0-or-later
- fedora 44: mrtg 2.17.10-14.fc44 Β· GPL-2.0-or-later
- nixos 26.05: mrtg 2.17.10
- nixpkgs unstable: mrtg 2.17.10
- ubuntu jammy: mrtg 2.17.10-3 Β· GPL-2.0-or-later
- ubuntu noble: mrtg 2.17.10-12 Β· GPL-2.0-or-later
- ubuntu questing: mrtg 2.17.10-13
- ubuntu resolute: mrtg 2.17.10-14 Β· GPL-2.0-or-later
- ubuntu stonking: mrtg 2.17.10-16 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms