AlanBarber.Org
Customized MRTG to Access Windows Performance Logs
You can get the original from the MRTG Multi Router Traffic Grapher website.
Requirements
1) Windows 2000 or XP Pro system.
2) Perl for win32. Preferably from ActiveState.
3) Your own copy of MRTG.
Background
I had been using MRTG for a while now, since version
2.9.18pre1, and I loved how it could graph my network card's bandwidth
usage in windows. While at college this was interesting to see how
much I used on a daily basis with surfing, email, file sharing, etc.
However back home for the summer and stuck with dialup it got pretty
boring. Since I only access the net through a shared dialup the MRTG
graphs rarely show numbers higher than 6KB. So I decided to setup
MRTG to start graphing other information about my system such as cpu and
memory usage.
** UPDATE **
Just a notice that I no longer use MRTG but I'm leaving this page up for reference. It should be rather obvious considering I haven't updated the script for newer versions of MRTG.
The Problem
I decided to use the windows performance monitor logs method since it seemed to be the most reliable. I used Paul Simmonson's scripts to set it up and while it worked there was one small but annoying flaw. The problem was every 5 minutes while MRTG was running a windows would pop up for a split second. This became very annoying very quickly. So I set out to figure out a way to stop this. Incase you want to know why this happens, the reason is because in order to create the graphs MRTG calls Paul's external scripts. Each time it calls this script a new window is opened as the script runs.
It seemed the only way to stop this problem was to use a program like FireDaemon to run MRTG as a windows service. However I didn't like running MRTG as a service and I didn't want to have to run another program just to get MRTG to work right so I had to try something else.
The Solution
After doing some thinking I figured I could just rewrite MRTG so it didn't have to load Paul's scripts externally. I decided to rewrite Paul's script into a subroutine that the MRTG program could access directly. It would allow MRTG to run and access the performance logs without the hassle of opening a system shell just to call Paul's script..
When MRTG loads a configuration file it can tell if it will use SNMP to get data or if it calls an external program to get data. However it turns out it's not smart enough to tell if the external program call is actually legit. If it isn't it finds out when it try's to call the program and it can't.
So all I had to do was rewrite the external call subroutine to detect that you want to call a subroutine and not an external program. I did this in around 20 lines of code. I'm sure a better programmer could do it in less but hey TMTOWTDI. :)
Your probably tired of me talking so lets get to the good stuff!
| Files
Modified MRTG Script (version 2.9.25) Modified mkcfg.pl (original by Paul Simmonson) Modified getlog_sub.pl (original by Paul Simmonson) |
Changes |
Instructions
The first thing you have to do is get your copy of MRTG modified. You can either download the copy above or check out the changes and modify it yourself. As stated above the modified version is based on MRTG version 2.9.25. If a new version is released and I haven't updated it then you'll have to modify it yourself.
Next you need to download and save the modified mkcfg.pl and getlog_sub.pl to the MRTG bin folder.
I'm not going to tell you how to setup the windows performance monitor stuff because honestly if you can't figure that out I doubt you even know what this website is about! However if you do need help check out Paul Simmonson's website. Just read the parts about setting the performance monitor up.
UPDATE: Apparently Paul's website is long gone. Thank goodness for the Wayback Machine you can find an archived copy of his website here.
UPDATE 2: Looks like Pauls website was just moved. Thanks goes to Gilles Dietschy for pointing out the new address for me!
Assuming your performance log file is at "c:\perflogs\stats.cvs"
Now at a command prompt (in the MRTG bin folder)
type this:
>perl mkcfg.pl c:\perflogs\stats.cvs > stats.cfg
If you check in the MRTG bin folder there should be a stats.cfg file now. Open it in a text editor and modify it to your liking. You'll want to change the WorkDir variable and probably add the standard RunAsDaemon: yes.
Now you can run MRTG with your config file. If everything was setup right you should start getting graphs of your windows systems such as cpu, memory, etc
!! NOTICE !! I use ActiveState's Perl interpreter on Windows XP Pro. They include a special version of Perl called wperl. wperl doesn't open a window and stays hidden. since you want to run MRTG all the time it makes sense not to run it in a window.
Example
To see an example of what MRTG can do here is what my MRTG setup looks like click here.