Module: nagvis
Branch: master
Commit: 74c8d08c0962effa00216457e3a391c2cadc1466
URL:    
http://nagvis.git.sourceforge.net/git/gitweb.cgi?p=nagvis/nagvis/commit/?id=74c8d08c0962effa00216457e3a391c2cadc1466

Author: Roman Kyrylych <[email protected]>
Date:   Tue Aug 18 11:29:05 2009 +0300

gmap: Copy-pasted lots of code from Location to Link class

Signed-off-by: Roman Kyrylych <[email protected]>

---

 share/netmap/Link.php |  182 +++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 161 insertions(+), 21 deletions(-)

diff --git a/share/netmap/Link.php b/share/netmap/Link.php
index c2060d3..9cab11d 100644
--- a/share/netmap/Link.php
+++ b/share/netmap/Link.php
@@ -23,67 +23,207 @@
 
 class Link
 {
+       const STATE_UNKNOWN = 0;
+       const STATE_OK = 1;
+       const STATE_WARNING = 2;
+       const STATE_ERROR = 3;
+
        public $id1;
        public $id2;
-       public $services;
+       public $description;
+       public $action;
+       public $object;
+       public $state;
 
-       public function __construct($id1 = "", $id2 = "", $services = array())
+       public function __construct($id1 = "", $id2 = "", $description = "",
+               $action = "", $object = null, $state = self::STATE_UNKNOWN)
        {
                $this->id1 = $id1;
                $this->id2 = $id2;
-               $this->services = $services;
+               $this->description = $description;
+               $this->action = $action;
+               $this->object = $object;
+               $this->state = $state;
+       }
+
+       private function fromXML($node)
+       {
+               $object = null;
+               $object_type = '';
+
+               /* Note: there should be only one child of link node,
+                                but it is required to use foreach with 
children() */
+               foreach ($node->children() as $object_node)
+               {
+                       $object_type = $object_node->getName();
+                       switch ($object_type)
+                       {
+                               case 'host':
+                                       $object = Host::fromXML($object_node);
+                                       break;
+
+                               case 'hostgroup':
+                                       $object = 
HostGroup::fromXML($object_node);
+                                       break;
+
+                               case 'service':
+                                       $object = 
Service::fromXML($object_node);
+                                       break;
+
+                               case 'servicegroup':
+                                       $object = 
ServiceGroup::fromXML($object_node);
+                                       break;
+
+                               default:
+                                       throw new Exception('Unknown object 
type in links.xml');
+                       }
+               }
+
+               return new Link((string)$node['id1'], (string)$node['id2'],
+                       (string)$node['description'], (string)$node['action'], 
$object);
+       }
+
+       private function toXML($parent)
+       {
+               $node = $parent->addChild('link');
+               $node->addAttribute('id1', $this->id1);
+               $node->addAttribute('id2', $this->id2);
+               @$node->addAttribute('description', $this->description);
+               @$node->addAttribute('action', $this->action);
+
+               if (is_object($this->object))
+                       $this->object->toXML($node);
+
+               return $node;
+       }
+
+       private function updateState()
+       {
+               $db = new Database();
+
+               if (isset($this->object))
+                       switch (get_class($this->object))
+                       {
+                               case 'Host':
+                                       $this->state = 
$db->getHostState($this->object);
+                                       break;
+
+                               case 'HostGroup':
+                                       $this->state = 
$db->getHostGroupState($this->object);
+                                       break;
+
+                               case 'Service':
+                                       $this->state = 
$db->getServiceState($this->object);
+                                       break;
+
+                               case 'ServiceGroup':
+                                       $this->state = 
$db->getServiceGroupState($this->object);
+                                       break;
+
+                               default:
+                                       throw new Exception('Unknown object 
type in links.xml');
+                       }
+               else
+                       $this->state = self::STATE_UNKNOWN;
        }
 
        /**
+        * @param  boolean $problemonly
         * @return array of Link
         */
-       public function getAll()
+       public function getAll($problemonly = false)
        {
                if (($xml = @simplexml_load_file('links.xml')) === FALSE)
                        throw new Exception('Could not read links.xml');
 
                $links = array();
-               foreach ($xml->link as $link)
+               foreach ($xml->link as $node)
                {
-                       $services = array();
-                       foreach ($link->children() as $service)
-                               $services[] = array('id' => 
(string)$service['id'], 'description' => (string)$service['description']);
+                       $link = Link::fromXML($node);
+
+                       $link->updateState();
 
-                       $links[] = new Link((string)$link['id1'], 
(string)$link['id2'], $services);
+                       if (!$problemonly || $link->state != self::STATE_OK)
+                               $links[] = $link;
                }
 
                return $links;
        }
 
        /**
-        * @param  string $id1
-        * @param  string $id2
-        * @param  array $services
+        * @param  object $link
         * @return Link
         */
-       public function add($id1, $id2, $services = array())
+       public function add($link)
        {
                if (($xml = @simplexml_load_file('links.xml')) === FALSE)
                        throw new Exception('Could not read links.xml');
 
-               $node = $xml->addChild('link');
-               $node->addAttribute('id1', $id1);
-               $node->addAttribute('id2', $id2);
+               $link->updateState();
+               $node = $link->toXML($xml);
+
+               if (file_put_contents('links.xml', $xml->asXML()) !== FALSE)
+                       return $link;
+               else
+                       throw new Exception('Could not write links.xml');
+    }
 
-               foreach ($services as $service)
+       private function removeNode(&$xml, $id1, $id2)
+       {
+               $index = 0;
+               foreach ($xml->location as $node)
                {
-                       $child = $node->addChildren('service');
-                       $child->addAttribute('id', $service['id']);
-                       $child->addAttribute('description', 
$service['description']);
+                       if ($node['id1'] == $id1 && $node['id2'] == $id2)
+                       {
+                               // Note: unset($node) won't work thus the need 
for $index
+                               unset($xml->location[$index]);
+                               $success = true;
+                               break;
+                       }
+                       $index++;
                }
+               if (!isset($success))
+                       throw new Exception('Link does not exist');
+       }
+
+       /**
+        * @param  object $link
+        * @return Link
+        */
+       public function edit($link)
+       {
+               if (($xml = @simplexml_load_file('links.xml')) === FALSE)
+                       throw new Exception('Could not read links.xml');
+
+               $link->updateState();
 
-               $link = new Link($id1, $id2, $services);
+               Location::removeNode($xml, $link->id1, $link->id2);
+
+               $link->toXML($xml);
 
                if (file_put_contents('links.xml', $xml->asXML()) !== FALSE)
                        return $link;
                else
                        throw new Exception('Could not write links.xml');
     }
+
+       /**
+        * @param  string $id1
+        * @param  string $id2
+        * @return string
+        */
+       public function remove($id1, $id2)
+       {
+               if (($xml = @simplexml_load_file('links.xml')) === FALSE)
+                       throw new Exception('Could not read links.xml');
+
+               Location::removeNode($xml, $id1, $id2);
+
+               if (file_put_contents('links.xml', $xml->asXML()) !== FALSE)
+                       return true;
+               else
+                       throw new Exception('Could not write links.xml');
+    }
 }
 
 ?>


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Nagvis-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nagvis-checkins

Reply via email to