Code: Select all
<?
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');
?>
Code: Select all
php -q file_name.php