Monday, August 31, 2009

How To Run MRTG as a Windows Service with FireDaemon

Introduction
Multi Router Traffic Grapher (MRTG) is an excellent tool to monitor the traffic load on network-links (amongst other things). It achieves this by examining the various OID's of SNMP enabled devices that you specify to produce a range of graphs. It is written in Perl.

It is relatively easy to setup MRTG to run as a service under Windows NT/2K/XP/2K3. However, before you begin we recommend that your read ALL the documentation shipped with MRTG and available from the MRTG master web site: http://oss.oetiker.ch/mrtg/.

Detailed documentation on configuring MRTG under Windows can be found here: http://oss.oetiker.ch/mrtg/doc/mrtg-nt-guide.en.html

We strongly recommend that your get MRTG going manually before attempting to run it as a service (ie. make it the last step in your installation process). The example that we give below is very simple and concentrates on configuring MRTG and FireDaemon correctly.

Attempting to run MRTG off or generating output to network drives is not recommended. MRTG and FireDaemon should be installed locally. If you really have to use a network drives familiarise yourself with the use and limitations of UNC paths and install the service as a local or domain user. Refer to this article on configuring FireDaemon to use mapped drives or UNC paths.

You can also use Trinity to manage your MRTG services via a web browser.

Software
Download and install the latest version of the following software (adjust paths to suite your installation): SNMP Setup
Your SNMP configuration will be dependent on the device or software you are setting up. For example, on a Foundry Networks ethernet switch you might configure SNMP as follows:

Code:
aaa authentication snmp-server default local
snmp-server community public ro
snmp-server community private rw
snmp-server contact James Bourne
snmp-server location Cobbitty, NSW, Australia

interface ethernet 1/1
port-name External Interface
interface ethernet 1/2
port-name Internal Interface
On Astaro Security Linux you might configure it as follows:

This image has been resized. Click this bar to view the full image. The original image is sized 1020x770.


On a DLink DSL-G604T you might configure it as follows:

This image has been resized. Click this bar to view the full image. The original image is sized 841x634.


Web Server
You are going to need to setup a web server in order to serve the HTML pages MRTG generates. LightTPD, IIS or Apache all work fine under Windows. You will need to create a directory under your web server that MRTG can write to. For example: C:\Program Files\Apache2\htdocs\mrtg or C:\lighttpd\htdocs\mrtg.

MRTG Configuration File
Next step is to create an MRTG configuration file. Your community strings and IPs are specific to your installation. Do this as follows at a command prompt (Start/Run/cmd):

Code:
cd \program files\mrtg-2.14.3\bin
perl cfgmaker public@10.1.1.1 --output dlink.cfg
You then need to edit the configuration file that you just generated (in this case dlink.cfg). Use notepad or your favourite text editor and insert the the following lines at the start:

Code:
RunAsDaemon: yes
Interval: 5
WorkDir: C:\PROGRA~1\APACHE~1\APACHE2\HTDOCS\MRTG
Change WorkDir to where you want MRTG to generate it's HTML files and PNG graphs. MRTG won't accept a WorkDir with spaces in the name. Either use a WorkDir path with no spaces or use the 8.3 abbreviation. (ie. C:\PROGRA~1\APACHE~1\APACHE2\HTDOCS\MRTG). A quick way of determining the 8.3 naming of your path is to run a Command Prompt (Start/Run/cmd), then change directory to your WorkDir, then type "command" - the DOS prompt will display the path in 8.3 format.

Also, note that setting your Interval to less than 5 minutes will cause an error. Now run MRTG manually to test your configuration as follows (you might have to kill off MRTG via the Task Manager since it has been daemonised):

Code:
perl mrtg --logging=mrtg.log dlink.cfg
Check your WorkDir and confirm that the various HTML and PNG files have been generated and that there are no errors in the log file (mrtg.log). Alternately, you can log to the Windows EventLog by substituting "eventlog" for "mrtg.log" above.

FireDaemon Configuration
Once you are satisfied that your MRTG configuration is doing what you want and generating the appropriate graphs, you need to set it up as a service with FireDaemon. Start the FireDaemon Service Manager from the Start/Programs menu or desktop icon.

Click on the Create A New Service Definition button in the Toolbar or press Ctrl+N. Fill out the panel as per the screen shot below (adjust your paths and configuration file names to suite) - you can use the TAB or SHIFT+TAB keys to move between fields:



Now click on the Settings tab and uncheck Interact with Desktop:



Then install the service by clicking on the Install button. The service should install without errors and start automatically. Check that the service has been started correctly by examining the process list in the Task Manager (look for firedaemon.exe and perl.exe) and that the MRTG statistics are being updated (point your browser at http://localhost/mrtg/). You can also check the NT/2K/XP/2K3 event log for FireDaemon events and errors.

Running Multiple MRTG Instances
You can run multiple instances of MRTG on the same machine. Just ensure that you create a service with a differing name (eg. mrtg1, mrtg2 etc.). It is also advisable to configure each MRTG instance independantly of each other (ie. differing configuration files, target HTML directories and so forth).

Exporting FireDaemon Service Configurations
FireDaemon service configurations can be exported to XML. Simply drag and drop the service definition from the main FireDaemon GUI onto your desktop. Alternately, you can bulk import/export via batch script.

Troubleshooting
OK, so you have installed MRTG, tested the installation to the n-th degree and you now notice that your Application Event Log is filling up with a three FireDaemon error messages:
  • The FireDaemon Process was Started
  • The Service Failed To Shutdown Correctly ...
  • SubProcess Monitoring Failed ...
The error messages are generated by FireDaemon's Service Monitoring Facility (SMF). In a nutshell: MRTG is dying because of a misconfiguration. So how do you debug your configuration? There are several approaches but you are going to need: a text editor that handles Unix end of line characters (eg. VIM), your trusty Perl reference manual and some patience.

MRTG from versions 2.9.17 onwards include a command line switch: --logging, which allows all debugging and other output to be written to a file.

The most common error is that the MRTG configuration file is broken. For example, spaces in front of configuration directives (as listed above) will break your MRTG installation. If you are writing your output to a directory located on a network drive then you are going to have to setup your service to run as a local or domain user in order to access shares via mapped drive letter or UNC path.

Below is one approach to track down MRTG errors.
  • Ensure that your MRTG Service has been stopped via FireDaemon Service Manager, Services Applet or command line: net stop mrtg
  • Ensure that you have read _ALL_ the MRTG documentation
  • Ensure that you have followed this guide to letter!
  • Open a console window (command prompt - Start/Run/cmd.exe)
  • Change directory to your MRTG bin folder (eg. C:\Program Files\mrtg-2.14.3\bin)
  • Edit the file 'mrtg' with your text editor (eg. gvim mrtg)
  • Look for the following line: @main:EBUG=qw();
  • Modify this line to read the following: @main:EBUG=qw(base);
  • Save and quit out of your text editor
  • Now attempt to run MRTG damonised manually: perl mrtg -logging=mrtg.log astaro.cfg
  • If you want even more debugging: perl -w mrtg --logging=mrtg.log dlink.cfg (or -W)
  • Any configuration or processing errors will be easy to spot
  • Review the contents of your log file if you specified the --logging option
If your MRTG configuration is good then you should see something like this in the log file:

Code:
2006-05-22 03:31:54 -- Started mrtg with config 'dlink.cfg'
2006-05-22 03:31:54 -- --base: Creating Lockfiles dlink.cfg_l,dlink.cfg_l_932
2006-05-22 03:31:54 -- --base: Reading Config File: dlink.cfg
2006-05-22 03:31:54 -- --base: Reading Interface Config cache
2006-05-22 03:31:54 -- --base: Checking Config File
2006-05-22 03:31:54 -- --base: Loading default Locale
2006-05-22 03:31:54 -- --base: Starting main Loop
2006-05-22 03:31:54 -- --base: Populate Target object by polling SNMP and external Datasources
2006-05-22 03:31:54 -- --base: Act on Router/Target 10.1.1.1_2
2006-05-22 03:31:54 -- --base: Get Current values: in:0, out:453298, up:8 days, 13:03:55, name:DSL-G604T, time:1148232714
2006-05-22 03:31:54 -- --base: Create Graphics
2006-05-22 03:31:54 -- --base: Check for Thresholds
2006-05-22 03:31:54 -- --base: Check for Write HTML Pages
2006-05-22 03:31:54 -- --base: Act on Router/Target 10.1.1.1_3
2006-05-22 03:31:54 -- --base: Get Current values: in:52374150, out:19040990, up:8 days, 13:03:55, name:DSL-G604T, time:1148232714
2006-05-22 03:31:54 -- --base: Create Graphics
2006-05-22 03:31:54 -- --base: Check for Thresholds
2006-05-22 03:31:54 -- --base: Check for Write HTML Pages
2006-05-22 03:31:54 -- --base: Act on Router/Target 10.1.1.1_5
2006-05-22 03:31:54 -- --base: Get Current values: in:0, out:0, up:8 days, 13:03:55, name:DSL-G604T, time:1148232714
2006-05-22 03:31:54 -- --base: Create Graphics
2006-05-22 03:31:54 -- --base: Check for Thresholds
2006-05-22 03:31:54 -- --base: Check for Write HTML Pages
2006-05-22 03:31:54 -- --base: Act on Router/Target 10.1.1.1_6
2006-05-22 03:31:54 -- --base: Get Current values: in:15392606, out:52639324, up:8 days, 13:03:55, name:DSL-G604T, time:1148232714
2006-05-22 03:31:54 -- --base: Create Graphics
2006-05-22 03:31:54 -- --base: Check for Thresholds
2006-05-22 03:31:54 -- --base: Check for Write HTML Pages
Search throught this file looking for any errors. For example:

Code:
2006-05-22 03:34:34 -- Started mrtg with config 'dlink.cfg'
2006-05-22 03:34:34 -- --base: Creating Lockfiles dlink.cfg_l,dlink.cfg_l_1164
2006-05-22 03:34:34 -- --base: Reading Config File: dlink.cfg
2006-05-22 03:34:34 -- --base: Reading Interface Config cache
2006-05-22 03:34:34 -- --base: Checking Config File
2006-05-22 03:34:34 -- ERROR: CFG Error in "Interval": should be at least 5 Minutes (unless you use rrdtool) at c:/program files/mrtg-2.14.3/bin\..\lib\mrtg2/MRTG_lib.pm line 737.
2006-05-22 03:34:40 -- Started mrtg with config 'dlink.cfg'
2006-05-22 03:34:40 -- --base: Creating Lockfiles dlink.cfg_l,dlink.cfg_l_1844
2006-05-22 03:34:40 -- --base: Reading Config File: dlink.cfg
2006-05-22 03:34:40 -- --base: Reading Interface Config cache
2006-05-22 03:34:40 -- --base: Checking Config File
2006-05-22 03:34:40 -- ERROR: CFG Error in "Interval": should be at least 5 Minutes (unless you use rrdtool) at c:/program files/mrtg-2.14.3/bin\..\lib\mrtg2/MRTG_lib.pm line 737.
Now attempt to restart your MRTG Service: net start mrtg. If you are still seeing error messages in your Application Event Log then review your debug log files. Also it is highly likely that the arguments you stipulated when you installed MRTG as a service with FireDaemon are erroneous. We suggest you do the following:

- Remove your existing service via the FireDaemon Service Manager or command line: "%FIREDAEMON%" --uninstall mrtg
- Reinstall the service with FireDaemon checking that the arguments you are passing to it are correct.

For example, if your MRTG configuration file is myrouter.cfg then when you specify your Application Options type: mrtg myrouter.cfg. DON'T TYPE: mrtg dlink.cfg because THIS WILL NOT WORK because you do not have a configuration file called dlink.cfg.

Issues When Running Multiple FireDaemon / MRTG Services
You may run into Application Initialization errors when running extremely large numbers of non-interactive FireDaemon/MRTG services (30-40+).

Whilst at first this might seem like a problem with FireDaemon it is actually because the default size of the non-interactive window station desktop heap is too small. In order to fix this we need to increase the size of the desktop heap for each desktop that is associated with a "noninteractive" window station.

Read this article for more information on how to increase the size of the desktop heap.

Summary
In summary, MRTG is a comprehensive yet complicated product. Breaking it via misconfiguration is extremely easy. We urge you to read the MRTG documentation carefully and fully test your configuration of MRTG in daemonised and non-daemonised modes employing the debugging tips listed above before attempting to convert it to a service with

From

No comments:

Post a Comment

Popular Posts