Welcome To mkmrtg.sh

Hi! If your here it must mean your familiar with MRTG. The basis of this page is to help you with a front-end for MRTG. A quick over-view can be found to the right in the Who, What, Why, Where, How section.

Currently, mkmrtg.sh is up to Version 0.9
Remember to check out the stuff on the right.
Then click here to jump to the code. :)

------------------------------------------

Friday, September 5, 2008

Version 0.9

#! /bin/bash

# Version 0.9

### CONFIG ###
# What Kind Of Image Output Does Your MRTG Setup Produce? PNG or GIF?
pic="png"
# Where Is Your MRTG Output Located?
loc="/srv/www/htdocs/mrtg"
# Where Are Your Notes?
notes="/usr/local/bin/mkmrtg.sh.notes"

### USAGE ###
#` Place this script in /usr/local/bin (or /usr/bin)
# chmod 700 mkmrtg.sh
# ./mkmrtg.sh
# Note: This script assumes your nodes are all in the same place.
# Example:
# /srv/www/htdocs/mrtg/
# /srv/www/htdocs/mrtg/NODE1/
# /srv/www/htdocs/mrtg/NODE2/
# Then point your browser to the local machine (however you setup your Apache).


### RELEASE NOTES ###
# Yeah! mkmrtg.sh can now be run from anywhere on your system. My suggestion is to keep it in some place like "/usr/local/bin".
# Depending on how you installed MRTG, you may have "gif" images instead of "png" images. If so, change "png" to "gif" at the top.
# If your output from MRTG is not located in "/srv/www/htdocs/mrtg" then you need to change the location variable "loc" at the top to the place you pointed your MRTG output.
# Please make sure there are NO "-" (dashes) in your interface identifyer! Example: "eth0-1" is NOT good. Use "eth0_1" instead. :)
# NOTES Section For Your Devices. Be default it is located at /usr/local/bin/mkmrtg.sh.notes If this is not where you want your notes, change it. Notes should be added in the following syntax (making note that "folder name" should be the EXACT same as the folder name):
# [folder name]:[notes]
# Example= NODE1:Unable To Ping node1 as we are rejecting icmp so currently no data.

### FUTURE ###
# - PHP Integration
# - Web Based Mangement via PHP
# - Clean Up Coding :)

for h in `ls $loc | fgrep -v "."`
do
html="index.html"
htlc="$loc/$h"
echo "<html><head><title>MRTG:$h</title><meta http-equiv=refresh content=60></head><body>" > $htlc/$html
echo "<h3>MRTG:$h</h3><hr width=500 align=left><table border=0>" >> $htlc/$html
for s in `ls $htlc | grep $pic | grep day`
do
htnm=`echo "$s" | cut -d"." -f1 | cut -d"-" -f1`
echo "<tr><td><a href=$htnm.html><img src=$s border=0></a></td><td>$htnm</td></tr>" >> $htlc/$html
done
echo "<!-- NOTESDIV --></table>" >> $htlc/$html
echo "</body></html>" >> $htlc/$html
done

rm $loc/index.html

nvdr="$loc/nav.html"
echo "<html><head><title>Navigation</title></head><body>" > $nvdr
echo "<h4>MRTG Nodes</h4><table border=0>" >> $nvdr
for m in `find $loc | fgrep "index.html" | grep -v "~" | sort -n | cut -d"g" -f2 | cut -d"/" -f2-200`
do
indn=`echo "$m" | cut -d"/" -f1`
echo "<tr><td><a href=$m target=view><font size=2>$indn</font></a></td></tr>" >> $nvdr
done
echo "</table></body></html>" >> $nvdr

echo "<html><head><title>MRTG On `hostname`</title></head><frameset cols=200,100%><frame src=nav.html><frame src=welcome.html name=view></frameset>" > $loc/index.html

echo "<html><head><title>Welcome</title></head>" > $loc/welcome.html
echo "<h3>Welcome To MRTG!</h3>" > $loc/welcome.html
echo "<p>The 'Nodes' column on the left contains all nodes that you have selected to monitor via MRTG." >> $loc/welcome.html
echo "<p>Have fun! :D<p><font color=grey>mkmrtg.sh - Ver. 0.9" >> $loc/welcome.html
echo "<p>Script & Front-End Written By Joe McShinsky <font size=0>(Contributions Noted In Script)</font></font>" >> $loc/welcome.html

touch $notes
for n in `cat $notes | cut -d":" -f1`
do
ind=`cat $loc/$n/index.html | grep -B2000 NOTESDIV | grep -v NOTESDIV`
echo "$ind" > $loc/$n/index.html
echo "<!-- NOTESDIV --></table>" >> $loc/$n/index.html
echo "`cat $notes | grep $n`" | cut -d":" -f2-200 >> $loc/$n/index.html
echo "</body></html>" >> $loc/$n/index.html
done

8 comments:

ClovenLife said...

This script seems great.. the only problem is, its not picking up anything. not finding any of the pngs or pages. any thoughts?

Joe McShinsky said...

Where do you send your output? When you create your folder for each node, are they all under a specific directory?

ClovenLife said...

well a couple things are happening. I discovered using "." in a name will cause problems. It will truncate the name after the period so all my ports results were titled 192
(instead of 192.168.1.38_1)

I output to /home/httpd/mrtg
and don't have my results seperated into nodes.. to be honest, i didn't know that was possible.

I have it running in a basic fashion by telling it I output to /home/httpd and the script interprets mrtg as a node.

Joe McShinsky said...

I'll address the first one here and then the "." in the next comment.

Yes, you can output each node into their own directory. In your config files, all you need to do is put "WorkDir: /home/httpd/mrtg/[node]". You will need to create the directory for each node of course. Then, to keep the data you already have, just move the files for that node into your new directory. You would point mkmrtg.sh to "/home/httpd/mrtg". This will make the column on the left list each of your nodes separately as each node would be their own directory ("/home/httpd/mrtg/[node]").

Joe McShinsky said...

Ok. About the "." problem. Thank you very much for bringing this to my attention! All you need to do is to replace:
htnm=`echo "$s" | cut -d"." -f1 | cut -d"-" -f1`
with:
htnm=`echo "$s" | sed s/"-day.$pic"//g`
That actually fixes two problems. :) You can now have "-" in the name of your node, and "." too! Give it a shot and let me know how it goes. I will make sure its part of Ver. 1.0.

Unknown said...

I'm missing something ...."WorkDir: /home/httpd/mrtg/[node]", is that literal?
I have a default setup as well, using only
ip for large switch domain. I've created the dir. No Luck.

Joe McShinsky said...

"WorkDir: /home/httpd/mrtg/[node]"

This is where you want the output of your data to go. So, when you run MRTG, it will read the "WorkDir:" and then based on that, output the image files, html files, log files, etc. to that location. "[node]" is just a placement name. You will replace "[node]" (in your .cfg) with the name of your computer/printer/etc. Then create the directory name on your server as the same name in your .cfg file.

That is pre-mkmrtg. When you run mrtg it will output an error that something is not right, or nothing (initially the first couple times will output errors about a log file but after the 2nd time, it runs fine). For example, my crontab could contain something like:
"env LANG=C /etc/mrtg/bin/mrtg /etc/mrtg/monitor/computer1.cfg"
If that works correctly, then you can start using mkmrtg.sh

Joe McShinsky said...

(Continued)

Your .cfg file:
Incorrect:
WorkDir: /home/httpd/[computer1]
Correct:
WorkDir: /home/httpd/computer1

Your Linux box:
Incorrect:
/home/httpd/[computer1]
Correct:
/home/httpd/computer1