Creating profiles can be time consuming, especially in large and changing network infrastructures. Today we will show you how you can save your time using script to create profiles automatically.
I would like to show you how we can create a simple script to create profile for Flowmon. I will use the document from support portal Profile Import Using XML and the fact, that system is using a PHP for SNMP and database access so it does not need anything special for that.
It will work in any system version after 8.x and probably even in earlier however I did not test it on pre 8.x versions. So, what is this script going to do? It will get IPs and SNMP settings of my flow sources from the database and create a profile where I will have interfaces (as channels) with direction (in/out) on flow sources which have some specific string in the beginning of the interface description. I have made this script for one of our customer but it can be easily modified to do other things or search for some specific cases or different fields in SNMP.
This script is using field ifAlias which is used by network admins to describe interfaces on routers and switches. In customer’s environment where the script is deployed the string at the beginning is “SRV_IAS”.
The first part of the script is just opening database connection and file to save the file. I won’t explain it here as in general, there is no need to modify this one.
Then there is a part which create a basic structure of XML file with mandatory fields and set this profile as a shadow five minute profile.
fwrite($fh, "<inveaosconfig version=\"8.00.00\" hwid=\"..\">\n");
fwrite($fh, "<flowmon version=\"8.00.00\">\n");
fwrite($fh, "<monitoring-center xmlns=\"urn:invea:netconf:mc\">\n");
fwrite($fh, "<profiles>\n");
fwrite($fh, "<profile>\n");
fwrite($fh, "<rename>SRV_IAS</rename>\n");
fwrite($fh, "<name>srv_ias</name>\n");
fwrite($fh, "<description></description>\n");
fwrite($fh, "<group></group>\n");
fwrite($fh, "<parent>./live</parent>\n");
fwrite($fh, "<plugin>FMC</plugin>\n");
fwrite($fh, "<backup>false</backup>\n");
fwrite($fh, "<type>6</type>\n");
fwrite($fh, "<limit>\n");
fwrite($fh, "<size>1048576</size>\n");
fwrite($fh, "<time>0</time>\n");
fwrite($fh, "</limit>\n");
fwrite($fh, "<channels>\n");
Then I define some colors which we will use for those channels
$colpal = array("#2466f4", "#f44100", "#ffa216", "#1bb500", "#b400bd", "#00ace5", "#fb397d", "#78c800", "#d82a21", "#306eb6", "#ae42b6", "#20c9b3", "#c8c800", "#6324f7", "#fe7f00", "#aa0b00", "#80138a", "#35ac67", "#547ac1", "#3339d0");
By doing this we have the static part ready and now it is time to go through flow sources IPs from the database and load the details from the description. I’m using here function for REGEX match preg_match. I create two channels for each source to have both directions (in/out). It will be given a random color and only part of the description is going to be used as a channel name as there is a limit of 100 characters for the rename part.
This code below will do exactly that and save us the trouble with going though this manually. It can be also used in the case of changes so it will redefine the fields in the profile.
// Now lets iterate through result and get results
while ($row = pg_fetch_assoc($result))
{
echo "Source IP ".$row['snmp_ip']." - ";
// now let's establish SNMP connection to source
$hostname = stristr($row['snmp_ip'], '/', true);
if ($row['version'] == 1)
$session = new SNMP(SNMP::VERSION_1, $hostname, $row['community_string']);
elseif ($row['version'] == '3')
$session = new SNMP(SNMP::VERSION_3, $hostname, $row['community_string']);
else
$session = new SNMP(SNMP::VERSION_2C, $hostname, $row['community_string']);
if (!$objects = @$session->walk($mib, true))
{
echo "SNMP connection problem: ".$session->getError()."\n";
}
else
{
echo "has ".count($objects)." interfaces ";
// we have now results of descriptions of interfaces
foreach ($objects as $key => $intf)
{
if (preg_match('/'.$str_desc.'/', $intf))
{
preg_match('/'.$str_desc.'([A-Z, ,0-9,_,a-z]+)/', $intf, $rename);
$rename = rtrim((string)$rename[1]);
# input direction
fwrite($fh, "<channel>\n");
fwrite($fh, "<name>".$order."_in</name>\n");
fwrite($fh, "<rename>".$rename." input</rename>\n");
fwrite($fh, "<display>above</display>\n");
fwrite($fh, "<order>".$order++."</order>\n");
fwrite($fh, "<color>".$colpal[array_rand($colpal)]."</color>\n");
fwrite($fh, "<filter>in if ".$key."</filter>\n");
fwrite($fh, "<source>".$row['live_profile_channel_id']."</source>\n");
fwrite($fh, "</channel>\n");
# output direction
fwrite($fh, "<channel>\n");
fwrite($fh, "<name>".$order."_out</name>\n");
fwrite($fh, "<rename>".$rename." output</rename>\n");
fwrite($fh, "<display>below</display>\n");
fwrite($fh, "<order>".$order++."</order>\n");
fwrite($fh, "<color>".$colpal[array_rand($colpal)]."</color>\n");
fwrite($fh, "<filter>out if ".$key."</filter>\n");
fwrite($fh, "<source>".$row['live_profile_channel_id']."</source>\n");
fwrite($fh, "</channel>\n");
}
}
}
Now the last part is to generate a footer in file to finish all required parts.
fwrite($fh, "\n");
fwrite($fh, "\n");
fwrite($fh, "\n");
fwrite($fh, "\n");
fwrite($fh, "\n");
fwrite($fh, "\n");
fclose($fh);
If we would like to automate the import of the file we can use the following line which will execute another script and import the configuration without dependency check so that way it would be imported even without sources part which is required if we would like to do it though GUI.
echo shell_exec('/usr/bin/php /var/www/shtml/index.php Cli:SetConfigurationXML -method=add -xml=/home/flowmon/generated_profile.xml -nodeps');
Now this script can be downloaded for free – feel free to contact us. It needs to be run with PHP CLI interpreter this way once you are in /home/flowmon folder where it expects to be present.
View all posts from Jiri Knapek on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites