Below code works great with getting only a bunch IP address and add it to Cacti --description='".$dev."' --ip='".$dev."'
, now I want to add bunch of IP with their description in cacti forexample(--description='".$hostname."' --ip='".$dev."'
).
I am newbie to PHP and don't know how to add 2 values instead of 1 value in PHP.
devices.txt
1.1.1.1
add_device_bulk.php
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = chop($line);
print "[".$line."] \n";
createHost($line);
}
} else {
die("Could not open file $filename!");
}
die;
function createHost($dev)
{
global $community;
global $hosttemplate;
print "================== Creating Node & Graph for $dev =======================\n";
$ret = cmd("/usr/bin/php add_device.php --quiet --description='".$dev."' --ip='".$dev."' --template=$hosttemplate --community='".$community."' --avail=snmp ");
//Get host id from: [RET] Success - new device-id: (20)
if (preg_match("/\((\d+)\)/", $ret, $matches))
{
$deviceID = $matches[1];
print "Device ID: $deviceID \n";
//We got a device - create graphs for device
$ret = cmd("/usr/bin/php add_graphs.php --graph-type=ds --graph-template-id=5 --host-id=".$deviceID." --snmp-query-id=1 --snmp-query-type-id=10 --snmp-field=ifOperStatus --snmp-value-regex=Up --snmp-field=ifDescr --snmp-value-regex='GigabitEthernet'");
//RET: Graph Added - graph-id: (34) - data-source-ids: (37, 37)
if (preg_match("/\((\d+)\)/", $ret, $matches)) {
$graphID = $matches[1];
//We got a graph - add it to default tree
# cmd("/usr/bin/php /cacti/appl/cacti/cli/add_tree.php --type=node --node-type=graph --tree-id=1 --graph-id=".$graphID);
}
}
}
function cmd($cmd)
{
print "[CMD] $cmd\n";
$ret = exec($cmd)."\n";
print "[RET] $ret\n";
return $ret;
}
Now I want to add description instead of IP and have a text file as below:
devices.txt
Link1, 1.1.1.1