Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / inc / abstractmonitoreditem.class.php @ 166be9d2

History | View | Annotate | Download (5.26 KB)

1
<?php
2

    
3
abstract class PluginVigiloAbstractMonitoredItem extends VigiloXml
4
{
5
    protected $item;
6
    protected $addresses;
7
    protected $ventilation;
8
    protected $children;
9
    protected $agent;
10

    
11
    public function __construct(CommonDBTM $item)
12
    {
13
        $this->agent        = null;
14
        $this->item         = $item;
15
        $this->ventilation  = "Servers";
16
        $this->addresses    = array();
17
        $this->children     = array();
18

    
19
        if (class_exists('PluginFusioninventoryAgent')) {
20
            $agent = new PluginFusioninventoryAgent();
21
            if ($agent->getAgentWithComputerid($item->getID()) !== false) {
22
                $this->agent = $agent;
23
            }
24
        }
25

    
26
        $this->selectTemplates();
27
        $this->selectGroups();
28
        $this->monitorNetworkInterfaces();
29
    }
30

    
31
    public function getName()
32
    {
33
        return $this->item->getName();
34
    }
35

    
36
    protected function selectTemplates()
37
    {
38
        $template = $this->item->fields['vigilo_template'];
39
        if (null !== $template) {
40
            $this->children[] = new VigiloTemplate($template);
41
        }
42
    }
43

    
44
    protected function selectGroups()
45
    {
46
        $location = new Location();
47
        $location->getFromDB($this->item->fields["locations_id"]);
48

    
49
        $entity = new Entity();
50
        $entity->getFromDB($this->item->fields["entities_id"]);
51

    
52
        $candidates = array(
53
            'Locations' => $location,
54
            'Entities'  => $entity,
55
        );
56

    
57
        foreach ($candidates as $type => $candidate) {
58
            if ('N/A' === $candidate->getName()) {
59
                continue;
60
            }
61

    
62
            $completeName       = explode(" > ", $candidate->getField("completename"));
63
            // Ajout de "/" et de l'origine pour avoir le chemin complet.
64
            array_unshift($completeName, $type);
65
            array_unshift($completeName, "");
66
            $groupName          = implode("/", $completeName);
67
            $this->children[]   = new VigiloGroup($groupName);
68
        }
69

    
70
        $manufacturer = new Manufacturer();
71
        $manufacturer->getFromDB($this->item->fields["manufacturers_id"]);
72
        if ('N/A' !== $manufacturer->getName()) {
73
            $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName());
74
        }
75
    }
76

    
77
    protected function selectAddress()
78
    {
79
        static $address = null;
80

    
81
        if (null === $address && $this->agent) {
82
            $addresses = $this->agent->getIPs();
83
            if (count($addresses)) {
84
                $address = current($addresses);
85
            }
86
        }
87

    
88
        if (null === $address) {
89
            $address = $this->item->getName();
90
            foreach ($this->addresses as $addr) {
91
                if (!$addr->is_ipv4()) {
92
                    continue;
93
                }
94

    
95
                $textual = $addr->getTextual();
96
                if (is_string($textual)) {
97
                    $address = $textual;
98
                    break;
99
                }
100
            }
101
        }
102

    
103
        return $address;
104
    }
105

    
106
    protected function monitorNetworkInterfaces()
107
    {
108
        global $DB;
109

    
110
        $query = NetworkPort::getSQLRequestToSearchForItem(
111
            $this->item->getType(),
112
            $this->item->getID()
113
        );
114

    
115
        foreach ($DB->query($query) as $np) {
116
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
117
            $port       = new NetworkPort();
118
            $ethport    = new NetworkPortEthernet();
119

    
120
            $port->getFromDB($np['id']);
121
            if ($port->getName() == 'lo') {
122
                continue;
123
            }
124

    
125
            $label = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
126

    
127
            $this->children[] =
128
                        $test = new VigiloTest('Interface', $args);
129
            $test['label']  = $label;
130
            $test['ifname'] = $port->getName();
131

    
132
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
133
            foreach ($ethport as $rowEthPort) {
134
                if ($rowEthPort['speed']) {
135
                    $test['max'] = $rowEthPort['speed'];
136
                    break;
137
                }
138
            }
139

    
140
            // Récupère la liste de toutes les adresses IP pour l'interface.
141
            // Elles serviront plus tard dans selectAddress() pour choisir
142
            // l'adresse IP la plus appropriée pour interroger ce réseau.
143
            foreach ($DB->query($query2) as $nn) {
144
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
145
                foreach ($DB->query($query3) as $ip) {
146
                    $addr = new IPAddress();
147
                    if ($addr->getFromDB($ip['id'])) {
148
                        $this->addresses[] = $addr;
149
                    }
150
                }
151
            }
152
        }
153
    }
154

    
155
    public function __toString()
156
    {
157
        $outXML = new DOMDocument();
158
        $outXML->preserveWhiteSpace = false;
159
        $outXML->formatOutput       = true;
160
        $outXML->loadXML(
161
            self::sprintf(
162
                '<?xml version="1.0"?>' .
163
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
164
                $this->item->getName(),
165
                $this->selectAddress(),
166
                "Servers",
167
                $this->children
168
            )
169
        );
170
        return $outXML->saveXML();
171
    }
172
}