Как Получить Данные Для Mrtg Не Запуская SNMP-Сервер?
Рисование графиков загрузки сетевых интерфейсов – это, наверное, одно из самых популярных занятий UNIX-администраторов.
Отличным инфтрументом для этого является утилита mrtg. Она может быть без проблем настроена на создание графиков загрузки интерфейсов любого поддерживающего SNMP устройства (в том числе и Linux-сервера с запущенным на нем snmpd). Но, иногда, мы не можем запустить snmp-демон на Linux сервере из-за маленького обхема оперативной памяти или еще по каким-то объективным причинам. Как же быть в такой ситуации, если графики все-таки нужны, но нет SNMP?
Когда я устанавливал свой домашний сервер для подключения домашней сети к Internet, на машине было очень мало оперативной памяти и тратить ее на запуск snmpd было просто не допустмимо. В качестве решения в этой ситуации я написал маленький Perl-скрипт, который получает информацию из /proc/net/dev и передает ее скрипту mrtg.
Если у Вас есть желание попробовать этот скрипт в действии, Вам нужно выполнить всего несколько простых шагов на Вашем сервере:
- Скачайте мой скрипт, переименуйте его в get_if_stats.pl и положите в каталог /usr/local/bin на Вашем сервере.
- Сделайте его исполняемым:
# chmod +x usr/local/bin/get_if_stats.pl
- Добавьте новую запись в mrtg.conf (приведенный пример предназначен для интерфейса eth0):
Title[new_graph]: World channel (eth0) PageTop[new_graph]: <h1>Some new mrtg graph (eth0)</h1> Target[new_graph]: `/usr/local/bin/get_if_stats.pl eth0` MaxBytes[new_graph]: 100000000
Вот и все! Теперь Вы можете создавать различные графики для любых сетевых интерфейсов не держа постоянно в памяти сервера огромного демона snmpd.
Related posts:

11 Responses to this entry
Works excellent! Thanks! I’ve tried snmp with Cacti, but it almost killed my poor router. Now, it’s graphing easily
Just one sidenote. If you’re installing clean mrtg, then add this to mrtg.conf:
WorkDir: /var/www/htdocs/mrtg
That direcotory will be a place where mrtg will dump pngg raphs, log and html files.
Thanks Alexey!
I am really glad that this small note can help you. And thanks for your comment.
Cool stuff! Even if the load of an SNMP daemon wasn’t an issue (and it is for me); this saves you worrying about configuring communities properly, security issues, etc.
Необязательно как скрипт делать
вот поделюсь своими
#####################################################################
#
# eth0 traffic graphing section from /proc/net/dev
# change “grep eth0″ to something else to measure a different device
#
Target[eth0]: `perl -e ‘@a=split(/[:\s]+/,qx(grep eth0 /proc/net/dev));printf “%.0f\n%.0f\n1\neth0 traffic\n”,$a[2],$a[10];’`;
Options[eth0]: dorelpercent, growright, nobanner, noinfo, transparent
MaxBytes[eth0]: 2000000
AbsMax[eth0]: 10000000
kilo[eth0]: 1024
YLegend[eth0]: Bytes per second
ShortLegend[eth0]: B/s
Legend1[eth0]: Incoming Traffic in Bytes per second
Legend2[eth0]: Outgoing Traffic in Bytes per second
LegendI[eth0]: In:
LegendO[eth0]: Out:
Title[eth0]: net eth0 traffic
PageTop[eth0]: net eth0 traffic
#####################################################################
#####################################################################
# # alternative eth0 traffic graphing section
# using “sar” instead of /proc/*
#
Target[eth0sar]: `perl -e ‘printf “%.0f\n%.0f\n1\neth0sar traffic\n”,(qx(/usr/bin/sar -n DEV | grep eth0 | tail -n 2)=~/eth0\s+[^\s]+\s+[^\s]+\s+([^\s]+)\s+([^\s]+)/)’`;
Options[eth0sar]: dorelpercent, gauge, growright, nobanner, noinfo, transparent
MaxBytes[eth0sar]: 2000000
AbsMax[eth0sar]: 10000000
kilo[eth0sar]: 1024
YLegend[eth0sar]: Bytes per second
ShortLegend[eth0sar]: B/s
Legend1[eth0sar]: Incoming Traffic in Bytes per second
Legend2[eth0sar]: Outgoing Traffic in Bytes per second
LegendI[eth0sar]: In:
LegendO[eth0sar]: Out:
Title[eth0sar]: net eth0sar traffic
PageTop[eth0sar]: net eth0sar traffic
#####################################################################
2Bor: Thanks for example, but such complex config file is not so easily readable/modificable and that is why I’ve created my script
Traffic monitoring without snmp has at least two caveats:
1) The counters in /proc/net/dev (and in the output of ifconfig etc.) are 32bit and will overflow at around 4GiB. SNMP agents (should!) use 64bit counters.
2) Related to (1), the SNMP client can tell if the counter has been reset because of a reboot, as it knows the server’s uptime (SysUpTime).
Have you tried tinysnmp? Its memory footprint is smaller than perl’s
sb: is that true (the counter-size) even for 64 bit Linux?
Hi,
Do you have any idea how to get this script working on OpenBSD? There is no /proc/net/dev on OpenBSD.
Anyway, nice script, thanks
As for me i don’t like OpenBSD.
So, Linux Forewer ))))
Hi,
i’ve installed get_if_stats.pl, and my configuration is:
Target[webpec1eth0]: `/usr/local/bin/get_if_stats.pl eth0`
Options[webpec1eth0]: gauge,noinfo, nopercent, growright, nobanner
Title[webpec1eth0]: Traffico IN/OUT(eth0)
MaxBytes[webpec1eth0]: 100000000
PageTop[webpec1eth0]: Traffico interfaccia (eth0)
#
Target[webpec1eth1]: `/usr/local/bin/get_if_stats.pl eth1`
Options[webpec1eth1]: gauge,noinfo, nopercent, growright, nobanner
Title[webpec1eth1]: Traffico IN/OUT(eth1)
MaxBytes[webpec1eth1]: 100000000
PageTop[webpec1eth1]: Traffico interfaccia (eth1)
Eth0 graph is correct, while the eth1 graph shows empty values ?
If I run the script on command line I get:
227419173
261530900
0
0
thanks
andrea b
Perfect, just what I needed – thanks