New plugin Live RIPE
authorVladimir Kushnir <vkushnir@gmail.com>
Wed, 13 Mar 2013 09:01:14 +0000 (14:01 +0500)
committerVladimir Kushnir <vkushnir@gmail.com>
Wed, 13 Mar 2013 09:01:14 +0000 (14:01 +0500)
LiveRIPE/README.md [new file with mode: 0644]
LiveRIPE/plugins/ripe.php [new file with mode: 0644]
LiveRIPE/plugins/ripe_html.xsl [new file with mode: 0644]
LiveRIPE/plugins/ripe_text.xsl [new file with mode: 0644]

diff --git a/LiveRIPE/README.md b/LiveRIPE/README.md
new file mode 100644 (file)
index 0000000..2fd38d0
--- /dev/null
@@ -0,0 +1,14 @@
+Live RIPE:
+Author: Vladimir Kushnir <vkushnir@gmail.com>
+
+The plugin getting information from RIPE database about selected network and allow to clone it into network description.
+Note that the racktables instance must be able to http://apps.db.ripe.net for this plugin to be useful.
+
+Installation:
+- Copy the ripe.php, ripe_html.xsl, ripe_text.xsl files into the plugins/ folder of racktables.
+
+In use:
+After installation, log into racktables and access a IPv4 network instance and
+click on the "Live RIPE" tab. Here you will be presented with RIPE information about this network if it exists in ripe database.
+
+Known issues:
\ No newline at end of file
diff --git a/LiveRIPE/plugins/ripe.php b/LiveRIPE/plugins/ripe.php
new file mode 100644 (file)
index 0000000..5072b01
--- /dev/null
@@ -0,0 +1,116 @@
+<?php
+//
+// RIPE connector plugin.
+// Version 0.1
+//
+// Written by Vladimir Kushnir
+//
+// The purpose of this plugin is to load description of you IP ranges from RIPE database
+//
+// History
+// Version 0.1:  Initial release
+//
+// Requirements:
+//
+// Installation:
+// 1)  Copy script to plugins folder as ripe.php
+
+
+// Set variables
+$Version = "0.1";
+$username = $_SERVER['PHP_AUTH_USER'];
+$nextorder['odd'] = 'even';
+$nextorder['even'] = 'odd';
+
+// Depot Tab for objects.
+$tab['ipv4net']['ripe'] = 'Live RIPE';
+$tabhandler['ipv4net']['ripe'] = 'ripeTab';
+$ophandler['ipv4net']['ripe']['importRipeData'] = 'importRipeData';
+
+function importRipeData() {
+       $ripe_db = "http://apps.db.ripe.net/whois/search.xml?source=ripe&query-string=";
+       
+       // Prepare update       
+       assertStringArg('ripe_query');
+       assertUIntArg ('net_id');
+       assertStringArg('net_name');
+       
+       //$nbad = $ngood = 0;
+       
+       $net_id = $_REQUEST['net_id'];  
+       $ripe_query = htmlspecialchars_decode($_REQUEST['ripe_query']);
+       $ripe_result_str = file_get_contents($ripe_query, false, NULL);
+       $ripe_result = simplexml_load_string($ripe_result_str);
+       
+       $filedir = realpath (dirname (__FILE__) );
+       $ripe_xsl = simplexml_load_file($filedir.'/ripe_text.xsl');
+
+    $proc = new XSLTProcessor();
+    $proc->importStyleSheet( $ripe_xsl );
+       $newName = htmlspecialchars_decode($_REQUEST['net_name']);
+       $newComment = trim($proc->transformToXML( $ripe_result ));
+       
+       // Update
+       usePreparedUpdateBlade (
+               'IPv4Network',
+               array (
+                       'name' => $newName,
+                       'comment' => $newComment),
+               array (
+                       'id' => $net_id),
+               'AND');
+
+//             $retcode = 51;
+//    if (!$nbad)
+//             return showFuncMessage (__FUNCTION__, 'OK', array ($ngood));
+//     else
+//             return showFuncMessage (__FUNCTION__, 'ERR', array ($nbad, $ngood));
+}
+
+
+// Display the ping overview:
+function ripeTab($id) {
+       $ripe_db = "http://apps.db.ripe.net/whois/search.xml?source=ripe&query-string=";
+       assertUIntArg ('id');
+       $net = spotEntity ('ipv4net', $id);
+       loadIPAddrList ($net);  
+       $startip = ip4_bin2int ($net['ip_bin']);
+       $endip = ip4_bin2int (ip_last ($net));
+       // Get Data from RIPE
+       $ripe_inetnum_str = ip4_format (ip4_int2bin ($startip)) . ' - ' . ip4_format (ip4_int2bin ($endip));
+       $ripe_query = $ripe_db . ip4_format (ip4_int2bin ($startip)) ;
+       $ripe_result_str = file_get_contents($ripe_query, false, NULL);
+       $ripe_result = simplexml_load_string($ripe_result_str);
+       
+       // Check inetnum object 
+       $ripe_inetnum_check = "/whois-resources/objects/object[@type='inetnum']/attributes/attribute[@name='inetnum'][@value='$ripe_inetnum_str']";
+       $ripe_inetnum = $ripe_result->xpath($ripe_inetnum_check);
+       if (empty($ripe_inetnum)) {
+               echo "<div class=trerror><center><h1>${net['ip']}/${net['mask']}</h1><h2>${net['name']}</h2></center></div>\n";
+       } else {
+               $ripe_netname_check = "/whois-resources/objects/object[@type='inetnum']/attributes/attribute[@name='netname']";
+               $ripe_netname = $ripe_result->xpath($ripe_netname_check);
+               $netname = trim($ripe_netname[0]['value']);
+               if (strcmp($netname, $net['name']) != 0) {
+                       echo "<div class=trwarning><center><h1>${net['ip']}/${net['mask']}</h1><h2>${net['name']}</h2></center></div><div><center>";
+               } else {
+                       echo "<div class=trok><center><h1>${net['ip']}/${net['mask']}</h1><h2>${net['name']}</h2></center></div><div><center>";
+               }
+               printOpFormIntro ('importRipeData', array ('ripe_query' => $ripe_query, 'net_id' => $id, 'net_name' => $netname));
+               echo "<input type=submit value='Import RIPE records in to comments'></center></div>";
+               echo "</form>";
+       };
+       echo '<em>'.$ripe_query.'</em>';
+       echo "<table border=0 width='100%'><tr><td class=pcleft>";
+       
+       $filedir = realpath (dirname (__FILE__) );
+       $ripe_xsl = simplexml_load_file($filedir.'/ripe_html.xsl');
+       
+       startPortlet ('RIPE Information Datatbase');    
+    $proc = new XSLTProcessor();
+    $proc->importStyleSheet( $ripe_xsl );
+    echo '<div class=commentblock>'.trim($proc->transformToXML( $ripe_result )).'</div>';
+       finishPortlet();
+       echo "</td></tr></table>\n";
+}
+?>
diff --git a/LiveRIPE/plugins/ripe_html.xsl b/LiveRIPE/plugins/ripe_html.xsl
new file mode 100644 (file)
index 0000000..4a20ec5
--- /dev/null
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<xsl:stylesheet version="1.0" 
+       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+       xmlns:xlink="http://www.w3.org/1999/xlink">
+
+<xsl:output method="html" />
+
+<xsl:template match="whois-resources/objects">
+       <table class="widetable" border="0" cellpadding="5" cellspacing="0" style="font-family: monospace;" >
+               <tbody>
+                       <xsl:apply-templates select="object"/>
+               </tbody>
+       </table>
+</xsl:template>
+
+<xsl:template match="object">
+       <tr><th colspan="2" class="centered"><h3>
+       <xsl:for-each select="primary-key/attribute">
+               <xsl:choose>
+                       <xsl:when test="../../link/@xlink:href">
+                               <xsl:element name="a">
+                                       <xsl:attribute name="target">
+                                               <xsl:text>_blank</xsl:text>
+                                       </xsl:attribute>                                
+                                       <xsl:attribute name="href">
+                                               <xsl:value-of select="../../link/@xlink:href"/>
+                                       </xsl:attribute>                                
+                                       <xsl:value-of select="@value" />
+                               </xsl:element>
+                       </xsl:when>
+                       <xsl:otherwise>
+                               <xsl:value-of select="@value" />
+                       </xsl:otherwise>
+               </xsl:choose>
+               <xsl:if test="following-sibling::*">
+                       <br />
+               </xsl:if>
+       </xsl:for-each>
+       </h3></th></tr>
+       <xsl:for-each select="attributes/attribute">
+               <tr>
+               <td width="20%"><xsl:value-of select="@name" /><xsl:text>:</xsl:text></td>
+               <td><xsl:choose>
+                       <xsl:when test="link/@xlink:href">
+                               <xsl:element name="a">
+                                       <xsl:attribute name="target">
+                                               <xsl:text>_blank</xsl:text>
+                                       </xsl:attribute>                                
+                                       <xsl:attribute name="href">
+                                               <xsl:value-of select="link/@xlink:href"/>
+                                       </xsl:attribute>                                
+                                       <xsl:value-of select="@value" />
+                               </xsl:element>
+                       </xsl:when>
+                       <xsl:otherwise>
+                               <xsl:value-of select="@value" />
+                       </xsl:otherwise>
+               </xsl:choose></td>
+               </tr>
+       </xsl:for-each>
+</xsl:template>
+
+</xsl:stylesheet>
\ No newline at end of file
diff --git a/LiveRIPE/plugins/ripe_text.xsl b/LiveRIPE/plugins/ripe_text.xsl
new file mode 100644 (file)
index 0000000..6ce6add
--- /dev/null
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+<xsl:output method="text" />
+
+<xsl:variable name="newline" select="'&#10;'" />
+<xsl:variable name="maxWidth1">
+  <xsl:for-each select="whois-resources/objects/object/attributes/attribute">
+    <xsl:sort select="string-length(@name)" order="descending" data-type="number"/>
+    <xsl:if test="position() = 1">
+      <xsl:value-of select="string-length(@name) + 3" />
+    </xsl:if>
+  </xsl:for-each> 
+</xsl:variable>
+
+<xsl:template match="whois-resources/objects">  
+  <xsl:apply-templates select="object"/>  
+</xsl:template>
+
+<xsl:template match="object">
+  <xsl:apply-templates select="attributes"/>
+  <xsl:value-of select="$newline"/>
+</xsl:template>
+
+<xsl:template match="object/primary-key/attribute">
+  <xsl:value-of select="@value" />  
+</xsl:template>
+
+<xsl:template match="object/attributes">
+  <xsl:apply-templates select="attribute"/>
+</xsl:template>
+
+<xsl:template match="object/attributes/attribute">
+  <xsl:variable name="cell1"><xsl:value-of select="@name" /><xsl:text>:</xsl:text></xsl:variable>
+  <xsl:variable name="cell2"><xsl:value-of select="@value" /></xsl:variable>
+  
+  <xsl:variable name="pad1"><xsl:value-of select="$maxWidth1 - string-length($cell1)" /></xsl:variable>   
+  <xsl:variable name="pad2"><xsl:value-of select="$maxWidth1 - string-length($cell2)" /></xsl:variable>
+
+  <xsl:value-of select="$cell1" />
+  <xsl:call-template name="append-pad"><xsl:with-param name="length" select="$pad1" /></xsl:call-template>
+  <xsl:value-of select="$cell2" />
+  <xsl:call-template name="append-pad"><xsl:with-param name="length" select="$pad1" /></xsl:call-template>
+  <xsl:value-of select="$newline"/>
+</xsl:template>
+
+<xsl:template name="append-pad">
+  <xsl:param name="length" select="0" />
+  <xsl:if test="$length > 0">
+    <xsl:value-of select="'&#32;'"/>
+    <xsl:call-template name="append-pad">
+      <xsl:with-param name="length" select="$length - 1"/>
+    </xsl:call-template>
+  </xsl:if>
+</xsl:template>
+</xsl:stylesheet>
\ No newline at end of file