Мониторинг нагрузки канала (MRTG)

Идея не моя, вычитал на просторах интернета. Часто бывает, что Вам необходимо видеть текущую нагрузку на Ваш канал.

Попробуем реализовать у себя:

 

1.    Для начала нам необходим net-snmp:

#cd /usr/ports/net-mgmt/net-snmp
#make install clean

2.    После установки, необходимо добавить в rc.conf следующее

snmpd_enable=”YES”
snmpd_flags=”-a -Lsd -p /var/run/snmpd.pid”

3.    Дальше, запускаем snmpconf –i и делаем по примеру.

#snmpconf -i

The following installed configuration files were found:

1:  /usr/local/share/snmp/snmpd.conf

Would you like me to read them in?  Their content will be merged with the
output files created by this session.

Valid answer examples: “all”, “none”,”3″,”1,2,5″

Read in which (default = all): all

I can create the following types of configuration files for you.
Select the file type you wish to create:
(you can create more than one as you run this program)

1:  snmpd.conf
2:  snmptrapd.conf
3:  snmp.conf

Other options: quit

Select File: 1

The configuration information which can be put into snmpd.conf is divided
into sections.  Select a configuration section for snmpd.conf
that you wish to create:

1:  System Information Setup
2:  Access Control Setup
3:  Trap Destinations
4:  Monitor Various Aspects of the Running Host
5:  Extending the Agent
6:  Agent Operating Mode

Other options: finished

Select section: 1

Section: System Information Setup
Description:
This section defines some of the information reported in
the “system” mib group in the mibII tree.

Select from:

1:  The [typically physical] location of the system.
2:  The contact information for the administrator
3:  The proper value for the sysServices object.

Other options: finished, list

Select section: 1

Configuring: syslocation
Description:
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

The location of the system: Kyiv

Finished Output: syslocation  Kyiv

Section: System Information Setup
Description:
This section defines some of the information reported in
the “system” mib group in the mibII tree.

Select from:

1:  The [typically physical] location of the system.
2:  The contact information for the administrator
3:  The proper value for the sysServices object.

Other options: finished, list

Select section: 2

Configuring: syscontact
Description:
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

The contact information: email@gmail.com

Finished Output: syscontact  email@gmail.com

Section: System Information Setup
Description:
This section defines some of the information reported in
the “system” mib group in the mibII tree.

Select from:

1:  The [typically physical] location of the system.
2:  The contact information for the administrator
3:  The proper value for the sysServices object.

Other options: finished, list

Select section: 3

Configuring: sysservices
Description:
The proper value for the sysServices object.
arguments:  sysservices_number

does this host offer physical services (eg, like a repeater) [answer 0 or 1]: 0
does this host offer datalink/subnetwork services (eg, like a bridge): 0
does this host offer internet services (eg, supports IP): 0
does this host offer end-to-end services (eg, supports TCP): 0
does this host offer application services (eg, supports SMTP): 1

Finished Output: sysservices 64

Section: System Information Setup
Description:
This section defines some of the information reported in
the “system” mib group in the mibII tree.

Select from:

1:  The [typically physical] location of the system.
2:  The contact information for the administrator
3:  The proper value for the sysServices object.

Other options: finished, list

Select section: finished

The configuration information which can be put into snmpd.conf is divided
into sections.  Select a configuration section for snmpd.conf
that you wish to create:

1:  System Information Setup
2:  Access Control Setup
3:  Trap Destinations
4:  Monitor Various Aspects of the Running Host
5:  Extending the Agent
6:  Agent Operating Mode

Other options: finished

Select section: finished

The configuration information which can be put into snmpd.conf is divided
into sections.  Select a configuration section for snmpd.conf
that you wish to create:

1:  System Information Setup
2:  Access Control Setup
3:  Trap Destinations
4:  Monitor Various Aspects of the Running Host
5:  Extending the Agent
6:  Agent Operating Mode

Other options: finished

Select section: 2

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

Select from:

1:  a SNMPv3 read-write user
2:  a SNMPv3 read-only user
3:  a SNMPv1/SNMPv2c read-only access community name
4:  a SNMPv1/SNMPv2c read-write access community name

Other options: finished, list

Select section: 3

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

The community name to add read-only access for: pcbsd
The hostname or network address to accept this community name from [RETURN for all]: localhost
The OID that this community should be restricted to [RETURN for no-restriction]:

Finished Output: rocommunity  pcbsd localhost

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

Select from:

1:  a SNMPv3 read-write user
2:  a SNMPv3 read-only user
3:  a SNMPv1/SNMPv2c read-only access community name
4:  a SNMPv1/SNMPv2c read-write access community name

Other options: finished, list

Select section: finished

The configuration information which can be put into snmpd.conf is divided
into sections.  Select a configuration section for snmpd.conf
that you wish to create:

1:  System Information Setup
2:  Access Control Setup
3:  Trap Destinations
4:  Monitor Various Aspects of the Running Host
5:  Extending the Agent
6:  Agent Operating Mode

Other options: finished

Select section: 6

Section: Agent Operating Mode
Description:
This section defines how the agent will operate when it
is running.

Select from:

1:  Should the agent operate as a master agent or not.
2:  The system user that the agent runs as.
3:  The system group that the agent runs as.
4:  The IP address and port number that the agent will listen on.

Other options: finished, list

Select section: 4

Configuring: agentaddress
Description:
The IP address and port number that the agent will listen on.
By default the agent listens to any and all traffic from any
interface on the default SNMP port (161).  This allows you to
specify which address, interface, transport type and port(s) that you
want the agent to listen on.  Multiple definitions of this token
are concatenated together (using ‘:’s).
arguments: [transport:]port[@interface/address],…

Enter the port numbers, etc that you want the agent to listen to: localhost

Finished Output: agentaddress  localhost

Section: Agent Operating Mode
Description:
This section defines how the agent will operate when it
is running.

Select from:

1:  Should the agent operate as a master agent or not.
2:  The system user that the agent runs as.
3:  The system group that the agent runs as.
4:  The IP address and port number that the agent will listen on.

Other options: finished, list

Select section: finished

The configuration information which can be put into snmpd.conf is divided
into sections.  Select a configuration section for snmpd.conf
that you wish to create:

1:  System Information Setup
2:  Access Control Setup
3:  Trap Destinations
4:  Monitor Various Aspects of the Running Host
5:  Extending the Agent
6:  Agent Operating Mode

Other options: finished

Select section: finished

I can create the following types of configuration files for you.
Select the file type you wish to create:
(you can create more than one as you run this program)

1:  snmpd.conf
2:  snmptrapd.conf
3:  snmp.conf

Other options: quit

Select File: quit

The following files were created:

snmpd.conf installed in /usr/local/share/snmp

4.    На этом по сути настройка snmpd закончена. Остается только сделать

#/usr/local/etc/rc.d/snmpd.sh start

И проверить, работает ли он

#sockstat -4 | grep snmpd
root     snmpd      621   7  tcp4   *:199                 *:*
root     snmpd      621   9  udp4   127.0.0.1:161         *:*

5.    Дальше, нам необходим пакет mrtg

#cd /usr/ports/net-mgmt/mrtg/
#make install clean

6.    Для настройки мы будем использовать специальную утилиту cfgmaker

#cfgmaker –global ‘WorkDir: /usr/local/www/data-dist’ –global ‘Options[_]: bits,growright’ –output /usr/local/etc/mrtg/mrtg.cfg pcbsd@localhost

WorkDir: – здесь прописывается путь до директории, в которую Вы хотите выкладывать инфу для apache
comm@localhost – comm – это пользователь, который имеет доступ до net-snmp, а localhost – – это имя хоста, на котором, собственно и запущен net-snmp.
7.    Теперь будем запускать генератор графиков раз в 5 минут от пользователя www.
Зададим права на папки пользователю www:

#chown -R www /usr/local/etc/mrtg/
#chown -R www /usr/local/www/data-dist

Тут возможно и другое решение, не обязательно такое.
И последний штрих в /etc/crontab добавим следуюшую строку:

*/5 * * * * root /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg.cfg

8.    И в довершение, для большей красоты, используем

indexmaker –output /usr/local/www/data-dist index.html –columns=1 /usr/local/etc/mrtg/mrtg.cfg