Community discussions

MikroTik App
 
User avatar
[ASM]
Member Candidate
Member Candidate
Topic Author
Posts: 284
Joined: Sun Jun 06, 2004 12:59 am
Location: Sofia, Bulgaria
Contact:

Queue Tree -> MRTG config files

Tue Aug 03, 2004 8:31 pm

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.
 
User avatar
evert
Member Candidate
Member Candidate
Posts: 130
Joined: Thu Jul 15, 2004 3:06 pm
Location: Sarpsborg, Norway
Contact:

Mon Aug 09, 2004 3:02 pm

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

Regards,
Evert
 
User avatar
[ASM]
Member Candidate
Member Candidate
Topic Author
Posts: 284
Joined: Sun Jun 06, 2004 12:59 am
Location: Sofia, Bulgaria
Contact:

Mon Aug 09, 2004 6:54 pm

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
 
User avatar
evert
Member Candidate
Member Candidate
Posts: 130
Joined: Thu Jul 15, 2004 3:06 pm
Location: Sarpsborg, Norway
Contact:

Tue Aug 10, 2004 9:46 am

Yup, you got me there... 8)

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...
 
User avatar
netcomp
Frequent Visitor
Frequent Visitor
Posts: 50
Joined: Thu Jul 08, 2004 5:55 pm

Tue Aug 10, 2004 11:30 am

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 :)
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26912
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia
Contact:

Tue Aug 10, 2004 11:51 am

 
User avatar
netcomp
Frequent Visitor
Frequent Visitor
Posts: 50
Joined: Thu Jul 08, 2004 5:55 pm

Tue Aug 10, 2004 12:12 pm

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 :) 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
 
User avatar
[ASM]
Member Candidate
Member Candidate
Topic Author
Posts: 284
Joined: Sun Jun 06, 2004 12:59 am
Location: Sofia, Bulgaria
Contact:

Thu Aug 12, 2004 11:34 pm

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 :) I just don't have so much time for that :)
 
User avatar
netcomp
Frequent Visitor
Frequent Visitor
Posts: 50
Joined: Thu Jul 08, 2004 5:55 pm

Tue Aug 17, 2004 8:45 pm

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...
 
User avatar
[ASM]
Member Candidate
Member Candidate
Topic Author
Posts: 284
Joined: Sun Jun 06, 2004 12:59 am
Location: Sofia, Bulgaria
Contact:

Tue Aug 17, 2004 9:48 pm

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...

Who is online

Users browsing this forum: yhfung and 18 guests