# Setting up MRTG on FreeBSD



## empty (Nov 17, 2008)

So you wanna monitor bandwidth usage using MRTG and SNMPD.

Installing the ports tree.


```
portsnap fetch && portsnap extract && portsnap fetch update
```

Installing MRTG and SNMPD.


```
# Then install SNMPd port with:
cd /usr/ports/net-mgmt/net-snmp; make install

# Then install MRTG port with:
cd /usr/ports/net-mgmt/mrtg ; make install
```


# Then copy the code below, and edit to this location: /usr/local/share/snmp/snmpd.conf 



```
###########################################################################
#
# /usr/local/share/snmp/snmpd.conf
#
###########################################################################
#
# snmpd.conf
#
#   - created by the snmpconf configuration program
#
###########################################################################
# SECTION: System Information Setup
#
#   This section defines some of the information reported in
#   the "system" mib group in the mibII tree.

# syslocation: The [typically physical] location of the system.
#   Note that setting this value here means that when trying to
#   perform an snmp SET operation to the sysLocation.0 variable will make
#   the agent return the "notWritable" error code.  IE, including
#   this token in the snmpd.conf file will disable write access to
#   the variable.
#   arguments:  location_string

syslocation  My Country 

# syscontact: The contact information for the administrator
#   Note that setting this value here means that when trying to
#   perform an snmp SET operation to the sysContact.0 variable will make
#   the agent return the "notWritable" error code.  IE, including
#   this token in the snmpd.conf file will disable write access to
#   the variable.
#   arguments:  contact_string

syscontact  Name - email@domain.com



###########################################################################
# SECTION: Access Control Setup
#
#   This section defines who is allowed to talk to your running
#   snmp agent.

# rocommunity: a SNMPv1/SNMPv2c read-only access community name
#   arguments:  community [default|hostname|network/bits] [oid]

rocommunity  dumburk localhost
```

Starting SNMPD:


```
Add snmpd_enable= "YES" to /etc/rc.conf and save. Then type /usr/local/etc/rc.d/snmpd start
```

Configure MRTG using cfgmaker


```
cfgmaker --global 'WorkDir: /usr/local/www/data-dist/mrtg' --global 'Options[_]: growright,unknaszero' --output /usr/local/www/data-dist/mrtg.cfg dumburk@localhost
```

/usr/local/www/data-dist/mrtg = The location you want at your webserver
/usr/local/www/data-dist/mrtg/mrtg.cfg = The location you want of your mrtg.cfg

Edit this to suite your needs. dumburk is the name of my machine. If you wanna change, you need to edit both snmpd.conf and edit cfgmaker command  before run.


OK. Now everything should be setup right try run

```
/usr/local/bin/mrtg /path/to/mrtg.cfg
```

Adding the following command to /etc/crontab will update mrtg-graphs every 5th minute.


```
*/5     *       *       *       *       root    /usr/bin/env LANG=C /usr/local/bin/mrtg /path/to/mrtg.cfg --logging /var/log/mrtg.log
```

This guide is a couple of years old, but still works great.


http://oss.oetiker.ch/mrtg/
http://www.net-snmp.org/
http://www.FreeBSD.org


----------



## taromaru (Nov 17, 2008)

Thank you empty for this little howto, I hope all these guides encourage people to move on and give BSDs a try.

About FreeBSD and Snmp, back in the days I found useful this other document:

http://silverwraith.com/papers/freebsd-snmp.php

Its somewhat old (dated 2003) but still usefull. Imho its interesting to manually select which counters and events to monitor using snmpget and store them on rrd databases for later processing (rrdtool also generates graphs 

Perhaps it would be nice to use bsnmp. Im no expert, but I think its not as mature as net-snmp yet, though useful for testing and home-lab purposes. Besides, its BSD-licensed


----------



## mfaridi (Dec 8, 2008)

empty said:
			
		

> So you wanna monitor bandwidth usage using MRTG and SNMPD.
> 
> Installing the ports tree.
> 
> ...



I create snmpd.conf in /usr/local/share/snmp and put everything you put in your code and edit location or country and email,  but when I type 

```
/usr/local/etc/rc.d/snmpd start
```

I see this error


```
snmpd configuration file /usr/local/share/snmpd.conf not set or not found
```
where I make mistake ??


----------



## bsddaemon (Dec 8, 2008)

mfaridi said:
			
		

> I create snmpd.conf in /usr/local/share/snmp and put everything you put in your code and edit location or country and email,  but when I type
> 
> ```
> /usr/local/etc/rc.d/snmpd start
> ...



The error message was self explainatory. You created the file in the wrong dir, should be /usr/local/share/


----------



## mfaridi (Dec 8, 2008)

but when I type this command 


```
cfgmaker --global 'WorkDir: /usr/local/www/mrtg' --global 'Options[_]: growright,unknaszero' --output /usr/local/www/mrtg/mrtg.cfg mostafafaridi@192.168.0.41
```

I see this error


```
Can't locate SNMP_util.pm in @INC (@INC contains: /usr/local/bin/../lib/mrtg2 /usr/local/bin /usr/local/lib/perl5/5.8.8/BSDPAN /usr/local/lib/perl5/site_perl/5.8.8/mach /usr/local/lib/perl5/site_perl/5.8.8 /usr/local/lib/perl5/site_perl /usr/local/lib/perl5/5.8.8/mach /usr/local/lib/perl5/5.8.8 .) at /usr/local/bin/cfgmaker line 105.
```


----------



## thompsa@ (Dec 8, 2008)

make sure the net-mgmt/p5-SNMP_Session port is installed.


----------



## mfaridi (Dec 8, 2008)

thompsa@ said:
			
		

> make sure the net-mgmt/p5-SNMP_Session port is installed.



p5-SNMP_Session

was not install 
 but rigth now I see new error

when I type 

```
cfgmaker --global 'WorkDir: /usr/local/www/mrtg' --global 'Options[_]: growright,unknaszero' --output /usr/local/www/mrtg/mrtg.cfg mostafafaridi@192.168.0.42
```

I see this error


```
SNMP Error:
no response received
SNMPv1_Session (remote host: "192.168.0.42" [192.168.0.42].161)
                  community: "mostafafaridi"
                 request ID: -1201549495
                PDU bufsize: 8000 bytes
                    timeout: 2s
                    retries: 5
                    backoff: 1)
 at /usr/local/lib/perl5/site_perl/5.8.8/SNMP_util.pm line 629
SNMPWALK Problem for 1.3.6.1.2.1.1 on mostafafaridi@192.168.0.42::::::v4only
 at /usr/local/bin/cfgmaker line 950
WARNING: Skipping mostafafaridi@192.168.0.42: as no info could be retrieved
```


----------



## brd@ (Dec 9, 2008)

empty said:
			
		

> Installing the ports tree.
> 
> 
> ```
> ...



You don't need the fetch update on the end because it will grab and extract the latest sources by default and the first part can be shortened to:

```
portsnap fetch extract
```


----------



## Ole (Dec 9, 2008)

BTW! bsnmpd is working perfect for standart resource snmp polling instead of fat net-snmpd if only apply UCD-SNMP-MIB.
Another a little bsnmpd advantage  over net-snmpd - it part of FreeBSD OS (starting from 6x):

to /etc/rc.conf:


> bsnmpd_enable="YES"



cd /usr/ports/net-mgmt/bsnmp-ucd && make install

set community as you wish (or comment write and trap) in /etc/snmpd.conf


> read := "mostafafaridi"
> # Uncomment begemotSnmpdCommunityString.0.2 below that sets the community
> # string to enable write access.
> #write := "mostafafaridi"
> #trap := "mytrap"


+
uncomment host resource module


> begemotSnmpdModulePath."hostres" = "/usr/lib/snmp_hostres.so"


+
add new  records


> cat /usr/ports/net-mgmt/bsnmp-ucd/work/bsnmp-ucd-0.2.1/snmpd.config.sample >> /etc/snmpd.config



and start bsnmp:


> /etc/rc.d/bsnmpd start



PS: in some cases cfgmaker fetch correct information about interfaces, but #comment records for really active interfaces. Work-around is --no-down argument in cfgmaker ... string.


----------



## edhunter (Dec 27, 2008)

*A little addtion to this guide ..*

in cases where mrtg is needed to log the traffic going through the local (where mrtg is installed) machine's nic: you dont need snmp at all. here is a little script

```
#!/bin/sh
grep="/usr/bin/grep"
cut="/usr/bin/cut"
uptime="/usr/bin/uptime"
netstat="/usr/bin/netstat"
awk="/usr/bin/awk"
# netstat name interface
name=$1
iface=$2
$netstat -bI $iface | $grep "<Link#.*>" | $awk -F" " '{print $7"\n"$10}'
$uptime | $awk -F"up " '{print $2}' #| $cut -d"," -f1,2
echo $name
```
Save it as ns-mrtg.sh, make it executable. And use it like this in mrtg.conf

```
###lan###
[color="Red"]Target[lan]: `/usr/local/etc/mrtg/[b]ns-mrtg.sh[/b] myhost mynic`[/color]
Title[lan]: lan
Directory[lan]: lan
PageTop[lan]: <H1>LAN</H1>
```

just replace myhost with actual name e.g. peter.pan.org, and and mynic with network adapter name e.g. fxp0

this script is by friend of mine - realy clever guy tbyte


----------



## chamba (Apr 3, 2010)

hi folks,

i just want to say tanx for this how to and make one question.

my is running but it still give me the old IPs and in my http://localhost/mrtg i show all files and not just the MRTG page, so i have to click on localhost_1.html and localhost_2.html. How can i fix this.

cheers/


----------



## manilaboy1vic (Jul 24, 2013)

Hello, I am seeing issues as well with this how to.


```
root@yeaguy:/root # cfgmaker --global 'WorkDir: /usr/local/www/apache22/data/mrtg' --global 'Options[_]: growright,unknaszero' --output /usr/local/www/apache22/data/mrtg/mrtg.cfg yeaguy@localhost
Can't locate MRTG_lib.pm in @INC (@INC contains: /usr/local/bin/../lib/mrtg2 /usr/local/bin /usr/local/lib/perl5/5.14.2/BSDPAN /usr/local/lib/perl5/site_perl/5.14.2/mach /usr/local/lib/perl5/site_perl/5.14.2 /usr/local/lib/perl5/5.14.2/mach /usr/local/lib/perl5/5.14.2 .) at /usr/local/bin/cfgmaker line 49.
BEGIN failed--compilation aborted at /usr/local/bin/cfgmaker line 49.
root@yeaguy:/root #
```


```
root@yeaguy:/root # locate MRTG_lib.pm
/usr/local/lib/perl5/site_perl/5.14/MRTG_lib.pm
root@yeaguy:/root #
```

line 49 of /usr/local/bin/cfgmaker 

```
use MRTG_lib "2.100015";
```


----------



## manilaboy1vic (Jul 25, 2013)

I changed line 49 of /usr/local/bin/cfgmaker  to the full path to MRTG_lib.pm.  Then it breaks on line 52. 

Which is 
	
	



```
use Pod::Usage;
```


----------

