Queue Tree -> MRTG config files

OK. I’ve made this simple php script that generates MRTG config files using SNMP. It works well but needs some more scripting.

<?
error_reporting(E_ALL);
$host = '10.0.1.1';
$comunity = 'public';

$queues = array();
exec('/usr/local/bin/snmpwalk -v 1 -c ' . $comunity . ' ' . $host . ' 1.3.6.1.4.1.14988.1.1.2.2.1.2', $queues);
$c = count($queues);
for ($i = 0; $i < $c; $i++) {
	$a = explode(' = ', $queues[$i]);
	$b = explode('.', $a[0]);
	$num = intval($b[count($b)-1]);
	$b = explode(': ', $a[1]);
	$name = trim($b[1], '"');
	$q[$num]["name"] = $name;
//	$q[$i]["num"] = $num;
	$parent = array();
	exec('/usr/local/bin/snmpget -v 1 -c ' . $comunity . ' ' . $host . ' 1.3.6.1.4.1.14988.1.1.2.2.1.4.' . $num, $parent);
	$b = explode(': ', $parent[0]);
	$q[$num]["parent"] = intval($b[1]);
}

$tree = array();
$keys = array();

foreach ($q as $key => $value) {
	if ($value["parent"] == 16777201 || $value["parent"] == 16777202) {
		$tree[] = array("parent" => "", "name" => $value["name"], "num" => $key, "sub" => array());
		$keys[$key] =& $tree[count($tree) -1];
	} else {
		$parent =& $keys[$value["parent"]];
		$parent["sub"][] = array("parent" => $value["parent"], "name" => $value["name"], "num" => $key, "sub" => array());
		$keys[$key] =& $parent["sub"][count($parent["sub"]) - 1];
	}
};

function make_cfg($c, $name) 
{
	global $host, $comunity;

	if (count($c) > 0) {
		$fp = fopen($name.'.cfg', 'w');
		if ($fp) {
			print($name.'.cfg created!'."\n");
			fputs($fp, 'WorkDir: /var/www/htdocs/mrtg/'."\n");
			fputs($fp, 'Options[_]: growright,bits,avgpeak'."\n");
			fputs($fp, 'EnableIPv6: no'."\n");
			fputs($fp, '#################################################'."\n\n");

			foreach ($c as $v) {
				fputs($fp, 'Target['. $v["name"] .']: 1.3.6.1.4.1.14988.1.1.2.2.1.5.'. $v["num"] .'&1.3.6.1.4.1.14988.1.1.2.2.1.5.'. $v["num"] .':'.$comunity.'@'.$host."\n");
				fputs($fp, 'XSize['. $v["name"] .']: 350'."\n");
				fputs($fp, 'YSize['. $v["name"] .']: 100'."\n");
				fputs($fp, 'Title['. $v["name"] .']: '.$v["name"]."\n");
				fputs($fp, 'MaxBytes['. $v["name"] .']: 12500000'."\n");
				fputs($fp, 'PageTop['. $v["name"] .']: <H1>'.$v["name"].'</H1>'."\n\n");
				
				make_cfg($v["sub"], $v["name"]);
			}
			fclose($fp);
		}
	}
};

make_cfg($tree, 'main');

?>

To use it just copy&paste to some file. Change $host and $comunity to yours. And run it with

php -q file_name.php

Please write any suggestions about it.

The script gives me:

Warning: Invalid argument supplied for foreach() in /root/mikrotikTOphp.php on line 26

Regards,
Evert

I think that you don’t have any queues in /queue tree

Yup, you got me there… :sunglasses:

But, on a unit that DOES have queues, I get:

php -q mikrotikTOphp.php
 
Notice: Undefined offset:  1 in /root/mikrotikTOphp.php on line 13
 
Notice: Undefined offset:  1 in /root/mikrotikTOphp.php on line 14
Timeout: No Response from 192.168.2.1.
 
Notice: Undefined offset:  0 in /root/mikrotikTOphp.php on line 19
 
Notice: Undefined offset:  1 in /root/mikrotikTOphp.php on line 20

And before you ask, the community string was correct. snmpwalk does return data…

Hey ASM!

I know that guys from Bulgaria are genies, so, can you spend a little time and make php or whatever so you can pick up html page from .cgi file and put it in lets say microsoft access .mdb database. This I need for putting traffic accounting into one database and after that, I’ll make prog myself to analyze .mdb (see accounting).

Ofcourse I bet this will use plenty of mikrotik users so we can record what/when/howmuch users used internet, not just with mrtg. Also if you do this if you can please resolve ip addresses to their names via dns and put also those names in database not only the ip’s.

I bet you’ll do it :slight_smile:

For accounting you can use the traffic counter:

http://www.mikrotik.com/download.html#traffic_management_utils
http://www.mikrotik.com/docs/ros/2.8/guide/aaa.content#2.4.11

Yep, ofcourse, I also mentioned traffic accounting, just to point ASM to that section in case he or others doesn’t know what I was talking about. But with /ip accounting, for now we can use either your program provided in the downloads which is not so good :(, or that wget that I never had chanse to see it couse I dont have any ‘extra’ linux machine, but I have Windows and I’ll like to have some soft. to pick up that http://routerIP/accounting/ip.cgi content and put it in .mdb database and also resolve ip’s into names, OR :slight_smile: if you can provide us with source code of Log Downloader and we’ll play with it and make it put things in .mdb instead of plain .txt file.

See ya

netcomp: One day I’ve made a php script that using wget downloads ip accounting data and parses it… but It was not so accurate as using SNMP data…

I’m not so sure why this script doesn’t work for you… And I don’t want to support it :slight_smile: I just don’t have so much time for that :slight_smile:

Yep, I also use snmp/mrtg, but it shows only graphs, what if I want to see what a user did 35 days ago, how many mb… he used…

If you have 100 users and you log the data every day, then at day 100 you will have 10 000 database records… that’s the reason that every database is “commpressed”. 7 days are stored as 1 week record, 4 weeks are stored as 1 mount record and so on…